Jump to content

EA to move stoploss on fractals


Recommended Posts

Thanks for the helps and ideas about programming this EA.

My aim was to write an EA which puts stoploss to previous fractal as it forms naturally support and resistance and if it breaks there can be a possiblity for reversal or retracement.

Actually I took partially the code from the link given by Starting.

http://codebase.mql4.com/ru/1101

You can set the variables:

tmfrm -- which timeframe should be used to look for Fractals (1,5,15...)

frktl_brs -- 3 is minumum (e.g.: 2 higher low than a lower low in the middle)

pipfromfractal -- stoploss distance from fractal (e.g.: 2 or 20(5 digit broker for 2 pips below or above the fractal))

TrailWhileLoss --trailing while in loss

 

Please try it on demo first, as I have tried it just on 2 trades on M1, it worked for me.

 

http://[email protected]/file/213718691/40c8541f/Fractal_trail.html

Link to comment
Share on other sites

Re: EA to move stoploss on fractals

 

Here it is:

 

//+------------------------------------------------------------------+
//|                                                Fractal trail.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        [url]http://www.metaquotes.net[/url] |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, konorti@indo source: http://codebase.mql4.com/ru/1101"
#property link      ""

//int ticket;
extern int tmfrm=5;
extern int frktl_bars=3;
extern int pipfromfractal=20;
extern bool TrailWhileLoss=true;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
 {
//----
  
//----
  return(0);
 }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
 {
//----
  
//----
  return(0);
 }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
 {
//----
  int total = OrdersTotal();
   for(int cnt=0;cnt<total;cnt++)
   {
       OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
   if(OrderSymbol()==Symbol())
  {
  int i, z; // counters
  int ticket = OrderTicket();
  int extr_n; 
  double temp;
  int after_x, be4_x;
  int ok_be4, ok_after; 
  int sell_peak_n, buy_peak_n; 
  
  temp = frktl_bars;
     
  if (MathMod(frktl_bars,2)==0)
  extr_n = temp/2;
  else                
  extr_n = MathRound(temp/2);

  after_x = frktl_bars - extr_n;
  if (MathMod(frktl_bars,2)!=0)
  be4_x = frktl_bars - extr_n;
  else
  be4_x = frktl_bars - extr_n - 1;    
  

  if (OrderType()==OP_BUY)
     {

     for (i=extr_n;i<iBars(Symbol(),tmfrm);i++)
        {
        ok_be4 = 0; ok_after = 0;
        
        for (z=1;z<=be4_x;z++)
           {
           if (iLow(Symbol(),tmfrm,i)>=iLow(Symbol(),tmfrm,i-z)) 
              {
              ok_be4 = 1;
              break;
              }
           }
           
        for (z=1;z<=after_x;z++)
           {
           if (iLow(Symbol(),tmfrm,i)>iLow(Symbol(),tmfrm,i+z)) 
              {
              ok_after = 1;
              break;
              }
           }            
        
        if ((ok_be4==0) && (ok_after==0))                
           {
           sell_peak_n = i; 
           break;
           }
        }
    
     if (TrailWhileLoss==true)
        {   
        if ((iLow(Symbol(),tmfrm,sell_peak_n)-pipfromfractal*Point>OrderStopLoss()) && (iLow(Symbol(),tmfrm,sell_peak_n)-pipfromfractal*Point<Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point))
           {
           if (!OrderModify(ticket,OrderOpenPrice(),iLow(Symbol(),tmfrm,sell_peak_n)-pipfromfractal*Point,OrderTakeProfit(),OrderExpiration()))
           Comment("");
           }
        }
     else
        {
        if ((iLow(Symbol(),tmfrm,sell_peak_n)-pipfromfractal*Point>OrderStopLoss()) && (iLow(Symbol(),tmfrm,sell_peak_n)-pipfromfractal*Point>OrderOpenPrice()) && (iLow(Symbol(),tmfrm,sell_peak_n)-pipfromfractal*Point<Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point))
           {
           if (!OrderModify(ticket,OrderOpenPrice(),iLow(Symbol(),tmfrm,sell_peak_n)-pipfromfractal*Point,OrderTakeProfit(),OrderExpiration()))
           Comment("");
           }
        }
     }
     
  if (OrderType()==OP_SELL)
     {

     for (i=extr_n;i<iBars(Symbol(),tmfrm);i++)
        {
        ok_be4 = 0; ok_after = 0;
        
        for (z=1;z<=be4_x;z++)
           {
           if (iHigh(Symbol(),tmfrm,i)<=iHigh(Symbol(),tmfrm,i-z)) 
              {
              ok_be4 = 1;
              break;
              }
           }
           
        for (z=1;z<=after_x;z++)
           {
           if (iHigh(Symbol(),tmfrm,i)<iHigh(Symbol(),tmfrm,i+z)) 
              {
              ok_after = 1;
              break;
              }
           }            
        
        if ((ok_be4==0) && (ok_after==0))                
           {
           buy_peak_n = i;
           break;
           }
        }        
     
     if (TrailWhileLoss==true)
        {
        if (((iHigh(Symbol(),tmfrm,buy_peak_n)+(pipfromfractal+MarketInfo(Symbol(),MODE_SPREAD))*Point<OrderStopLoss()) || (OrderStopLoss()==0)) && (iHigh(Symbol(),tmfrm,buy_peak_n)+(pipfromfractal+MarketInfo(Symbol(),MODE_SPREAD))*Point>Ask+MarketInfo(Symbol(),MODE_STOPLEVEL)*Point))
           {
           if (!OrderModify(ticket,OrderOpenPrice(),iHigh(Symbol(),tmfrm,buy_peak_n)+(pipfromfractal+MarketInfo(Symbol(),MODE_SPREAD))*Point,OrderTakeProfit(),OrderExpiration()))
           Comment("");
           }
        }      
     else
        {
        if ((((iHigh(Symbol(),tmfrm,buy_peak_n)+(pipfromfractal+MarketInfo(Symbol(),MODE_SPREAD))*Point<OrderStopLoss()) || (OrderStopLoss()==0))) && (iHigh(Symbol(),tmfrm,buy_peak_n)+(pipfromfractal+MarketInfo(Symbol(),MODE_SPREAD))*Point<OrderOpenPrice()) && (iHigh(Symbol(),tmfrm,buy_peak_n)+(pipfromfractal+MarketInfo(Symbol(),MODE_SPREAD))*Point>Ask+MarketInfo(Symbol(),MODE_STOPLEVEL)*Point))
           {
           if (!OrderModify(ticket,OrderOpenPrice(),iHigh(Symbol(),tmfrm,buy_peak_n)+(pipfromfractal+MarketInfo(Symbol(),MODE_SPREAD))*Point,OrderTakeProfit(),OrderExpiration()))
           Comment("");
           }
        }
     }      
  }
 }
//+------------------------------------------------------------------+

//----
  return(0);
 }
//+------------------------------------------------------------------+

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...