Пример #1
0
 private bool IsShortSetup(PriceBars priceBar)
 {
     if (!upperBandPierced)
     {
         upperBandPierced = bb.UpperBand() < priceBar.LastItem.High && bb.UpperBand() > priceBar.LastItem.Close;
         if (upperBandPierced)
         {
             lowerBandPierced     = false;
             piercingBar          = priceBar.LastItem.GetHashCode();
             piercingBarBandWidth = bb.UpperBand() - bb.LowerBand();
         }
     }
     return(upperBandPierced && priceBar.LastItem.GetHashCode() != piercingBar && priceBar.LastItem.Close < bb.UpperBand() && (bb.UpperBand() - bb.LowerBand()) > piercingBarBandWidth);
 }
Пример #2
0
        private bool IsLongSetup(PriceBars priceBar)
        {
            if (!crossedBelow20)
            {
                crossedBelow20 = stoch.PercentD() < 20;
            }

            if (!reached10)
            {
                reached10 = crossedBelow20 && stoch.PercentD() >= 10;
            }

            return(reached10 && stoch.PercentD() > 20);
        }
Пример #3
0
        private bool BearishReversal(PriceBars priceBar, int lookbackPeriod = 1)
        {
            bool hasReversed = false;

            for (int i = 0; i < lookbackPeriod; i++)
            {
                if (psar.NextSAR(i) > priceBar.Last(i).High&& psar.NextSAR(i + 1) < priceBar.Last(i + 1).Low)
                {
                    hasReversed = true;
                    break;
                }
            }
            return(hasReversed);
        }
Пример #4
0
        protected override void Calculate(PriceBars priceAction)
        {
            if (priceChangeCore.HasValue(maxBarIndex))
            {
                double averageChangeUp   = priceChangeCore.Average(barCount, 1);
                double averageChangeDown = priceChangeCore.Average(barCount, 2);

                this.Add(new SingleValueIndicatorItem(
                             priceAction.LastItem.Time,
                             100 - (100 / (1 + (averageChangeUp / averageChangeDown)))
                             ));
                lastCalculationSuccessful = true;
            }
        }
Пример #5
0
        private bool IsShortSetup(PriceBars priceBar)
        {
            if (!crossedAbove80)
            {
                crossedAbove80 = stoch.PercentD() > 80;
            }

            if (!reached90)
            {
                reached90 = crossedAbove80 && stoch.PercentD() >= 90;
            }

            return(reached90 && stoch.PercentD() <= 80);
        }
Пример #6
0
        protected override void OutPosition(PriceBars priceBar, BarItemType barType)
        {
            if (CrossesAbove(macd.MACDValue(1), macd.MACDValue(), macd.SignalLine(1), macd.SignalLine()))
            {
                buySignal++;
                sellSignal = 0;
            }
            else if (CrossesBelow(macd.MACDValue(1), macd.MACDValue(), macd.SignalLine(1), macd.SignalLine()))
            {
                sellSignal++;
                buySignal = 0;
            }

            if (psar.Direction(1) == -1 && psar.Direction() == 1)
            {
                buySignal++;
                sellSignal = 0;
            }
            else if (psar.Direction(1) == 1 && psar.Direction() == -1)
            {
                sellSignal++;
                buySignal = 0;
            }

            if (buySignal > 0 || sellSignal > 0)
            {
                barSignalCount++;
            }

            if (barSignalCount > signalLookbackPeriod)
            {
                buySignal      = 0;
                sellSignal     = 0;
                barSignalCount = 0;
            }

            if (IsLongSetup(priceBar))
            {
                Enter(PositionMode.Long);
                buySignal  = 0;
                sellSignal = 0;
            }
            else if (IsShortSetup(priceBar))
            {
                Enter(PositionMode.Short);
                buySignal  = 0;
                sellSignal = 0;
            }
        }
Пример #7
0
        public void PriceBasicTest()
        {
            _priceBars      = new PriceBars(100);
            _priceBarReader = new PriceBarReader(_priceBars);

            TestFeedFactory testFeed = new TestFeedFactory();

            _maxwellFeed = testFeed.CreateMaxwellFeed(IntervalTypeOption.M1, DateTime.Now);

            _maxwellFeed.PushFeed += OnPushFeed;

            EMAOfPrices ema = new EMAOfPrices(3);

            ProbeReader <float, float> reader = ema.GetReader();

            IPriceEvaluate evaluator = ema;

            _maxwellFeed.PushNext(1);

            evaluator.Evaluate(_priceBarReader);

            Assert.AreEqual(0, reader.Count);

            _maxwellFeed.PushNext(1);

            evaluator.Evaluate(_priceBarReader);

            Assert.AreEqual(0, reader.Count);

            _maxwellFeed.PushNext(1);

            evaluator.Evaluate(_priceBarReader);

            Assert.AreEqual(0, reader.Count);

            _maxwellFeed.PushNext(1);

            evaluator.Evaluate(_priceBarReader);

            Assert.AreEqual(1, reader.Count);
            Assert.AreEqual(4, reader.Current);

            _maxwellFeed.PushNext(1);

            evaluator.Evaluate(_priceBarReader);

            Assert.AreEqual(2, reader.Count);
            Assert.AreEqual(4, reader.Current);
        }
Пример #8
0
 protected override void OutPosition(PriceBars priceBar, BarItemType barType)
 {
     if (IsLongSetup(priceBar))
     {
         Enter(PositionMode.Long);
         stopLossPrice   = priceBar.LastItem.Close - (20 * 0.0001);
         enteredPosition = true;
     }
     else if (IsShortSetup(priceBar))
     {
         Enter(PositionMode.Short);
         stopLossPrice   = priceBar.LastItem.Close + (20 * 0.0001);
         enteredPosition = true;
     }
 }
Пример #9
0
        protected override void Calculate(PriceBars priceAction)
        {
            if (dxCore.HasValue(maxBarIndex))
            {
                double average = dxCore.Average(barCount);

                this.Add(new MultiValueIndicatorItem(
                             priceAction.LastItem.Time,
                             average,
                             dxCore.LastItem.Values[DirectionalMovementIndexCore.PLUS_DI_FIELD_INDEX],
                             dxCore.LastItem.Values[DirectionalMovementIndexCore.MINUS_DI_FIELD_INDEX]
                             ));
                lastCalculationSuccessful = true;
            }
        }
Пример #10
0
        protected override void OutPosition(PriceBars priceBar, BarItemType barType)
        {
            if (priceBar.LastItem.Time >= new DateTime(2012, 8, 2, 4, 0, 0))
            {
            }

            if (IsLongSetup(priceBar))
            {
                Enter(PositionMode.Long);
            }
            else if (IsShortSetup(priceBar))
            {
                Enter(PositionMode.Short);
            }
        }
Пример #11
0
 protected override void OutPosition(PriceBars priceBar, BarItemType barType)
 {
     if (IsLongSetup(priceBar))
     {
         Enter(PositionMode.Long);
         crossedBelow20 = false;
         reached10      = false;
     }
     else if (IsShortSetup(priceBar))
     {
         Enter(PositionMode.Short);
         crossedAbove80 = false;
         reached90      = false;
     }
 }
Пример #12
0
 protected override void OutPosition(PriceBars priceBar, BarItemType barType)
 {
     if (IsLongSetup(priceBar))
     {
         Enter(PositionMode.Long);
         lowerBandPierced = false;
         upperBandPierced = false;
     }
     else if (IsShortSetup(priceBar))
     {
         Enter(PositionMode.Short);
         lowerBandPierced = false;
         upperBandPierced = false;
     }
 }
