//+------------------------------------------------------------------+ //| AccountSentry.mq4 | //| Copyright © 2008, David E. Fulton | //| http://www.defulton.com/forex| //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, David E. Fulton" #property link "http://www.defulton.com/forex" //******************************************************* extern string version= "AccountSentry V1.0"; //* extern string ContactMe="david@defulton.com"; //* extern string Build = "Build 014"; //* extern string BuildDate="-- 19 December 2008 --"; //* //******************************************************* double MaxAccountMargin = 0.0; double MaxFloatingLoss = 0.0; double MinMarginLevel = 0.0; double AccountMarginLevel = 0.0; double AccountCurrentBalance; bool ContinueOpening=true; string Status=""; int nTickCount; int deinit() { return(0); } int init() { if (AccountMargin() > 0) // eleminate zero divide error MinMarginLevel = AccountEquity() / AccountMargin() * 100; if (!GlobalVariableCheck("GV_AccountBeginningBalance")) GlobalVariableSet("GV_AccountBeginningBalance",0.0); if (!GlobalVariableCheck("GV_AccountEquityProtection")) GlobalVariableSet("GV_AccountEquityProtection",0.0); if (!GlobalVariableCheck("GV_TrailingEquityProtection")) GlobalVariableSet("GV_TrailingEquityProtection",0.0); if (!GlobalVariableCheck("GV_CloseAllAndHalt")) GlobalVariableSet("GV_CloseAllAndHalt",0.0); if (!GlobalVariableCheck("GV_MarginLevel")) GlobalVariableSet("GV_MarginLevel",0.0); if (!GlobalVariableCheck("GV_MaxAccountMargin")) GlobalVariableSet("GV_MaxAccountMargin",0.0); if (!GlobalVariableCheck("GV_MaxFloatingLoss")) GlobalVariableSet("GV_MaxFloatingLoss",0.0); if (!GlobalVariableCheck("GV_MaxFloatingProfit")) GlobalVariableSet("GV_MaxFloatingProfit",0.0); if (!GlobalVariableCheck("GV_MaxOpenOrders")) GlobalVariableSet("GV_MaxOpenOrders",0.0); if (!GlobalVariableCheck("GV_MinMarginLevel")) GlobalVariableSet("GV_MinMarginLevel",0.0); if (!GlobalVariableCheck("GV_MinEquityLevel")) GlobalVariableSet("GV_MinEquityLevel",0.0); return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { nTickCount++; GlobalVariableSet("GV_AccountCurrentBalance",AccountBalance()); // Account equity protection if (AccountEquity() <= GlobalVariableGet("GV_TrailingEquityProtection")) { ContinueOpening=False; GlobalVariableSet("GV_CloseAllAndHalt",1.0); CloseAll(); Status="HALTED"; DisplayComments(); return(0); } else { GlobalVariableSet("GV_CloseAllAndHalt",0.0); ContinueOpening=true; } if (GlobalVariableGet("GV_AccountCurrentBalance") > GlobalVariableGet("GV_AccountBeginningBalance")) { GlobalVariableSet("GV_TrailingEquityProtection",GlobalVariableGet("GV_AccountEquityProtection") + GlobalVariableGet("GV_AccountCurrentBalance") - GlobalVariableGet("GV_AccountBeginningBalance")); } else { GlobalVariableSet("GV_AccountCurrentBalance",AccountBalance()); GlobalVariableSet("GV_TrailingEquityProtection",GlobalVariableGet("GV_AccountEquityProtection")); } if (AccountMargin() > 0) { // eleminate zero divide error AccountMarginLevel = AccountEquity() / AccountMargin() * 100; } if (GlobalVariableGet("GV_MinMarginLevel") == 0) { GlobalVariableSet("GV_MinMarginLevel",AccountMarginLevel); } if (AccountMarginLevel < GlobalVariableGet("GV_MinMarginLevel")) { GlobalVariableSet("GV_MinMarginLevel",AccountMarginLevel); //update minimum margin level } if (AccountMargin() > GlobalVariableGet("GV_MaxAccountMargin")) { MaxAccountMargin = AccountMargin(); GlobalVariableSet("GV_MaxAccountMargin",AccountMargin()); //update GV_MaxAccountMargin } if (AccountProfit() > GlobalVariableGet("GV_MaxFloatingProfit")) { GlobalVariableSet("GV_MaxFloatingProfit",AccountProfit()); } if (GlobalVariableGet("GV_MinEquityLevel") == 0) { GlobalVariableSet("GV_MinEquityLevel",AccountEquity()); } else { if (AccountEquity() < GlobalVariableGet("GV_MinEquityLevel")) GlobalVariableSet("GV_MinEquityLevel",AccountEquity()); } if (AccountProfit() < MaxFloatingLoss) { MaxFloatingLoss = AccountProfit(); GlobalVariableSet("GV_MaxFloatingLoss",MaxFloatingLoss); //update GV_MaxFloatingLoss } if (OrdersTotal() > GlobalVariableGet("GV_MaxOpenOrders")) { GlobalVariableSet("GV_MaxOpenOrders",OrdersTotal()); //update GV_MaxOpenOrders } if (ContinueOpening>0) { Status="RUNNING"; } else { Status="HALTED"; } if (!IsTesting()) { DisplayComments(); } return(0); } void DisplayComments() { Comment("\n",version," ",Build, "\n", "\nTick=",nTickCount, "\nCurrent Terminal Status=",Status, "\nAccount Leverage=1:",AccountLeverage(), "\n", "\nBeginning Account Balance=",GlobalVariableGet("GV_AccountBeginningBalance"), "\nCurrent Account Balance=",GlobalVariableGet("GV_AccountCurrentBalance"), "\nTrailing Equity Stop Loss=",GlobalVariableGet("GV_TrailingEquityProtection"), "\n", "\nCurrent AccountEquity=",AccountEquity(), "\nCurrent AccountMargin=",AccountMargin(), "\nCurrent Account Margin Level=",MathRound(AccountMarginLevel),"%", "\nCurrent Open Orders=",OrdersTotal(), "\n", "\nMinumum Equity Level=",GlobalVariableGet("GV_MinEquityLevel"), "\nMaximum Floating Profit=",GlobalVariableGet("GV_MaxFloatingProfit"), "\nMaximum Floating Loss=",GlobalVariableGet("GV_MaxFloatingLoss"), "\nMaximum Account Margin=",GlobalVariableGet("GV_MaxAccountMargin"), "\nMinimum Margin Level=",MathRound(GlobalVariableGet("GV_MinMarginLevel")),"%", "\nMaximum Open Orders=",GlobalVariableGet("GV_MaxOpenOrders")); } //+------------------------------------------------------------------+ //| Close all orders | //+------------------------------------------------------------------+ void CloseAll() { for (int i=0;i<5;i++) { int total_trades=OrdersTotal(); for (int j=0;j<total_trades;j++) { while (!IsTradeAllowed()) { // wait if trade context is busy } if (OrderSelect(j,SELECT_BY_POS,MODE_TRADES)) { int order_type=OrderType(); double order_openprice=OrderOpenPrice(); int order_ticket=OrderTicket(); double order_volume=OrderLots(); string order_symbol=OrderSymbol(); //close pending orders if (order_type>=OP_BUYLIMIT) OrderDelete(order_ticket,CLR_NONE); //close sell orders if (order_type==OP_SELL) OrderClose(order_ticket,order_volume, MarketInfo(order_symbol,MODE_ASK),3,CLR_NONE); //close buy orders if (order_type==OP_BUY) OrderClose(order_ticket,order_volume, MarketInfo(order_symbol,MODE_BID),3,CLR_NONE); } } } total_trades=OrdersTotal(); return(0); } //+------------------------------------------------------------------+
Sample
Analysis
Market Information Used:
Indicator Curves created:
Indicators Used:
Custom Indicators Used:
Order Management characteristics:
Checks for the total of open orders
It Closes Orders by itself
Other Features: