Slopey Graeme: another pipEasy-inspired trend trading EA

Post Reply
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: Graeme: another pipEasy-inspired trend trading EA

Post by SteveHopwood »

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
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: Graeme: another pipEasy-inspired trend trading EA

Post by SteveHopwood »

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
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
alex_forex
Trader
Posts: 438
Joined: Tue Nov 15, 2011 5:46 pm
Location: F R A N C E

Re: Graeme: another pipEasy-inspired trend trading EA

Post by alex_forex »

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

Regards
Alex
MichaelM
Trader
Posts: 117
Joined: Sun Dec 04, 2011 11:04 pm

Re: Graeme: another pipEasy-inspired trend trading EA

Post by MichaelM »

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 **/
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: Graeme: another pipEasy-inspired trend trading EA

Post by SteveHopwood »

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
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
MichaelM
Trader
Posts: 117
Joined: Sun Dec 04, 2011 11:04 pm

Re: Graeme: another pipEasy-inspired trend trading EA

Post by MichaelM »

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;
User avatar
gaheitman
Trader
Posts: 655
Joined: Tue Nov 15, 2011 10:55 pm
Location: Richmond, VA, US

Re: Graeme: another pipEasy-inspired trend trading EA

Post by gaheitman »

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
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: Graeme: another pipEasy-inspired trend trading EA

Post by SteveHopwood »

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
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
MichaelM
Trader
Posts: 117
Joined: Sun Dec 04, 2011 11:04 pm

Re: Graeme: another pipEasy-inspired trend trading EA

Post by MichaelM »

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
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: Graeme: another pipEasy-inspired trend trading EA

Post by SteveHopwood »

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
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
Post Reply

Return to “Automated trading systems”