Пример #13
0
        protected override void Calculate(PriceBars priceAction)
        {
            if (priceAction.HasValue(maxBarIndex))
            {
                double previousEMA = HasValue() ? LastItem.Value : priceAction.Average(barCount);
                double ema         = (priceAction.LastItem.Close * smoothingConstant) + (previousEMA * (1 - smoothingConstant));

                this.Add(new SingleValueIndicatorItem(
                             priceAction.LastItem.Time,
                             ema //Exponential Moving Average
                             ));

                lastCalculationSuccessful = true;
            }
        }
Пример #14
0
        protected override void Calculate(PriceBars priceAction)
        {
            if (priceAction.HasValue(maxBarIndex))
            {
                double change = priceAction.LastItem.Close - priceAction.Last(1).Close;

                this.Add(new MultiValueIndicatorItem(
                             priceAction.LastItem.Time,
                             change,                          //Raw change
                             change > 0? change: 0,           //Change Up
                             change <= 0? Math.Abs(change): 0 //Change down
                             ));
                lastCalculationSuccessful = true;
            }
        }
Пример #15
0
        protected override void Calculate(PriceBars priceAction)
        {
            if (priceAction.HasValue(maxBarIndex) && tpmaCore.HasValue(maxBarIndex))
            {
                double smatp         = tpmaCore.LastItem.Value;
                double meanDeviation = GetMeanDeviation(priceAction);
                double cci           = (priceAction.LastItem.TypicalPrice() - smatp) / (0.015 * meanDeviation);
                this.Add(new SingleValueIndicatorItem(
                             priceAction.LastItem.Time,
                             cci
                             ));

                lastCalculationSuccessful = true;
            }
        }
Пример #16
0
        private bool IsShortSetup(PriceBars priceBar)
        {
            if (!crossedBelow25)
            {
                crossedBelow25 = CrossesBelow(ema10.Value(1), ema10.Value(), ema25.Value(1), ema25.Value());
            }

            bool crossedBelow50 = crossedBelow25 && CrossesBelow(ema10.Value(1), ema10.Value(), ema50.Value(1), ema50.Value());

            if (crossedBelow50)
            {
                crossedAbove25 = false;
            }

            return(crossedBelow50);
        }
Пример #17
0
        private bool IsShortSetup(PriceBars priceBar)
        {
            if (!crossedBelow14)
            {
                crossedBelow14 = CrossesBelow(sma7.Value(1), sma7.Value(), sma14.Value(1), sma14.Value());
            }

            bool crossedBelow21 = crossedBelow14 && CrossesBelow(sma7.Value(1), sma7.Value(), sma21.Value(1), sma21.Value());

            if (crossedBelow21)
            {
                crossedAbove14 = false;
            }

            return(crossedBelow21);
        }
Пример #18
0
        protected override void Calculate(PriceBars priceAction)
        {
            if (priceAction.HasValue(maxBarIndex))
            {
                BarItem currentPrice  = priceAction.LastItem;
                BarItem previousPrice = priceAction.Last(1);

                double trueRange = Math.Max(currentPrice.High - currentPrice.Low,
                                            Math.Max(Math.Abs(currentPrice.High - previousPrice.Close), Math.Abs(currentPrice.Low - previousPrice.Close)));

                this.Add(new SingleValueIndicatorItem(
                             priceAction.LastItem.Time,
                             trueRange
                             ));
                lastCalculationSuccessful = true;
            }
        }
Пример #19
0
        public void TestBookmarkedPriceBarWritesShouldWork()
        {
            PriceBars      priceBars = new PriceBars(10);
            PriceBarReader reader    = new PriceBarReader(priceBars);

            priceBars.NextPoolItem.Write(DateTime.Now, 1, 2, 3, 4);
            priceBars.MoveNext();

            Bookmark <IPriceBar> bookmark = reader.GetBookmark();

            Assert.AreEqual(priceBars.Current.Open, bookmark.Current.Open);

            priceBars.NextPoolItem.Write(DateTime.Now, 5, 6, 7, 8);
            priceBars.MoveNext();

            Assert.AreNotEqual(priceBars.Current.Open, bookmark.Current.Open);
        }
