Back

Time values and accessing variables

Hi, everyone, I am making some progress on my code, but I am unable to access Time values. Specifically, I am trying to find out if it is 9:31 am in OnData(TradeBars data) by getting the Time.Hours and Time.Minute values and converting them to string, but they haven'nt worked.

Also, I need help accessing variables in OnData(TradeBars data) after declaring them in initialize. For example, how would I access values from the string[] stocksToTrade array in OnData(TradeBars data)?

I am basically working off the basic template from Quantopian which I cloned. I am a decent progrogrammer in various languages, but I am new to C#.

Unfortunately, I have no successful backtests, because I havn't been able to get the code to run. I have been receiving only error messages. But I have attached my source code. 

Sorry for the long post. I would greatly appreciate any help anyone could me. Thank you.

 

 

 

 


namespace QuantConnect
{  
    // Name your algorithm class anything, as long as it inherits QCAlgorithm
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize()
        {
            SetStartDate(2013, 9, 13);        
            SetEndDate(DateTime.Now.Date.AddDays(-1));
            SetCash(1000);
            AddSecurity(SecurityType.Equity, "MSFT", Resolution.Minute);
            string[] stocksToTrade = {"AES","ASIX","BAC","CHK","F","FCX","FTR","HBAN","HPQ"};
            foreach (var stock in stocksToTrade)
                                {
        AddSecurity(SecurityType.Equity, stock, Resolution.Minute);
                }
        }

        //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        public void OnData(TradeBars data)
        {  
            if (!Portfolio.HoldStock)
            {
            //                Console.WriteLine("Just testing");
                Order("MSFT", (int)Math.Floor(Portfolio.Cash / data["MSFT"].Close) );
                Debug("Debug Purchased MSFT");
                var hourVal = Time.Hours.
                var minuteVal = Time.minute
                Debug(hourVal);
                Debug(minuteVal);
            }
        }
    }
}

Update Backtest








There are some typos in this code snippet.

In C# we need to end a statement with a dot-comma. When we forget it, the IDE tell us something like "; expected". 
 
Time is a System.DateTime object which Hour and Minute properties. In this code snipped, we see Time.Hours and Time.minute, while the correct would be Time.Hour and Time.Minute:

var hourVal = Time.Hour;
var minuteVal = Time.Minute;

DateTime.Hour and DateTime.Minute are integer objects. Debug method only accepts string object and C# does not make type convertions like, for example, Python does. We have to convert them or give C# compiler a hint:

// Explicit conversion
Debug(hourVal.ToString());
Debug(minuteVal.ToString());

// Implicit conversion
// (Hint to the compiler: we add a string)
Debug("Hour: " + hourVal);
Debug("Minute: " + minuteVal);

Finally, when you subscribe the securities using AddSecurity method, these will be part of the Securities object:

foreach(var security in Securities.Values)
{
Debug(security.Symbol.Value);
Debug("Quantity: " + security.Holdings.Quantity);
Debug("Price: " + security.Price);
}

Please checkout the documentaion on the Securities and Portfolio section for more information about these objects.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


I will check out the Securities and Portfolio section. Thank you very much for your help and your patience with a hopeless newbie.

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed