示例#1
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);
            }
        }
示例#2
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);
            }
        }