StepMAExpert_v1.42_usdchf

Author: Copyright � 2005, TrendLaboratory Ltd.
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt Closes Orders by itself It automatically opens orders when conditions are reached
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites

Profitability Reports

AUD/USD Oct 2024 - Jan 2025
80.00 %
Total Trades 58
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff -2.23
Gross Profit 512.40
Gross Loss -642.00
Total Net Profit -129.60
-100%
-50%
0%
50%
100%
StepMAExpert_v1.42_usdchf
//+------------------------------------------------------------------+
//|                                           StepMAExpert_v1.42.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.42 ----";

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.2;
extern double     InitialStop = 84;        // Initial Stop Value
extern double     TakeProfit = 0;         // Take Profit Value
extern int        TrailingStopMode = 1;   // Trailing Stop Mode Switch   
extern double     TrailingStop=0;         // Classic Trailing Stop Value
extern bool       SwingTrade = true;     // Swing Trade Switch
extern bool       PendingOrder=false;     // PendingOrder/InstantExecution Switch
extern double     PendOrdGap=10;          // Gap from High/Low for Pending Orders
extern double     BreakEven=90;            // BreakEven Level in points
extern double     BreakEvenGap=5;         // 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=15;             // Constant Step Size (if need)
extern bool       HighLow=false;          // High/Low Mode Switch (more sensitive)
extern double     DeltaLong =5;           // Breakout Depth for Long in points
extern double     DeltaShort=13;           // Breakout Depth for Short in points
extern double     Ks=8.0;                 // StopLoss Factor
extern double     Kp=7.5;                 // TakeProfit Factor
extern int        FilterPeriod=56;        // Filter Period (EMA Period)
extern int        Nbars=1000;             // Bars Number for calculation during initialization

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, point=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 = OrderTakeProfit();
            //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 = OrderTakeProfit();
            //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);
   point  = MarketInfo(Symbol(),MODE_POINT);  
   
   double Lotsi=Lots;     
      
   if ( MM ) Lotsi=NormalizeDouble(Lots*AccountFreeMargin()*Risk/LossMax,1);   
   if (Lotsi<0.1) Lotsi=0.1;
       
   if ( Nbars == 0 ) int NBars = Bars-Length-1; else NBars = Nbars-Length-1;
   if ( !FirstTime ) NBars = 1;
   
   int MA_Mode = 0;
   
   for ( int i=NBars; i>=1;i--)
   {
     
      if(StepSize>0) 
      {
      Step=Kv*StepSize*point; 
      }
      else
      {
      double AvgRange=0;
	     for (int j=Length-1;j>=0;j--)
	     { 
            if(MA_Mode==0) double alfa= 1.0; else alfa= 1.0+1.0*(Length-i)/Length; 
            AvgRange+= alfa*(High[j+i]-Low[j+i]);
        }
	   double ATR0 = AvgRange/Length;
   
	   if (ATR0>ATRmax) ATRmax=ATR0;
	   if (ATR0<ATRmin) ATRmin=ATR0;
	
	   Step=MathRound(0.5*Kv*(ATRmax+ATRmin)/point)*point;
	   
	   }  
	  	
	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[1]>Smax1) matrend=1; 
	if(Close[1]<Smin1) matrend=-1;
	  
	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;
	   }	  
   bsmin2=bsmin1;
	bsmax2=bsmax1;
	  
	bsmin1=Smin0;
	bsmax1=Smax0; 
      
   if ( i==1 ){ FirstTime = false;}
   }

// Trade Signal area	
      if(!SwingTrade) TradeSignal=0; 	
// - BUY      
      if 
      (
      Close[1]>iMA(NULL,0,FilterPeriod,0,MODE_EMA,0,1)
      &&
      line0-line1>DeltaLong*point
      &&
      line1>0
      && 
      prevtrend<0
      )  TradeSignal= 1;
// - SELL      
      if 
      (
      Close[1]<iMA(NULL,0,FilterPeriod,0,MODE_EMA,0,1)
      &&
      line1-line0>DeltaShort*point
      &&
      line1>0
      && 
      prevtrend>0
      )  TradeSignal= -1;

// Exit Signal area	
       	
// - BUY Orders      
      if 
      (
      //Close[1]<iMA(NULL,0,50,0,MODE_EMA,0,1)
      //||
      line1-line0>0
      )  ExitSignal= -1;
// - SELL Orders      
      if 
      (
      //Close[1]>iMA(NULL,0,50,0,MODE_EMA,0,1)
      //||
      line0-line1>0
      )  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 && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
      {
	 	OrderDelete(OrderTicket());
	   return(0);
	   }
	    
      if (mode==OP_SELLSTOP && PendClose<0 && OrderSymbol()==Symbol() && 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 - TakeProfit*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 + TakeProfit*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);
  }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---