Fix manual position open
This commit is contained in:
@@ -24,6 +24,7 @@ namespace Managing.Application.Abstractions
|
||||
decimal GetProfitAndLoss();
|
||||
decimal GetTotalFees();
|
||||
Task LoadAccount();
|
||||
Task LoadLastCandle();
|
||||
Task<Position> OpenPositionManually(TradeDirection direction);
|
||||
|
||||
Task CloseTrade(LightSignal signal, Position position, Trade tradeToClose, decimal lastPrice,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ public class TradingBotBase : ITradingBot
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoadLastCandle()
|
||||
public async Task LoadLastCandle()
|
||||
{
|
||||
await ServiceScopeHelpers.WithScopedService<IExchangeService>(_scopeFactory, async exchangeService =>
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.Candles;
|
||||
using Managing.Domain.Indicators;
|
||||
using Managing.Domain.Trades;
|
||||
using Managing.Domain.Users;
|
||||
@@ -114,4 +115,10 @@ public class TradingBotGrainState
|
||||
/// </summary>
|
||||
[Id(17)]
|
||||
public DateTime? LastPositionClosingTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The last candle data used for trading decisions
|
||||
/// </summary>
|
||||
[Id(18)]
|
||||
public Candle? LastCandle { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user