Either download V 1a from post 1, or make the edit yourself - I describe how in post 2. Go on, have a play folks. You can always re-download tomorrow if you muck things up. You really, really, really will benefit from learning a bit of basic code editing.SteveHopwood wrote:Nice one Jason, thanks.theforexedge wrote:Steve
Can you post the code for adding the HedgeLotSize as I can only see it at line 2942 in the excluded code. Not sure if this should be included or not? Just slowly trying to understand the code as you suggested![]()
Anyway thanks for coding such a great EA..and enjoying the ride![]()
Jason![]()
I added the hedge lot size code to CountOpenTrades() and then thought that it would be a good idea to add all the position picture code from my existing shells, so I commented out the original void CountOpenTrades() so I would not lose anything, and imported the function from my shell ea's forgetting to add back in the HedgeLotSize stuff.
I am about to upload a fixed version, but here is the new function. Do a search for HedgeLotSize and you will see where it is initialised to zero and then set to the lot size of the trade furthest back in time. I coded it that way on the assumption that the trade furthest back would be the original trade, or close to it, and so would have the correct lot size.
Cheers
Code: Select all
void CountOpenTrades() { //Not all these will be needed. Which ones are depends on the individual EA. //Market Buy trades BuyOpen=false; MarketBuysCount=0; LatestBuyPrice=0; EarliestBuyPrice=0; HighestBuyPrice=0; LowestBuyPrice=million; BuyTicketNo=-1; HighestBuyTicketNo=-1; LowestBuyTicketNo=-1; LatestBuyTicketNo=-1; EarliestBuyTicketNo=-1; BuyPipsUpl=0; BuyCashUpl=0; LatestBuyTradeTime=0; EarliestBuyTradeTime=TimeCurrent(); //Market Sell trades SellOpen=false; MarketSellsCount=0; LatestSellPrice=0; EarliestSellPrice=0; HighestSellPrice=0; LowestSellPrice=million; SellTicketNo=-1; HighestSellTicketNo=-1; LowestSellTicketNo=-1; LatestSellTicketNo=-1; EarliestSellTicketNo=-1;; SellPipsUpl=0; SellCashUpl=0; LatestSellTradeTime=0; EarliestSellTradeTime=TimeCurrent(); //BuyStop trades BuyStopOpen=false; BuyStopsCount=0; LatestBuyStopPrice=0; EarliestBuyStopPrice=0; HighestBuyStopPrice=0; LowestBuyStopPrice=million; BuyStopTicketNo=-1; HighestBuyStopTicketNo=-1; LowestBuyStopTicketNo=-1; LatestBuyStopTicketNo=-1; EarliestBuyStopTicketNo=-1;; LatestBuyStopTradeTime=0; EarliestBuyStopTradeTime=TimeCurrent(); //BuyLimit trades BuyLimitOpen=false; BuyLimitsCount=0; LatestBuyLimitPrice=0; EarliestBuyLimitPrice=0; HighestBuyLimitPrice=0; LowestBuyLimitPrice=million; BuyLimitTicketNo=-1; HighestBuyLimitTicketNo=-1; LowestBuyLimitTicketNo=-1; LatestBuyLimitTicketNo=-1; EarliestBuyLimitTicketNo=-1;; LatestBuyLimitTradeTime=0; EarliestBuyLimitTradeTime=TimeCurrent(); /////SellStop trades SellStopOpen=false; SellStopsCount=0; LatestSellStopPrice=0; EarliestSellStopPrice=0; HighestSellStopPrice=0; LowestSellStopPrice=million; SellStopTicketNo=-1; HighestSellStopTicketNo=-1; LowestSellStopTicketNo=-1; LatestSellStopTicketNo=-1; EarliestSellStopTicketNo=-1;; LatestSellStopTradeTime=0; EarliestSellStopTradeTime=TimeCurrent(); //SellLimit trades SellLimitOpen=false; SellLimitsCount=0; LatestSellLimitPrice=0; EarliestSellLimitPrice=0; HighestSellLimitPrice=0; LowestSellLimitPrice=million; SellLimitTicketNo=-1; HighestSellLimitTicketNo=-1; LowestSellLimitTicketNo=-1; LatestSellLimitTicketNo=-1; EarliestSellLimitTicketNo=-1;; LatestSellLimitTradeTime=0; EarliestSellLimitTradeTime=TimeCurrent(); //Not related to specific order types TicketNo=-1;OpenTrades=0; LatestTradeTime=0; EarliestTradeTime=million;//More specific times are in each individual section LatestTradeTicketNo=-1; EarliestTradeTicketNo=-1; PipsUpl=0;//For keeping track of the pips PipsUpl of multi-trade/hedged positions CashUpl=0;//For keeping track of the cash PipsUpl of multi-trade/hedged positions TradeComment = " "; //For hedging, so ea knows whether tp/sl need removing TpSet = false; SlSet = false; HedgeLotSize=0; //FIFO ticket resize ArrayResize(FifoTicket, 0); int type;//Saves the OrderType() for consulatation later in the function if (OrdersTotal() == 0) return; //Iterating backwards through the orders list caters more easily for closed trades than iterating forwards for (int cc = OrdersTotal() - 1; cc >= 0; cc--) { bool TradeWasClosed = false;//See 'check for possible trade closure' //Ensure the trade is still open if (!OrderSelect(cc, SELECT_BY_POS, MODE_TRADES) ) continue; //Ensure the EA 'owns' this trade if (OrderSymbol() != Symbol() ) continue; if (OrderMagicNumber() != MagicNumber) continue; if (OrderCloseTime() > 0) continue; //All conditions passed, so carry on //The time of the most recent pending order if (OrderType() > 1) if (OrderOpenTime() > LatestTradeTime) LatestTradeTime = OrderOpenTime(); //Time of the furthest back in time trade if (OrderOpenTime() < EarliestTradeTime) EarliestTradeTime = OrderOpenTime(); HedgeLotSize = OrderLots(); type = OrderType();//Store the order type if (!CloseEnough(OrderTakeProfit(), 0) ) TpSet = true; if (!CloseEnough(OrderStopLoss(), 0) ) SlSet = true; TradeComment = OrderComment();//For sending with grid trades OpenTrades++; //Store the latest trade sent. Most of my EA's only need this final ticket number as either they are single trade //bots or the last trade in the sequence is the important one. Adapt this code for your own use. if (TicketNo == -1) TicketNo = OrderTicket(); //Store ticket numbers for FIFO ArrayResize(FifoTicket, OpenTrades + 1); FifoTicket[OpenTrades] = OrderTicket(); //The next line of code calculates the pips upl of an open trade. As yet, I have done nothing with it. //something = CalculateTradeProfitInPips() double pips = 0; //Buile up the position picture of market trades if (OrderType() < 2) { CashUpl+= (OrderProfit() + OrderSwap() + OrderCommission()); MarketTradesTotal++; pips = CalculateTradeProfitInPips(OrderType()); PipsUpl+= pips; //Buys if (OrderType() == OP_BUY) { BuyOpen = true; BuyTicketNo = OrderTicket(); MarketBuysCount++; BuyPipsUpl+= pips; BuyCashUpl+= (OrderProfit() + OrderSwap() + OrderCommission()); //Latest trade if (OrderOpenTime() > LatestBuyTradeTime) { LatestBuyTradeTime = OrderOpenTime(); LatestBuyPrice = OrderOpenPrice(); LatestBuyTicketNo = OrderTicket(); }//if (OrderOpenTime() > LatestBuyTradeTime) //Furthest back in time if (OrderOpenTime() < EarliestBuyTradeTime) { EarliestBuyTradeTime = OrderOpenTime(); EarliestBuyPrice = OrderOpenPrice(); EarliestBuyTicketNo = OrderTicket(); }//if (OrderOpenTime() < EarliestBuyTradeTime) //Highest trade price if (OrderOpenPrice() > HighestBuyPrice) { HighestBuyPrice = OrderOpenPrice(); HighestBuyTicketNo = OrderTicket(); }//if (OrderOpenPrice() > HighestBuyPrice) //Lowest trade price if (OrderOpenPrice() > LowestBuyPrice) { LowestBuyPrice = OrderOpenPrice(); LowestBuyTicketNo = OrderTicket(); }//if (OrderOpenPrice() > LowestBuyPrice) }//if (OrderType() == OP_BUY) //Sells if (OrderType() == OP_SELL) { SellOpen = true; SellTicketNo = OrderTicket(); MarketSellsCount++; SellPipsUpl+= pips; SellCashUpl+= (OrderProfit() + OrderSwap() + OrderCommission()); //Latest trade if (OrderOpenTime() > LatestSellTradeTime) { LatestSellTradeTime = OrderOpenTime(); LatestSellPrice = OrderOpenPrice(); LatestSellTicketNo = OrderTicket(); }//if (OrderOpenTime() > LatestSellTradeTime) //Furthest back in time if (OrderOpenTime() < EarliestSellTradeTime) { EarliestSellTradeTime = OrderOpenTime(); EarliestSellPrice = OrderOpenPrice(); EarliestSellTicketNo = OrderTicket(); }//if (OrderOpenTime() < EarliestSellTradeTime) //Highest trade price if (OrderOpenPrice() > HighestSellPrice) { HighestSellPrice = OrderOpenPrice(); HighestSellTicketNo = OrderTicket(); }//if (OrderOpenPrice() > HighestSellPrice) //Lowest trade price if (OrderOpenPrice() > LowestSellPrice) { LowestSellPrice = OrderOpenPrice(); LowestSellTicketNo = OrderTicket(); }//if (OrderOpenPrice() > LowestSellPrice) }//if (OrderType() == OP_SELL) }//if (OrderType() < 2) //Build up the position details of stop/limit orders if (OrderType() > 1) { //Buystops if (OrderType() == OP_BUYSTOP) { BuyStopOpen = true; BuyStopTicketNo = OrderTicket(); BuyStopsCount++; //Latest trade if (OrderOpenTime() > LatestBuyStopTradeTime) { LatestBuyStopTradeTime = OrderOpenTime(); LatestBuyStopPrice = OrderOpenPrice(); LatestBuyStopTicketNo = OrderTicket(); }//if (OrderOpenTime() > LatestBuyStopTradeTime) //Furthest back in time if (OrderOpenTime() < EarliestBuyStopTradeTime) { EarliestBuyStopTradeTime = OrderOpenTime(); EarliestBuyStopPrice = OrderOpenPrice(); EarliestBuyStopTicketNo = OrderTicket(); }//if (OrderOpenTime() < EarliestBuyStopTradeTime) //Highest trade price if (OrderOpenPrice() > HighestBuyStopPrice) { HighestBuyStopPrice = OrderOpenPrice(); HighestBuyStopTicketNo = OrderTicket(); }//if (OrderOpenPrice() > HighestBuyStopPrice) //Lowest trade price if (OrderOpenPrice() > LowestBuyStopPrice) { LowestBuyStopPrice = OrderOpenPrice(); LowestBuyStopTicketNo = OrderTicket(); }//if (OrderOpenPrice() > LowestBuyStopPrice) }//if (OrderType() == OP_BUYSTOP) //Sellstops if (OrderType() == OP_SELLSTOP) { SellStopOpen = true; SellStopTicketNo = OrderTicket(); SellStopsCount++; //Latest trade if (OrderOpenTime() > LatestSellStopTradeTime) { LatestSellStopTradeTime = OrderOpenTime(); LatestSellStopPrice = OrderOpenPrice(); LatestSellStopTicketNo = OrderTicket(); }//if (OrderOpenTime() > LatestSellStopTradeTime) //Furthest back in time if (OrderOpenTime() < EarliestSellStopTradeTime) { EarliestSellStopTradeTime = OrderOpenTime(); EarliestSellStopPrice = OrderOpenPrice(); EarliestSellStopTicketNo = OrderTicket(); }//if (OrderOpenTime() < EarliestSellStopTradeTime) //Highest trade price if (OrderOpenPrice() > HighestSellStopPrice) { HighestSellStopPrice = OrderOpenPrice(); HighestSellStopTicketNo = OrderTicket(); }//if (OrderOpenPrice() > HighestSellStopPrice) //Lowest trade price if (OrderOpenPrice() > LowestSellStopPrice) { LowestSellStopPrice = OrderOpenPrice(); LowestSellStopTicketNo = OrderTicket(); }//if (OrderOpenPrice() > LowestSellStopPrice) }//if (OrderType() == OP_SELLSTOP) //Buy limits if (OrderType() == OP_BUYLIMIT) { BuyLimitOpen = true; BuyLimitTicketNo = OrderTicket(); BuyLimitsCount++; //Latest trade if (OrderOpenTime() > LatestBuyLimitTradeTime) { LatestBuyLimitTradeTime = OrderOpenTime(); LatestBuyLimitPrice = OrderOpenPrice(); LatestBuyLimitTicketNo = OrderTicket(); }//if (OrderOpenTime() > LatestBuyLimitTradeTime) //Furthest back in time if (OrderOpenTime() < EarliestBuyLimitTradeTime) { EarliestBuyLimitTradeTime = OrderOpenTime(); EarliestBuyLimitPrice = OrderOpenPrice(); EarliestBuyLimitTicketNo = OrderTicket(); }//if (OrderOpenTime() < EarliestBuyLimitTradeTime) //Highest trade price if (OrderOpenPrice() > HighestBuyLimitPrice) { HighestBuyLimitPrice = OrderOpenPrice(); HighestBuyLimitTicketNo = OrderTicket(); }//if (OrderOpenPrice() > HighestBuyLimitPrice) //Lowest trade price if (OrderOpenPrice() > LowestBuyLimitPrice) { LowestBuyLimitPrice = OrderOpenPrice(); LowestBuyLimitTicketNo = OrderTicket(); }//if (OrderOpenPrice() > LowestBuyLimitPrice) }//if (OrderType() == OP_BUYLIMIT) //Sell limits if (OrderType() == OP_SELLLIMIT) { SellLimitOpen = true; SellLimitTicketNo = OrderTicket(); SellLimitsCount++; //Latest trade if (OrderOpenTime() > LatestSellLimitTradeTime) { LatestSellLimitTradeTime = OrderOpenTime(); LatestSellLimitPrice = OrderOpenPrice(); LatestSellLimitTicketNo = OrderTicket(); }//if (OrderOpenTime() > LatestSellLimitTradeTime) //Furthest back in time if (OrderOpenTime() < EarliestSellLimitTradeTime) { EarliestSellLimitTradeTime = OrderOpenTime(); EarliestSellLimitPrice = OrderOpenPrice(); EarliestSellLimitTicketNo = OrderTicket(); }//if (OrderOpenTime() < EarliestSellLimitTradeTime) //Highest trade price if (OrderOpenPrice() > HighestSellLimitPrice) { HighestSellLimitPrice = OrderOpenPrice(); HighestSellLimitTicketNo = OrderTicket(); }//if (OrderOpenPrice() > HighestSellLimitPrice) //Lowest trade price if (OrderOpenPrice() > LowestSellLimitPrice) { LowestSellLimitPrice = OrderOpenPrice(); LowestSellLimitTicketNo = OrderTicket(); }//if (OrderOpenPrice() > LowestSellLimitPrice) }//if (OrderType() == OP_SELLLIMIT) }//if (OrderType() > 1) //Add missing tp/sl in case rapidly moving markets prevent their addition - ECN if (!Hedged) { if (CloseEnough(OrderStopLoss(), 0) && !CloseEnough(StopLoss, 0)) InsertStopLoss(OrderTicket()); if (CloseEnough(OrderTakeProfit(), 0) && !CloseEnough(TakeProfit, 0)) InsertTakeProfit(OrderTicket() ); }//if (!Hedged) //Replace missing tp and sl lines if (HiddenPips > 0) ReplaceMissingSlTpLines(); if (!Hedged) { TradeWasClosed = LookForTradeClosure(OrderTicket() ); if (TradeWasClosed) { if (type == OP_BUY) BuyOpen = false;//Will be reset if subsequent trades are buys that are not closed if (type == OP_SELL) SellOpen = false;//Will be reset if subsequent trades are sells that are not closed cc++; continue; }//if (TradeWasClosed) //Profitable trade management if (OrderProfit() > 0) { TradeManagementModule(OrderTicket() ); }//if (OrderProfit() > 0) }//if (!Hedged) }//for (int cc = OrdersTotal() - 1; cc <= 0; c`c--) //Sort ticket numbers for FIFO if (ArraySize(FifoTicket) > 0) ArraySort(FifoTicket, WHOLE_ARRAY, 0, MODE_DESCEND); //Is the position hedged? Hedged = false; if (BuyOpen) if (SellOpen) Hedged=true; //Remove stop losses and take profits if (Hedged) { if (TpSet) RemoveTakeProfits(); if (SlSet) RemoveStopLosses(); }//if (Hegded) }//End void CountOpenTrades();
Holy Graily Bob 'n Grid
- SteveHopwood
- Owner
- Posts: 9904
- Joined: Tue Nov 15, 2011 8:43 am
- Location: Misterton - an insignificant village in England. Very pleasant to live in.
Holy Graily Bob 'n Grid
The bloop-spotters are keeping a wary eye on what is going on. Here is a pm exchange between Jason and myself:

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.
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.
-
Anima999
- Trader
- Posts: 13
- Joined: Wed Mar 02, 2016 11:05 pm
Holy Graily Bob 'n Grid
Thanks Steve,
The modifications look brilliant. My contribution to the test can be found here:
https://www.myfxbook.com/members/myoung ... il/1546823
Its a GP demo running version 1a. Settings are as per Steve's defaults but I've enabled just Bob's 60/240 MA filter; I hope it may help in our comparison of the new features and the various combinations.
Obviously ignore the previous two weeks as I was playing around with elements of PAMSY at the same time as running Bob last week so things might look a bit strange.
The modifications look brilliant. My contribution to the test can be found here:
https://www.myfxbook.com/members/myoung ... il/1546823
Its a GP demo running version 1a. Settings are as per Steve's defaults but I've enabled just Bob's 60/240 MA filter; I hope it may help in our comparison of the new features and the various combinations.
Obviously ignore the previous two weeks as I was playing around with elements of PAMSY at the same time as running Bob last week so things might look a bit strange.
- SteveHopwood
- Owner
- Posts: 9904
- Joined: Tue Nov 15, 2011 8:43 am
- Location: Misterton - an insignificant village in England. Very pleasant to live in.
Holy Graily Bob 'n Grid
Another one for the fledgling coders.
I have added a version of HGBnG with added Stochastic sauce to post 2, along with a description of the steps I took to do so. You can quickly learn to add your own fav indi's into the mix with only a modest amount of coding expertise.

I have added a version of HGBnG with added Stochastic sauce to post 2, along with a description of the steps I took to do so. You can quickly learn to add your own fav indi's into the mix with only a modest amount of coding expertise.
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.
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.
- PeterUK
- Trader
- Posts: 79
- Joined: Fri Jun 27, 2014 2:18 pm
- Location: London, UK
Holy Graily Bob 'n Grid
Thanks Steve, for the tutorials. Much appreciated...

Knowledge owned is Pointless
Knowledge Shared is Priceless
Last edited by PeterUK on Tue Mar 22, 2016 1:41 am, edited 2 times in total.
Knowledge owned is Pointless. Knowledge Shared is Priceless!
- woodlands
- Trader
- Posts: 226
- Joined: Sun Mar 23, 2014 6:16 pm
Holy Graily Bob 'n Grid
I used your idea of WG on the HGB 'n Grid and set it on at 20:40 GMT 17/03/16 and shut down at 17:00 on the 18/03/16 there was 36% drawdown at one stage in the period, this is with 11 pairs, GP demo, M5 timeframe, GBP 100 start balance, no semafor, 10 basket closures with Milanese's sAPBM.Gertje » Fri Mar 18, 2016 3:38 pm wrote:Fired up a new demo with GP![]()
Installed all the gismo's I want to use and made up my gameplan for next week.
Closed some trades on a semafor, and 1 basket by the manager.
196 pips banked in a few hours.
Now all but 2 pairs are outside tradinghours, looking forward to next week!
.
For today's open I have put Steve's new tnavi MA addition to see if we can carry on with less drawdown
Regards and thanks to all.
Tony
You do not have the required permissions to view the files attached to this post.
-
szfxtrader
- Trader
- Posts: 377
- Joined: Sun Feb 10, 2013 9:36 am
Holy Graily Bob 'n Grid
SteveHopwood » Sun Mar 20, 2016 12:34 pm wrote:Another one for the fledgling coders.
I have added a version of HGBnG with added Stochastic sauce to post 2, along with a description of the steps I took to do so. You can quickly learn to add your own fav indi's into the mix with only a modest amount of coding expertise.
Thank you very much, I am learning a lot from your code's functions.
-
ForexFox
- Posts: 5
- Joined: Tue Oct 28, 2014 1:12 pm
Holy Graily Bob 'n Grid
Sorry to disturb you, guys.
Because I got some issue with V1a latest version.
I want to come back to use the V2n version. (the last version in V2 series.)
But yesterday I already replace that version by V1a without backup.
So come here to ask for a help.
Hope there's guy still owned the V2n version and shared with me.
Thanks again and hope this reply won't make you feel trouble.
Regards.
Because I got some issue with V1a latest version.
I want to come back to use the V2n version. (the last version in V2 series.)
But yesterday I already replace that version by V1a without backup.
So come here to ask for a help.
Hope there's guy still owned the V2n version and shared with me.
Thanks again and hope this reply won't make you feel trouble.
Regards.
- Gertje
- Trader
- Posts: 1936
- Joined: Mon Dec 12, 2011 1:17 pm
- Location: Middle of the Netherlands
Holy Graily Bob 'n Grid
Here you go
You do not have the required permissions to view the files attached to this post.
- keydcuk
- Trader
- Posts: 104
- Joined: Tue Dec 04, 2012 8:01 pm
Holy Graily Bob 'n Grid
Hey Gertje,
How's your experiment going with the low timeframe method you mentioned last week?
How's your experiment going with the low timeframe method you mentioned last week?
- Gertje
- Trader
- Posts: 1936
- Joined: Mon Dec 12, 2011 1:17 pm
- Location: Middle of the Netherlands
Holy Graily Bob 'n Grid
To early to tell.keydcuk » Mon Mar 21, 2016 3:05 pm wrote:Hey Gertje,
How's your experiment going with the low timeframe method you mentioned last week?
USDCAD had some 20 trades [2buy/18sell] open over the weekend, and of course PA went north.....
676 banked pips today, but DD ~15% at the moment, none of the pairs reached their ADR yet.
I'm waiting to see the DD clear, and some more pairs TP at a semafor.
I wonder what will happen when a pair strikes a homerun without looking back.
More to come this week I suppose.