EA for modifying stop-loss on orders in line with SAR?

 

Hi, is anyone aware of an EA that can modify your orders by updating the stop loss in line with the Parabolic SAR value? I trade by moving my stop loss to the Parabolic SAR every 15 minutes until I get stopped out in profit but I have to keep changing it manually and an EA to do this would be very usefull.


Thanks,

 

2217

Something like this as an EA will get you started - bit rough but OTTOMH :)

Run it on an M15 to check every 15 minutes, on M5 to check every 5

With MagicNumber=0 it will handle manual trades

If you have scripted or EA trades with a MagicNumber, you could put that in the external value instead of 0

You can run as many copies of this EA on as many charts as you are trading

Personally I would be running CheckEveryTick=true......

//+------------------------------------------------------------------+
//|                                           Move Stops To PSAR.mq4 |
//|                                                               BD |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, SelectFX"
#property link      "http://www.selectfx.net"

extern int MagicNumber=0;
extern double PSARStep = 0.02;
extern double PSARMax =  0.2;
extern bool CheckEveryTick = false;

string strSymbol;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

  strSymbol = Symbol();
  
  if ((!CheckEveryTick) && (Volume[0]==1)) // Go for it on first tick only
    {
     MoveStopsToPSAR(MagicNumber);  
     return(0);
    }
    
  if(CheckEveryTick) MoveStopsToPSAR(MagicNumber); // Check for Stop move every tick
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

void MoveStopsToPSAR(int iMN)
{
int icnt, itotal;
double ActiveSAR;

  double Close_1 = Close[1];

  ActiveSAR = iSAR(NULL,0,PSARStep,PSARMax,1);
 

itotal=OrdersTotal();

   for(icnt=0;icnt<itotal;icnt++) 
     {                               // order loop boundary
      OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
       // check for opened position, symbol & MagicNumber
      if(OrderType()==OP_SELL && OrderSymbol()==strSymbol  && OrderMagicNumber()==iMN)  
        {         
           if ((ActiveSAR > Close_1) && (ActiveSAR!=OrderStopLoss())) OrderModify(OrderTicket(),OrderOpenPrice(),ActiveSAR,OrderTakeProfit(),0,Yellow);
        }
        
        
       if(OrderType()==OP_BUY && OrderSymbol()==strSymbol  && OrderMagicNumber()==iMN)  
        {       
          if ((ActiveSAR < Close_1) && (ActiveSAR!=OrderStopLoss())) OrderModify(OrderTicket(),OrderOpenPrice(),ActiveSAR,OrderTakeProfit(),0,White);   
        }

     }  // order loop boundary

return(0);
     
}
 
BarrowBoy:

2217

Something like this as an EA will get you started - bit rough but OTTOMH :)

Run it on an M15 to check every 15 minutes, on M5 to check every 5

With MagicNumber=0 it will handle manual trades

If you have scripted or EA trades with a MagicNumber, you could put that in the external value instead of 0

You can run as many copies of this EA on as many charts as you are trading

Personally I would be running CheckEveryTick=true......

Wow, cheers BarrowBoy, just exactly what I needed and works perfectly!

Thank you so much, this helps me considerably!!

 
2217:

Wow, cheers BarrowBoy, just exactly what I needed and works perfectly!

Thank you so much, this helps me considerably!!

Sorry BarrowBoy, this seems to be lagging behind by one PSAR dot. The stop loss is moving perfectly with the SAR movement but only at the previous SAR dot and not the current one. Can you think what might be causing this?


Cheers,

 

> I trade by moving my stop loss to the Parabolic SAR every 15 minutes

I took that to mean that you adjust to the last closed bar?

So I defaulted the code to do that!

To move the stop each time a new dot forms, set CheckEveryTick = true

 
Thanks for your reply but I had CheckEveryTick at true all the time as you recommended but still it lags. Sorry if my explanation was unclear, I meant that on M15 chart, as soon as the new bar opened with the new SAR dot, I'd change the stop loss then. Would there be something else I'm missing? Cheers,
 

You are correct...

