//+------------------------------------------------------------------+ //| StepMAExpert_v1.4.mq4 | //| Copyright © 2005, TrendLaboratory Ltd. | //| http://finance.groups.yahoo.com/group/TrendLaboratory | //| E-mail: igorad2004@list.ru | //+------------------------------------------------------------------+ #property copyright "Copyright © 2005, TrendLaboratory Ltd." #property link "http://finance.groups.yahoo.com/group/TrendLaboratory" #include <stdlib.mqh> #include <Tracert.mqh> //---- input parameters extern string Expert_Name = "---- StepMAExpert_v1.4 ----"; extern int Magic=1007; extern int Slippage=6; extern bool Trace = false; // Trace Switch extern string Main_Parameters = " Trade Volume & Trade Method"; extern double Lots = 0.1; extern double InitialStop = 0; // Initial Stop Value extern double TakeProfit = 0; // Take Profit Value extern int TrailingStopMode = 0; // Trailing Stop Mode Switch extern double TrailingStop=0; // Classic Trailing Stop Value extern bool SwingTrade = false; // Swing Trade Switch extern bool PendingOrder=false; // PendingOrder/InstantExecution Switch extern double PendOrdGap=10; // Gap from High/Low for Pending Orders extern double BreakEven=0; // BreakEven Level in points extern double BreakEvenGap=0; // BreakEven Gap in points extern string Calc_Parameters = " StepMA Parameters "; extern int Length=10; // ATR Period extern double Kv=1.0; // Sensivity Factor extern double StepSize=0; // Constant Step Size (if need) extern bool HighLow=false; // High/Low Mode Switch (more sensitive) extern double Delta=0; // Breakout Depth in points extern double Ks=1.0; // StopLoss Factor extern double Kp=1.0; // TakeProfit Factor extern int Nbars=1000; extern string MM_Parameters = " MoneyManagement by L.Williams "; extern bool MM=false; // ÌÌ Switch extern double Risk=0.15; // Risk Factor extern double LossMax=1000; // Maximum Loss by 1 Lot int trend=0,TradeSignal=0,ExitSignal=0,prevtrend=0,matrend=0,Kz=0,mode=0,Type=0, numords; double Smin0=0,Smax0=0,Smin1=0,Smax1=0,Price=0,PriceStop=0, BuyProfit=0, SellProfit=0; double line0=0,line1=0,bsmin1=0,bsmax1=0, Step=0; double BuyStop=0,SellStop=0,ATRmin=10000,ATRmax=0,bsmax2=0,bsmin2=0; int b=0,cnt=0, digit=0; bool FirstTime = false; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- FirstTime = true; //---- return(0); } // ---- Scan Trades int ScanTrades() { int total = OrdersTotal(); int numords = 0; for(int cnt=0; cnt<total; cnt++) { OrderSelect(cnt, SELECT_BY_POS); if(OrderSymbol() == Symbol() && OrderType()<=OP_SELLSTOP && OrderMagicNumber() == Magic) numords++; } return(numords); } void TrailProfit() { int total=OrdersTotal(); for (cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS); mode=OrderType(); if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic ) { if ( mode==OP_BUY ) { if (Kp > 0) BuyProfit = line0 + Kp*Step/Kv; else BuyProfit = 0; //Print("BuyProfit=",BuyProfit," lin0=",line0," Step=",Step," Kp,Kv=", Kp,",",Kv); if( BuyProfit > OrderTakeProfit()) { OrderModify(OrderTicket(),OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(BuyProfit,digit),0,LightGreen); return(0); } } if ( mode==OP_SELL ) { if (Kp > 0) SellProfit = line0 - Kp*Step/Kv; else SellProfit = 0; //Print("SellProfit=",SellProfit," lin0=",line0," Step=",Step," Kp,Kv=", Kp,",",Kv); if( SellProfit < OrderTakeProfit()) { OrderModify(OrderTicket(),OrderOpenPrice(), OrderStopLoss(), NormalizeDouble(SellProfit, digit),0,Yellow); return(0); } } } } } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { int total,ticket; //---- if ( Trace ) SetTrace(); if (Bars>b) { b=Bars; digit = MarketInfo(Symbol(),MODE_DIGITS); double Lotsi=Lots; if ( MM ) Lotsi=NormalizeDouble(Lots*AccountFreeMargin()*Risk/LossMax,1); if (Lotsi<0.1) Lotsi=0.1; if ( FirstTime ) Nbars = Nbars; else Nbars = 1; for ( int i=Nbars; i>=1;i--) { if(StepSize>0) { Step=Kv*StepSize*Point; } else { double ATR0 = iATR(NULL,0,Length,i); ATRmax=MathMax(ATR0,ATRmax); ATRmin=MathMin(ATR0,ATRmin); Step=0.5*Kv*(ATRmax+ATRmin); } Smin1=Smin0; Smax1=Smax0; if(HighLow) { Smax0=Low[i]+2.0*Step; Smin0=High[i]-2.0*Step; } else { Smax0=Close[i]+2.0*Step; Smin0=Close[i]-2.0*Step; } prevtrend = matrend; //if(Close[i]>Smax1 && Smax1>0) matrend=1; //if(Close[i]<Smin1 && Smin1>0) matrend=-1; if(Close[1]>Smax1) matrend=1; if(Close[1]<Smin1) matrend=-1; //Print(" Close=",Close[1]," smax =",Smax0," smin=",Smin0," smax1 =",Smax1," smin1=",Smin1); line1=line0; if(matrend>0) { if(Smin0<Smin1) Smin0=Smin1; line0=Smin0+Step; } else if (matrend<0) { if(Smax0>Smax1) Smax0=Smax1; line0=Smax0-Step; } //Print (" line0=",line0," line1=",line1," matrend=", matrend, " Step=", Step); if ( i==1 ) FirstTime = false; } bsmin2=bsmin1; bsmax2=bsmax1; bsmin1=Smin0; bsmax1=Smax0; // Trade Signal area if(!SwingTrade) TradeSignal=0; // - BUY if ( Close[1]>iMA(NULL,0,50,0,MODE_EMA,0,1) && line0-line1>Delta*Point && line1>0 && prevtrend<0 ) TradeSignal= 1; // - SELL if ( Close[1]<iMA(NULL,0,50,0,MODE_EMA,0,1) && line1-line0>Delta*Point && line1>0 && prevtrend>0 ) TradeSignal= -1; //Print (" line0=",line0," line1=",line1," matrend=", matrend, " signal=", TradeSignal); // Exit Signal area // - BUY Orders if ( //Close[1]<iMA(NULL,0,50,0,MODE_EMA,0,1) //|| line1-line0>Delta*Point ) ExitSignal= -1; // - SELL Orders if ( //Close[1]>iMA(NULL,0,50,0,MODE_EMA,0,1) //|| line0-line1>Delta*Point ) ExitSignal= 1; if(SwingTrade) ExitSignal=TradeSignal; TrailProfit(); //----------------------------- total=OrdersTotal(); for (cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); mode=OrderType(); if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { // Orders Close area : // - BUY Orders if ( ExitSignal<0 ) { if (mode==OP_BUY) { OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Yellow); } } // - SELL Orders if ( ExitSignal>0 ) { if (mode==OP_SELL) { OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White); } } if (BreakEven > 0) { if (mode==OP_BUY) { BuyStop = OrderStopLoss(); if (Bid-OrderOpenPrice() > Kz*BreakEven*Point) { BuyStop=OrderOpenPrice()+((Kz-1)*BreakEven+BreakEvenGap)*Point; OrderModify(OrderTicket(),OrderOpenPrice(), NormalizeDouble(BuyStop, digit), OrderTakeProfit(),0,LightBlue); Kz=Kz+1; return(0); } } if (mode==OP_SELL) { SellStop = OrderStopLoss(); if (OrderOpenPrice()-Ask > Kz*BreakEven*Point) { SellStop=OrderOpenPrice()-((Kz-1)*BreakEven+BreakEvenGap)*Point; OrderModify(OrderTicket(),OrderOpenPrice(), NormalizeDouble(SellStop, digit), OrderTakeProfit(),0,Orange); Kz=Kz+1; return(0); } } } // Orders Modify Area if (TrailingStopMode > 0) { if (mode==OP_BUY) { if (TrailingStop > 0) BuyStop = Bid - TrailingStop*Point; else if ( Ks > 0) BuyStop = line0 - Ks*Step/Kv; else BuyStop = OrderStopLoss(); if ( BuyStop > OrderStopLoss() || OrderStopLoss()==0) { OrderModify(OrderTicket(),OrderOpenPrice(), NormalizeDouble(BuyStop, digit), OrderTakeProfit(),0,LightGreen); return(0); } } // - SELL Orders if (mode==OP_SELL) { if (TrailingStop > 0) BuyStop = Ask + TrailingStop*Point; else if ( Ks > 0) SellStop = line0 + Ks*Step/Kv; else SellStop = OrderStopLoss(); if( SellStop < OrderStopLoss() || OrderStopLoss()==0) { OrderModify(OrderTicket(),OrderOpenPrice(), NormalizeDouble(SellStop, digit), OrderTakeProfit(),0,Yellow); return(0); } } } } // Closing of Pending Orders if(Close[1]<MathMin(iMA(NULL,0,50,0,MODE_EMA,0,1),line0))int PendClose=1; if(Close[1]>MathMax(iMA(NULL,0,50,0,MODE_EMA,0,1),line0)) PendClose=-1; if (mode==OP_BUYSTOP && PendClose>0 && OrderMagicNumber()==Magic) { OrderDelete(OrderTicket()); return(0); } if (mode==OP_SELLSTOP && PendClose<0 && OrderMagicNumber()==Magic) { OrderDelete(OrderTicket()); return(0); } }//for (cnt=0;cnt<total;cnt++) // Orders open area : // - SELL Orders if (ScanTrades()<1) { if ( trend>=0 && TradeSignal<0 ) { if(PendingOrder) { Type=OP_SELLSTOP; Price=Low[1]-PendOrdGap*Point; PriceStop=Price; } else { Type=OP_SELL; Price=Bid; PriceStop=Ask; } if (InitialStop > 0) SellStop = PriceStop + InitialStop*Point; else if( Ks > 0) SellStop= line0 + Ks*Step/Kv; else SellStop=0; if (TakeProfit > 0) SellProfit = Price - InitialStop*Point; else if (Kp > 0) SellProfit = line0 - Kp*Step/Kv; else SellProfit=0; ticket=OrderSend(Symbol(),Type,Lotsi, NormalizeDouble(Price, digit), Slippage, NormalizeDouble(SellStop, digit), NormalizeDouble(SellProfit, digit), "sell",Magic,0,Red); Kz=1; if(SwingTrade) trend=-1; else {trend=0;} if(ticket<0) { Print("OrderSend failed with error #",GetLastError()); return(0); } return(0); } // - BUY Orders if (trend <=0 && TradeSignal>0) { if(PendingOrder) { Type=OP_BUYSTOP; Price=High[1]+PendOrdGap*Point; PriceStop = Price; } else { Type=OP_BUY; Price=Ask; PriceStop = Bid; } if (InitialStop > 0) BuyStop = PriceStop - InitialStop*Point; else if ( Ks > 0) BuyStop=line0-Ks*Step/Kv; else BuyStop=0; if (TakeProfit > 0) BuyProfit = Price + InitialStop*Point; else if (Kp > 0) BuyProfit = line0 + Kp*Step/Kv; else BuyProfit=0; ticket=OrderSend(Symbol(),Type,Lotsi, NormalizeDouble(Price, digit), Slippage, NormalizeDouble(BuyStop, digit), NormalizeDouble(BuyProfit, digit), "buy",Magic,0,Blue); Kz=1; if(SwingTrade) trend=1; else {trend=0;} if(ticket<0) { Print("OrderSend failed with error #",GetLastError()); return(0); } return(0); } } }//if (Bars>b) //---- //---- return(0); } //+------------------------------------------------------------------+
Sample
Analysis
Market Information Used:
Series array that contains the lowest prices of each bar
Series array that contains the highest prices of each bar
Series array that contains close prices for each bar
Indicator Curves created:
Indicators Used:
Indicator of the average true range
Moving average indicator
Custom Indicators Used:
Order Management characteristics:
Checks for the total of open orders
It can change open orders parameters, due to possible stepping strategy
It Closes Orders by itself
Other Features: