/// <summary>
 /// Event Handler for Nifty Data Events: These Nifty objects are created from our
 /// "Nifty" type below and fired into this event handler.
 /// </summary>
 /// <param name="data">One(1) Nifty Object, streamed into our algorithm synchronised in time with our other data streams</param>
 public void OnData(DollarRupee data)
 {
     _today = new CorrelationPair(data.Time)
     {
         CurrencyPrice = Convert.ToDouble(data.Close)
     };
 }
Esempio n. 2
0
        /// <summary>
        /// Event Handler for Nifty Data Events: These Nifty objects are created from our
        /// "Nifty" type below and fired into this event handler.
        /// </summary>
        /// <param name="data">One(1) Nifty Object, streamed into our algorithm synchronised in time with our other data streams</param>
        public void OnData(Slice data)
        {
            if (data.ContainsKey("USDINR"))
            {
                _today = new CorrelationPair(Time)
                {
                    CurrencyPrice = Convert.ToDouble(data["USDINR"].Close)
                };
            }

            if (!data.ContainsKey("NIFTY"))
            {
                return;
            }

            try
            {
                _today.NiftyPrice = Convert.ToDouble(data["NIFTY"].Close);
                if (_today.Date == data["NIFTY"].Time)
                {
                    _prices.Add(_today);

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

                //Strategy
                var quantity     = (int)(Portfolio.MarginRemaining * 0.9m / data["NIFTY"].Close);
                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["NIFTY"].Open) >= highestNifty)
                    {
                        var code = Order("NIFTY", quantity - Portfolio["NIFTY"].Quantity);
                        Debug("LONG " + code + " Time: " + Time.ToShortDateString() + " Quantity: " + quantity + " Portfolio:" + Portfolio["NIFTY"].Quantity + " Nifty: " + data["NIFTY"].Close + " Buying Power: " + Portfolio.TotalPortfolioValue);
                    }
                    else if (Convert.ToDouble(data["NIFTY"].Open) <= lowestNifty)
                    {
                        var code = Order("NIFTY", -quantity - Portfolio["NIFTY"].Quantity);
                        Debug("SHORT " + code + " Time: " + Time.ToShortDateString() + " Quantity: " + quantity + " Portfolio:" + Portfolio["NIFTY"].Quantity + " Nifty: " + data["NIFTY"].Close + " Buying Power: " + Portfolio.TotalPortfolioValue);
                    }
                }
            }
            catch (Exception err)
            {
                Debug("Error: " + err.Message);
            }
        }