Exemple #1
0
        /// <summary>
        /// Computes the next value of this indicator from the given state
        /// </summary>
        /// <param name="input">The input given to the indicator</param>
        /// <returns>A new value for this indicator</returns>
        protected override decimal ComputeNextValue(IBaseDataBar input)
        {
            _adx.Update(input);
            _adxHistory.Add(_adx);

            return((_adx + _adxHistory[Math.Min(_adxHistory.Count - 1, _period - 1)]) / 2);
        }
Exemple #2
0
        /// <summary>
        /// Computes the next value of this indicator from the given state
        /// </summary>
        /// <param name="input">The input given to the indicator</param>
        /// <returns>A new value for this indicator</returns>
        protected override decimal ComputeNextValue(IBaseDataBar input)
        {
            _adx.Update(input);

            if (_adx.IsReady)
            {
                _adxHistory.Add(_adx);
            }

            return(IsReady ? (_adx + _adxHistory[_period - 1]) / 2 : 50m);
        }
        public void ResetsProperly()
        {
            var adxIndicator = new AverageDirectionalIndex("ADX", 14);
            foreach (var data in TestHelper.GetTradeBarStream("spy_with_adx.txt", false))
            {
                adxIndicator.Update(data);
            }

            Assert.IsTrue(adxIndicator.IsReady);

            adxIndicator.Reset();
        }
        /// <summary>
        /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
        /// </summary>
        public override void Initialize()
        {
            // initialize algorithm level parameters
            SetStartDate(2013, 10, 07);
            SetEndDate(2013, 10, 11);
            //SetStartDate(2014, 01, 01);
            //SetEndDate(2014, 06, 01);
            SetCash(100000);

            // leverage tradier $1 traders
            SetBrokerageModel(BrokerageName.TradierBrokerage);

            // request high resolution equity data
            AddSecurity(SecurityType.Equity, Symbol, Resolution.Second);

            // save off our security so we can reference it quickly later
            Security = Securities[Symbol];

            // Set our max leverage
            Security.SetLeverage(MaximumLeverage);

            // define our longer term indicators
            ADX14 = ADX(Symbol, 28, Resolution.Hour);
            STD14 = STD(Symbol, 14, Resolution.Daily);
            ATR14 = ATR(Symbol, 14, resolution: Resolution.Daily);
            PSARMin = new ParabolicStopAndReverse(Symbol, afStart: 0.0001m, afIncrement: 0.0001m);

            // smooth our ATR over a week, we'll use this to determine if recent volatilty warrants entrance
            var oneWeekInMarketHours = (int)(5*6.5);
            SmoothedATR14 = new ExponentialMovingAverage("Smoothed_" + ATR14.Name, oneWeekInMarketHours).Of(ATR14);
            // smooth our STD over a week as well
            SmoothedSTD14 = new ExponentialMovingAverage("Smoothed_"+STD14.Name, oneWeekInMarketHours).Of(STD14);

            // initialize our charts
            var chart = new Chart(Symbol);
            chart.AddSeries(new Series(ADX14.Name));
            chart.AddSeries(new Series("Enter", SeriesType.Scatter));
            chart.AddSeries(new Series("Exit", SeriesType.Scatter));
            chart.AddSeries(new Series(PSARMin.Name, SeriesType.Scatter));
            AddChart(chart);

            var history = History(Symbol, 20, Resolution.Daily);
            foreach (var bar in history)
            {
                ADX14.Update(bar);
                ATR14.Update(bar);
                STD14.Update(bar.EndTime, bar.Close);
            }

            // schedule an event to run every day at five minutes after our Symbol's market open
            Schedule.Event("MarketOpenSpan")
                .EveryDay(Symbol)
                .AfterMarketOpen(Symbol, minutesAfterOpen: OpeningSpanInMinutes)
                .Run(MarketOpeningSpanHandler);

            Schedule.Event("MarketOpen")
                .EveryDay(Symbol)
                .AfterMarketOpen(Symbol, minutesAfterOpen: -1)
                .Run(() => PSARMin.Reset());
        }