Fix positions for backtests
This commit is contained in:
@@ -263,7 +263,8 @@ public class TradingBotBase : ITradingBot
|
||||
await UpdateSignals(candles, null);
|
||||
}
|
||||
|
||||
public async Task UpdateSignals(HashSet<Candle> candles, Dictionary<DateTime, LightSignal> preCalculatedSignals = null)
|
||||
public async Task UpdateSignals(HashSet<Candle> candles,
|
||||
Dictionary<DateTime, LightSignal> preCalculatedSignals = null)
|
||||
{
|
||||
// Skip indicator checking if flipping is disabled and there's an open position
|
||||
// This prevents unnecessary indicator calculations when we can't act on signals anyway
|
||||
@@ -283,27 +284,10 @@ public class TradingBotBase : ITradingBot
|
||||
|
||||
if (Config.IsForBacktest)
|
||||
{
|
||||
LightSignal backtestSignal;
|
||||
|
||||
if (preCalculatedSignals != null && LastCandle != null && preCalculatedSignals.TryGetValue(LastCandle.Date, out backtestSignal))
|
||||
{
|
||||
// Use pre-calculated signal - fast path
|
||||
if (backtestSignal == null) return;
|
||||
await AddSignal(backtestSignal);
|
||||
}
|
||||
else if (candles != null)
|
||||
{
|
||||
// Fallback to original calculation if no pre-calculated signals available
|
||||
backtestSignal = TradingBox.GetSignal(candles, Config.Scenario, Signals, Config.Scenario.LoopbackPeriod,
|
||||
PreCalculatedIndicatorValues);
|
||||
if (backtestSignal == null) return;
|
||||
await AddSignal(backtestSignal);
|
||||
}
|
||||
else
|
||||
{
|
||||
// No candles provided - skip signal update
|
||||
return;
|
||||
}
|
||||
var backtestSignal = TradingBox.GetSignal(candles, Config.Scenario, Signals, Config.Scenario.LoopbackPeriod,
|
||||
PreCalculatedIndicatorValues);
|
||||
if (backtestSignal == null) return;
|
||||
await AddSignal(backtestSignal);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -601,7 +585,8 @@ public class TradingBotBase : ITradingBot
|
||||
$"Checking position history before marking as closed...");
|
||||
|
||||
// Verify in exchange history before assuming it's closed
|
||||
var (existsInHistory, hadWeb3ProxyError) = await CheckPositionInExchangeHistory(positionForSignal);
|
||||
var (existsInHistory, hadWeb3ProxyError) =
|
||||
await CheckPositionInExchangeHistory(positionForSignal);
|
||||
|
||||
if (hadWeb3ProxyError)
|
||||
{
|
||||
@@ -781,7 +766,8 @@ public class TradingBotBase : ITradingBot
|
||||
|
||||
// Position might be canceled by the broker
|
||||
// Check if position exists in exchange history with PnL before canceling
|
||||
var (positionFoundInHistory, hadWeb3ProxyError) = await CheckPositionInExchangeHistory(positionForSignal);
|
||||
var (positionFoundInHistory, hadWeb3ProxyError) =
|
||||
await CheckPositionInExchangeHistory(positionForSignal);
|
||||
|
||||
if (hadWeb3ProxyError)
|
||||
{
|
||||
@@ -3087,7 +3073,8 @@ public class TradingBotBase : ITradingBot
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "Error during position recovery for position {PositionId}", positionForSignal.Identifier);
|
||||
Logger.LogError(ex, "Error during position recovery for position {PositionId}",
|
||||
positionForSignal.Identifier);
|
||||
await LogWarning($"Position recovery failed due to exception: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user