Пример #1
0
        public void Equals_WithDifferentSignal_ReturnsFalse()
        {
            var signal1 = new BuySignal(4, 7);
            var signal2 = new BuySignal(4, 8);

            Assert.IsFalse(signal1.Equals(signal2));
        }
Пример #2
0
        public void Equals_WithSimilarSignal_ReturnsTrue()
        {
            var signal1 = new BuySignal(4, 7);
            var signal2 = new BuySignal(4, 7);

            Assert.IsTrue(signal1.Equals(signal2));
        }
Пример #3
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            if (CurrentBar < rSILen + 10)
            {
                return;
            }

            CummRSI_DS[1] = CummRSI;
            CummRSI       = 0;

            for (int i = 0; i < numDaysX; i++)
            {
                CummRSI = CummRSI + RSI(Close, 2, 1)[i];
            }

            CummRSI_DS[0] = CummRSI;

            //CummRSIPlot.Set(CummRSI);
            //Print("CummRSI = " + CummRSI + "  CummRSI_DS[1] = " + CummRSI_DS[1] + " CummRSI_DS[0] = " + CummRSI_DS[0]);
            if ((CummRSI_DS[1] >= cummRSIThres) && (CummRSI_DS[0] < cummRSIThres))
            {
                BuySignal.Set(5);
            }
            else
            {
                BuySignal.Set(0);
            }
        }
Пример #4
0
        public void Strength_Buy100Neutral50_ReturnsBuy25()
        {
            var combinedSignal = new DefensiveCombinedSignal(new BuySignal(100), new NeutralSignal(50));

            var expectedSignal = new BuySignal(25);

            Assert.That(combinedSignal, Is.EqualTo(expectedSignal));
        }
Пример #5
0
        public void Weigth_WhenCalled_SignalWithAdjustedStrengthIsReturned()
        {
            var signal = new BuySignal(4);

            var weightedSignal = signal.Weight(0.5);

            Assert.That(weightedSignal.Strength.Value, Is.EqualTo(2));
        }
Пример #6
0
        public void Strength_Buy100Sell0_ReturnsBuy25()
        {
            var combinedSignal = new OffensiveCombinedSignal(new BuySignal(100), new SellSignal(0));

            var expectedSignal = new BuySignal(25);

            Assert.That(combinedSignal, Is.EqualTo(expectedSignal));
        }
Пример #7
0
        public async Task Execute(IReadOnlyList <Candle> candles)
        {
            var buy  = BuySignal.BuildExpression();
            var sell = SellSignal.BuildExpression();

            // TODO: Add live and backtest modes, backtests run the full collection, live only calculates the last candle
            for (int i = 24; i < candles.Count; i++)
            {
                try
                {
                    var candle = candles[i];

                    if (buy.Invoke(candles, i) &&
                        await _shouldBuy.Invoke(Trades, candle))
                    {
                        //How to get buy amount from user?
                        var v = Trades.Any() && Trades.Last().Direction == TradeDirection.Sell ? Trades.Last().VolumeEur : InitialInvestment;

                        Trades.Add(new Trade()
                        {
                            TradeDate = candle.CloseDate,
                            Direction = TradeDirection.Buy,
                            Price     = candle.Close,
                            Volume    = Trades.Any() ? Trades.Last().VolumeEur / candle.Close : InitialInvestment / candle.Close,
                            VolumeEur = Trades.Any() ? Trades.Last().VolumeEur : InitialInvestment
                        });
                    }

                    //TODO: Add sell signal object with perc
                    //TODO: support 2 types of api, one with delegate and other with expression
                    if (sell.Invoke(candles, i) &&
                        await _shouldSell.Invoke(Trades, candle))
                    {
                        //How to get sell amount from user?
                        var v = Trades.Any() && Trades.Last().Direction == TradeDirection.Buy ? Trades.Last().Volume : InitialInvestment;
                        Trades.Add(new Trade()
                        {
                            TradeDate = candle.CloseDate,
                            Direction = TradeDirection.Sell,
                            Price     = candle.Close,
                            Volume    = v,
                            VolumeEur = v * candle.Close,
                        });
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message + ex.StackTrace);
                    throw;
                }
            }

            if (Trades.Any())
            {
                ProfitEur  = Trades.Last().Direction == TradeDirection.Sell ? Trades.Last().VolumeEur : Trades.Last().Volume *candles[candles.Count - 1].Close;
                ProfitCoin = Trades.Last().Direction == TradeDirection.Buy ? Trades.Last().Volume : Trades.Last().VolumeEur / candles[candles.Count - 1].Close;
            }
        }
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            // Use this method for calculating your indicator values. Assign a value to each
            // plot below by replacing 'Close[0]' with your own formula.
            double currentMacd;
            bool   buySignal1;
            bool   buySignal2;
            bool   buySignal3;
            bool   buySignal4;

            currentMacd = MACD(Close, mACDShortLen, mACDLongLen, 9)[0];

            if (CrossBelow(MACD(Close, mACDShortLen, mACDLongLen, 9), 0.03, 1))
            {
                macdDropUpperThres[0] = 1;
            }
            else
            {
                macdDropUpperThres[0] = 0;
            }

            if (CrossAbove(MACD(Close, mACDShortLen, mACDLongLen, 9), 0.02, 1))
            {
                macdCrossUpperThres[0] = 1;
            }
            else
            {
                macdCrossUpperThres[0] = 0;
            }


            buySignal3 = !CrossBelow(MACD(Close, mACDShortLen, mACDLongLen, 9), -0.06, 15);
            buySignal4 = EMA(Close, 20)[0] > EMA(Close, 100)[0];

            if (CrossAbove(macdDropUpperThres, 0.5, 10) && macdCrossUpperThres[0] == 1)
            {
                BuySignal.Set(Low[0]);
            }
            //if(macdDropUpperThres[0]==1)
            // SellSignal.Set(Low[0]);
        }
