stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Slopey Graeme: another pipEasy-inspired trend trading EA
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=223
Page 8 of 14
Author:  SteveHopwood [ Fri Jan 20, 2012 8:05 am ]
Post subject:  Re: Graeme: another pipEasy-inspired trend trading EA

cla4ss wrote:Hi Steve. Thank you for the EA. Get this error all the time while testing "Graeme EURUSD,H1: OrderModify error 130". Can you plese fix it?
Nope. Sorry. Never get it here and never have done, so nothing I can do.

:D
Author:  SteveHopwood [ Sun Jan 22, 2012 7:25 pm ]
Post subject:  Re: Graeme: another pipEasy-inspired trend trading EA

Latest update in post 1.

I have added code to the Rsi trend detection that ensures Rsi is travelling in the right direction as well as being outside the 45-55 band. The relevant input is RsiLookbackCandles, which tells the bot how many RsiTdTf candles to look back for the comparison.

I have also added a Caterpillar trailing stop, but this is experimental and probably buggy. Here is what is supposed to happen:
  • At the start of each new CatTimeFrame candle, move the stop of an open trade one candle to the right - low of the candle for a buy, and high for a sell. This means the stops of a basket of trade will move along caterpillar style, rather than all bunching x pips below the price (buy) or above (sell).
    G uses Global Variables to keep track of the open time of the relevant candle so it knows which shift value to use. G cannot apply a cat stop to existing trades, as they do not have a GV.
:D
Author:  alex_forex [ Sun Jan 22, 2012 9:35 pm ]
Post subject:  Re: Graeme: another pipEasy-inspired trend trading EA

Nice update Steve, thanks
Will update tomorrow on my actual demo.

Regards
Alex
Author:  MichaelM [ Mon Jan 23, 2012 4:28 am ]
Post subject:  Re: Graeme: another pipEasy-inspired trend trading EA

Hi Steve,

Been playing around with the Risk based lot size code.

I've got it working for G so that the lot size is always divisible by 2:

Add the following external variable:

Code: Select all

extern int     LotDivider = 2;   // <<---- MichaelM ADD (don't change the value for Graeme!)
...then UNcomment the "Stop Loss calculator" line in both HasBuyFilled() and HasSellFilled()
i.e. HasBuyFilled()

Code: Select all

//Stop loss calculator
if (StopLoss > 0) stop = NormalizeDouble(price - (StopLoss * Point), Digits);  // <<---- MichaelM MOD (removed comment symbols)
...then add the following line after the "Risk based lot calculator" line as follows

Code: Select all

//Risk based lot calculator
if (RiskPercent > 0) SendLot = CalculateLotSize(stop, price);
stop = 0;  // <<---- MichaelM ADD (To preserve the hidden SL feature)
...and finally modify the CalculateLotSize function as follows:

Code: Select all

double CalculateLotSize(double price1, double price2)
{
   //Calculate the lot size by risk. Code kindly supplied by jmw1970. Nice one jmw.
   
   if (price1 == 0 || price2 == 0) return(Lot);//Just in case
   
   double FreeMargin = AccountFreeMargin();
   double TickValue = MarketInfo(Symbol(),MODE_TICKVALUE) ;
   double LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);
   
   double SLPts = MathAbs(price1 - price2);
   SLPts/= Point;
   
   double Exposure = SLPts * TickValue; // Exposure based on 1 full lot

   double AllowedExposure = (FreeMargin * RiskPercent) / 100;
   
   int TotalSteps = ((AllowedExposure / Exposure) / LotStep);
   double LotSize = TotalSteps * LotStep;
   
   /** MichaelM ADD START **/
   if (LotDivider > 0) {
     TotalSteps = MathFloor(TotalSteps / LotDivider) * LotDivider;
     LotSize = TotalSteps * LotStep;
   }
   /** MichaelM ADD END **/
   
   double MinLots = MarketInfo(Symbol(), MODE_MINLOT);
   double MaxLots = MarketInfo(Symbol(), MODE_MAXLOT);
   
   if (LotSize < MinLots) LotSize = MinLots;
   if (LotSize > MaxLots) LotSize = MaxLots;
   
   return(LotSize);

}//double CalculateLotSize(double price1, double price1)
To see the actual lotsize G wants to use, modify the EA comments code as follows:

Code: Select all

/** MichaelM MOD START **/
if (RiskPercent > 0) {
   ScreenMessage = StringConcatenate(ScreenMessage,Gap, "Lot size based on ", StopLoss, " points and ", RiskPercent, "% risk: ", CalculateLotSize(Bid, Bid+(StopLoss*Point)));
} else {
   ScreenMessage = StringConcatenate(ScreenMessage,Gap, "Lot size: ", Lot);
}
ScreenMessage = StringConcatenate(ScreenMessage," (Criminal's minimum lot size: ", MarketInfo(Symbol(), MODE_MINLOT), ")", NL);
/** MichaelM MOD END **/
Author:  SteveHopwood [ Tue Jan 24, 2012 12:10 am ]
Post subject:  Re: Graeme: another pipEasy-inspired trend trading EA

