Global fix (#9)
* Fix time for candle * Fix out ouf range * Fix pnl, fix custom money management * Clean a bit
This commit is contained in:
@@ -17,7 +17,7 @@ public static class ScenarioHelpers
|
||||
strategy.Period.Value),
|
||||
StrategyType.RsiDivergenceConfirm => new RSIDivergenceConfirmStrategy(strategy.Name, strategy.Timeframe,
|
||||
strategy.Period.Value),
|
||||
StrategyType.MacdCross => new MACDCrossStrategy(strategy.Name, strategy.Timeframe,
|
||||
StrategyType.MacdCross => new MacdCrossStrategy(strategy.Name, strategy.Timeframe,
|
||||
strategy.FastPeriods.Value, strategy.SlowPeriods.Value, strategy.SignalPeriods.Value),
|
||||
StrategyType.EmaCross => new EmaCrossStrategy(strategy.Name, strategy.Timeframe, strategy.Period.Value),
|
||||
StrategyType.ThreeWhiteSoldiers => new ThreeWhiteSoldiersStrategy(strategy.Name, strategy.Timeframe,
|
||||
|
||||
@@ -46,8 +46,8 @@ public static class TradingBox
|
||||
Signal signal = null;
|
||||
if (strategies.Count > 1)
|
||||
{
|
||||
var trendSignal = signalOnCandles.Where(s => s.SignalType == SignalType.Trend);
|
||||
var signals = signalOnCandles.Where(s => s.SignalType == SignalType.Signal);
|
||||
var trendSignal = signalOnCandles.Where(s => s.SignalType == SignalType.Trend).ToList();
|
||||
var signals = signalOnCandles.Where(s => s.SignalType == SignalType.Signal).ToList();
|
||||
var contextStrategiesCount = strategies.Count(s => s.SignalType == SignalType.Context);
|
||||
var validContext = true;
|
||||
|
||||
@@ -111,7 +111,7 @@ public static class TradingBox
|
||||
{
|
||||
var position = positions[i];
|
||||
var nextPosition = i + 1 < positions.Count ? positions[i + 1] : null;
|
||||
var (stopLoss, takeProfit) = GetBestSLTPForPosition(candles, position, nextPosition);
|
||||
var (stopLoss, takeProfit) = GetBestSltpForPosition(candles, position, nextPosition);
|
||||
|
||||
stoplossPercentage.Add(stopLoss);
|
||||
takeProfitsPercentage.Add(takeProfit);
|
||||
@@ -126,13 +126,14 @@ public static class TradingBox
|
||||
return moneyManagement;
|
||||
}
|
||||
|
||||
public static (decimal Stoploss, decimal TakeProfit) GetBestSLTPForPosition(List<Candle> candles, Position position,
|
||||
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));
|
||||
c.Date >= position.Date && c.Date <= (nextPosition == null ? candles.Last().Date : nextPosition.Date))
|
||||
.ToList();
|
||||
|
||||
if (position.OriginDirection == TradeDirection.Long)
|
||||
{
|
||||
|
||||
@@ -21,7 +21,7 @@ public static class TradingHelpers
|
||||
|
||||
public static PerformanceMetrics GetStatistics(Dictionary<DateTime, decimal> pnls)
|
||||
{
|
||||
var priceSeries = new TimePriceSeries(pnls);
|
||||
var priceSeries = new TimePriceSeries(pnls.DistinctBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value));
|
||||
|
||||
return priceSeries.CalculatePerformanceMetrics();
|
||||
}
|
||||
|
||||
@@ -6,11 +6,12 @@ using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Domain.Strategies;
|
||||
|
||||
public class MACDCrossStrategy : Strategy
|
||||
public class MacdCrossStrategy : Strategy
|
||||
{
|
||||
public List<Signal> Signals { get; set; }
|
||||
|
||||
public MACDCrossStrategy(string name, Timeframe timeframe, int fastPeriods, int slowPeriods, int signalPeriods) : base(name, timeframe, StrategyType.MacdCross)
|
||||
public MacdCrossStrategy(string name, Timeframe timeframe, int fastPeriods, int slowPeriods, int signalPeriods) :
|
||||
base(name, timeframe, StrategyType.MacdCross)
|
||||
{
|
||||
Signals = new List<Signal>();
|
||||
FastPeriods = fastPeriods;
|
||||
@@ -20,7 +21,7 @@ public class MACDCrossStrategy : Strategy
|
||||
|
||||
public override List<Signal> Run()
|
||||
{
|
||||
if (Candles.Count <= 2*(SlowPeriods + SignalPeriods))
|
||||
if (Candles.Count <= 2 * (SlowPeriods + SignalPeriods))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
@@ -29,7 +30,7 @@ public class MACDCrossStrategy : Strategy
|
||||
{
|
||||
var macd = Candles.GetMacd(FastPeriods.Value, SlowPeriods.Value, SignalPeriods.Value).ToList();
|
||||
var macdCandle = MapMacdToCandle(macd, Candles.TakeLast(SignalPeriods.Value));
|
||||
|
||||
|
||||
if (macd.Count == 0)
|
||||
return null;
|
||||
|
||||
@@ -72,19 +73,20 @@ public class MACDCrossStrategy : Strategy
|
||||
Date = candle.Date,
|
||||
Ticker = candle.Ticker,
|
||||
Exchange = candle.Exchange,
|
||||
FastEma = currentMacd.FastEma.Value,
|
||||
SlowEma = currentMacd.SlowEma.Value,
|
||||
Macd = currentMacd.Macd.Value,
|
||||
Histogram = currentMacd.Histogram.Value
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return macdList;
|
||||
}
|
||||
|
||||
private void AddSignal(CandleMacd candleSignal, Timeframe timeframe, TradeDirection direction, Confidence confidence)
|
||||
private void AddSignal(CandleMacd candleSignal, Timeframe timeframe, TradeDirection direction,
|
||||
Confidence confidence)
|
||||
{
|
||||
var signal = new Signal(MiscExtensions.ParseEnum<Ticker>(candleSignal.Ticker), direction, confidence, candleSignal, candleSignal.Date, candleSignal.Exchange, timeframe, Type, SignalType);
|
||||
var signal = new Signal(MiscExtensions.ParseEnum<Ticker>(candleSignal.Ticker), direction, confidence,
|
||||
candleSignal, candleSignal.Date, candleSignal.Exchange, timeframe, Type, SignalType);
|
||||
if (!Signals.Any(s => s.Identifier == signal.Identifier))
|
||||
{
|
||||
Signals.AddItem(signal);
|
||||
@@ -96,7 +98,5 @@ public class MACDCrossStrategy : Strategy
|
||||
public double Macd { get; set; }
|
||||
public double Signal { get; set; }
|
||||
public double Histogram { get; set; }
|
||||
public double FastEma { get; set; }
|
||||
public double SlowEma { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user