Пример #20
0
        public void RecurseCalculationDependencies(ICoreIndicator coreIndicator, PriceBars priceAction)
        {
            foreach (CalculationDependencyItem dependency in coreIndicator.Dependencies)
            {
                ICoreIndicator subIndicator = indicators[dependency.BarType.Code].CoreIndicators[dependency.Identity.IdentityCode];
                List <CalculationDependencyItem> subDependencies = subIndicator.Dependencies;
                if (subDependencies.Count > 0)
                {
                    RecurseCalculationDependencies(subIndicator, priceAction);
                }
            }

            if (!coreIndicator.PriceAction.HasCalculated(priceAction))
            {
                coreIndicator.PriceAction.Calculator(priceAction);
            }
        }
Пример #21
0
        protected override void Calculate(PriceBars priceAction)
        {
            if (priceAction.HasValue(maxBarIndex))
            {
                BarItem currentPrice  = priceAction.LastItem;
                BarItem previousPrice = priceAction.Last(1);

                double plusDM  = (currentPrice.High - previousPrice.High) > (previousPrice.Low - currentPrice.Low) ? Math.Max(currentPrice.High - previousPrice.High, 0) : 0;
                double minusDM = (previousPrice.Low - currentPrice.Low) > (currentPrice.High - previousPrice.High) ? Math.Max(previousPrice.Low - currentPrice.Low, 0) : 0;

                this.Add(new MultiValueIndicatorItem(
                             priceAction.LastItem.Time,
                             plusDM,
                             minusDM
                             ));
                lastCalculationSuccessful = true;
            }
        }
Пример #22
0
        protected override void OutPosition(PriceBars priceBar, BarItemType barType)
        {
            if (CrossesAbove(sma10.Value(1), sma10.Value(), sma50.Value(1), sma50.Value()))
            {
                trend = TrendDirectionMode.Bullish;
            }
            else if (CrossesBelow(sma10.Value(1), sma10.Value(), sma50.Value(1), sma50.Value()))
            {
                trend = TrendDirectionMode.Bearish;
            }

            if (IsLongSetup(priceBar))
            {
                Enter(PositionMode.Long);
            }
            else if (IsShortSetup(priceBar))
            {
                Enter(PositionMode.Short);
            }
        }
Пример #23
0
        private bool ExitShort(PriceBars priceBar)
        {
            bool emaCross = CrossesAbove(ema9.Value(1), ema9.Value(), ema26.Value(1), ema26.Value()) && adx.PlusDI() > adx.MinusDI();

            if (!emaCross)
            {
                bool diCross = CrossesAbove(adx.PlusDI(1), adx.PlusDI(), adx.MinusDI(1), adx.MinusDI()) || CrossesBelow(adx.PlusDI(1), adx.PlusDI(), adx.MinusDI(1), adx.MinusDI());
                if (diCross)
                {
                    return(false);
                }

                if (priceBar.LastItem.High > ema9.Value() && priceBar.LastItem.Low < ema9.Value())
                {
                    return(false);
                }
            }

            return(emaCross && adx.MinusDI() > adx.PlusDI());
        }
Пример #24
0
        protected override void Calculate(PriceBars priceAction)
        {
            if (priceAction.HasValue(maxBarIndex) && slowEMA.HasValue(maxBarIndex))
            {
                double macd      = fastEMA.LastItem.Value - slowEMA.LastItem.Value;
                double emaOfMACD = double.NaN;
                if (HasValue(signalLinePeriod - 1, 0))
                {
                    double previousEMA = double.IsNaN(LastItem.Values[SIGNAL_LINE_FIELD_INDEX]) ? this.Average(signalLinePeriod, 0) : LastItem.Values[SIGNAL_LINE_FIELD_INDEX];
                    emaOfMACD = (macd * signalLineSmoothingConstant) + (previousEMA * (1 - signalLineSmoothingConstant));
                }

                this.Add(new MultiValueIndicatorItem(
                             priceAction.LastItem.Time,
                             macd,
                             emaOfMACD,
                             double.IsNaN(emaOfMACD)? double.NaN: macd - emaOfMACD //Histogram
                             ));
                lastCalculationSuccessful = true;
            }
        }
