Esempio n. 1
0
        decimal GetAverageSRForDays(DateTime time, int days)
        {
            if (pastPerf.Count() < 40)
            {
                return(-1);
            }
            var perf = pastPerf.Where(x => x.EndTime > time.AddDays(-days)).Select(x => x.Value);

            //we need at least 5 values otherwise too random
            if (perf.Count() >= 5)
            {
                return(perf.Average() / perf.StdDev());
            }

            var perf2 = pastPerf.Skip(Math.Max(0, pastPerf.Count() - 5)).Select(x => x.Value);

            return(perf2.Average() / perf.StdDev());
        }
        /// <summary>
        ///      Computes the average value
        /// </summary>
        /// <param name="time"></param>
        /// <param name="input">The data for the calculation</param>
        /// <returns>The average value</returns>
        protected override DoubleArray Forward(long time, DoubleArray input)
        {
            var price = (input[HighIdx] + input[LowIdx]) / 2;

            _high.Add(input[HighIdx]);
            _low.Add(input[LowIdx]);

            // our first data point just return identity
            if (_high.Samples <= _high.Size)
            {
                return(price);
            }

            var hh = _high.Take(_n / 2).Max();
            var ll = _low.Take(_n / 2).Min();
            var n1 = (hh - ll) / (_n / 2);

            hh = _high.Skip(_n / 2).Take(_n / 2).Max();
            ll = _low.Skip(_n / 2).Take(_n / 2).Min();

            var n2 = (hh - ll) / (_n / 2);
            var n3 = (_high.Max() - _low.Min()) / _n;

            double dimen = 0;

            if (n1 + n2 > 0 && n3 > 0)
            {
                dimen = Math.Log((n1 + n2) / n3) / Math.Log(2);
            }

            var alpha = Math.Exp(_w * (dimen - 1));

            if (alpha < .01d)
            {
                alpha = .01d;
            }

            if (alpha > 1)
            {
                alpha = 1;
            }

            return(alpha * price + (1 - alpha) * Current.Value);
        }
Esempio n. 3
0
        public void Scan(TradeBar data)
        {
            var isDecreasingVolume = _volume.Skip(27).Average() < _volume.Average();

            var tb = new TradeBar
            {
                EndTime = data.EndTime,
                Close   = data.Close
            };

            if (_rsi > 70 && !_securityHolding.Invested)
            {
                Signal = SignalType.Short;
            }
            else if (_rsi < 30 && !_securityHolding.Invested)
            {
                Signal = SignalType.Long;
            }
            else
            {
                Signal = SignalType.NoSignal;
            }
        }
        public void OnData(TradeBars data)
        {
            if (!_slow.IsReady)
            {
                _volume.Add(Securities[Symbol].Volume);
                _price.Add(Securities[Symbol].Price);
                return;
            }

            var averageVolume = _volume.Sum() / _volume.Count;
            var volumeSignal  = Securities[Symbol].Volume > averageVolume ||
                                _volume.Skip(_volume.Count - 7).Any(av => av > averageVolume);

            var holdings = Portfolio[Symbol].Quantity;

            if (_openStopMarketOrder != null && _openStopMarketOrder.Status != OrderStatus.Filled)
            {
                if (_openStopMarketOrder.Tag.Equals("long") && _price[0] < Securities[Symbol].Price)
                {
                    var newLongLimit = Securities[Symbol].Price * (1 - 0.05m);

                    _openStopMarketOrder.Update(new UpdateOrderFields
                    {
                        StopPrice = newLongLimit
                    });
                }
                else if (_openStopMarketOrder.Tag.Equals("short") && _price[0] > Securities[Symbol].Price)
                {
                    var newShortLimit = Securities[Symbol].Price * 1.05m;

                    _openStopMarketOrder.Update(new UpdateOrderFields
                    {
                        StopPrice = newShortLimit
                    });
                }
            }
            else if (holdings <= 0 && _fast > _slow * (1 + tolerance) && volumeSignal)
            {
                Log("BUY  >> " + Securities[Symbol].Price);
                //SetHoldings(Symbol, 1.0);

                var orderTicket = StopMarketOrder(Symbol, (int)(Portfolio.Cash / Securities[Symbol].Price), Securities[Symbol].Price * (1 - 0.05m), "long");

                _openStopMarketOrder?.Cancel();
                _openStopMarketOrder = orderTicket;
            }
            else if (holdings >= 0 && _fast < _slow * (1 - tolerance) && volumeSignal)
            {
                Log("SELL >> " + Securities[Symbol].Price);
                //SetHoldings(Symbol, -1.0);

                var shortAmount = Portfolio[Symbol].HoldingsValue == 0 ? Portfolio.Cash : Portfolio[Symbol].HoldingsValue;
                var orderTicket = StopMarketOrder(Symbol, -(int)(shortAmount / Securities[Symbol].Price), Securities[Symbol].Price * 1.05m, "short");

                _openStopMarketOrder?.Cancel();
                _openStopMarketOrder = orderTicket;
            }

            _volume.Add(Securities[Symbol].Volume);
            _price.Add(Securities[Symbol].Price);

            Plot("SMA Cross", "400", _slow.RollingSum / _slow.Period);
            Plot("SMA Cross", "100", _fast.RollingSum / _fast.Period);
            Plot("SMA Cross", "Price", Securities[Symbol].Price);
        }
Esempio n. 5
0
 public static bool Falling(this RollingWindow <IndicatorDataPoint> window, int lookback = 1, decimal tolerance = 0m)
 {
     return(window.Take(lookback).Zip(window.Skip(1).Take(lookback), (w1, w2) => Tuple.Create(w1, w2)).All((w) => w.Item1 < w.Item2 * (1 - tolerance)));
 }
Esempio n. 6
0
 public static bool Rising(this RollingWindow <decimal> window, int lookback = 1, decimal tolerance = 0m)
 {
     return(window.Take(lookback).Zip(window.Skip(1).Take(lookback), (w1, w2) => Tuple.Create(w1, w2)).All((w) => w.Item1 > w.Item2 * (1 + tolerance)));
 }