Exemplo n.º 1
0
        public override void OnData(Slice slice)
        {
            if (_dailyReturns.Samples == 0)
            {
                SetHoldings(spy, _spyAllocation);
                SetHoldings(tlt, _tltAllocation);
            }
            else if (_dailyReturns.IsReady)
            {
                var highestSharpe = 0m;
                for (decimal spyAllocation = 0.0m, tltAllocation = 1.0m;
                     spyAllocation <= 1.0m;
                     spyAllocation += 0.1m, tltAllocation -= 0.1m)
                {
                    var sharpe = VolatilityScaledSharpeRatio(spyAllocation, tltAllocation);
                    if (sharpe > highestSharpe)
                    {
                        _spyAllocation = spyAllocation;
                        _tltAllocation = tltAllocation;
                    }
                }

                _sd.Reset();
                _dailyReturns.Reset();
                _previousTradeBar = null;
                return;
            }

            if (_previousTradeBar != null)
            {
                UpdateDailyReturn();
            }
            _previousTradeBar = new Tuple <TradeBar, TradeBar>(slice[spy], slice[tlt]);
        }
Exemplo n.º 2
0
 public void Reset()
 {
     // Not resetting the ITrend increases returns
     sma.Reset();
     SMAMomentum.Reset();
     MomentumWindow.Reset();
 }
 public void Reset()
 {
     DecycleTrend.Reset();
     InverseFisher.Reset();
     InvFisherRW.Reset();
     LightSmoothPrice.Reset();
     Momersion.Reset();
 }
Exemplo n.º 4
0
 /// <summary>
 ///      Resets to the initial state
 /// </summary>
 public override void Reset()
 {
     _price.Reset();
     _filt.Reset();
     _filt.Add(0);
     _filt.Add(0);
     base.Reset();
 }
Exemplo n.º 5
0
        public void ResetsProperly()
        {
            var window = new RollingWindow <int>(3)
            {
                0, 1, 2
            };

            window.Reset();
            Assert.AreEqual(0, window.Samples);
        }
Exemplo n.º 6
0
        public void RetievesNonZeroIndexProperlyAfterReset()
        {
            var window = new RollingWindow <int>(3);

            window.Add(0);
            Assert.AreEqual(1, window.Count);
            Assert.AreEqual(0, window[0]);

            window.Add(1);
            Assert.AreEqual(2, window.Count);
            Assert.AreEqual(0, window[1]);
            Assert.AreEqual(1, window[0]);

            window.Add(2);
            Assert.AreEqual(3, window.Count);
            Assert.AreEqual(0, window[2]);
            Assert.AreEqual(1, window[1]);
            Assert.AreEqual(2, window[0]);

            window.Add(3);
            Assert.AreEqual(3, window.Count);
            Assert.AreEqual(1, window[2]);
            Assert.AreEqual(2, window[1]);
            Assert.AreEqual(3, window[0]);

            window.Reset();
            window.Add(0);
            Assert.AreEqual(1, window.Count);
            Assert.AreEqual(0, window[0]);

            window.Add(1);
            Assert.AreEqual(2, window.Count);
            Assert.AreEqual(0, window[1]);
            Assert.AreEqual(1, window[0]);

            window.Add(2);
            Assert.AreEqual(3, window.Count);
            Assert.AreEqual(0, window[2]);
            Assert.AreEqual(1, window[1]);
            Assert.AreEqual(2, window[0]);

            window.Add(3);
            Assert.AreEqual(3, window.Count);
            Assert.AreEqual(1, window[2]);
            Assert.AreEqual(2, window[1]);
            Assert.AreEqual(3, window[0]);
        }
Exemplo n.º 7
0
        public override void Initialize()
        {
            SetStartDate(2015, 1, 1);
            SetEndDate(DateTime.Now);
            SetCash(10000);

            AddSecurity(SecurityType.Equity, spy, _dataResolution);
            AddSecurity(SecurityType.Equity, tlt, _dataResolution);

            _sd           = new StandardDeviation(LookbackPeriod - 1);
            _dailyReturns = new RollingWindow <Tuple <decimal, decimal> >(LookbackPeriod - 1);

            var spyHistory    = History(spy, TimeSpan.FromDays(LookbackPeriod), _dataResolution);
            var tltHistory    = History(tlt, TimeSpan.FromDays(LookbackPeriod), _dataResolution);
            var history       = spyHistory.Zip(tltHistory, (spyBar, tltBar) => new Tuple <TradeBar, TradeBar>(spyBar, tltBar));
            var highestSharpe = 0m;

            for (decimal spyAllocation = 0.0m, tltAllocation = 1.0m; spyAllocation <= 1.0m; spyAllocation += 0.1m, tltAllocation -= 0.1m)
            {
                var previousBar = history.First();

                foreach (var tradeBar in history.Skip(1))
                {
                    var spyReturn = spyAllocation * (tradeBar.Item1.Close - previousBar.Item1.Close) / previousBar.Item1.Close;
                    var tltReturn = tltAllocation * (tradeBar.Item2.Close - previousBar.Item2.Close) / previousBar.Item2.Close;

                    _dailyReturns.Add(new Tuple <decimal, decimal>(spyReturn, tltReturn));
                    _sd.Update(tradeBar.Item1.EndTime, spyReturn + tltReturn);
                    previousBar = tradeBar;
                }

                var mean   = _dailyReturns.Select(dailyReturn => dailyReturn.Item1 + dailyReturn.Item2).Average();
                var sharpe = mean / Convert.ToDecimal(Math.Pow(Convert.ToDouble(_sd), _volatilityFactor));

                if (sharpe > highestSharpe)
                {
                    highestSharpe  = sharpe;
                    _spyAllocation = spyAllocation;
                    _tltAllocation = tltAllocation;
                }

                _sd.Reset();
                _dailyReturns.Reset();
            }
        }
Exemplo n.º 8
0
        public void UpdateWeights()
        {
            var highestSharpe = 0m;

            for (decimal spyAllocation = 0.0m, tltAllocation = 1.0m;
                 spyAllocation <= 1.0m;
                 spyAllocation += 0.1m, tltAllocation -= 0.1m)
            {
                var sharpe = VolatilityScaledSharpeRatio(spyAllocation, tltAllocation);
                if (sharpe > highestSharpe)
                {
                    _spyAllocation = spyAllocation;
                    _tltAllocation = tltAllocation;
                    highestSharpe  = sharpe;
                }
            }

            _sd.Reset();
            _dailyReturns.Reset();
        }
