Пример #1
0
        protected void SetupIndicators()
        {
            if (SlowMaPeriod <= FastMaPeriod)
            {
                throw new ArgumentOutOfRangeException("SlowMaPeriod",
                    "Slow MA period must be greater than fast MA period");
            }

            CurrentExecutionBarSeries = GetBars(BarType.Time, CurrentExecutionTimePeriodInSeconds);
            SlowEmaIndicator = new EMA(CurrentExecutionBarSeries, SlowMaPeriod, Color.Orange);
            FastEmaIndicator = new EMA(CurrentExecutionBarSeries, FastMaPeriod, Color.Cyan);
            FastEmaIndicator.Color = Color.Cyan;

            Draw(SlowEmaIndicator, 0);
            Draw(FastEmaIndicator, 0);

            KSlowIndicator = new K_Slow(CurrentExecutionBarSeries, StochasticsKPeriod, StochasticsSmoothPeriod, Color.Yellow);
            DSlowIndicator = new D_Slow(CurrentExecutionBarSeries, StochasticsKPeriod, StochasticsDPeriod,
                StochasticsSmoothPeriod,
                Color.Red);

            Draw(KSlowIndicator, 2);
            Draw(DSlowIndicator, 2);

            CurrentDailyBarSeries = GetBars(BarType.Time, PeriodConstants.PERIOD_DAILY);
            DailyAtrIndicator = new ATR(CurrentDailyBarSeries, AtrPeriod, Color.Wheat);
            DailyVolumeSmaIndicator = new SMA(CurrentDailyBarSeries, 20, BarData.Volume);
            DailyPriceSmaIndicator = new SMA(CurrentDailyBarSeries, 20, BarData.Close);
            Draw(DailyAtrIndicator, 3);
        }
Пример #2
0
        protected ATR GetAtr(Instrument instrument, int period)
        {
            BarSeries dailyBarSeries = null;

            string instId = instrument.ToIdentifier();
            dailyBarSeriesDictionary.TryGetValue(instId, out dailyBarSeries);

            if (dailyBarSeries == null || dailyBarSeries.Count <= 0)
                throw new ApplicationException("Daily bar series has not been initialized");

            string atrId = string.Format("{0}:{1}", instId, period);

            if (!atrDictionary.ContainsKey(atrId))
            {
                lock (LockObject)
                {
                    if (!atrDictionary.ContainsKey(atrId))
                    {
                        ATR atr = new ATR(dailyBarSeries, period);
                        atrDictionary.Add(atrId, atr);
                    }
                }
            }

            return atrDictionary[atrId];
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="atrPeriod"></param>
        /// <param name="triggerTime"></param>
        /// <returns></returns>
        private double GetAtrValue(int atrPeriod, DateTime triggerTime, double price)
        {
            ATR atr = new ATR(DailyBarSeries, atrPeriod);
            double returnValue = 0;
            int idx = atr.Count - 1;
            bool found = false;

            while (!found && idx >= 0)
            {
                if (atr.GetDateTime(idx) < triggerTime)
                {
                    found = true;
                    returnValue = atr[idx];
                    break;
                }
                idx--;
            }

            if (!found || returnValue <= 0)
                throw new ApplicationException(string.Format("Count not retrieve an ATR for {0} before bar {1}. If it is due to exchange holidays - then set the 'DaysToGoBackForMinutelyData' parameter to fetch more data.", Instrument.Symbol, triggerTime));

            LoggingUtility.WriteInfo(LoggingConfig, string.Format("Found ATR value of {0:c} ({2:p}) as of {1}", returnValue, atr.GetDateTime(idx), returnValue / price ));

            return returnValue;
        }