Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
Hi everyone,
i have a problem on my EA, which works with a grid system.
The system should open a new buy position when bid price is 180 points lower than last opened position price. This level is called "nexyBuy".
The problem occurs only on certain moments, for example backtesting with usdcad on 10 August 2020, it opens many many positions also if price is higher than the " nexyBuy" target price of the grid system:
All I see is that there are a lot of variables that are simultaneously declared and defined on the local scope. As a matter of best practice, I declare as many variables as possible up top on the global scope, define them in OnInit(), and then update their values in OnTick() or in custom functions. I would try rearranging those variables and then re-Debugging.
i solved the problem putting orders with BuyLimit and SellLimit. So the EA puts just one order at time, and not many orders simultaneously.
if(buyCount>=1 && buyCount<MaxOrders && count_buy_limits==0){ //buy signal LotsB=Lot*(int)MathPow(MultiplierLot,buyCount); if(ask>nextBuy){ m_trade.BuyLimit(LotsB,nextBuy); }else if(ask<=nextBuy){ m_trade.BuyLimit(LotsB,ask); } }else if(sellCount>=1 && sellCount<MaxOrders && count_sell_stops==0){ //sell signal LotsS=Lot*(int)MathPow(MultiplierLot,sellCount); if(bid<nextSell){ m_trade.SellLimit(LotsS,nextSell); }else if(bid>=nextSell){ m_trade.SellLimit(LotsS,bid); } }
But if i declare variables in Oninit section many errors occurs: "undeclared identifier", so i have to declare them before, just after input section.
All works now but i didn't understand what was the problem putting orders just with trade. command
That is what is called the "global scope" or "global environment" -- the section that ryan advised you of.
I found before that using global variables to track the number of opened buy positions or sell positions (or orders) doesn't work efficiently. It's better to reset those variables to 0 on every new tick and then loop through open buy and sell positions to count how many are now open. It will update accurately that way.
I found before that using global variables to track the number of opened buy positions or sell positions (or orders) doesn't work efficiently. It's better to reset those variables to 0 on every new tick...
To be clear, variables declared on the global scope can be defined or redefined anywhere and at any time in the program--hence the name of their scope, "global." Declaring, or redeclaring, variables throughout OnTick() is not programmatically more efficient.
To be clear, variables declared on the global scope can be defined or redefined anywhere and at any time in the program--hence the name of their scope, "global." Declaring, or redeclaring, variables throughout OnTick() is not programmatically more efficient.
For me, I only declare variables in the global scope for holding persistent data. If I need variables to constantly update, I like to reset the definition of the variable along with the value locally, because memory may sometimes not be free'd properly especially as MQL5 is c++ based architecture. Local variables get allocated and deallocated with each new tick, avoiding memory leaks.
For me, I only declare variables in the global scope for holding persistent data. If I need variables to constantly update, I like to reset the definition of the variable along with the value locally, because memory may sometimes not be free'd properly especially as MQL5 is c++ based architecture. Local variables get allocated and deallocated with each new tick, avoiding memory leaks.
Thanks for that. If I ever decide to embark on high frequency trading, I'll keep that in mind.
I found before that using global variables to track the number of opened buy positions or sell positions (or orders) doesn't work efficiently. It's better to reset those variables to 0 on every new tick and then loop through open buy and sell positions to count how many are now open. It will update accurately that way.
agreed. however that means that you need to send instruction to broker on every tick.
suggestion for a more efficient method to save buy and sell counts, is via a fixed or dynamic array. It is just as efficient when used either globally or local.
agreed. however that means that you need to send instruction to broker on every tick.
suggestion for a more efficient method to save buy and sell counts, is via a fixed or dynamic array. It is just as efficient when used either globally or local.
Thanks.
I use a global int with somewhat convoluted global ENUM's but it's fast enough for swing trading:
void GetPositions() { if(PositionsTotal() >= 1) { for(int i = PositionsTotal() - 1; i >= 0; i--) // count all currency pair positions { // get the ticket number PositionTicket = PositionGetTicket(i); ENUM_POSITION_TYPE positiontype = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); openprice = (ENUM_POSITION_PROPERTY_DOUBLE)PositionGetDouble(POSITION_PRICE_OPEN); opentime = (ENUM_POSITION_PROPERTY_INTEGER)PositionGetInteger(POSITION_TIME); if(positiontype == POSITION_TYPE_BUY) { positionType = 1; // buy position positionTime = POSITION_TIME; } if(positiontype == POSITION_TYPE_SELL) { positionType = 2; // sell position positionTime = POSITION_TIME; } if(PositionsTotal() == 0) { positionType = 0; // flat } } } }

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi everyone,
i have a problem on my EA, which works with a grid system.
The system should open a new buy position when bid price is 180 points lower than last opened position price. This level is called "nexyBuy".
The problem occurs only on certain moments, for example backtesting with usdcad on 10 August 2020, it opens many many positions also if price is higher than the " nexyBuy" target price of the grid system: