Exemple #1
0
        /// <summary>
        /// Reader converts each line of the data source into BaseData objects. Each data type creates its own factory method, and returns a new instance of the object
        /// each time it is called.
        /// </summary>
        public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
        {
            //New Nifty object
            var index = new Nifty();

            try
            {
                //Example File Format:
                //Date,       Open       High        Low       Close     Volume      Turnover
                //2011-09-13  7792.9    7799.9     7722.65    7748.7    116534670    6107.78
                var data = line.Split(',');
                index.Time    = DateTime.Parse(data[0], CultureInfo.InvariantCulture);
                index.EndTime = index.Time.AddDays(1);
                index.Open    = Convert.ToDecimal(data[1], CultureInfo.InvariantCulture);
                index.High    = Convert.ToDecimal(data[2], CultureInfo.InvariantCulture);
                index.Low     = Convert.ToDecimal(data[3], CultureInfo.InvariantCulture);
                index.Close   = Convert.ToDecimal(data[4], CultureInfo.InvariantCulture);
                index.Symbol  = "NIFTY";
                index.Value   = index.Close;
            }
            catch
            {
            }
            return(index);
        }
        /// <summary>
        /// OnData is the primary entry point for youm algorithm. New data is piped into your algorithm here
        /// via TradeBars objects.
        /// </summary>
        /// <param name="data">TradeBars IDictionary object</param>
        public void OnData(Nifty data)
        {
            try
            {
                var quantity = (int)(Portfolio.TotalPortfolioValue * 0.9m / data.Close);

                _today.NiftyPrice = Convert.ToDouble(data.Close);
                if (_today.Date == data.Time)
                {
                    _prices.Add(_today);

                    if (_prices.Count > _minimumCorrelationHistory)
                    {
                        _prices.RemoveAt(0);
                    }
                }

                //Strategy
                var highestNifty = (from pair in _prices select pair.NiftyPrice).Max();
                var lowestNifty  = (from pair in _prices select pair.NiftyPrice).Min();
                if (Time.DayOfWeek == DayOfWeek.Wednesday) //prices.Count >= minimumCorrelationHistory &&
                {
                    //List<double> niftyPrices = (from pair in prices select pair.NiftyPrice).ToList();
                    //List<double> currencyPrices = (from pair in prices select pair.CurrencyPrice).ToList();
                    //double correlation = Correlation.Pearson(niftyPrices, currencyPrices);
                    //double niftyFraction = (correlation)/2;

                    if (Convert.ToDouble(data.Open) >= highestNifty)
                    {
                        var code = Order("NIFTY", quantity - Portfolio["NIFTY"].Quantity);
                        Debug("LONG " + code + " Time: " + Time.ToShortDateString() + " Quantity: " + quantity + " Portfolio:" + Portfolio["NIFTY"].Quantity + " Nifty: " + data.Close + " Buying Power: " + Portfolio.TotalPortfolioValue);
                    }
                    else if (Convert.ToDouble(data.Open) <= lowestNifty)
                    {
                        var code = Order("NIFTY", -quantity - Portfolio["NIFTY"].Quantity);
                        Debug("SHORT " + code + " Time: " + Time.ToShortDateString() + " Quantity: " + quantity + " Portfolio:" + Portfolio["NIFTY"].Quantity + " Nifty: " + data.Close + " Buying Power: " + Portfolio.TotalPortfolioValue);
                    }
                }
            }
            catch (Exception err)
            {
                Debug("Error: " + err.Message);
            }
        }