Exemplo n.º 9
0
 public void ResetsProperly()
 {
     var window = new RollingWindow<int>(3) { 0, 1, 2 };
     window.Reset();
     Assert.AreEqual(0, window.Samples);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Resets this instance.
 /// </summary>
 public void Reset()
 {
     fastEMA.Reset();
     slowEMA.Reset();
     EMADiffRW.Reset();
 }
Exemplo n.º 11
0
        /// <summary>
        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// </summary>
        /// <param name="data">TradeBars IDictionary object with your stock data</param>
        public void OnData(TradeBars data)
        {
            #region logging
            comment     = string.Empty;
            tradingDate = this.Time;
            #endregion
            barcount++;
            // Logs a TradeBar to the mylog
            TradeBar        tradebar;
            List <TradeBar> list = new List <TradeBar>();
            foreach (var item in data.Values)
            {
                list.Add(new TradeBar(item.EndTime, item.Symbol, item.Open, item.High, item.Low, item.Close, item.Volume, null));
            }
            string output = JsonConvert.SerializeObject(list);



            string path     = @"C:\Users\Nick\Documents\Visual Studio 2013\Projects\LeanITrend\Engine\bin\Debug\";
            string pathname = path + "BrokerSimulatorTestData.json";
            if (File.Exists(pathname))
            {
                File.Delete(pathname);
            }
            using (StreamWriter sw = new StreamWriter(pathname))
            {
                sw.Write(output);
                sw.Flush();
                sw.Close();
            }



            _brokerSimulator.PricesWindow.Add(data);
            // Add the history for the bar
            var time = this.Time;
            Price.Add(idp(time, (data[symbol].Close + data[symbol].Open) / 2));

            //// Update the indicators
            trend.Update(idp(time, Price[0].Value));
            trendHistory.Add(CalculateNewTrendHistoryValue(barcount, time, Price, trend));
            #region lists

            foreach (var listitem in trendHistoryList)
            {
                trendList[listitem.Key].Update(idp(time, Price[0].Value));
                listitem.Value.Add(idp(time, CalculateNewTrendHistoryValue(barcount, time, Price, trendList[listitem.Key])));
            }

            #endregion
            if (Portfolio[symbol].Invested)
            {
                tradesize = Math.Abs(Portfolio[symbol].Quantity);
            }
            else
            {
                tradesize = (int)(_transactionSize / Convert.ToInt32(Price[0].Value + 1));
            }


            string matrix = GetTradingSignals(data);

            #region logging
            sharesOwned = Portfolio[symbol].Quantity;
            string logmsg =
                string.Format(
                    "{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23}",
                    this.Time,
                    barcount,
                    tradesize,
                    data[symbol].Open,
                    data[symbol].High,
                    data[symbol].Low,
                    data[symbol].Close,
                    this.Time.ToShortTimeString(),
                    Price[0].Value,
                    //trend.Current.Value,
                    //trendTrigger[0].Value,
                    comment,
                    signal,
                    nEntryPrice,
                    nEntryPrice1,
                    orderId,
                    Portfolio.TotalUnrealisedProfit,
                    sharesOwned,
                    tradeprofit,
                    tradefees,
                    tradenet,
                    Portfolio.TotalPortfolioValue,
                    "",
                    "",
                    "",
                    ""
                    );
            logmsg += matrix;
            mylog.Debug(logmsg);

            // reset the trade profit
            tradeprofit = 0;
            tradefees   = 0;
            tradenet    = 0;
            #endregion

            if (this.Time.Hour == 16)
            {
                trend.Reset();
                trendHistory.Reset();
                iTrendStrategy.Reset();
                foreach (var r in trendHistoryList)
                {
                    r.Value.Reset();
                    strategyList[r.Key].Reset();
                }
                //trendTrigger.Reset();
                barcount = 0;
                Plot("Strategy Equity", "Portfolio", Portfolio.TotalPortfolioValue);
            }
        }
Exemplo n.º 12
0
        public void RetievesNonZeroIndexProperlyAfterReset()
        {
            var window = new RollingWindow<int>(3);
            window.Add(0);
            Assert.AreEqual(1, window.Count);
            Assert.AreEqual(0, window[0]);

            window.Add(1);
            Assert.AreEqual(2, window.Count);
            Assert.AreEqual(0, window[1]);
            Assert.AreEqual(1, window[0]);

            window.Add(2);
            Assert.AreEqual(3, window.Count);
            Assert.AreEqual(0, window[2]);
            Assert.AreEqual(1, window[1]);
            Assert.AreEqual(2, window[0]);

            window.Add(3);
            Assert.AreEqual(3, window.Count);
            Assert.AreEqual(1, window[2]);
            Assert.AreEqual(2, window[1]);
            Assert.AreEqual(3, window[0]);

            window.Reset();
            window.Add(0);
            Assert.AreEqual(1, window.Count);
            Assert.AreEqual(0, window[0]);

            window.Add(1);
            Assert.AreEqual(2, window.Count);
            Assert.AreEqual(0, window[1]);
            Assert.AreEqual(1, window[0]);

            window.Add(2);
            Assert.AreEqual(3, window.Count);
            Assert.AreEqual(0, window[2]);
            Assert.AreEqual(1, window[1]);
            Assert.AreEqual(2, window[0]);

            window.Add(3);
            Assert.AreEqual(3, window.Count);
            Assert.AreEqual(1, window[2]);
            Assert.AreEqual(2, window[1]);
            Assert.AreEqual(3, window[0]);
        }
Exemplo n.º 13
0
 /// <summary>
 ///      Resets this indicator to its initial state
 /// </summary>
 public override void Reset()
 {
     _multipliedDiffWindow.Reset();
     base.Reset();
 }
Exemplo n.º 14
0
        /// <summary>
        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// </summary>
        /// <param name="data">TradeBars IDictionary object with your stock data</param>
        public void OnData(TradeBars data)
        {
            #region logging
            comment     = string.Empty;
            tradingDate = this.Time;
            #endregion
            barcount++;

            // Add the history for the bar
            var time = this.Time;
            Price.Add(idp(time, (data[symbol].Close + data[symbol].Open) / 2));

            // Update the indicators
            trend.Update(idp(time, Price[0].Value));
            trendHistory.Add(CalculateNewTrendHistoryValue(barcount, time, Price, trend));
            #region lists
            #endregion

            if (barcount > 17)
            {
                comment = "";
            }

            var of = CancelUnfilledLimitOrders();
            iTrendStrategy.orderFilled = of;

            signal = Strategy(data);

            #region logging
            sharesOwned = Portfolio[symbol].Quantity;
            string logmsg =
                string.Format(
                    "{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20}" +
                    ",{21},{22},{23},{24},{25},{26},{27},{28},{29},{30},{31},{32},{33},{34},{35},{36},{37}",
                    time,
                    barcount,
                    data[symbol].Volume,
                    data[symbol].Open,
                    data[symbol].High,
                    data[symbol].Low,
                    data[symbol].Close,
                    data[symbol].EndTime,
                    data[symbol].Period,
                    data[symbol].DataType,
                    data[symbol].IsFillForward,
                    data[symbol].Time,
                    data[symbol].Symbol,
                    data[symbol].Value,
                    data[symbol].Price,
                    "",
                    time.ToShortTimeString(),
                    Price[0].Value,
                    trend.Current.Value,
                    signals[0],
                    iTrendStrategy.nTrig,
                    iTrendStrategy.orderFilled,
                    iTrendStrategy.nEntryPrice,
                    comment,
                    "",
                    nEntryPrice,
                    nExitPrice,

                    tradeResult,
                    orderId,
                    Portfolio.TotalUnrealisedProfit,
                    sharesOwned,
                    tradeprofit,
                    tradefees,
                    tradenet,
                    Portfolio.TotalPortfolioValue,
                    "",
                    "",
                    ""
                    );
            mylog.Debug(logmsg);

            // reset the trade profit
            tradeprofit = 0;
            tradefees   = 0;
            tradenet    = 0;
            #endregion

            // At the end of day, reset the trend and trendHistory
            if (time.Hour == 16)
            {
                trend.Reset();
                trendHistory.Reset();
                iTrendStrategy.Reset();
                barcount = 0;
                Plot("Strategy Equity", "Portfolio", Portfolio.TotalPortfolioValue);
            }
        }
Exemplo n.º 15
0
 /// <summary>
 /// Resets all indicators of this object to its initial state
 /// </summary>
 public void Reset()
 {
     _roc.Updated -= OnRateOfChangeUpdated;
     _roc.Reset();
     _window.Reset();
 }
Exemplo n.º 16
0
        /// <summary>
        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// </summary>
        /// <param name="data">TradeBars IDictionary object with your stock data</param>
        public void OnData(TradeBars data)
        {
            #region logging
            comment     = string.Empty;
            tradingDate = this.Time;
            #endregion
            barcount++;

            // Add the history for the bar
            var time = this.Time;
            Price.Add(idp(time, (data[symbol].Close + data[symbol].Open) / 2));

            // Update the indicators
            trend.Update(idp(time, Price[0].Value));
            trendHistory.Add(CalculateNewTrendHistoryValue(barcount, time, Price, trend));
            #region lists
            #endregion
            if (Portfolio[symbol].Invested)
            {
                tradesize = Math.Abs(Portfolio[symbol].Quantity);
            }
            else
            {
                tradesize = (int)(_transactionSize / Convert.ToInt32(Price[0].Value + 1));
            }
            if (barcount > 100)
            {
                comment = "";
            }
            CanceledUnfilledLimitOrder();

            Strategy(data);

            #region logging
            sharesOwned = Portfolio[symbol].Quantity;
            string logmsg =
                string.Format(
                    "{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25},{26},{27},{28},{29},{30},{31},{32},{33}",
                    time,
                    barcount,
                    tradesize,
                    data[symbol].Volume,
                    data[symbol].Open,
                    data[symbol].High,
                    data[symbol].Low,
                    data[symbol].Close,
                    data[symbol].EndTime,
                    data[symbol].Period,
                    data[symbol].DataType,
                    data[symbol].IsFillForward,
                    data[symbol].Time,
                    data[symbol].Symbol,
                    data[symbol].Value,
                    data[symbol].Price,
                    "",
                    time.ToShortTimeString(),
                    Price[0].Value,
                    trend.Current.Value,
                    comment,
                    signal,
                    nEntryPrice,
                    nExitPrice,
                    tradeResult,
                    orderId,
                    Portfolio.TotalUnrealisedProfit,
                    sharesOwned,
                    tradeprofit,
                    tradefees,
                    tradenet,
                    Portfolio.TotalPortfolioValue,
                    "",
                    "",
                    ""
                    );
            mylog.Debug(logmsg);

            // reset the trade profit
            tradeprofit = 0;
            tradefees   = 0;
            tradenet    = 0;
            #endregion

            if (time.Hour == 16)
            {
                trend.Reset();
                trendHistory.Reset();
                barcount = 0;
                Plot("Strategy Equity", "Portfolio", Portfolio.TotalPortfolioValue);
            }
        }
Exemplo n.º 17
0
 public void Reset()
 {
     ITrend.Reset();
     ITrendMomentum.Reset();
     MomentumWindow.Reset();
 }
Exemplo n.º 18
0
 /// <summary>
 ///     Resets this indicator to its initial state
 /// </summary>
 public override void Reset()
 {
     _window.Reset();
     _windowTimes.Reset();
     base.Reset();
 }
Exemplo n.º 19
0
 /// <summary>
 /// Resets this indicator to its initial state
 /// </summary>
 public override void Reset()
 {
     base.Reset();
     _rollingRvi.Reset();
 }
Exemplo n.º 20
0
 /// <summary>
 /// Resets this instance.
 /// </summary>
 public void Reset()
 {
     rsi.Reset();
     rsiRW.Reset();
 }
 /// <summary>
 ///      Resets this indicator to its initial state
 /// </summary>
 public override void Reset()
 {
     _adx.Reset();
     _adxHistory.Reset();
     base.Reset();
 }
 /// <summary>
 ///      Resets the average to its initial state
 /// </summary>
 public override void Reset()
 {
     _high.Reset();
     _low.Reset();
     base.Reset();
 }