Fix manual position open
This commit is contained in:
@@ -249,8 +249,8 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
|
||||
throw new InvalidOperationException("Account name is required for live trading");
|
||||
}
|
||||
|
||||
// Create the trading bot instance
|
||||
var logger = _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<ILogger<TradingBotBase>>();
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var logger = scope.ServiceProvider.GetRequiredService<ILogger<TradingBotBase>>();
|
||||
var tradingBot = new TradingBotBase(logger, _scopeFactory, config);
|
||||
|
||||
// Restore state from grain state
|
||||
@@ -261,6 +261,7 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
|
||||
tradingBot.ExecutionCount = _state.State.ExecutionCount;
|
||||
tradingBot.Identifier = _state.State.Identifier;
|
||||
tradingBot.LastPositionClosingTime = _state.State.LastPositionClosingTime;
|
||||
tradingBot.LastCandle = _state.State.LastCandle;
|
||||
|
||||
return tradingBot;
|
||||
}
|
||||
@@ -311,6 +312,17 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
|
||||
throw new InvalidOperationException("Bot is not running");
|
||||
}
|
||||
|
||||
// Ensure LastCandle is available for manual position opening
|
||||
if (_tradingBot.LastCandle == null)
|
||||
{
|
||||
_logger.LogInformation("LastCandle is null, loading latest candle data for manual position opening");
|
||||
await _tradingBot.LoadLastCandle();
|
||||
|
||||
// Sync the loaded candle to grain state
|
||||
SyncStateFromBase();
|
||||
await _state.WriteStateAsync();
|
||||
}
|
||||
|
||||
return await _tradingBot.OpenPositionManually(direction);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -367,6 +379,7 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
|
||||
_tradingBot.ExecutionCount = _state.State.ExecutionCount;
|
||||
_tradingBot.Identifier = _state.State.Identifier;
|
||||
_tradingBot.LastPositionClosingTime = _state.State.LastPositionClosingTime;
|
||||
_tradingBot.LastCandle = _state.State.LastCandle;
|
||||
}
|
||||
|
||||
private void SyncStateFromBase()
|
||||
@@ -379,6 +392,7 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
|
||||
_state.State.ExecutionCount = _tradingBot.ExecutionCount;
|
||||
_state.State.Identifier = _tradingBot.Identifier;
|
||||
_state.State.LastPositionClosingTime = _tradingBot.LastPositionClosingTime;
|
||||
_state.State.LastCandle = _tradingBot.LastCandle;
|
||||
_state.State.Config = _tradingBot.Config;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user