Esempio n. 1
0
        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;
        }
Esempio n. 2
0
 public ModelFXMole(List <MarketData> fx, ModelMacD macD, decimal volCoeff)
 {
     _fx        = fx;
     _macD      = macD;
     _volCoeff  = volCoeff;
     _rsi       = new IndicatorRSI(_fx[0], 3, 45);
     _mapRsiRef = new IndicatorRSI[_fx.Count - 1];
     for (int idxFX = 1; idxFX < _fx.Count; idxFX++)
     {
         _mapRsiRef[idxFX - 1] = new IndicatorRSI(_fx[idxFX], 1, 14);
     }
 }
Esempio n. 3
0
 public IndicatorWatershed(MarketData mktData, int resolutionMinutes, int nbPools, IndicatorRSI rsi)
     : base("Water_" + resolutionMinutes + "_" + nbPools + "_", new List <MarketData> {
     mktData
 })
 {
     _resolutionSeconds = resolutionMinutes * 60;
     _nbPools           = nbPools;
     _rsi = rsi;
     if (Config.Settings.ContainsKey("ASSUMPTION_TREND"))
     {
         _trend = Config.Settings["ASSUMPTION_TREND"] == "BEAR" ? SIGNAL_CODE.SELL : SIGNAL_CODE.BUY;
     }
 }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        protected override bool Process(MarketData indicator, DateTime updateTime, Price value, ref Signal.Tick tradingOrder)
        {
            if (_calendar == null)
            {
                _calendar = new Calendar(updateTime);
            }
            IndicatorRSI rsi = (IndicatorRSI)indicator;

            if (indicator.TimeSeries.TotalMinutes(updateTime) < _timeFrameMn)
            {
                return(false);
            }
            if (!_startValue.HasValue)
            {
                _startValue = value.Mid();
            }
            Price curRsi  = rsi.TimeSeries.Last();
            Price prevRsi = rsi.TimeSeries.PrevValue(updateTime).Value.Value;

            if ((prevRsi.Bid <= 50m && curRsi.Bid >= 50m) || (curRsi.Bid <= 50m && prevRsi.Bid >= 50m))
            {
                _rsi_loss_reset = true;
            }
            //_rsiSellThreshold += Math.Max(0m, curRsi.Bid - (decimal)getSellThreshold()) / 2m;
            //_rsiBuyThreshold += Math.Min(0m, curRsi.Bid - (decimal)getBuyThreshold()) / 2m;
            var minVal = _asset.TimeSeries.Min(updateTime.AddMinutes(-_timeFrameStopLossMn), updateTime);
            var maxVal = _asset.TimeSeries.Max(updateTime.AddMinutes(-_timeFrameStopLossMn), updateTime);

            if (maxVal - minVal > _maxVol)
            {
                //WriteBlockReason(updateTime, string.Format("reset due to vol > {0}", _maxVol));
                _rsi_loss_reset = false;
            }
            minVal = _asset.TimeSeries.Min(updateTime.AddMinutes(-_timeFrameBottomMn), updateTime);
            maxVal = _asset.TimeSeries.Max(updateTime.AddMinutes(-_timeFrameBottomMn), updateTime);
            if (maxVal - minVal > _maxLongVol)
            {
                //WriteBlockReason(updateTime, string.Format("reset due to long vol > {0}", _maxLongVol));
                _rsi_loss_reset = false;
            }
            if (Math.Abs(value.Mid() - _startValue.Value) > _maxTotalVol)
            {
                WriteBlockReason(updateTime, string.Format("stopped trading due to vol > {0}", _maxTotalVol));
                _stopTrading = true;
            }
            if (_tradingStart > DateTime.MinValue)
            {
                if ((decimal)(updateTime - _tradingStart).TotalMilliseconds > _timeFrameStopLossMn * 60000m)
                {
                    _rsi_loss_reset = false;
                    return(close(_id + string.Format(" close event due to timeout, AssetStart = {0}, Value = {1}", _tradingStartValue, value.Bid), ref tradingOrder));
                }
                else
                {
                    int     idxStopLoss = -1;
                    decimal ratio       = 0m;
                    foreach (var interval in stopLossTimes)
                    {
                        idxStopLoss++;
                        if (interval.IsInside((int)(updateTime - _tradingStart).TotalMinutes))
                        {
                            ratio = interval.Ratio((decimal)(updateTime - _tradingStart).TotalMinutes);
                            break;
                        }
                    }
                    if (_signalCode == SIGNAL_CODE.BUY)
                    {
                        var assetMin    = rsi.MinAsset((int)(updateTime - _tradingStart).TotalMinutes + 1);
                        var stopWin     = stopWinValues[idxStopLoss].Value(ratio);
                        var stopBigLoss = bigStopLossValues[idxStopLoss].Value(ratio);
                        if (value.Bid >= _tradingStartValue + stopWin && curRsi.Bid >= getBuyThreshold() + 10)
                        {
                            return(close(_id + string.Format(" close event due to stop win. SELL AssetStart = {0}, LastValue = {1}, StopWin = {2}", _tradingStartValue, value.Bid, stopWin), ref tradingOrder));
                        }
                        else if (value.Bid - assetMin >= stopWin && curRsi.Bid >= getBuyThreshold() + 10)
                        {
                            if (value.Bid > _tradingStartValue)
                            {
                                return(close(_id + string.Format(" close event due to stop win. SELL AssetStart = {0}, LastValue = {1}", _tradingStartValue, value.Bid), ref tradingOrder));
                            }
                            else
                            {
                                _rsi_loss_reset = false;
                                return(close(_id + string.Format(" close event due to stop loss. SELL AssetStart = {0}, LastValue = {1}", _tradingStartValue, value.Bid), ref tradingOrder));
                            }
                        }
                        else if (value.Bid - _tradingStartValue >= stopBigLoss)
                        {
                            _rsi_loss_reset = false;
                            return(close(_id + string.Format(" close event due to stop loss. SELL AssetMin = {0}, LastValue = {1}, StopLoss = {2}", assetMin, value.Bid, stopBigLoss), ref tradingOrder));
                        }/*
                          * else if (_trend.TimeSeries.Last().Bid <= _tradingTrend - 5m && curRsi.Bid >= getBuyThreshold() + 10)
                          * {
                          * if (value.Offer > _tradingStartValue)
                          *     return close(_id + string.Format(" close event due to stop win forced by trend. SELL AssetStart = {0}, LastValue = {1}", _tradingStartValue, value.Offer), ref tradingOrder);
                          * else
                          * {
                          *     _rsi_loss_reset = false;
                          *     return close(_id + string.Format(" close event due to stop loss forced by trend. SELL AssetStart = {0}, LastValue = {1}", _tradingStartValue, value.Offer), ref tradingOrder);
                          * }
                          * }*/
                        else
                        {
                            var stopLoss    = stopLossValues[idxStopLoss].Value(ratio);
                            var bigStopLoss = bigStopLossValues[idxStopLoss].Value(ratio);
                            if ((_tradingStartValue - value.Bid >= stopLoss && curRsi.Bid >= getBuyThreshold() + 20) ||
                                (_tradingStartValue - value.Bid >= bigStopLoss))
                            {
                                _rsi_loss_reset = false;
                                return(close(_id + string.Format(" close event due to stop loss. SELL AssetStart = {0}, LastValue = {1}, StopLoss = {2}", _tradingStartValue, value.Bid, stopLoss), ref tradingOrder));
                            }
                        }
                    }
                    else
                    {
                        var assetMax    = rsi.MaxAsset((int)(updateTime - _tradingStart).TotalMinutes + 1);
                        var stopWin     = stopWinValues[idxStopLoss].Value(ratio);
                        var stopBigLoss = bigStopLossValues[idxStopLoss].Value(ratio);
                        if (value.Offer <= _tradingStartValue - stopWin && curRsi.Bid <= getSellThreshold() - 10)
                        {
                            return(close(_id + string.Format(" close event due to stop win. BUY AssetStart = {0}, LastValue = {1}, StopWin = {2}", _tradingStartValue, value.Offer, stopWin), ref tradingOrder));
                        }
                        else if (assetMax - value.Offer >= stopWin && curRsi.Bid <= getSellThreshold() - 10)
                        {
                            if (value.Offer < _tradingStartValue)
                            {
                                return(close(_id + string.Format(" close event due to stop win. BUY AssetStart = {0}, LastValue = {1}", _tradingStartValue, value.Offer), ref tradingOrder));
                            }
                            else
                            {
                                _rsi_loss_reset = false;
                                return(close(_id + string.Format(" close event due to stop loss. BUY AssetStart = {0}, LastValue = {1}", _tradingStartValue, value.Offer), ref tradingOrder));
                            }
                        }
                        else if (assetMax - value.Offer >= stopBigLoss)
                        {
                            _rsi_loss_reset = false;
                            return(close(_id + string.Format(" close event due to stop loss. BUY AssetMax = {0}, LastValue = {1}, StopLoss = {2}", assetMax, value.Offer, stopBigLoss), ref tradingOrder));
                        }/*
                          * else if (_trend.TimeSeries.Last().Bid >= _tradingTrend + 5m && curRsi.Bid <= getSellThreshold() - 10)
                          * {
                          * if (value.Offer < _tradingStartValue)
                          *     return close(_id + string.Format(" close event due to stop win forced by trend. BUY AssetStart = {0}, LastValue = {1}", _tradingStartValue, value.Offer), ref tradingOrder);
                          * else
                          * {
                          *     _rsi_loss_reset = false;
                          *     return close(_id + string.Format(" close event due to stop loss forced by trend. BUY AssetStart = {0}, LastValue = {1}", _tradingStartValue, value.Offer), ref tradingOrder);
                          * }
                          * }*/
                        else
                        {
                            var stopLoss    = stopLossValues[idxStopLoss].Value(ratio);
                            var bigStopLoss = bigStopLossValues[idxStopLoss].Value(ratio);
                            if ((value.Offer - _tradingStartValue >= stopLoss && curRsi.Bid <= getSellThreshold() - 20) ||
                                (value.Offer - _tradingStartValue >= bigStopLoss))
                            {
                                _rsi_loss_reset = false;
                                return(close(_id + string.Format(" close event due to stop loss. BUY AssetStart = {0}, LastValue = {1}, StopLoss = {2}", _tradingStartValue, value.Offer, stopLoss), ref tradingOrder));
                            }
                        }
                    }
                }
            }
            else
            {
                if (!_rsi_loss_reset || _stopTrading || maxVal - minVal < _minVol || value.Offer - value.Bid > _maxSpread)
                {
                    return(false);
                }

                /*
                 * foreach (var rsiRef in _rsiRefs)
                 * {
                 *  if (rsiRef.TimeSeries.Count > 0)
                 *  {
                 *      if (Math.Abs(curRsi.Mid() - 50m) < Math.Abs(rsiRef.TimeSeries.Last().Mid() - 50m))
                 *          return false;
                 *  }
                 * }*/
                string eventName = "";
                if (_calendar.IsNearEvent(_asset.Name, updateTime, ref eventName))
                {
                    WriteBlockReason(updateTime, "deal blocked by event: " + eventName);
                    _rsi_loss_reset = false;
                    return(false);
                }
                var rsiMax = rsi.MaxRsi(_timeFramePeakMn);
                var rsiMin = rsi.MinRsi(_timeFramePeakMn);
                //var rsiLongMax = _rsiLong.MaxRsi(_timeFrameRsiLongMn);
                //var rsiLongMin = _rsiLong.MinRsi(_timeFrameRsiLongMn);
                var rsiAdjustment   = Math.Max(0, rsi.MaxRsi(_timeFrameBottomMn) - getSellThreshold()) - Math.Max(0, getBuyThreshold() - rsi.MinRsi(_timeFrameBottomMn));
                var curEmaVeryShort = _emaVeryShort.TimeSeries.Last();
                var curEmaShort     = _emaShort.TimeSeries.Last();
                var curEmaLong      = _emaLong.TimeSeries.Last();
                var curVol          = _wmvol.TimeSeries.Last().Bid;
                if (curVol < _maxWmVol)
                {
                    WriteBlockReason(updateTime, "deal blocked due to vol < " + _maxWmVol);
                    return(false);
                }
                var curVolAvg = (_wmvol.Min() + _wmvol.Max()) / 2m;
                if (curVol < curVolAvg)
                {
                    WriteBlockReason(updateTime, "deal blocked due to vol < vol average");
                    return(false);
                }
                if ((curRsi.Bid >= getSellThreshold()) &&
                    //(curRsi.Bid - rsiAdjustment >= getSellThreshold() && curRsi.Bid >= (getSellThreshold() - 10))) && (rsiLongMax >= getSellThreshold() - 15m) &&
                    (curRsi.Bid > rsiMax - _startThreshold) && (curEmaVeryShort.Mid() + _maxShortMacDSpread > curEmaLong.Mid()) &&
                    (Math.Abs(curEmaShort.Mid() - curEmaLong.Mid()) < _maxLongMacDSpread))
                {
                    var curVolTrend = _volTrend.TimeSeries.Last().Bid;
                    if (curVolTrend > 0.5m || curVolTrend < 0m)
                    {
                        WriteBlockReason(updateTime, "deal blocked due to vol not within 0 < trend < 0.5");
                        return(false);
                    }
                    if (!_volTrendTrend.IsMin(2, curVolTrend, 0.01m) || _volTrendTrend.IsMax(2, curVolTrend, 0.02m))
                    {
                        WriteBlockReason(updateTime, "sell event blocked due to vol trend not 1mn minimum");
                        return(false);
                    }
                    tradingOrder       = _onSell;
                    _signalCode        = SIGNAL_CODE.SELL;
                    _tradingStart      = updateTime;
                    _tradingStartValue = value.Bid;
                    _tradingTrend      = _trend.TimeSeries.Last().Bid;
                    if (curRsi.Bid >= getSellThreshold())
                    {
                        Log.Instance.WriteEntry(_id + " sell event due to RSI >= getSellThreshold()");
                    }
                    //                    else if (Math.Abs(curRsi.Bid - rsiMax) < _startThreshold)
                    //                      Log.Instance.WriteEntry(_id + " sell event due highest RSI peak reached");
                    else
                    {
                        Log.Instance.WriteEntry(_id + " sell event due to adjusted RSI >= getSellThreshold()");
                    }
                    return(true);
                }
                else if ((curRsi.Bid <= getBuyThreshold()) &&
                         //(curRsi.Bid + rsiAdjustment <= getBuyThreshold() && curRsi.Bid <= (getBuyThreshold() + 10))) && (rsiLongMin <= getBuyThreshold() + 15m) &&
                         (curRsi.Bid < rsiMin + _startThreshold) && (curEmaVeryShort.Mid() - _maxShortMacDSpread < curEmaLong.Mid()) &&
                         (Math.Abs(curEmaShort.Mid() - curEmaLong.Mid()) < _maxLongMacDSpread))
                {
                    var curVolTrend = _volTrend.TimeSeries.Last().Bid;
                    if (curVolTrend < -0.5m || curVolTrend > 0m)
                    {
                        WriteBlockReason(updateTime, "deal blocked due to vol not within -0.5 < trend < 0");
                        return(false);
                    }
                    if (!_volTrendTrend.IsMax(2, curVolTrend, 0.01m) || _volTrendTrend.IsMin(2, curVolTrend, 0.02m))
                    {
                        WriteBlockReason(updateTime, "buy event blocked due to vol trend not 1mn maximum");
                        return(false);
                    }
                    tradingOrder       = _onBuy;
                    _signalCode        = SIGNAL_CODE.BUY;
                    _tradingStart      = updateTime;
                    _tradingStartValue = value.Offer;
                    _tradingTrend      = _trend.TimeSeries.Last().Bid;
                    if (curRsi.Bid <= getBuyThreshold())
                    {
                        Log.Instance.WriteEntry(_id + " buy event due to RSI <= getBuyThreshold()");
                    }
                    //                   else if (Math.Abs(curRsi.Bid - rsiMin) < _startThreshold)
                    //                     Log.Instance.WriteEntry(_id + " buy event due lowest RSI peak reached");
                    else
                    {
                        Log.Instance.WriteEntry(_id + " buy event due to adjusted RSI <= getBuyThreshold()");
                    }
                    return(true);
                }
                else
                {
                    _signalCode        = SIGNAL_CODE.HOLD;
                    _tradingStart      = DateTime.MinValue;
                    _tradingStartValue = 0m;
                }
            }
            return(false);
        }
Esempio n. 6
0
        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));
            }
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        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));
            }
        }