fix position initiator + pnl for trade
This commit is contained in:
@@ -126,7 +126,7 @@ public class TradingBot : Bot, ITradingBot
|
||||
await InitWorker(Run);
|
||||
}
|
||||
|
||||
Fee = TradingService.GetFee(Account, IsForBacktest);
|
||||
// Fee = TradingService.GetFee(Account, IsForBacktest);
|
||||
}
|
||||
|
||||
public async Task LoadAccount()
|
||||
@@ -249,14 +249,14 @@ public class TradingBot : Bot, ITradingBot
|
||||
|
||||
private async Task AddSignal(Signal signal)
|
||||
{
|
||||
Signals.Add(signal);
|
||||
|
||||
// if (!IsForBacktest)
|
||||
// TradingService.InsertSignal(signal);
|
||||
|
||||
if (IsForWatchingOnly || (ExecutionCount < 1 && !IsForBacktest))
|
||||
signal.Status = SignalStatus.Expired;
|
||||
|
||||
Signals.Add(signal);
|
||||
|
||||
var signalText = $"{ScenarioName} trigger a signal. Signal told you " +
|
||||
$"to {signal.Direction} {Ticker} on {Timeframe}. The confidence in this signal is {signal.Confidence}. Identifier : {signal.Identifier}";
|
||||
|
||||
@@ -285,9 +285,6 @@ public class TradingBot : Bot, ITradingBot
|
||||
|
||||
private async Task ManagePositions()
|
||||
{
|
||||
if (!IsForBacktest && ExecutionCount < 1)
|
||||
return;
|
||||
|
||||
// Update position
|
||||
foreach (var signal in Signals.Where(s => s.Status == SignalStatus.PositionOpen))
|
||||
{
|
||||
@@ -346,11 +343,26 @@ public class TradingBot : Bot, ITradingBot
|
||||
|
||||
if (!IsForBacktest)
|
||||
{
|
||||
position = positionsExchange.FirstOrDefault(p => p.Ticker == Ticker);
|
||||
|
||||
if (position != null)
|
||||
var brokerPosition = positionsExchange.FirstOrDefault(p => p.Ticker == Ticker);
|
||||
if (brokerPosition != null)
|
||||
{
|
||||
UpdatePositionPnl(positionForSignal.Identifier, position.ProfitAndLoss.Realized);
|
||||
UpdatePositionPnl(positionForSignal.Identifier, brokerPosition.ProfitAndLoss.Realized);
|
||||
|
||||
if (position.Status.Equals(PositionStatus.New))
|
||||
{
|
||||
await SetPositionStatus(position.SignalIdentifier, PositionStatus.Filled);
|
||||
}
|
||||
|
||||
position = brokerPosition;
|
||||
}
|
||||
else
|
||||
{
|
||||
// No position, position close on the broker
|
||||
if (!position.Status.Equals(PositionStatus.New))
|
||||
{
|
||||
// Setup the previous status of the position
|
||||
position.Status = PositionStatus.Filled;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -540,7 +552,7 @@ public class TradingBot : Bot, ITradingBot
|
||||
MoneyManagement,
|
||||
signal.Direction,
|
||||
Ticker,
|
||||
IsForBacktest ? PositionInitiator.PaperTrading : PositionInitiator.Bot,
|
||||
PositionInitiator.Bot,
|
||||
signal.Date,
|
||||
User,
|
||||
IsForBacktest,
|
||||
@@ -587,6 +599,8 @@ public class TradingBot : Bot, ITradingBot
|
||||
|
||||
private bool CanOpenPosition(Signal signal)
|
||||
{
|
||||
if (ExecutionCount < 1)
|
||||
return false;
|
||||
if (Positions.Count == 0)
|
||||
return true;
|
||||
|
||||
@@ -625,6 +639,7 @@ public class TradingBot : Bot, ITradingBot
|
||||
}
|
||||
else
|
||||
{
|
||||
position.Initiator = PositionInitiator.Bot;
|
||||
var command = new ClosePositionCommand(position, lastPrice);
|
||||
try
|
||||
{
|
||||
@@ -826,7 +841,7 @@ public class TradingBot : Bot, ITradingBot
|
||||
IsForWatchingOnly = IsForWatchingOnly,
|
||||
WalletBalances = WalletBalances,
|
||||
MoneyManagement = MoneyManagement,
|
||||
StartupTime = DateTime.Now
|
||||
StartupTime = StartupTime,
|
||||
};
|
||||
BotService.SaveOrUpdateBotBackup(Name, BotType, JsonConvert.SerializeObject(data));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user