public IStrategyDecision GetStrategyDecision(RateRecord rec) { DateTime date = rec.UpdateTime; decimal price = rec.Value; IDictionary <DateTime, decimal> historyData = _historyData[rec.Name]; if (!historyData.Keys.Contains(date)) { historyData.Add(date, price); } //return TestAnalize(historyData); decimal lastValue = StrategyHelper.GetLastValue(historyData); decimal decrease = StrategyHelper.GetMaximum(INTERVAL_MINUTES, historyData) - lastValue; decimal increase = lastValue - StrategyHelper.GetMinimum(INTERVAL_MINUTES, historyData); decimal largeMedium = (StrategyHelper.GetMaximum(LARGE_MEDIUM_INTERVAL_MINUTES, historyData) + StrategyHelper.GetMinimum(LARGE_MEDIUM_INTERVAL_MINUTES, historyData)) / 2; int momentDirection = 0; if (decrease > MAX_DIRECTION) { momentDirection = -1; } if (increase > MAX_DIRECTION) { momentDirection = 1; } int longDirection = 0; if (lastValue > largeMedium) { longDirection = 1; } if (lastValue < largeMedium) { longDirection = -1; } int result = 0; if (momentDirection == -1 && longDirection == +1) { result = 1; } if (momentDirection == 1 && longDirection == -1) { result = -1; } BaseStrategyDecision decision = new BaseStrategyDecision(); decision.TakeProfit = lastValue + (TAKE_PROFIT_POINTS * result); decision.StopLoss = lastValue - (STOP_LOSS_POINTS * result); StringBuilder additionalInfo = new StringBuilder(); additionalInfo.AppendFormat("Максимальное изменение за две минуты:{0}", (decrease > increase ? decrease : increase) * momentDirection); additionalInfo.AppendFormat("Текущее отклонение от среднего за 30 минут:{0}", lastValue - largeMedium); return(decision); }
public static IStrategyDecision TestAnalize(IDictionary <DateTime, decimal> historyData) { decimal lastValue = StrategyHelper.GetLastValue(historyData); BaseStrategyDecision decision = new BaseStrategyDecision(); if ((decimal)DateTime.Now.Minute % 2 == 0) { decision.TakeProfit = lastValue + (TAKE_PROFIT_POINTS * -1); decision.StopLoss = lastValue - (STOP_LOSS_POINTS * -1); return(decision); } else { decision.TakeProfit = lastValue + (TAKE_PROFIT_POINTS); decision.StopLoss = lastValue - (STOP_LOSS_POINTS); return(decision); } }
public IStrategyProvider Init(IDictionary <string, IDictionary <DateTime, decimal> > historyData) { _candles = StrategyHelper.BuildCandles(historyData, CANDLES_INTERVAL_MINUTES); return(this); }
public virtual IStrategyDecision GetStrategyDecision(RateRecord rec) { StrategyHelper.CloseCandle(_candles, rec, CANDLES_INTERVAL_MINUTES); BaseStrategyDecision decision = new BaseStrategyDecision(); Candle maximum = GetMaximumForInterval(rec.Name, CANDLES_RANGE, _candles[rec.Name].Last()); Candle minimum = GetMinimumForInterval(rec.Name, CANDLES_RANGE, _candles[rec.Name].Last()); if (maximum == null || minimum == null) { return(decision); } if (maximum.OpenTime > minimum.OpenTime) //тренд вверх { Candle previousMaximum = GetMaximumBetween(rec.Name, minimum, maximum); if (previousMaximum != null) { Candle previousMinimum = GetMinimumBetween(rec.Name, previousMaximum, maximum); if (previousMinimum != null) { //пробойный бар Candle breakout = GetMaximumBreakout(rec.Name, previousMinimum, previousMaximum.HighPrice); if (breakout != null) { Candle previousMaximum2 = GetMaximumBetween(rec.Name, minimum, previousMaximum); if (previousMaximum2 != null) { //наш takeProfit Candle previousMinimum2 = GetMinimumBetween(rec.Name, previousMaximum2, previousMaximum); if (previousMinimum2 != null && rec.Value < breakout.LowPrice) { if (rec.Value - previousMinimum2.LowPrice > 0.0010m && breakout.HighPrice - rec.Value > 0.0005m ) { decision.TakeProfit = previousMinimum2.LowPrice; decision.StopLoss = breakout.HighPrice; StringBuilder sb = new StringBuilder(); sb.AppendFormat("Takeprofit:{0}{1}", decision.TakeProfit, Environment.NewLine); sb.AppendFormat("Stoploss:{0}{1}", decision.StopLoss, Environment.NewLine); sb.AppendFormat("Предпоследний колебательный минимум был:{0:yyy-MM-dd HH:mm}{1}", previousMinimum2.OpenTime, Environment.NewLine); sb.AppendFormat("Пробойный бар:{0:yyy-MM-dd HH:mm}{1}", breakout.OpenTime, Environment.NewLine); decision.AdditionalInfo = sb.ToString(); } } } } } } } if (minimum.OpenTime > maximum.OpenTime) // тренд вниз { Candle previousMinimum = GetMinimumBetween(rec.Name, maximum, minimum); if (previousMinimum != null) { Candle previousMaximum = GetMaximumBetween(rec.Name, previousMinimum, minimum); if (previousMaximum != null) { //пробойный бар Candle breakout = GetMinimumBreakout(rec.Name, previousMaximum, previousMinimum.LowPrice); if (breakout != null) { Candle previousMinimum2 = GetMinimumBetween(rec.Name, maximum, previousMinimum); if (previousMinimum2 != null) { //наш takeProfit Candle previousMaximum2 = GetMaximumBetween(rec.Name, previousMinimum2, previousMinimum); if (previousMaximum2 != null && rec.Value > breakout.HighPrice) { if (previousMaximum2.HighPrice - rec.Value > 0.0010m && rec.Value - breakout.LowPrice > 0.0005m ) { decision.TakeProfit = previousMaximum2.HighPrice; decision.StopLoss = breakout.LowPrice; StringBuilder sb = new StringBuilder(); sb.AppendFormat("Takeprofit:{0}{1}", decision.TakeProfit, Environment.NewLine); sb.AppendFormat("Stoploss:{0}{1}", decision.StopLoss, Environment.NewLine); sb.AppendFormat("Предпоследний колебательный максимум был:{0:yyy-MM-dd HH:mm}{1}", previousMaximum2.OpenTime, Environment.NewLine); sb.AppendFormat("Пробойный бар:{0:yyy-MM-dd HH:mm}{1}", breakout.OpenTime, Environment.NewLine); decision.AdditionalInfo = sb.ToString(); } } } } } } } return(decision); }