Didnt read my code all the way through :(

//+------------------------------------------------------------------+
//|                                   Move Stops To PSAR Ver 1.1.mq4 |
//|                                                               BD |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, SelectFX"
#property link      "http://www.selectfx.net"

extern int MagicNumber=0;
extern double PSARStep = 0.02;
extern double PSARMax =  0.2;
extern bool CheckEveryTick = true;


string strSymbol;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

  strSymbol = Symbol();
  
  if ((!CheckEveryTick) && (Volume[0]==1)) // Go for it on first tick only
    {
     MoveStopsToPSAR(MagicNumber, 1);  
     return(0);
    }
    
  if(CheckEveryTick) MoveStopsToPSAR(MagicNumber, 0); // Check for Stop move every tick
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

void MoveStopsToPSAR(int iMN, int iShift)
{
int icnt, itotal;
double ActiveSAR;

  double Close_1 = Close[iShift];

  ActiveSAR = iSAR(NULL,0,PSARStep,PSARMax,iShift);


itotal=OrdersTotal();

   for(icnt=0;icnt<itotal;icnt++) 
     {                               // order loop boundary
      OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
       // check for opened position, symbol & MagicNumber
      if(OrderType()==OP_SELL && OrderSymbol()==strSymbol  && OrderMagicNumber()==iMN)  
        {         
           if ((ActiveSAR > Close_1) && (ActiveSAR!=OrderStopLoss())) OrderModify(OrderTicket(),OrderOpenPrice(),ActiveSAR,OrderTakeProfit(),0,Yellow);
        }
        
        
       if(OrderType()==OP_BUY && OrderSymbol()==strSymbol  && OrderMagicNumber()==iMN)  
        {       
          if ((ActiveSAR < Close_1) && (ActiveSAR!=OrderStopLoss())) OrderModify(OrderTicket(),OrderOpenPrice(),ActiveSAR,OrderTakeProfit(),0,White);   
        }

     }  // order loop boundary

return(0);
     
}
 
BarrowBoy:

You are correct...

Didnt read my code all the way through :(


BarrowBoy:

You are correct...

Didnt read my code all the way through :(


Cheers BarrowBoy, moving to the existing SAR dot now. You're the man!!
 
2217 wrote >>

Cheers BarrowBoy, moving to the existing SAR dot now. You're the man!!

I tried it too and it works like magic. Thanks a zillion BarrowBoy for your help as I traded manually like 2217 did. I also want it to stop and reverse as I trade on higher time frames like 4Hhly and Daily. Another thing was the initial stop-loss which should move immediately to +1 pip when price moves 15pips, and the SL thereafter must follow the PSAR. This would make my trade safer even if I am taken out at 1pip. I have another EA to do just this, but can we have 2 EAs on the same chart for the same trade? Also, can I use your EA on any time frame? Thanks for your effort and help. Appreciate it.
 

PP

The code above is just a generic example EA and can be used on any pair & timeframe.

You cant have two EA's on a chart, the second one added unloads the first one - you can run scripts OK on a chart with an EA already on

You could be trading a pair on one or more charts and have this EA on another chart (of the same pair) and it would move the stops by reference to MagicNumber.

Note that MagicNumber=0 handles manually placed trades (or EA & scripted trades where no MagicNumber is set)

I am always surprised at the amount of interest in the PSAR!
Agreed it works OK on some pairs & timeframes but always suffers from lag :(

I have crunched numbers on 550+ EA's and PSAR hardly ever features well.

Either trailing the stop from the SL or move stop to break even with gain of x pips and then trail from there by y pips is almost always better IME

<advert alert>

It so happens that I happen to have an EA to do the 'TS move' or' BE then TS' that can be used on any pair/timeframe/MagicNumber

You can run several copies of the EA (on separate charts) to handle multiple traded pairs - see

http://www.selectfx.net/products.htm

Maybe the next version could do the "...initial stop-loss which should move immediately to +1 pip when price moves 15pips, and the SL thereafter must follow the PSAR..."

</advert alert>

Good Luck

-BB-

 
BarrowBoy wrote >>

PP

The code above is just a generic example EA and can be used on any pair & timeframe.

You cant have two EA's on a chart, the second one added unloads the first one - you can run scripts OK on a chart with an EA already on

You could be trading a pair on one or more charts and have this EA on another chart (of the same pair) and it would move the stops by reference to MagicNumber.

Note that MagicNumber=0 handles manually placed trades (or EA & scripted trades where no MagicNumber is set)

I am always surprised at the amount of interest in the PSAR!
Agreed it works OK on some pairs & timeframes but always suffers from lag :(

I have crunched numbers on 550+ EA's and PSAR hardly ever features well.

Either trailing the stop from the SL or move stop to break even with gain of x pips and then trail from there by y pips is almost always better IME

<advert alert>

It so happens that I happen to have an EA to do the 'TS move' or' BE then TS' that can be used on any pair/timeframe/MagicNumber

You can run several copies of the EA (on separate charts) to handle multiple traded pairs - see

http://www.selectfx.net/products.htm

Maybe the next version could do the "...initial stop-loss which should move immediately to +1 pip when price moves 15pips, and the SL thereafter must follow the PSAR..."

</advert alert>

Good Luck

-BB-

Thanks BarrowBoy for your prompt reply. What I am actually looking for are SLs at each PSAR levels, with the initial one to capture +1 on a move of the first 15 pips, and 'stop and reverse' facility. I am quite fascinated by PSAR on higher time frames with the settings 0.9/0.2. I hardly find time to trade so if I had an EA to help me out, it would be wonderful. I sincerely appreciate your effort and helping nature. May God bless you.

PP

Reason: