Detected Country () : English
Automatic Set Stoploss By Fractal - codingforex.com

Welcome to CodingForex.Com!

Knowledge portal where you can learn how to create forex indicators and robot.

Click button below to choose level that suite you:

Automatic Set Stoploss By Fractal

Samples [2018-05-27 20:29:11] - by hillzx
Featured Image

Hello fellow traders. This time we will create an EA to install and shift the Stoploss according to the current fractal price. The way is quite simple.

First we create an extern variable:

  • AutoMoveSL = Set True if you want the robot to install automatic stoploss
extern int Corner=1;
extern int XPos=20;
extern int YPos=10;
extern bool AutoMoveSL=false;
int oldTime;
int i;
double frup,frdown;

 

Create OnInit, OnDeinit, DeleteObjects modules:

  • DeleteObjects is used to clear the screen
  • UpdateFractal is used for updated fractal price updates
  • WriteText is used to write down the current fractal price in the corner of the chart
  • Oldtime is used for bookmarks, so the price is updated only when a new candle occurs
int OnInit()
  {
   DeleteObjects();
   UpdateFractal();
   WriteText();
   oldTime=Time[0];
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   DeleteObjects();
  }
  
void DeleteObjects()
  {
   for(int i=ObjectsTotal()-1; i>-1; i--)
     {
      if(StringFind(ObjectName(i),"HiLLzX_Auto_Fractal_SL")>=0) ObjectDelete(ObjectName(i));
     }
  }



Next, setting of stoploss:

void ModifyStopLoss(double ldStopLoss)
 {
   bool fmSL;
   fmSL=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
 }
 
 void SetSL(){
 
   int itotal;
   itotal=OrdersTotal();

   for(int cnt=itotal-1;cnt>=0;cnt--)
   {
      bool tmpselect=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      
      if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
        {
         ModifyStopLoss(frdown);
        }

      if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
        {
         ModifyStopLoss(frup);
        }
   }

 

Next, create a module to write text on chart:

void WriteText(){

ObjectCreate("HiLLzX_Auto_Fractal_SL_1",OBJ_LABEL,0,0,0,0,0);
ObjectSet("HiLLzX_Auto_Fractal_SL_1",OBJPROP_CORNER,Corner);
ObjectSet("HiLLzX_Auto_Fractal_SL_1",OBJPROP_XDISTANCE,XPos);
ObjectSet("HiLLzX_Auto_Fractal_SL_1",OBJPROP_YDISTANCE,0 + YPos);
ObjectSetText("HiLLzX_Auto_Fractal_SL_1","Latest Upper Fractal : " + DoubleToStr(frup,Digits) ,16,"Rockwell",Yellow);

ObjectCreate("HiLLzX_Auto_Fractal_SL_2",OBJ_LABEL,0,0,0,0,0);
ObjectSet("HiLLzX_Auto_Fractal_SL_2",OBJPROP_CORNER,Corner);
ObjectSet("HiLLzX_Auto_Fractal_SL_2",OBJPROP_XDISTANCE,XPos);
ObjectSet("HiLLzX_Auto_Fractal_SL_2",OBJPROP_YDISTANCE,20 + YPos);
ObjectSetText("HiLLzX_Auto_Fractal_SL_2","Latest Lower Fractal : " + DoubleToStr(frdown,Digits) ,16,"Rockwell",Yellow);

}

 

Next, create module to update fractal price:

void UpdateFractal(){

   i = 1;
   frup=0;
   frdown=0;

   
   while(i <= Bars)
     {   
     
     if (frup==0){
         frup = iFractals(NULL, 0, MODE_UPPER, i);
         
         if(frup > 0) 
         {
            frup = High[i]; 
         }
      }
      
     if (frdown==0){
         frdown = iFractals(NULL, 0, MODE_LOWER, i);
         
         if(frdown > 0) 
         {
           frdown = Low[i]; 
         }
      }
       
       i++;
      
      if ( (frup!=0) && (frdown!=0) ){break;}
      
     }   
}

 

Finally, create the main formula in OnTick :

  • Every new candle appeared, do UpdateFractal + WriteText. If AutoMoveSL = True then set Stoploss automatically
void OnTick()
  {
//---
   if(oldTime != Time[0] )
   {
      oldTime = Time[0];
      UpdateFractal();
      WriteText();
      if (AutoMoveSL){SetSL();}
   }
   
  }



Done, the EA look will look like this:

automoveslbyfractal

 

You can enable automatic Stoploss in Input Tab:

automoveslbyfractalinput

 

If you need the complete source code, please copy and paste the code below:

//+------------------------------------------------------------------+
//|                                              Auto Fractal SL.mq4 |
//|                                                 hillzx@gmail.com |
//|                                                 hillzx@gmail.com |
//+------------------------------------------------------------------+
#property copyright "hillzx@gmail.com"
#property link      "hillzx@gmail.com"
#property version   "1.00"
#property strict

extern int Corner=1;
extern int XPos=20;
extern int YPos=10;
extern bool AutoMoveSL=false;

int oldTime;
int i;
double frup,frdown;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   DeleteObjects();
   UpdateFractal();
   WriteText();
   oldTime=Time[0];
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   DeleteObjects();
  }
  
