//|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ //| Close //| Last Updated 12-12-2006 10:00pm //|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ #define NL "\n" extern double EquityTargetPercent = 10; // closes all orders once Equity hits this percent amount extern bool CloseAllNow = false; // closes all orders now extern bool CloseProfitableTradesOnly = false; // closes only profitable trades extern double ProftableTradeAmount = 1; // Only trades above this amount close out extern bool ClosePendingOnly = false; // closes pending orders only extern bool UseAlerts = false; extern bool SendEmail = true; double EquityTarget; string gEquityTargetName; //+-------------+ //| Custom init | //|-------------+ int init() { GetGlobalVars(); } //+----------------+ //| Custom DE-init | //+----------------+ int deinit() { } //+------------------------------------------------------------------------+ //| Closes everything //+------------------------------------------------------------------------+ void CloseAll() { for(int i=OrdersTotal()-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); bool result = false; if ( OrderType() == OP_BUY) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ); if ( OrderType() == OP_SELL) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); if ( OrderType()== OP_BUYSTOP) result = OrderDelete( OrderTicket() ); if ( OrderType()== OP_SELLSTOP) result = OrderDelete( OrderTicket() ); } if (UseAlerts) { PlaySound("alert.wav"); SendMail("All trades closed.", ""); } GlobalVariableSet("GV_CloseAllAndHalt",1.0); return; } //+------------------------------------------------------------------------+ //| cancels all orders that are in profit //+------------------------------------------------------------------------+ void CloseAllinProfit() { for(int i=OrdersTotal()-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); bool result = false; if ( OrderType() == OP_BUY && OrderProfit()+OrderSwap()>ProftableTradeAmount) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ); if ( OrderType() == OP_SELL && OrderProfit()+OrderSwap()>ProftableTradeAmount) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); } if (UseAlerts) { PlaySound("alert.wav"); SendMail("All profitable trades closed.", ""); } return; } //+------------------------------------------------------------------------+ //| cancels all pending orders //+------------------------------------------------------------------------+ void ClosePendingOrdersOnly() { for(int i=OrdersTotal()-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); bool result = false; if ( OrderType()== OP_BUYSTOP) result = OrderDelete( OrderTicket() ); if ( OrderType()== OP_SELLSTOP) result = OrderDelete( OrderTicket() ); } return; } //+-----------+ //| Main | //+-----------+ int start() { int OrdersBUY; int OrdersSELL; double BuyLots, SellLots, BuyProfit, SellProfit; double MarginPercent; //+------------------------------------------------------------------+ // Determine last order price | //-------------------------------------------------------------------+ if (OrdersTotal() > 0) { for(int i=0;i= EquityTarget) CloseAll(); if(ClosePendingOnly) ClosePendingOrdersOnly(); } else { EquityTarget = AccountBalance() + AccountBalance() * EquityTargetPercent/100.0; SaveEquityTarget(EquityTarget); } // fix zero divide error from Comment statement if (AccountMargin() > 0) MarginPercent = MathRound( (AccountEquity()/(AccountMargin()*100) )); else MarginPercent = 0; Comment(" Comments Last Update 12-12-2006 10:00pm", NL, " Buys ", OrdersBUY, NL, " BuyLots ", BuyLots, NL, " Sells ", OrdersSELL, NL, " SellLots ", SellLots, NL, " Balance ", AccountBalance(), NL, " Equity ", AccountEquity(), NL, " Margin ", AccountMargin(), NL, " MarginPercent ", MarginPercent, NL, " Current Time is ",TimeHour(CurTime()),":",TimeMinute(CurTime()),".",TimeSeconds(CurTime())); } // start() void GetGlobalVars() { NameGlobalVars(); InitGlobalVars(); GetGlobalVarValues(); } void GetGlobalVarValues() { // Booleans stored as double so convert to boolean // > 0 true, < 0 false EquityTarget = GlobalVariableGet(gEquityTargetName); } void InitGlobalVars() { // check variable before use if(!GlobalVariableCheck(gEquityTargetName)) GlobalVariableSet(gEquityTargetName,10000000.0); } void NameGlobalVars() { string prefix; prefix = "CAELH_" + Symbol(); gEquityTargetName = prefix + "_EquityTarget"; } void SaveEquityTarget (double myVal) { GlobalVariableSet(gEquityTargetName,myVal); } //+------------------------------------------------------------------+