in this ea order open is no issues. but order close are not work properly. when i compile the ea, the opposite signal found and order close. what happened?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Why the position seems to close only when you re-compile
──────────────────────────────────────────────────────────
Re-compiling (or simply removing / re-attaching the EA) fires
• OnDeinit() → all global/static variables are destroyed
• OnInit() → everything is re-initialised (network weights, prev_bar_time = 0, …)
When the very next tick arrives, OnTick() treats it as a brand-new bar (because
prev_bar_time == 0).
So the “new-bar filter”
cpp
if(Time[0] == prev_bar_time) return;
is bypassed once and the logic is executed immediately – that is why you
suddenly get an opposite signal and the order is closed.
During normal running the EA sees only one execution per bar.
If your neural net gives a SELL signal inside the same bar in which you opened
a BUY, the EA will not notice it (because it is ignoring all ticks of that bar).
The close decision is postponed to the next bar; if by then the NN has flipped
back to BUY (> 0.50) the position is never closed.
In short – the “close-on-opposite” rule is throttled by the new-bar filter.
After a re-compile the filter is (temporarily) gone, so the position is closed
and it looks like “compiling fixed it”.
──────────────────────────────────────────
Fix 1 – keep the open logic on new bars, but evaluate the close logic on every tick
──────────────────────────────────────────
cpp
// ----------------------------------
// 1. OPEN & TRAIN : once per bar
// ----------------------------------
bool isNewBar = (Time[0] != prev_bar_time);
if(isNewBar)
{
prev_bar_time = Time[0];
GenerateFeatures(features);
prediction = ForwardPropagate(features);
// ----- train, open, etc (UNCHANGED) -----
}
// ----------------------------------
// 2. CLOSE : check each tick
// ----------------------------------
if(OrdersTotal() > 0) // we have at least one order
{
for(int i=OrdersTotal()-1; i>=0; i--) // loop backwards is safer
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)
&& OrderMagicNumber() == MagicNumber
&& OrderSymbol() == Symbol())
{
// We need the *latest* price -> RefreshRates();
RefreshRates();
// Decide with the *latest* prediction. If we did not
// calculate it on this tick (because it was not a new bar),
// calculate it now:
if(!isNewBar)
{
GenerateFeatures(features);
prediction = ForwardPropagate(features);
}
if(OrderType() == OP_BUY && prediction < 0.50)
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clrBlue);
if(OrderType() == OP_SELL && prediction > 0.50)
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clrBlue);
}
}
• Opening / training is still restricted to one execution per bar → cleaner
back-tests.
• Closing runs on every tick → the moment an opposite signal appears, the
trade can be closed.
──────────────────────────────────────────
Fix 2 – save the signal that opened the trade and close when it flips
──────────────────────────────────────────
Sometimes the network output oscillates around 0.50 sending
“BUY – SELL – BUY – …” on consecutive bars.
A small global flag keeps things crystal-clear:
if(prediction > 0.50 && CurrentPosition <= 0) // no or short
{
OrderSend(... OP_BUY ...);
CurrentPosition = +1;
}
else if(prediction < 0.50 && CurrentPosition >= 0) // no or long
{
OrderSend(... OP_SELL ...);
CurrentPosition = -1;
}
Whenever a signal flips:
cpp
// inside the “order exists” section
if(CurrentPosition == +1 && prediction < 0.50) // long → short
{
OrderClose(ticket, lots, Bid, Slippage, clrBlue);
CurrentPosition = 0; // we are flat again
}
else if(CurrentPosition == -1 && prediction > 0.50) // short → long
{
OrderClose(ticket, lots, Ask, Slippage, clrBlue);
CurrentPosition = 0;
}
Now the EA closes exactly when the direction changes, no matter whether
the change happens intra-bar or inter-bar.
──────────────────────────────────────────
Other best-practice tweaks
──────────────────────────────────────────
• Always call RefreshRates() just before using Bid / Ask
• Check OrderClose() return value and GetLastError() to see why a close
may have failed (requote, FIFO, not enough margin, …)
• Use ENUM_TIMEFRAMES instead of Period() if you want to freeze the logic
to a certain TF.
• Consider stop-loss / take-profit instead of “signal only” exits – this will
protect you if the NN gets stuck on one side.
Give one of the two fixes above a try, re-compile, and the position should
now be closed exactly when an opposite signal occurs – without having to
re-compile again. Happy trading! 📈🤖