Hello everyone,
I try to nail down a bug in my strategy. In its current state, it opens sometimes more than one order, and this one is never closed.
Here some technical informations:
- The strategy is set to Unique Entries with Amount 1
- I have max 1 order open, for example, if there is a long open, the signal logic would not hit for a short, it would first be able to detect a short if the stop logic already hits
- I encounter this bug today in live trading in 3/5 trades
- It seems to happen in around 3/2000 trades in backtest simulation aswell
I have defined some public string variables in my strategy class like this:
public string long_entry = Guid.NewGuid().ToString();
public string long_exit = Guid.NewGuid().ToString();
public string short_entry = Guid.NewGuid().ToString();
public string short_exit = Guid.NewGuid().ToString();
When I open and close orders, I do around that:
public void OpenLongOrder()
{
if (order != 0) return;
if (!market_open) return;
if (order_block) return;
order_block = true;
order_open_price = Close[0];
order = 1;
if (is_analyzer_mode || enable_realtime)
{
EnterLong(GetQuantity(), long_entry);
Print($"[{Instrument.FullName}][{DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss")}] Enter Long...");
}
}
public void CloseLongOrder()
{
if (order != 1) return;
order = 0;
profit_current += (Close[0] - order_open_price) / TickSize;
order_open_price = 0;
if (is_analyzer_mode || enable_realtime)
{
ExitLong(long_exit, long_entry);
Print($"[{Instrument.FullName}][{DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss")}] Exit Long...");
}
}
Now in live trading, I run 6 strategy instances, on 6 different charts, each with a different instrument. I would expect to see always the same strings in my signalnames for the same instrument, however, this isnt the case:
I see in this screenshot multiple different Names, not only 2 different. In addition, I see there is two Short Entries, what should not be possible.
Here is the thing, I set the order variable to 1 if I go long, and -1 if I go short. If I call the open/close funcitons, I check explicit if this variable is set, to prevent wrong trades. That means, after I open a short, there should not be the possibility to open another short. Paired with the problem, that my UUID names are not jsut two but multiple, I think my strategy somehow resets them variables to initial state at some point? And it seems to happen frequently, since I already encountered it multiple times today on its first live trades. This would explain the behaviour, because if order is reset to 0, it would be allwoed to open trades agian.
Still I’m confused, why the UniqueEntries Amount 1 setting allows the strategy to open more than 1 short? This looks wrong aswell.
I could imagine one of the following being the case:
- Serialization/Deserialization mechanic (I noticed that ninja uses some kind of save state)
- Recalculation at network disconnect/timeout (Maybe it reinit the strategy)
The only workaround I current see, is saving my own state in some kind of DB. But this seems massively overkill tbh.
Overall, I think there is a Ninja mechanic I didnt know about yet. Any help is appreciated.
EDIT:
The ConnectionLossHandling is set to Recalculate. Maybe this is the problem? The strategy would run fine if it misses a few price changes, thats not a complete disaster for me. If I set it to ConnectionLossHandling.KeepRunning, would this solve the problem most likely?
