public SignalFXMole(MarketData fx, IndicatorRSI rsi, ModelMacD macD, IndicatorRSI[] rsiRefs, decimal volCoeff) : base("FXMole_" + rsi.PeriodSizeMn + "_" + rsi.NbPeriods + "_" + fx.Id, fx) { _startThreshold = 2m; _timeFrameMn = 90; _timeFramePeakMn = 75; _timeFrameBottomMn = 30; _timeFrameStopLossMn = 30; _timeFrameRsiLongMn = 5; _rsiBuyThreshold = 30m; _rsiSellThreshold = 70m; _minVol = 15m * volCoeff; _maxVol = 30m * volCoeff; _maxLongVol = 50m * volCoeff; _maxTotalVol = 100m * volCoeff; _rsi_loss_reset = true; _emaVeryShort = macD.SignalLow.IndicatorLow; _emaShort = macD.SignalHigh.IndicatorLow; _emaLong = macD.SignalHigh.IndicatorHigh; _rsiRefs = rsiRefs; _nominalVol = 10m * volCoeff; _highVol = 25m * volCoeff; _maxSpread = volCoeff * 1.20m; // max +20% spread _maxShortMacDSpread = 5m * volCoeff; _maxLongMacDSpread = 10m * volCoeff; _maxWmVol = 2m * volCoeff; //_rsiLong = new IndicatorRSI(fx, rsi.PeriodSizeMn, rsi.NbPeriods * 2); _trend = new IndicatorTrend(fx, rsi.PeriodSizeMn * 30, rsi.NbPeriods, false); _wmvol = new IndicatorWMVol(fx, _emaVeryShort, 60, 90); //_wmvolLong = new IndicatorWMVol(fx, _emaLong); _volTrend = new IndicatorTrend(_wmvol, 30, 6, true); //_volTrendAvg = new IndicatorEMA(_wmvol, _timeFrameMn); _volTrendTrend = new IndicatorTrend(_volTrend, 30, 6, true); //_volCurve = new IndicatorCurve(_volTrend); for (int idxInterval = 0; idxInterval < _timeFrameStopLossMn; idxInterval++) { stopLossTimes.Add(new Interval(idxInterval, idxInterval + 1)); } decimal prevStopLoss = _nominalVol; for (int idxLin = 0; idxLin < _timeFrameStopLossMn; idxLin++) { decimal stopLoss = _nominalVol * (1m - (decimal)idxLin / _timeFrameStopLossMn); stopLossValues.Add(new Interval(prevStopLoss, stopLoss)); stopWinValues.Add(new Interval(prevStopLoss, stopLoss)); prevStopLoss = stopLoss; } for (int idxLin = 0; idxLin <= _timeFrameStopLossMn; idxLin++) { bigStopLossValues.Add(new Interval(_highVol, _highVol)); } _trend.Subscribe(OnUpdateTrend, null); //_rsiLong.Subscribe(OnUpdateRsiLong, null); _wmvol.Subscribe(OnUpdateWmVol, null); //_wmvolLong.Subscribe(OnUpdateWmVolLong, null); _mktIndicator.Add(rsi); _signalCode = SIGNAL_CODE.HOLD; }
public override void Subscribe(string[] epics, IHandyTableListener tableListener) { Dictionary <string, List <CqlQuote> > priceData = GetReplayData(epics); if (priceData.Count == 0) { return; } Calendar dayCalendar = new Calendar(priceData.First().Value[0].t); foreach (var epic in epics) { // for each quote, associate the observed gains in the near future var mktData = new MarketData(epic); var wmaLow = new IndicatorEMA(mktData, 2); var wmaMid = new IndicatorEMA(mktData, 10); var wmaHigh = new IndicatorEMA(mktData, 30); var wmaVeryHigh = new IndicatorEMA(mktData, 90); var rsiShort = new IndicatorRSI(mktData, 1, 14); var rsiLong = new IndicatorRSI(mktData, 2, 14); var trendShort = new IndicatorTrend(mktData, 90, 14, false); var trendLong = new IndicatorTrend(mktData, 180, 14, false); var wmvolLow = new IndicatorWMVol(mktData, wmaLow, 60, 90); var wmvolHigh = new IndicatorWMVol(mktData, wmaMid, 60, 90); var volTrendLow = new IndicatorTrend(wmvolLow, 30, 6, true); var volTrendHigh = new IndicatorTrend(wmvolHigh, 60, 6, true); var allIndicators = new List <IndicatorWMA>(); allIndicators.Add(wmaLow); allIndicators.Add(wmaMid); allIndicators.Add(wmaHigh); allIndicators.Add(wmaVeryHigh); allIndicators.Add(rsiShort); allIndicators.Add(rsiLong); allIndicators.Add(trendShort); allIndicators.Add(trendLong); allIndicators.Add(wmvolLow); allIndicators.Add(wmvolHigh); allIndicators.Add(volTrendLow); allIndicators.Add(volTrendHigh); foreach (var quote in priceData[epic]) { var mktDataValue = new Price(quote.MidPrice()); mktData.Process(quote.t, mktDataValue); foreach (var ind in allIndicators) { ind.Process(quote.t, mktDataValue); } } var expectations = new Dictionary <DateTime, KeyValuePair <CqlQuote, decimal> >(); var gainDistribution = new SortedList <int, DateTime>(); KeyValuePair <int, DateTime> minProfit = new KeyValuePair <int, DateTime>(1000000, DateTime.MinValue); KeyValuePair <int, DateTime> maxProfit = new KeyValuePair <int, DateTime>(-1000000, DateTime.MinValue); var rnd = new Random(155); var tradingStart = Config.ParseDateTimeLocal(Config.Settings["TRADING_START_TIME"]); var tradingStop = Config.ParseDateTimeLocal(Config.Settings["TRADING_STOP_TIME"]); var wmaVeryHighStart = wmaVeryHigh.Average(tradingStart); var amplitude = 100.0m; foreach (var quote in priceData[epic]) { if (quote.t.TimeOfDay < tradingStart.TimeOfDay || quote.t.TimeOfDay > tradingStop.TimeOfDay) { continue; } string evtName = ""; if (dayCalendar.IsNearEvent(mktData.Name, quote.t, ref evtName)) { continue; } var futureVal = (mktData.TimeSeries.Max(quote.t.AddMinutes(5), quote.t.AddMinutes(20)) + mktData.TimeSeries.Min(quote.t.AddMinutes(5), quote.t.AddMinutes(20))) / 2m; var profit = (int)Math.Round(futureVal - quote.MidPrice()); expectations.Add(quote.t, new KeyValuePair <CqlQuote, decimal>(quote, profit)); if (gainDistribution.ContainsKey(profit)) { if ((quote.t - gainDistribution[profit]).Hours > 3 && (rnd.Next(100) == 0)) { gainDistribution[profit] = quote.t; } } else { gainDistribution[profit] = quote.t; } if (profit < minProfit.Key) { minProfit = new KeyValuePair <int, DateTime>(profit, gainDistribution[profit]); } if (profit > maxProfit.Key) { maxProfit = new KeyValuePair <int, DateTime>(profit, gainDistribution[profit]); } quote.b = (quote.b - wmaVeryHighStart.Bid) / amplitude; quote.o = (quote.o - wmaVeryHighStart.Offer) / amplitude; } gainDistribution = new SortedList <int, DateTime>((from elt in gainDistribution where !isTooClose(elt, gainDistribution) select elt).ToDictionary(keyVal => keyVal.Key, keyVal => keyVal.Value)); int nbPoints = 10; int idxProfit = 0; KeyValuePair <int, DateTime> nextProfit = minProfit; var selection = new SortedList <DateTime, KeyValuePair <int, CqlQuote> >(); while (idxProfit++ < nbPoints) { selection.Add(gainDistribution[nextProfit.Key], new KeyValuePair <int, CqlQuote>(nextProfit.Key, expectations[gainDistribution[nextProfit.Key]].Key)); var nextKeyVal = gainDistribution.FirstOrDefault(keyVal => keyVal.Key > nextProfit.Key && keyVal.Key >= ((decimal)minProfit.Key + (decimal)idxProfit * (decimal)(maxProfit.Key - minProfit.Key) / (decimal)nbPoints)); if (nextKeyVal.Equals(default(KeyValuePair <int, DateTime>))) { break; } nextProfit = nextKeyVal; } foreach (var dt in selection.Keys) { bool allValid = true; foreach (var ind in allIndicators) { if (ind.TimeSeries[dt] == null) { allValid = false; break; } } if (!allValid) { continue; } PublisherConnection.Instance.Insert(dt, wmaLow, (wmaLow.TimeSeries[dt].Value.Value.Mid() - wmaVeryHighStart.Mid()) / amplitude); PublisherConnection.Instance.Insert(dt, wmaMid, (wmaMid.TimeSeries[dt].Value.Value.Mid() - wmaVeryHighStart.Mid()) / amplitude); PublisherConnection.Instance.Insert(dt, wmaHigh, (wmaHigh.TimeSeries[dt].Value.Value.Mid() - wmaVeryHighStart.Mid()) / amplitude); PublisherConnection.Instance.Insert(dt, wmaVeryHigh, (wmaVeryHigh.TimeSeries[dt].Value.Value.Mid() - wmaVeryHighStart.Mid()) / amplitude); PublisherConnection.Instance.Insert(dt, rsiShort, (rsiShort.TimeSeries[dt].Value.Value.Mid() - 50m) / amplitude); PublisherConnection.Instance.Insert(dt, rsiLong, (rsiLong.TimeSeries[dt].Value.Value.Mid() - 50m) / amplitude); PublisherConnection.Instance.Insert(dt, trendShort, trendShort.TimeSeries[dt].Value.Value.Mid() / 1000m); PublisherConnection.Instance.Insert(dt, trendLong, trendLong.TimeSeries[dt].Value.Value.Mid() / 1000m); PublisherConnection.Instance.Insert(dt, wmvolLow, wmvolLow.TimeSeries[dt].Value.Value.Mid() / 10m); PublisherConnection.Instance.Insert(dt, wmvolHigh, wmvolHigh.TimeSeries[dt].Value.Value.Mid() / 10m); PublisherConnection.Instance.Insert(dt, volTrendLow, volTrendLow.TimeSeries[dt].Value.Value.Mid()); PublisherConnection.Instance.Insert(dt, volTrendHigh, volTrendHigh.TimeSeries[dt].Value.Value.Mid()); PublisherConnection.Instance.Insert(dt, epic, new Value((double)selection[dt].Key / ((double)amplitude / 2.0))); } priceData[epic] = selection.Values.Select(kv => kv.Value).ToList(); } replay(priceData, tableListener); }
public override void Init() { base.Init(); _wma_verylow = new IndicatorEMA(_macD.Index, 2); _wma_low = new IndicatorEMA(_macD.SignalLow.IndicatorLow); _wma_low.PublishingEnabled = false; _wma_mid = new IndicatorEMA(_macD.SignalLow.IndicatorHigh); _wma_mid.PublishingEnabled = false; _wma_high = new IndicatorEMA(_macD.SignalHigh.IndicatorHigh); _wma_high.PublishingEnabled = false; var rsiShort = new IndicatorRSI(_macD.Index, 1, 14); rsiShort.PublishingEnabled = false; var rsiLong = new IndicatorRSI(_macD.Index, 2, 14); rsiLong.PublishingEnabled = false; var wmVol = new IndicatorWMVol(_macD.Index, _wma_low, 60, 90); wmVol.PublishingEnabled = false; _mktIndices.AddRange(_otherIndices); //_mktIndicators.Add(_wma_verylow); //_mktIndicators.Add(_wma_low); //_mktIndicators.Add(_wma_mid); //_mktIndicators.Add(_wma_high); //_mktIndicators.Add(wmVol); //_mktIndicators.Add(new IndicatorWMVol(_index, _wma_mid, 60, 90)); _mktIndicators.Add(new IndicatorNearestLevel(_index)); int lastversion = StaticDataConnection.Instance.GetAnnLatestVersion(_annId, _index.Id); _annWeights = StaticDataConnection.Instance.GetAnnWeights(_annId, _index.Id, lastversion); var signalType = Type.GetType("MidaxLib.SignalANN" + _annId); List <Indicator> annIndicators = new List <Indicator>(); annIndicators.Add(_wma_verylow); annIndicators.Add(_wma_low); annIndicators.Add(_wma_mid); annIndicators.Add(_wma_high); annIndicators.Add(rsiShort); annIndicators.Add(rsiLong); annIndicators.Add(wmVol); List <object> signalParams = new List <object>(); signalParams.Add(_index); signalParams.Add(annIndicators); signalParams.Add(_annWeights); this._ann = (SignalANN)Activator.CreateInstance(signalType, signalParams.ToArray()); this._mktSignals.Add(this._ann); var allIndices = new List <MarketData>(); allIndices.Add(_index); allIndices.AddRange(_otherIndices); foreach (var index in allIndices) { var indicatorLow = new IndicatorLow(index); var indicatorHigh = new IndicatorHigh(index); var indicatorCloseBid = new IndicatorCloseBid(index); var indicatorCloseOffer = new IndicatorCloseOffer(index); _mktIndicators.Add(indicatorLow); _mktIndicators.Add(indicatorHigh); _mktIndicators.Add(indicatorCloseBid); _mktIndicators.Add(indicatorCloseOffer); _mktEODIndicators.Add(indicatorLow); _mktEODIndicators.Add(indicatorHigh); _mktEODIndicators.Add(indicatorCloseBid); _mktEODIndicators.Add(indicatorCloseOffer); _mktEODIndicators.Add(new IndicatorLevelPivot(index)); _mktEODIndicators.Add(new IndicatorLevelR1(index)); _mktEODIndicators.Add(new IndicatorLevelR2(index)); _mktEODIndicators.Add(new IndicatorLevelR3(index)); _mktEODIndicators.Add(new IndicatorLevelS1(index)); _mktEODIndicators.Add(new IndicatorLevelS2(index)); _mktEODIndicators.Add(new IndicatorLevelS3(index)); } }