Example #1
0
 public IndicatorWMA(IndicatorWMA indicator)
     : base(indicator.Id, new List <MarketData> {
     indicator.SignalStock
 })
 {
     _subPeriodSeconds   = indicator.Period;
     _periodMilliSeconds = _subPeriodSeconds * 1000m;
     _timeDecay          = indicator.TimeDecay;
 }
Example #2
0
 public IndicatorWMA(string id, MarketData mktData, int periodMinutes)
     : base(id, new List <MarketData> {
     mktData
 })
 {
     _subPeriodSeconds   = periodMinutes * 60;
     _periodMilliSeconds = _subPeriodSeconds * 1000m;
     if (Config.Settings.ContainsKey("TIME_DECAY_FACTOR"))
     {
         _timeDecay = new TimeDecayLinear(int.Parse(Config.Settings["TIME_DECAY_FACTOR"]), new TimeSpan(0, periodMinutes, 0));
     }
     else
     {
         _timeDecay = new TimeDecayNull(new TimeSpan(0, periodMinutes, 0));
     }
 }
Example #3
0
        protected virtual bool MobileAverage(ref Price curavg, DateTime startTime, DateTime updateTime)
        {
            bool      started       = false;
            TimeDecay timeDecay     = _timeDecay;
            DateTime  originTime    = startTime;
            DateTime  startTimePrev = DateTime.MinValue;
            IEnumerable <KeyValuePair <DateTime, Price> > generator = SignalStock.TimeSeries.ValueGenerator(startTime, updateTime, false);
            KeyValuePair <DateTime, Price> beginPeriodValue         = new KeyValuePair <DateTime, Price>();
            decimal curTimeDecayWeight = 1m;

            foreach (var endPeriodValue in generator)
            {
                if (!started)
                {
                    started = true;
                    var diffTime = (int)(startTime - endPeriodValue.Key).TotalMilliseconds;
                    if (diffTime < 0)
                    {
                        return(false);
                    }
                    startTimePrev    = endPeriodValue.Key;
                    beginPeriodValue = new KeyValuePair <DateTime, Price>(startTime, endPeriodValue.Value);
                    _timeDecay.Update(beginPeriodValue.Key, endPeriodValue.Key, updateTime);
                    continue;
                }
                curTimeDecayWeight = _timeDecay.Update(beginPeriodValue.Key, endPeriodValue.Key, updateTime);
                curavg            += beginPeriodValue.Value * curTimeDecayWeight;
                beginPeriodValue   = endPeriodValue;
            }
            if (beginPeriodValue.Value != null && beginPeriodValue.Key != updateTime)
            {
                curavg += beginPeriodValue.Value * curTimeDecayWeight;
            }
            return(true);

            /*
             * bool started = false;
             * DateTime startTimePrev = DateTime.MinValue;
             * IEnumerable<KeyValuePair<DateTime, Price>> generator = MarketData.TimeSeries.ValueGenerator(startTime, updateTime);
             * KeyValuePair<DateTime, Price> beginPeriodValue = new KeyValuePair<DateTime, Price>();
             * var tmp = new StreamWriter(File.OpenWrite(string.Format("tmp_{0}_{1}.csv", updateTime.Minute, updateTime.Second)));
             * decimal curTimeDecayWeight = 1m;
             * if (!incremental)
             * {
             *  foreach (var endPeriodValue in generator)
             *  {
             *      if (!started)
             *      {
             *          started = true;
             *          var diffTime = (int)(startTime - endPeriodValue.Key).TotalMilliseconds;
             *          if (!acceptMissingValues && diffTime < 0)
             *          {
             *              tmp.Close();
             *              return false;
             *          }
             *          startTimePrev = endPeriodValue.Key;
             *          beginPeriodValue = new KeyValuePair<DateTime, Price>(startTime, endPeriodValue.Value);
             *          _timeDecay.Update(beginPeriodValue.Key, endPeriodValue.Key, updateTime);
             *          continue;
             *      }
             *      curTimeDecayWeight = _timeDecay.Update(beginPeriodValue.Key, endPeriodValue.Key, updateTime);
             *      tmp.WriteLine(string.Format("{0},{1}", beginPeriodValue.Value.Bid, curTimeDecayWeight));
             *
             *      curavg += beginPeriodValue.Value * curTimeDecayWeight;
             *      beginPeriodValue = endPeriodValue;
             *  }
             *  if (beginPeriodValue.Value != null && beginPeriodValue.Key != updateTime)
             *      curavg += beginPeriodValue.Value * curTimeDecayWeight;
             * }
             * else
             * {
             *  IEnumerable<KeyValuePair<DateTime, Price>> generatorTimeDecay = _timeDecay.TimeDecaySeries.ValueGenerator(startTime, updateTime);
             *  foreach (var endPeriodValue in Zip(generator, generatorTimeDecay))
             *  {
             *      if (!started)
             *      {
             *          started = true;
             *          var diffTime = (int)(startTime - endPeriodValue.Key.Key).TotalMilliseconds;
             *          if (!acceptMissingValues && diffTime < 0)
             *          {
             *              tmp.Close();
             *              return false;
             *          }
             *          startTimePrev = endPeriodValue.Key.Key;
             *          beginPeriodValue = new KeyValuePair<DateTime, Price>(startTime, endPeriodValue.Key.Value);
             *          continue;
             *      }
             *      else if (endPeriodValue.Key.Key == default(DateTime))
             *          break;
             *      if (incremental)
             *          curTimeDecayWeight = _timeDecay.Update(beginPeriodValue.Key, endPeriodValue.Key.Key, updateTime);
             *      else
             *          curTimeDecayWeight = endPeriodValue.Value.Value.Bid;
             *      tmp.WriteLine(string.Format("{0},{1}", beginPeriodValue.Value.Bid, curTimeDecayWeight));
             *
             *      curavg += beginPeriodValue.Value * curTimeDecayWeight;
             *      weight += curTimeDecayWeight;
             *      beginPeriodValue = endPeriodValue.Key;
             *  }
             *  if (beginPeriodValue.Value != null && beginPeriodValue.Key != updateTime)
             *  {
             *      curavg += beginPeriodValue.Value * _timeDecay.Update(beginPeriodValue.Key, updateTime, updateTime);
             *      weight += curTimeDecayWeight;
             *  }
             * }
             * tmp.Close();
             * return true;*/
        }