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:
@@ -9,7 +9,8 @@ namespace Managing.Domain.Shared.Helpers;
|
||||
|
||||
public static class TradingBox
|
||||
{
|
||||
public static Signal GetSignal(HashSet<Candle> newCandles, HashSet<IStrategy> strategies, HashSet<Signal> previousSignal)
|
||||
public static Signal GetSignal(HashSet<Candle> newCandles, HashSet<IStrategy> strategies,
|
||||
HashSet<Signal> previousSignal)
|
||||
{
|
||||
var signalOnCandles = new HashSet<Signal>();
|
||||
foreach (var strategy in strategies)
|
||||
@@ -35,10 +36,12 @@ public static class TradingBox
|
||||
return null;
|
||||
|
||||
var data = newCandles.First();
|
||||
return ComputeSignals(strategies, signalOnCandles, MiscExtensions.ParseEnum<Ticker>(data.Ticker), data.Timeframe);
|
||||
return ComputeSignals(strategies, signalOnCandles, MiscExtensions.ParseEnum<Ticker>(data.Ticker),
|
||||
data.Timeframe);
|
||||
}
|
||||
|
||||
public static Signal ComputeSignals(HashSet<IStrategy> strategies, HashSet<Signal> signalOnCandles, Ticker ticker, Timeframe timeframe)
|
||||
public static Signal ComputeSignals(HashSet<IStrategy> strategies, HashSet<Signal> signalOnCandles, Ticker ticker,
|
||||
Timeframe timeframe)
|
||||
{
|
||||
Signal signal = null;
|
||||
if (strategies.Count > 1)
|
||||
@@ -49,34 +52,36 @@ public static class TradingBox
|
||||
var validContext = true;
|
||||
|
||||
if (contextStrategiesCount > 0 &&
|
||||
signalOnCandles.Count(s => s.SignalType == SignalType.Context) != contextStrategiesCount)
|
||||
signalOnCandles.Count(s => s.SignalType == SignalType.Context) != contextStrategiesCount)
|
||||
{
|
||||
validContext = false;
|
||||
}
|
||||
|
||||
if (signals.All(s => s.Direction == TradeDirection.Long) && trendSignal.All(t => t.Direction == TradeDirection.Long) && validContext)
|
||||
if (signals.All(s => s.Direction == TradeDirection.Long) &&
|
||||
trendSignal.All(t => t.Direction == TradeDirection.Long) && validContext)
|
||||
{
|
||||
signal = new Signal(
|
||||
ticker,
|
||||
TradeDirection.Long,
|
||||
Confidence.High,
|
||||
signals.Last().Candle,
|
||||
signals.Last().Date,
|
||||
signals.Last().Exchange,
|
||||
timeframe,
|
||||
StrategyType.Composite, SignalType.Signal);
|
||||
ticker,
|
||||
TradeDirection.Long,
|
||||
Confidence.High,
|
||||
signals.Last().Candle,
|
||||
signals.Last().Date,
|
||||
signals.Last().Exchange,
|
||||
timeframe,
|
||||
StrategyType.Composite, SignalType.Signal);
|
||||
}
|
||||
else if (signals.All(s => s.Direction == TradeDirection.Short) && trendSignal.All(t => t.Direction == TradeDirection.Short) && validContext)
|
||||
else if (signals.All(s => s.Direction == TradeDirection.Short) &&
|
||||
trendSignal.All(t => t.Direction == TradeDirection.Short) && validContext)
|
||||
{
|
||||
signal = new Signal(
|
||||
ticker,
|
||||
TradeDirection.Short,
|
||||
Confidence.High,
|
||||
signals.Last().Candle,
|
||||
signals.Last().Date,
|
||||
signals.Last().Exchange,
|
||||
timeframe,
|
||||
StrategyType.Composite, SignalType.Signal);
|
||||
ticker,
|
||||
TradeDirection.Short,
|
||||
Confidence.High,
|
||||
signals.Last().Candle,
|
||||
signals.Last().Date,
|
||||
signals.Last().Exchange,
|
||||
timeframe,
|
||||
StrategyType.Composite, SignalType.Signal);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -88,7 +93,8 @@ public static class TradingBox
|
||||
return signal;
|
||||
}
|
||||
|
||||
public static MoneyManagement GetBestMoneyManagement(List<Candle> candles, List<Position> positions, MoneyManagement originMoneyManagement)
|
||||
public static MoneyManagement GetBestMoneyManagement(List<Candle> candles, List<Position> positions,
|
||||
MoneyManagement originMoneyManagement)
|
||||
{
|
||||
// Foreach positions, identitify the price when the position is open
|
||||
// Then, foreach candles, get the maximum price before the next position
|
||||
@@ -120,11 +126,13 @@ public static class TradingBox
|
||||
return moneyManagement;
|
||||
}
|
||||
|
||||
public static (decimal Stoploss, decimal TakeProfit) GetBestSLTPForPosition(List<Candle> candles, Position position, Position nextPosition)
|
||||
public static (decimal Stoploss, decimal TakeProfit) GetBestSLTPForPosition(List<Candle> candles, Position position,
|
||||
Position nextPosition)
|
||||
{
|
||||
var stopLoss = 0M;
|
||||
var takeProfit = 0M;
|
||||
var candlesBeforeNextPosition = candles.Where(c => c.Date >= position.Date && c.Date <= (nextPosition == null ? candles.Last().Date : nextPosition.Date));
|
||||
var candlesBeforeNextPosition = candles.Where(c =>
|
||||
c.Date >= position.Date && c.Date <= (nextPosition == null ? candles.Last().Date : nextPosition.Date));
|
||||
|
||||
if (position.OriginDirection == TradeDirection.Long)
|
||||
{
|
||||
@@ -140,7 +148,7 @@ public static class TradingBox
|
||||
stopLoss = GetPercentageFromEntry(position.Open.Price, maxPrice);
|
||||
takeProfit = GetPercentageFromEntry(position.Open.Price, minPrice);
|
||||
}
|
||||
|
||||
|
||||
return (stopLoss, takeProfit);
|
||||
}
|
||||
|
||||
@@ -149,13 +157,18 @@ public static class TradingBox
|
||||
return Math.Abs(100 - ((100 * price) / entry));
|
||||
}
|
||||
|
||||
public static ProfitAndLoss GetProfitAndLoss(Position position, decimal quantity, decimal price)
|
||||
public static ProfitAndLoss GetProfitAndLoss(Position position, decimal quantity, decimal price, decimal leverage)
|
||||
{
|
||||
var orders = new List<Tuple<decimal, decimal>>
|
||||
{
|
||||
new Tuple<decimal, decimal>(position.Open.Quantity, position.Open.Price),
|
||||
new Tuple<decimal, decimal>(-quantity, price)
|
||||
};
|
||||
return new ProfitAndLoss(orders, position.OriginDirection);
|
||||
{
|
||||
new Tuple<decimal, decimal>(position.Open.Quantity, position.Open.Price),
|
||||
new Tuple<decimal, decimal>(-quantity, price)
|
||||
};
|
||||
|
||||
|
||||
var pnl = new ProfitAndLoss(orders, position.OriginDirection);
|
||||
// Apply leverage on the realized pnl
|
||||
pnl.Realized = pnl.Realized * leverage;
|
||||
return pnl;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user