// ------------------------------------------------------------------------------------------ // // 3 - S T E P S E X P E R T A D V I S O R // // by fosgate_r © January 2009 // // URL : http://forexfreeea.blogspot.com/ // // ------------------------------------------------------------------------------------------ // #property copyright "© January 2009 fosgate_r" #property link "http://forexfreeea.blogspot.com/" #include #include // ------------------------------------------------------------------------------------------ // // E X T E R N A L V A R I A B L E S // // ------------------------------------------------------------------------------------------ // extern double Lots = 0.03; extern double TrailingStop = 0; extern double TakeProfit = 150; extern double StopLoss = 40; extern double Steps = 3; extern bool MM = true; extern double Risk = 0.1; extern double LossMax = 3200; // ------------------------------------------------------------------------------------------ // // I N T E R N A L V A R I A B L E S // // ------------------------------------------------------------------------------------------ // string strtmp; double lotsi; string eacmd = "3S-26117702"; int magic = 26117702; double poinWB = 0.0001; // ------------------------------------------------------------------------------------------ // // I N I T I A L I S A T I O N // // ------------------------------------------------------------------------------------------ // int init() { poinWB = MarketInfo(Symbol(),MODE_POINT); if (poinWB<0.0001) poinWB = 0.0001; return(0); } // ------------------------------------------------------------------------------------------ // // D E - I N I T I A L I S A T I O N // // ------------------------------------------------------------------------------------------ // int deinit() { return(0); } // ------------------------------------------------------------------------------------------ // // M A I N P R O C E D U R E // // ------------------------------------------------------------------------------------------ // int start() { if (MM) lotsi = MoneyManagement(); else lotsi = Lots; double nextTP = 0; /* double Signal11 = iMA(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,1); double Signal12 = iMA(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,2); if (CntOrd(magic)==0) { if (Open[1]>Signal11 && Close[2]Signal12) Sell (magic,lotsi,eacmd); } */ double Signal11 = iOsMA(NULL,0,12,26,9,PRICE_CLOSE,1); double Signal12 = iOsMA(NULL,0,12,26,9,PRICE_CLOSE,2); if (CntOrd(magic)==0) { if (Signal11>0 && Signal12<0) Buy (magic,lotsi,eacmd); if (Signal11<0 && Signal12>0) Sell (magic,lotsi,eacmd); } if (CntOrd(magic)>0) { nextTP = (TakeProfit/Steps) * (Steps - Step(magic) + 1); if (CalcPips(magic)>=nextTP) { ClosePartial(magic); Sleep(5000); Modify(magic); } } return(0); } // ------------------------------------------------------------------------------------------ // // C A L C U L A T E P I P S // // ------------------------------------------------------------------------------------------ // int CalcPips(int mgc) { int pips = 0; double p = 0; for (int i=0; i0) tp = Ask + (TakeProfit*poinWB); if (StopLoss >0) sl = Ask - (StopLoss *poinWB); if (IsTradeAllowed() == true) { for(int c=0; c<5; c++) { RefreshRates(); ticket = OrderSend(Symbol(),OP_BUY,lot,Ask,3,sl,tp,cmd, mgc,0,Blue); err = GetLastError(); if(err==0) break; else { Print("Errors opening BUY order"); Print(ErrorDescription(err),", error ",err); if(err==4 || err==137 ||err==146 || err==136) {Sleep(5000);continue;} else break; //normal error // Busy errors } } } return(ticket); } // ------------------------------------------------------------------------------------------ // // S E L L F U N C T I O N // // ------------------------------------------------------------------------------------------ // int Sell(int mgc, double lot, string cmd) { int ticket=0, err=0; double tp=0, sl=0; if (TakeProfit>0) tp = Bid - (TakeProfit*poinWB); if (StopLoss >0) sl = Bid + (StopLoss *poinWB); if (IsTradeAllowed() == true) { for(int c=0; c<5; c++) { RefreshRates(); ticket = OrderSend(Symbol(),OP_SELL,lot,Bid,3,sl,tp,cmd,mgc,0,Red); err = GetLastError(); if(err==0) break; else { Print("Errors opening SELL order"); Print(ErrorDescription(err),", error ",err); if(err==4 || err==137 ||err==146 || err==136) {Sleep(5000);continue;} else break; //normal error // Busy errors } } } return(ticket); } // ------------------------------------------------------------------------------------------ // // M O D I F Y F U N C T I O N // // ------------------------------------------------------------------------------------------ // int Modify(int mgc) { int tot = OrdersTotal(); double sl = 0; double slmod = (Steps-Step(magic)-1) * (TakeProfit/Steps); if (IsTradeAllowed()==true) { for (int cnt=tot-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderMagicNumber()==mgc) { switch(OrderType()) { case OP_BUY : { sl = OrderOpenPrice() + (slmod*poinWB); OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Green); return(0); } break; case OP_SELL : { sl = OrderOpenPrice() - (slmod*poinWB); OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Red); return(0); } break; } } } } } // ------------------------------------------------------------------------------------------ // // C L O S E P A R T I A L // // ------------------------------------------------------------------------------------------ // void ClosePartial(int mgc) { int cnt,c,tot=0,ticket=0,err=0; double lot = lotsi/Steps; if (IsTradeAllowed() == true) { tot = OrdersTotal(); for(cnt=tot-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderMagicNumber()==mgc) { switch(OrderType()) { case OP_BUY : for(c=0;c<5;c++) { RefreshRates(); OrderClose(OrderTicket(),lot,OrderClosePrice(),3,Yellow); err = GetLastError(); if (err==0) break; else { Print("Errors Closing BUY order"); Print(ErrorDescription(err),", error ",err); if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) { Sleep(5000); continue; } // Busy errors } } break; case OP_SELL : for(c=0;c<5;c++) { RefreshRates(); OrderClose(OrderTicket(),lot,OrderClosePrice(),3,Yellow); err = GetLastError(); if (err==0) break; else { Print("Errors Closing SELL order"); Print(ErrorDescription(err),", error ",err); if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) { Sleep(5000); continue; } // Busy errors } } break; } // end of switch } // end of if } // end of for } return(0); } // ------------------------------------------------------------------------------------------ // // C L O S E O R D E R S // // ------------------------------------------------------------------------------------------ // void CloseOrders(int mgc) { int cnt,c,tot=0,ticket=0,err=0; if (IsTradeAllowed() == true) { tot = OrdersTotal(); for(cnt=tot-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderMagicNumber()==mgc) { switch(OrderType()) { case OP_BUY : for(c=0;c<5;c++) { RefreshRates(); OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Yellow); err = GetLastError(); if (err==0) break; else { Print("Errors Closing BUY order"); Print(ErrorDescription(err),", error ",err); if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) { Sleep(5000); continue; } // Busy errors } } break; case OP_SELL : for(c=0;c<5;c++) { RefreshRates(); OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Yellow); err = GetLastError(); if (err==0) break; else { Print("Errors Closing SELL order"); Print(ErrorDescription(err),", error ",err); if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) { Sleep(5000); continue; } // Busy errors } } break; } // end of switch } // end of if } // end of for } return(0); } // ------------------------------------------------------------------------------------------ // // T R A I L I NG F U N C T I O N // // ------------------------------------------------------------------------------------------ // int Trailing(int mgc) { int tot = OrdersTotal(); if (IsTradeAllowed()==true && TrailingStop>0 && tot==0) { for (int cnt=tot-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderMagicNumber()==mgc) { switch(OrderType()) { case OP_BUY : if (Bid-OrderOpenPrice()>poinWB*TrailingStop) if (OrderStopLoss()(poinWB*TrailingStop)) if ((OrderStopLoss()>(Ask+poinWB*TrailingStop)) || (OrderStopLoss()==0)) { OrderModify(OrderTicket(),OrderOpenPrice(),Ask+poinWB*TrailingStop, OrderTakeProfit(),0,Red); return(0); } break; } } } } } // ------------------------------------------------------------------------------------------ // // M O N E Y M A N A G E M E N T // // ------------------------------------------------------------------------------------------ // double MoneyManagement() { double MinLots = NormalizeDouble(MarketInfo(Symbol(),23),2); double LotStep = NormalizeDouble(MarketInfo(Symbol(),24),2); double MaxLots = NormalizeDouble(MarketInfo(Symbol(),25),2); int LotDigit; MinLots = MinLots * Steps; if (LotStep==1) { LotDigit = 0; } if (LotStep==0.1) { LotDigit = 1; } if (LotStep==0.01) { LotDigit = 2; } double lt = NormalizeDouble(AccountEquity()*Risk/LossMax,LotDigit); if (ltMaxLots) lt = MaxLots; return(lt); } // ------------------------------------------------------------------------------------------ // // E N D O F E X P E R T A D V I S O R // // ------------------------------------------------------------------------------------------ //