/*============================================================ ==============================================================*/ #property copyright "Programmed by ForexBarometer for VantageFX" #property link "www.forexbarometer.com" string Version= "1103121111"; extern double PSStep= 0.02, PSMax= 0.2; extern int TakeProfit= 70, StopLoss= 70; extern double Lots= 0.1; extern int MagicNumber= 110313; datetime NextProcessTime; int STOP_LEVEL, AttemptCount= 10, AttemptDelay= 1000; double DM; /*============================================================ ==============================================================*/ int init() { if (Digits== 3 || Digits== 5) DM= 10; else DM= 1; StopLoss= DM* StopLoss; TakeProfit= DM* TakeProfit; STOP_LEVEL= MarketInfo(Symbol(), MODE_STOPLEVEL); NextProcessTime= 0; } /*============================================================ ==============================================================*/ int start() { bool OK, Buy, Sell; if (TimeCurrent()< NextProcessTime) return; OK= true; //---------------------------------------------------------- double PS1= iSAR( Symbol(), Period(), PSStep, PSMax, 1); double PS2= iSAR( Symbol(), Period(), PSStep, PSMax, 2); Buy= PS1< Low[1] && PS2> High[2]; Sell= PS1> High[1] && PS2< Low[2]; //---------------------------------------------------------- if (Buy) OK= CloseAll(OP_SELL); if (Sell)OK= CloseAll(OP_BUY); if (NoOpenOrders()) { if (Buy) OK= SendOrder(OP_BUY); if (Sell) OK= SendOrder(OP_SELL); } if (OK) NextProcessTime= Time[0]+ 60* Period(); // when current period will elapse else NextProcessTime= 0; // there was an error during sending an order, repeat cycle } /*============================================================ ==============================================================*/ bool SendOrder(int dir) { double sl, tp; if (StopLoss!= 0) sl= StopLoss* Point; else sl= 0; if (TakeProfit!= 0) tp= TakeProfit* Point; else tp= 0; return(OpenOrder(dir, sl, tp, "")); } /*============================================================ ==============================================================*/ bool OpenOrder(int cmd, double volume, double stop, double take) { int i, j, ticket, t=0; double prc, sl, tp, lots; string cmt; color clr; lots = CheckLots(Lots); for (i= 0; i< AttemptCount; i++) { for (j= 0; (j< 50) && IsTradeContextBusy(); j++) Sleep(100); RefreshRates(); if (cmd== OP_BUY) { prc = Ask; sl = stop; tp = take; clr = Blue; } if (cmd== OP_SELL) { prc = Bid; sl = stop; tp = take; clr = Red; } cmt = ""; if (t > 0) cmt= t; ticket= OrderSend(Symbol(), cmd, lots, prc, 3, 0, 0, cmt, MagicNumber,0,clr); if (ticket != -1 && ( sl > 0 || tp > 0) ) { OrderSelect( ticket, SELECT_BY_TICKET, MODE_TRADES); for (i=0; i< AttemptCount; i++) { for (j=0; (j<50) && IsTradeContextBusy(); j++) Sleep(100); RefreshRates(); if( cmd== OP_BUY ) { if( tp != 0 ) tp= CheckTakeProfit(cmd, OrderOpenPrice(), take); else tp= 0; if( sl != 0 ) sl= CheckStopLoss(cmd, OrderOpenPrice(), stop); else sl = 0; if ( OrderModify(ticket, 0, sl, tp, 0) ) { break; } } if (cmd== OP_SELL ) { if( tp != 0 ) tp = CheckTakeProfit(cmd, OrderOpenPrice(), take); else tp = 0; if (sl!= 0 ) sl= CheckStopLoss(cmd, OrderOpenPrice(), stop); else sl= 0; if ( OrderModify(ticket, 0, sl,tp, 0) ) { break; } } Print("OpenTrade: error \'"+ErrorDescription(GetLastError())+"\' when setting SL/TP"); Sleep(AttemptDelay); } return (true); } else { if( ticket > 0 ) { return( true ); } } Print("OpenOrder: error "+ ErrorDescription(GetLastError())); Sleep(AttemptDelay); } //for (i= 0; i< AttemptCount Print("OpenOrder failed after "+ AttemptCount+ " attempts"); return (false); } /*============================================================ ==============================================================*/ double CheckTakeProfit(int dir, double openPrc, double take) { double tp = 0; string Msg= "Take profit is too close to market price. Corrected to acceptable value"; RefreshRates(); if (dir == OP_BUY) { tp= openPrc+ take; if (tp- Bid< STOP_LEVEL* Point) { tp = Bid + STOP_LEVEL* Point; Print(Msg); } } else if (dir == OP_SELL) { tp = openPrc - take; if (Ask- tp< STOP_LEVEL* Point) { tp = Ask - STOP_LEVEL* Point; Print(Msg); } } return(tp); } /*============================================================ ==============================================================*/ double CheckStopLoss(int dir, double openPrc, double stop) { double sl = 0; string Msg= "Stop loss is too close to market price. Corrected to acceptable value"; RefreshRates(); if (dir == OP_BUY) { sl = openPrc - stop; if (Bid- sl< STOP_LEVEL*Point) { sl= Bid - STOP_LEVEL*Point; Print(Msg); } } else if (dir == OP_SELL) { sl = openPrc + stop; if (sl- Ask< STOP_LEVEL*Point) { sl= Ask + STOP_LEVEL* Point; Print(Msg); } } return(sl); } /*============================================================ ==============================================================*/ double CheckLots(double lots) { double lot, lotmin, lotmax, lotstep, margin; lotmin= MarketInfo(Symbol(), MODE_MINLOT); lotmax= MarketInfo(Symbol(), MODE_MAXLOT); lotstep= MarketInfo(Symbol(), MODE_LOTSTEP); margin= MarketInfo(Symbol(), MODE_MARGINREQUIRED); if (lots*margin> AccountFreeMargin()) lots= AccountFreeMargin()/ margin; lot = MathFloor(lots/lotstep + 0.5) * lotstep; if (lot< lotmin) lot = lotmin; if (lot> lotmax) lot = lotmax; return (lot); } /*============================================================ ==============================================================*/ bool NoOpenOrders(){ int total = OrdersTotal(); for(int i= 0; i < total; i++) { OrderSelect(i, SELECT_BY_POS,MODE_TRADES); if (OrderMagicNumber()== MagicNumber && OrderSymbol()== Symbol()) return (false); } return (true); } /*============================================================ ==============================================================*/ bool CloseOrder(int ticket, int dir, double volume, color clr, int t = 0) { int i, j, cmd; double prc, sl, tp, lots; string cmt; for (i= 0; i< AttemptCount; i++) { for (j=0; (j< 50) && IsTradeContextBusy(); j++) Sleep(100); RefreshRates(); if (dir == OP_BUY) prc = Bid; else if (dir == OP_SELL) prc = Ask; bool OK= OrderClose(ticket,volume,prc, 3, clr); if (OK) return (true); else { Print( "CloseOrder error "+ ErrorDescription(GetLastError()) ); Sleep(AttemptDelay); } } Print("Unable to close order after "+ AttemptCount+ " attempts"); return (false); } /*============================================================ ==============================================================*/ bool CloseAll(int typ) { bool OK= true; for (int i = OrdersTotal()-1; i >= 0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol()== Symbol() && OrderMagicNumber() == MagicNumber) { if (OrderType()== OP_BUY && typ== OP_BUY) OK= CloseOrder(OrderTicket(), OP_BUY, OrderLots(), Blue); if (OrderType()== OP_SELL && typ== OP_SELL) OK= CloseOrder( OrderTicket(), OP_SELL, OrderLots(), Red); } } return(OK); } /*============================================================= ===============================================================*/ string ErrorDescription(int error_code) { string error_string; switch(error_code) { //---- codes returned from trade server case 0: case 1: error_string="no error"; break; case 2: error_string="common error"; break; case 3: error_string="invalid trade parameters"; break; case 4: error_string="trade server is busy"; break; case 5: error_string="old version of the client terminal"; break; case 6: error_string="no connection with trade server"; break; case 7: error_string="not enough rights"; break; case 8: error_string="too frequent requests"; break; case 9: error_string="malfunctional trade operation (never returned error)"; break; case 64: error_string="account disabled"; break; case 65: error_string="invalid account"; break; case 128: error_string="trade timeout"; break; case 129: error_string="invalid price"; break; case 130: error_string="invalid stops"; break; case 131: error_string="invalid trade volume"; break; case 132: error_string="market is closed"; break; case 133: error_string="trade is disabled"; break; case 134: error_string="not enough money"; break; case 135: error_string="price changed"; break; case 136: error_string="off quotes"; break; case 137: error_string="broker is busy (never returned error)"; break; case 138: error_string="requote"; break; case 139: error_string="order is locked"; break; case 140: error_string="long positions only allowed"; break; case 141: error_string="too many requests"; break; case 145: error_string="modification denied because order too close to market"; break; case 146: error_string="trade context is busy"; break; case 147: error_string="expirations are denied by broker"; break; case 148: error_string="amount of open and pending orders has reached the limit"; break; case 149: error_string="hedging is prohibited"; break; case 150: error_string="prohibited by FIFO rules"; break; //---- mql4 errors case 4000: error_string="no error (never generated code)"; break; case 4001: error_string="wrong function pointer"; break; case 4002: error_string="array index is out of range"; break; case 4003: error_string="no memory for function call stack"; break; case 4004: error_string="recursive stack overflow"; break; case 4005: error_string="not enough stack for parameter"; break; case 4006: error_string="no memory for parameter string"; break; case 4007: error_string="no memory for temp string"; break; case 4008: error_string="not initialized string"; break; case 4009: error_string="not initialized string in array"; break; case 4010: error_string="no memory for array\' string"; break; case 4011: error_string="too long string"; break; case 4012: error_string="remainder from zero divide"; break; case 4013: error_string="zero divide"; break; case 4014: error_string="unknown command"; break; case 4015: error_string="wrong jump (never generated error)"; break; case 4016: error_string="not initialized array"; break; case 4017: error_string="dll calls are not allowed"; break; case 4018: error_string="cannot load library"; break; case 4019: error_string="cannot call function"; break; case 4020: error_string="expert function calls are not allowed"; break; case 4021: error_string="not enough memory for temp string returned from function"; break; case 4022: error_string="system is busy (never generated error)"; break; case 4050: error_string="invalid function parameters count"; break; case 4051: error_string="invalid function parameter value"; break; case 4052: error_string="string function internal error"; break; case 4053: error_string="some array error"; break; case 4054: error_string="incorrect series array using"; break; case 4055: error_string="custom indicator error"; break; case 4056: error_string="arrays are incompatible"; break; case 4057: error_string="global variables processing error"; break; case 4058: error_string="global variable not found"; break; case 4059: error_string="function is not allowed in testing mode"; break; case 4060: error_string="function is not confirmed"; break; case 4061: error_string="send mail error"; break; case 4062: error_string="string parameter expected"; break; case 4063: error_string="integer parameter expected"; break; case 4064: error_string="double parameter expected"; break; case 4065: error_string="array as parameter expected"; break; case 4066: error_string="requested history data in update state"; break; case 4099: error_string="end of file"; break; case 4100: error_string="some file error"; break; case 4101: error_string="wrong file name"; break; case 4102: error_string="too many opened files"; break; case 4103: error_string="cannot open file"; break; case 4104: error_string="incompatible access to a file"; break; case 4105: error_string="no order selected"; break; case 4106: error_string="unknown symbol"; break; case 4107: error_string="invalid price parameter for trade function"; break; case 4108: error_string="invalid ticket"; break; case 4109: error_string="trade is not allowed in the expert properties"; break; case 4110: error_string="longs are not allowed in the expert properties"; break; case 4111: error_string="shorts are not allowed in the expert properties"; break; case 4200: error_string="object is already exist"; break; case 4201: error_string="unknown object property"; break; case 4202: error_string="object is not exist"; break; case 4203: error_string="unknown object type"; break; case 4204: error_string="no object name"; break; case 4205: error_string="object coordinates error"; break; case 4206: error_string="no specified subwindow"; break; default: error_string="unknown error"; } return(error_string); }