Пример #25
0
        protected override void OutPosition(PriceBars priceBar, BarItemType barType)
        {
            if (CrossesAbove(ema5.Value(1), ema5.Value(), ema10.Value(1), ema10.Value()))
            {
                ema5CrossedAboveEMA10 = true;
                emaCrossed            = true;
            }
            else if (CrossesBelow(ema5.Value(1), ema5.Value(), ema10.Value(1), ema10.Value()))
            {
                ema5CrossedAboveEMA10 = false;
                emaCrossed            = true;
            }

            if (IsLongSetup(priceBar))
            {
                Enter(PositionMode.Long);
            }
            else if (IsShortSetup(priceBar))
            {
                Enter(PositionMode.Short);
            }
        }
Пример #26
0
        public void ReceivePriceAction(BarItemType barType, PriceBars priceAction)
        {
            lastPriceBar = priceAction.LastItem;

            foreach (CoreSignal core in signals[barType.Code].CoreSignals.Values)
            {
                if (this.cachingEnabled)
                {
                    UpdateCache(priceAction.LastItem.Time, core.Signal.RegisteredAnalytics);
                }

                if (core.Signal.IsInPosition)
                {
                    core.Signal.InPositionState(priceAction, barType);
                }

                if (!core.Signal.IsInPosition)
                {
                    core.Signal.OutPositionState(priceAction, barType);
                }
            }
        }
Пример #27
0
        protected override void Calculate(PriceBars priceAction)
        {
            if (priceAction.HasValue(maxBarIndex) && smaCore.HasValue(maxBarIndex))
            {
                BarItem lastBar = priceAction.LastItem;

                double lastSMA         = this.smaCore.LastItem.Value;
                double sumOfDeviations = 0;
                for (int index = 0; index < barCount; index++)
                {
                    sumOfDeviations += Math.Pow(priceAction.Last(index).Close - smaCore.Last(index).Value, 2);
                }
                double std = Math.Sqrt(sumOfDeviations / barCount);

                this.Add(new MultiValueIndicatorItem(
                             lastBar.Time,
                             lastSMA + (stdMultiplier * std), //Upper band
                             lastSMA,                         //Middle band
                             lastSMA - (stdMultiplier * std)  // Lower band
                             ));
                lastCalculationSuccessful = true;
            }
        }
Пример #28
0
 protected override void InPosition(PositionMode position, PriceBars priceBar, BarItemType barType)
 {
     if (bouncedOff)
     {
         if ((position == PositionMode.Long && (priceBar.LastItem.High > bb.UpperBand())) ||
             (position == PositionMode.Short && (priceBar.LastItem.Low < bb.LowerBand())))
         {
             bouncedOff = false;
             bouncedOffPreviousClose = double.NaN;
             Exit();
         }
     }
     else
     {
         if (BouncesOff(position, priceBar))
         {
             lastPosition            = position;
             bouncedOff              = true;
             bouncedOffPreviousClose = priceBar.Last(1).Close;
             Exit();
         }
     }
 }
Пример #29
0
 private bool IsShortSetup(PriceBars priceBar)
 {
     return(stoc.PercentK() < 37 && psar.Direction(1) != psar.Direction() && psar.Direction() == -1 && priceBar.LastItem.Close < ema.Value());
 }
Пример #30
0
 private bool IsLongSetup(PriceBars priceBar)
 {
     return(stoc.PercentK() > 63 && psar.Direction(1) != psar.Direction() && psar.Direction() == 1 && priceBar.LastItem.Close > ema.Value());
 }