MichaelM wrote:Hi Steve,

Been playing around with the Risk based lot size code.

I've got it working for G so that the lot size is always divisible by 2:

Add the following external variable:

Code: Select all

extern int     LotDivider = 2;   // <<---- MichaelM ADD (don't change the value for Graeme!)
...then UNcomment the "Stop Loss calculator" line in both HasBuyFilled() and HasSellFilled()
i.e. HasBuyFilled()

Code: Select all

//Stop loss calculator
if (StopLoss > 0) stop = NormalizeDouble(price - (StopLoss * Point), Digits);  // <<---- MichaelM MOD (removed comment symbols)
...then add the following line after the "Risk based lot calculator" line as follows

Code: Select all

//Risk based lot calculator
if (RiskPercent > 0) SendLot = CalculateLotSize(stop, price);
stop = 0;  // <<---- MichaelM ADD (To preserve the hidden SL feature)
...and finally modify the CalculateLotSize function as follows:

Code: Select all

double CalculateLotSize(double price1, double price2)
{
   //Calculate the lot size by risk. Code kindly supplied by jmw1970. Nice one jmw.
   
   if (price1 == 0 || price2 == 0) return(Lot);//Just in case
   
   double FreeMargin = AccountFreeMargin();
   double TickValue = MarketInfo(Symbol(),MODE_TICKVALUE) ;
   double LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);
   
   double SLPts = MathAbs(price1 - price2);
   SLPts/= Point;
   
   double Exposure = SLPts * TickValue; // Exposure based on 1 full lot

   double AllowedExposure = (FreeMargin * RiskPercent) / 100;
   
   int TotalSteps = ((AllowedExposure / Exposure) / LotStep);
   double LotSize = TotalSteps * LotStep;
   
   /** MichaelM ADD START **/
   if (LotDivider > 0) {
     TotalSteps = MathFloor(TotalSteps / LotDivider) * LotDivider;
     LotSize = TotalSteps * LotStep;
   }
   /** MichaelM ADD END **/
   
   double MinLots = MarketInfo(Symbol(), MODE_MINLOT);
   double MaxLots = MarketInfo(Symbol(), MODE_MAXLOT);
   
   if (LotSize < MinLots) LotSize = MinLots;
   if (LotSize > MaxLots) LotSize = MaxLots;
   
   return(LotSize);

}//double CalculateLotSize(double price1, double price1)
To see the actual lotsize G wants to use, modify the EA comments code as follows:

Code: Select all

/** MichaelM MOD START **/
if (RiskPercent > 0) {
   ScreenMessage = StringConcatenate(ScreenMessage,Gap, "Lot size based on ", StopLoss, " points and ", RiskPercent, "% risk: ", CalculateLotSize(Bid, Bid+(StopLoss*Point)));
} else {
   ScreenMessage = StringConcatenate(ScreenMessage,Gap, "Lot size: ", Lot);
}
ScreenMessage = StringConcatenate(ScreenMessage," (Criminal's minimum lot size: ", MarketInfo(Symbol(), MODE_MINLOT), ")", NL);
/** MichaelM MOD END **/
Fantastic Michael. Many thanks. I shall add all this tomorrow.

Cheers

:D
Author:  MichaelM [ Tue Jan 24, 2012 3:54 am ]
Post subject:  Re: Graeme: another pipEasy-inspired trend trading EA

No worries.

I also found an issue that's somehow related to the crappiness of Empty4.

This code here inside init():

Code: Select all

   StopLoss*= multiplier;
   .
   .
   .
   MaxPendingPipsFromMarket*= multiplier;
   MinPipsBetweenTrades*= multiplier;
   TpAdjustment*= multiplier;
   MinTp*= multiplier;
will keep multiplying and multiplying each time you bring up the Expert properties and press okay, because they are not external variables.

To see this for yourself, implement my Risk based lot size code mods above and use a RiskPercent > 0
Then open up the expert properties and press okay and repeat.
You will notice each time you do this the *= multiplier; code will add an extra 0 say from, "Lot size based on 1000 points" (where 1000 points is the StopLoss variable hardcoded to 100 pips)...

...to instead making it say, "1000000 points" after you repeat the step three times.

To get around this, you will need to modify that code as follows:

Code: Select all

   StopLoss = 100;
   StopLoss*= multiplier;

   MaxPendingPipsFromMarket=150;
   MaxPendingPipsFromMarket*= multiplier;
   
   MinPipsBetweenTrades=90;
   MinPipsBetweenTrades*= multiplier;
   
   TpAdjustment=100;
   TpAdjustment*= multiplier;
   
   MinTp=50;
   MinTp*= multiplier;
Author:  gaheitman [ Tue Jan 24, 2012 7:06 am ]
Post subject:  Re: Graeme: another pipEasy-inspired trend trading EA

