GMX v2 - Trading (#7)
* Move PrivateKeys.cs * Update gitignore * Update gitignore * updt * Extract GmxServiceTests.cs * Refact * update todo * Update code * Fix hashdata * Replace static token hashed datas * Set allowance * Add get orders * Add get orders tests * Add ignore * add close orders * revert * Add get gas limit * Start increasePosition. Todo: Finish GetExecutionFee and estimateGas * little refact * Update gitignore * Fix namespaces and clean repo * Add tests samples * Add execution fee * Add increase position * Handle backtest on the frontend * Add tests * Update increase * Test increase * fix increase * Fix size * Start get position * Update get positions * Fix get position * Update rpc and trade mappers * Finish close position * Fix leverage
This commit is contained in:
@@ -76,7 +76,6 @@ public class TradingService : ITradingService
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Scenario GetScenarioByName(string scenario)
|
||||
{
|
||||
return _tradingRepository.GetScenarioByName(scenario);
|
||||
@@ -126,7 +125,8 @@ public class TradingService : ITradingService
|
||||
if (quantityInPosition > 0)
|
||||
{
|
||||
// Position still open
|
||||
position.ProfitAndLoss = TradingBox.GetProfitAndLoss(position, position.Open.Quantity, lastPrice);
|
||||
position.ProfitAndLoss =
|
||||
TradingBox.GetProfitAndLoss(position, position.Open.Quantity, lastPrice, position.Open.Leverage);
|
||||
_logger.LogInformation($"Position is still open - PNL : {position.ProfitAndLoss.Realized} $");
|
||||
_logger.LogInformation($"Requested trades : {orders.Count}");
|
||||
}
|
||||
@@ -138,7 +138,8 @@ public class TradingService : ITradingService
|
||||
// SL hit
|
||||
_logger.LogInformation($"Stop loss is filled on exchange.");
|
||||
position.StopLoss.SetStatus(TradeStatus.Filled);
|
||||
position.ProfitAndLoss = TradingBox.GetProfitAndLoss(position, position.StopLoss.Quantity, position.StopLoss.Price);
|
||||
position.ProfitAndLoss = TradingBox.GetProfitAndLoss(position, position.StopLoss.Quantity,
|
||||
position.StopLoss.Price, position.Open.Leverage);
|
||||
_ = _exchangeService.CancelOrder(account, position.Ticker);
|
||||
}
|
||||
else if (orders.All(o => o.TradeType != TradeType.TakeProfit))
|
||||
@@ -147,19 +148,22 @@ public class TradingService : ITradingService
|
||||
if (position.TakeProfit1.Status == TradeStatus.Filled && position.TakeProfit2 != null)
|
||||
{
|
||||
position.TakeProfit2.SetStatus(TradeStatus.Filled);
|
||||
position.ProfitAndLoss = TradingBox.GetProfitAndLoss(position, position.TakeProfit2.Quantity, position.TakeProfit2.Price);
|
||||
position.ProfitAndLoss = TradingBox.GetProfitAndLoss(position, position.TakeProfit2.Quantity,
|
||||
position.TakeProfit2.Price, 1);
|
||||
_logger.LogInformation($"TakeProfit 2 is filled on exchange.");
|
||||
}
|
||||
else
|
||||
{
|
||||
position.TakeProfit1.SetStatus(TradeStatus.Filled);
|
||||
position.ProfitAndLoss = TradingBox.GetProfitAndLoss(position, position.TakeProfit1.Quantity, position.TakeProfit1.Price);
|
||||
position.ProfitAndLoss = TradingBox.GetProfitAndLoss(position, position.TakeProfit1.Quantity,
|
||||
position.TakeProfit1.Price, 1);
|
||||
_logger.LogInformation($"TakeProfit 1 is filled on exchange.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation($"Position closed manually or forced close by exchange because quantity in position is below 0.");
|
||||
_logger.LogInformation(
|
||||
$"Position closed manually or forced close by exchange because quantity in position is below 0.");
|
||||
position.Status = PositionStatus.Finished;
|
||||
|
||||
if (orders.Any()) await _exchangeService.CancelOrder(account, position.Ticker);
|
||||
@@ -202,10 +206,8 @@ public class TradingService : ITradingService
|
||||
return 0.000665M;
|
||||
}
|
||||
|
||||
return _cacheService.GetOrSave($"Fee-{account.Exchange}", () =>
|
||||
{
|
||||
return _tradingRepository.GetFee(TradingExchanges.Evm)?.Cost ?? 0m;
|
||||
}, TimeSpan.FromHours(2));
|
||||
return _cacheService.GetOrSave($"Fee-{account.Exchange}",
|
||||
() => { return _tradingRepository.GetFee(TradingExchanges.Evm)?.Cost ?? 0m; }, TimeSpan.FromHours(2));
|
||||
}
|
||||
|
||||
public void UpdatePosition(Position position)
|
||||
@@ -246,7 +248,6 @@ public class TradingService : ITradingService
|
||||
{
|
||||
await ManageTrader(a, availableTickers);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
_cacheService.SaveValue(key, aqip, TimeSpan.FromMinutes(10));
|
||||
@@ -255,8 +256,10 @@ public class TradingService : ITradingService
|
||||
public IEnumerable<Trader> GetTradersWatch()
|
||||
{
|
||||
var watchAccount = _statisticRepository.GetBestTraders();
|
||||
var customWatchAccount = _accountService.GetAccounts(true, false).Where(a => a.Type == AccountType.Watch).ToList().MapToTraders();
|
||||
watchAccount.AddRange(customWatchAccount.Where(a => !watchAccount.Any(w => w.Address.Equals(a.Address, StringComparison.InvariantCultureIgnoreCase))));
|
||||
var customWatchAccount = _accountService.GetAccounts(true, false).Where(a => a.Type == AccountType.Watch)
|
||||
.ToList().MapToTraders();
|
||||
watchAccount.AddRange(customWatchAccount.Where(a =>
|
||||
!watchAccount.Any(w => w.Address.Equals(a.Address, StringComparison.InvariantCultureIgnoreCase))));
|
||||
return watchAccount;
|
||||
}
|
||||
|
||||
@@ -279,14 +282,16 @@ public class TradingService : ITradingService
|
||||
{
|
||||
if (oldTrade != null)
|
||||
{
|
||||
_logger.LogInformation($"[{shortAddress}][{ticker}] Trader previously got a position open but the position was close by trader");
|
||||
_logger.LogInformation(
|
||||
$"[{shortAddress}][{ticker}] Trader previously got a position open but the position was close by trader");
|
||||
await _messengerService.SendClosedPosition(a.Account.Address, oldTrade);
|
||||
a.Trades.Remove(oldTrade);
|
||||
}
|
||||
}
|
||||
else if ((newTrade != null && oldTrade == null) || (newTrade.Quantity > oldTrade.Quantity))
|
||||
{
|
||||
_logger.LogInformation($"[{shortAddress}][{ticker}] Trader increase {newTrade.Direction} by {newTrade.Quantity - (oldTrade?.Quantity ?? 0)} with leverage {newTrade.Leverage} at {newTrade.Price} leverage.");
|
||||
_logger.LogInformation(
|
||||
$"[{shortAddress}][{ticker}] Trader increase {newTrade.Direction} by {newTrade.Quantity - (oldTrade?.Quantity ?? 0)} with leverage {newTrade.Leverage} at {newTrade.Price} leverage.");
|
||||
|
||||
var index = a.Trades.IndexOf(oldTrade);
|
||||
if (index != -1)
|
||||
@@ -307,12 +312,14 @@ public class TradingService : ITradingService
|
||||
var decreaseAmount = oldTrade.Quantity - newTrade.Quantity;
|
||||
var index = a.Trades.IndexOf(oldTrade);
|
||||
a.Trades[index] = newTrade;
|
||||
_logger.LogInformation($"[{a.Account.Address.Substring(0, 6)}][{ticker}] Trader decrease position but didnt close it {decreaseAmount}");
|
||||
_logger.LogInformation(
|
||||
$"[{a.Account.Address.Substring(0, 6)}][{ticker}] Trader decrease position but didnt close it {decreaseAmount}");
|
||||
await _messengerService.SendDecreasePosition(a.Account.Address, newTrade, decreaseAmount);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation($"[{shortAddress}][{ticker}] No change - Quantity still {newTrade.Quantity}");
|
||||
_logger.LogInformation(
|
||||
$"[{shortAddress}][{ticker}] No change - Quantity still {newTrade.Quantity}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -324,7 +331,7 @@ public class TradingService : ITradingService
|
||||
|
||||
private List<TraderFollowup> GetAccountsQuantityInPosition(IEnumerable<Trader> watchAccount)
|
||||
{
|
||||
var result = new List<TraderFollowup> ();
|
||||
var result = new List<TraderFollowup>();
|
||||
foreach (var account in watchAccount)
|
||||
{
|
||||
var trader = SetupFollowUp(account);
|
||||
@@ -352,4 +359,4 @@ public class TradingService : ITradingService
|
||||
public List<Trade> Trades { get; set; }
|
||||
public List<string> PositionIdentifiers { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user