Пример #9
0
        void blt_GotNewBar(string symbol, int interval)
        {
            int  idx    = _active.getindex(symbol);
            Tick tOther = _kt[symbol];

            // calculate the TD_combination using high/low/close prices for so many bars back
            BarList _myBars = _blt[symbol, interval];

            // make sure the lastBar is full
            //Bar _lastBar = _myBars[_myBars.Count - 2];
            //Bar _secondBar = _myBars[_myBars.Count - 3];
            //Bar _thirdBar = _myBars[_myBars.Count - 4];
            //Bar _forthBar = _myBars[_myBars.Count - 5];
            //Bar _sixthBar = _myBars[_myBars.Count - 7];
            //Bar _seventhBar = _myBars[_myBars.Count - 8];
            //Bar _fifthBar = _myBars[_myBars.Count - 6];
            //Bar _ninthBar = _myBars[_myBars.Count - 10];
            //Bar _eighthBar = _myBars[_myBars.Count - 9];
            for (int i = 1; i <= 15; i++)
            {
                _barVec[i] = _myBars[_myBars.Count - 1 - i];
            }
            if (UseTD_combinationFromATSGlobalIndicator)
            {
                _TD_combinationFromATSGlobalIndicator.UpdateValue(_barVec);
                BuySignal  = _TD_combinationFromATSGlobalIndicator.GetBuySignal();
                SellSignal = _TD_combinationFromATSGlobalIndicator.GetSellSignal();
            }
            //else
            //{
            //    SMA = Calc.Avg(Calc.EndSlice(_blt[symbol].Open(), _barsback));
            //}
            // wait until we have the TD_combination+ and the TD_combination-
            //if (BuySignal == 0 && SellSignal == 0)
            //    return;


            //ensure we aren't waiting for previous order to fill
            if (!_wait[symbol])
            {
                // if we're flat and not waiting
                // if TD_combination+ is above TD_combination-, buy
                if ((Money_state == 0 && BuySignal == 1))
                {
                    Money_state = 1;
                    D("TD_BuySignal appears, buy");
                    //sendorder(new BuyMarket(symbol, EntrySize));
                    _side = true;
                    _adj  = (_side ? -1 : +1) * _quasiMarketOrderAdjSize;
                    Int64      _orderidLocal = _idtLocal.AssignId;
                    LimitOrder lOrder        = new LimitOrder(symbol, _side, EntrySize, tOther.trade - _adj, _orderidLocal);
                    sendorder(lOrder);
                    // wait for fill
                    _wait[symbol] = true;
                }
                // otherwise if TD_combination+ is above TD_combination-, sell
                if (Money_state == 1 && SellSignal == 1)
                {
                    Money_state = 0;
                    D("TD_SellSignal appears, sell");
                    //sendorder(new SellMarket(symbol, EntrySize));
                    _side = false;
                    _adj  = (_side ? -1 : +1) * _quasiMarketOrderAdjSize;
                    Int64      _orderidLocal = _idtLocal.AssignId;
                    LimitOrder lOrder        = new LimitOrder(symbol, _side, EntrySize, tOther.trade - _adj, _orderidLocal);
                    sendorder(lOrder);
                    // wait for fill
                    _wait[symbol] = true;
                }
            }

            // this way we can debug our indicators during development
            // indicators are sent in the same order as they are named above
            sendindicators(new string[] { _time.ToString(),
                                          BuySignal.ToString("F5", System.Globalization.CultureInfo.InvariantCulture),
                                          SellSignal.ToString("F5", System.Globalization.CultureInfo.InvariantCulture),
                                          _barVec[1].High.ToString("F5", System.Globalization.CultureInfo.InvariantCulture),
                                          _barVec[1].Low.ToString("F5", System.Globalization.CultureInfo.InvariantCulture),
                                          _barVec[1].Close.ToString("F5", System.Globalization.CultureInfo.InvariantCulture),
                                          //_previousBar.High.ToString("F5", System.Globalization.CultureInfo.InvariantCulture),
                                          //_previousBar.Low.ToString("F5", System.Globalization.CultureInfo.InvariantCulture),
                                          //_previousBar.Close.ToString("F5", System.Globalization.CultureInfo.InvariantCulture)
                           });
        }
Пример #10
0
        public void BuySignal_WhenCreated_StrengthIs50()
        {
            var signal = new BuySignal();

            Assert.That(signal.Strength.Value, Is.EqualTo(50));
        }
Пример #11
0
        public void Ctor_WithoutQuality_QualitySetTo100Percent()
        {
            var signal = new BuySignal(45);

            Assert.That(signal.Quality, Is.EqualTo(Percentage.Hundred));
        }