MichaelM wrote:No worries.

I also found an issue that's somehow related to the crappiness of Empty4.

This code here inside init():

Code: Select all

   StopLoss*= multiplier;
   .
   .
   .
   MaxPendingPipsFromMarket*= multiplier;
   MinPipsBetweenTrades*= multiplier;
   TpAdjustment*= multiplier;
   MinTp*= multiplier;
will keep multiplying and multiplying each time you bring up the Expert properties and press okay, because they are not external variables.

To see this for yourself, implement my Risk based lot size code mods above and use a RiskPercent > 0
Then open up the expert properties and press okay and repeat.
You will notice each time you do this the *= multiplier; code will add an extra 0 say from, "Lot size based on 1000 points" (where 1000 points is the StopLoss variable hardcoded to 100 pips)...

...to instead making it say, "1000000 points" after you repeat the step three times.

To get around this, you will need to modify that code as follows:

Code: Select all

   StopLoss = 100;
   StopLoss*= multiplier;

   MaxPendingPipsFromMarket=150;
   MaxPendingPipsFromMarket*= multiplier;
   
   MinPipsBetweenTrades=90;
   MinPipsBetweenTrades*= multiplier;
   
   TpAdjustment=100;
   TpAdjustment*= multiplier;
   
   MinTp=50;
   MinTp*= multiplier;
Good catch. Yeah, the sequence of events after bringing up the Expert Properties is a bit of a mystery. Personally, I think it should do a complete restart of the application, but all it seems to do is allow you to modify the externs and then it runs deinit() followed by init(). It doesn't reset global variables to their initial values (as shown here) and doesn't reset static variables in any procedures.

George
Author:  SteveHopwood [ Tue Jan 24, 2012 7:59 am ]
Post subject:  Re: Graeme: another pipEasy-inspired trend trading EA

MichaelM wrote:No worries.

I also found an issue that's somehow related to the crappiness of Empty4.

This code here inside init():

Code: Select all

   StopLoss*= multiplier;
   .
   .
   .
   MaxPendingPipsFromMarket*= multiplier;
   MinPipsBetweenTrades*= multiplier;
   TpAdjustment*= multiplier;
   MinTp*= multiplier;
will keep multiplying and multiplying each time you bring up the Expert properties and press okay, because they are not external variables.

To see this for yourself, implement my Risk based lot size code mods above and use a RiskPercent > 0
Then open up the expert properties and press okay and repeat.
You will notice each time you do this the *= multiplier; code will add an extra 0 say from, "Lot size based on 1000 points" (where 1000 points is the StopLoss variable hardcoded to 100 pips)...

...to instead making it say, "1000000 points" after you repeat the step three times.

To get around this, you will need to modify that code as follows:

Code: Select all

   StopLoss = 100;
   StopLoss*= multiplier;

   MaxPendingPipsFromMarket=150;
   MaxPendingPipsFromMarket*= multiplier;
   
   MinPipsBetweenTrades=90;
   MinPipsBetweenTrades*= multiplier;
   
   TpAdjustment=100;
   TpAdjustment*= multiplier;
   
   MinTp=50;
   MinTp*= multiplier;
Thanks again Michael. I am not going to do anything about this because it has never reared its head as a problem.

Cheers

:D
Author:  MichaelM [ Wed Jan 25, 2012 2:33 am ]
Post subject:  Re: Graeme: another pipEasy-inspired trend trading EA

Hi Steve,

Just noticed that this line will be affected by my risk based lot size code mod as it's directly referencing the variable "Lot"

Code: Select all

if (Bid >= take && take > 0 && OrderLots() == Lot) HalfCloseThisTrade = true;
My quick fix is the following as Empty4 changes the order comment when partially closing a trade:

Code: Select all

if (Bid >= take && take > 0 && OrderComment() == TradeComment) HalfCloseThisTrade = true;
Don't forget to do this for the Sell trade section as well!

MichaelM
Author:  SteveHopwood [ Wed Jan 25, 2012 8:21 am ]
Post subject:  Re: Graeme: another pipEasy-inspired trend trading EA

MichaelM wrote:Hi Steve,

Just noticed that this line will be affected by my risk based lot size code mod as it's directly referencing the variable "Lot"

Code: Select all

if (Bid >= take && take > 0 && OrderLots() == Lot) HalfCloseThisTrade = true;
My quick fix is the following as Empty4 changes the order comment when partially closing a trade:

Code: Select all

if (Bid >= take && take > 0 && OrderComment() == TradeComment) HalfCloseThisTrade = true;
Don't forget to do this for the Sell trade section as well!

MichaelM
The problem here is that G has to know when half the trade has already been closed so he does not keep on closing half the existing position. I did not see half-closure working with risk-based lot sizes, but I do not like them and so do not use them.

Yours is a simple but effective solution to this problem. I should have remembered that a half-close changes the comment. Nice one again.

Cheers

:D
All times are UTC Page 8 of 14