Back

Trading 4 Hr Timeframe

Hi,

I was looking at the Example DailyAlgorithm.cs and trying to apply that to the 4 hour time frame. In the attached project I am not seeing what I would expect in the log statement based on the criteria of the if, I've tried several different properties of the data object but they all seem have the same result.

I also had questions as to the Resolution. of the Security and the SMA indicator - Are these set right to ensure that the SMA6 value is for the close of a 4 hour bar in the OnData method?

Thanks for the help!
Greg
Update Backtest








Hey @Greg!

It looks like the project never got attached. Remember to include a back test or else it won't work!
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.


Sorry about that, didn't know I had to attach the backtest. Also to elaborate the Log statement seems to execute every minute of the backtest data which is not what I expected, I would have expected to see 0, 4, 8, 12, 16, 20....... when the Hour is logged out, not just repeated 0. Not sure what I am doing wrong here.

Also on a different note all together could you tell me what the ? in the below declaration means

decimal? last = null;

I saw this in DisplacedMovingAverageRibbon.cs but could not find what it means in the C# documentation.
0


Hey @Greg,

It looks like there's a few issues here. First, the 'Date' property will always have a TimeOfDay = 0, since it's the date portion of the DateTime. Secondly, it looks like the logic for detecting 4 hour marks wasn't correct.

I've attached an updated version of your code that will fire each day at 12pm and 4pm for equities.
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.


Thanks Michael, I guess I got wrapped around the axle on the different properties of the TradeBars data object.

Any thoughts on the other question about that decimal declaration that went like

decimal? last = null;

Thanks again for your assistance
Greg
0

? = nullable
0

Thanks Ian!

Not sure if you or Michael can help on this one but the last question I had in this thread was about the Resolution value for the Equity/Instrument and Indicators. To use this in a 4 hour time frame would Resolution.Hour be the right value or would I have do so something custom to make this work properly?

Thanks again for your help!
Greg
0

Greg, here's some documentation on nullable types.

As for your 4 hour time frame, what exactly are you trying to do? Are you trying to have indicators based on 4 hour data? Are you trying to make decisions every 4 hours? Is this 4 trading hours or 4 rounded clock hours (8/12/16)?

If you're looking to have indicators defined on the 4 hour interval, I would recommend usage of a consolidator. Consolidators allow you to 'consolidate' higher resolution bars into lower resolution bars (hours -> 4 hour bars).

I've attached an example of consolidating some SPY hourly data into 4 hour bars. Each day you'll receive data at noon and at 4pm.
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.


Thanks Michael, I will have a look at his code, I appreciate your putting it together for me.

I lean towards trading spot forex which is a 24 hour market starting Sunday night through Friday afternoon EST. I found a 4 SMA trading system on the internet that uses a 4 hour time frame (so in specific answer to your questions trying to have indicators based on the close price of a 4 hour bar to make trading decisions based on those indicators each 4 hour period for all the time the market is open) - I don think it will make any money but I thought it was a good place to start so I can learn more about the QuantConnect LEAN apis and how to build, optimize and measure strategies. I am also interested in ETF's as well but I dont have much experience trading those.

Thanks again for your help,
Greg
1

Hi Michael,

When using the RegisterIndicator method of the QCAlgorithm.Indicators.cs as shown in the example you attached in a local copy of the LEAN project (pulled from git this morning), the code throws an error due to ambiguity. Looks like a couple of the method signature allows a null value in the last (4th) argument but it can be of different types (decimal or T)

I believe these are

public void RegisterIndicator(string symbol, IndicatorBase indicator, IDataConsolidator consolidator, Func selector = null)

and

public void RegisterIndicator(string symbol, IndicatorBase indicator, IDataConsolidator consolidator, Func selector = null)

I think that argument is some kind of lamba expression of which I am not too familiar with yet so I'm not quite sure how to fix that.

Thanks for your assistance,
Greg
0

Calling it with a 4th argument like this x=> null seems to work. Is this a legitimate way to call the method?

Thanks again,
Greg
0

Hey sorry Greg for the very delayed response, somehow this slipped past me. The value 'x => null' I wouldn't expect to work. You could just pass 'null' and then we'll default to 'x => x.Value' which is the closing price of the bar.
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.


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