void DeleteObjects()
  {
   for(int i=ObjectsTotal()-1; i>-1; i--)
     {
      if(StringFind(ObjectName(i),"HiLLzX_Auto_Fractal_SL")>=0) ObjectDelete(ObjectName(i));
     }
  }
  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(oldTime != Time[0] )
   {
      oldTime = Time[0];
      UpdateFractal();
      WriteText();
      if (AutoMoveSL){SetSL();}
   }
   
  }
//+------------------------------------------------------------------+

void UpdateFractal(){

   i = 1;
   frup=0;
   frdown=0;

   
   while(i <= Bars)
     {   
     
     if (frup==0){
         frup = iFractals(NULL, 0, MODE_UPPER, i);
         
         if(frup > 0) 
         {
            frup = High[i]; 
         }
      }
      
     if (frdown==0){
         frdown = iFractals(NULL, 0, MODE_LOWER, i);
         
         if(frdown > 0) 
         {
           frdown = Low[i]; 
         }
      }
       
       i++;
      
      if ( (frup!=0) && (frdown!=0) ){break;}
      
     }   
}

void WriteText(){

ObjectCreate("HiLLzX_Auto_Fractal_SL_1",OBJ_LABEL,0,0,0,0,0);
ObjectSet("HiLLzX_Auto_Fractal_SL_1",OBJPROP_CORNER,Corner);
ObjectSet("HiLLzX_Auto_Fractal_SL_1",OBJPROP_XDISTANCE,XPos);
ObjectSet("HiLLzX_Auto_Fractal_SL_1",OBJPROP_YDISTANCE,0 + YPos);
ObjectSetText("HiLLzX_Auto_Fractal_SL_1","Latest Upper Fractal : " + DoubleToStr(frup,Digits) ,16,"Rockwell",Yellow);

ObjectCreate("HiLLzX_Auto_Fractal_SL_2",OBJ_LABEL,0,0,0,0,0);
ObjectSet("HiLLzX_Auto_Fractal_SL_2",OBJPROP_CORNER,Corner);
ObjectSet("HiLLzX_Auto_Fractal_SL_2",OBJPROP_XDISTANCE,XPos);
ObjectSet("HiLLzX_Auto_Fractal_SL_2",OBJPROP_YDISTANCE,20 + YPos);
ObjectSetText("HiLLzX_Auto_Fractal_SL_2","Latest Lower Fractal : " + DoubleToStr(frdown,Digits) ,16,"Rockwell",Yellow);

}

void ModifyStopLoss(double ldStopLoss)
 {
   bool fmSL;
   fmSL=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
 }
 
 void SetSL(){
 
   int itotal;
   itotal=OrdersTotal();

   for(int cnt=itotal-1;cnt>=0;cnt--)
   {
      bool tmpselect=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      
      if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
        {
         ModifyStopLoss(frdown);
        }

      if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
        {
         ModifyStopLoss(frup);
        }
   }
 }

 


Let us try, do not forget to share and follow our facebook and our youtube channel, happy trading!





- Provided by HiLLzSoft Studio -