Back

Help with 5 min consolidator code

Hi all, Trying to test a 50 SMA cross at 5 min timeframe. Tried a code for 5 min consolidator, but not sure if I'm passing the right resolution to the SMA call. Attached project, any pointers?

Overall objective is to go long on 50 SMA cross up and short on 50 SMA cross down at 5 min. I'll have to experiment on longer timeframes, but if I get the consolidator code working, that should be possible.

Thanks in advance.
Update Backtest








You're passing minutes for SMAs, not 5mins. I prefer to update indicators from OnData, fastest way to change anything.


//before init
SimpleMovingAverage _sma;

//inside init
_sma = new SimpleMovingAverage(20);

//inside consolidted bars:
TradeBar bar;
if (data.TryGetValue(_symbol, out bar))
{
_sma.Update(bar.Time, _anyData);
}

1

Thank you. Apologies, not following since I'm not a programmer. Is there a code for 5 minute or 30 minute bars. that someone can point me to? Feels like reinventing wheel
0

Hey @Vats,

I've attached some code that does 5 minutes bars into indicators. The point your algorithm is missing is making use of the consolidator. When you want to use a consolidator with an indicator you can't use the normal helper functions (SMA, EMA, ect...) since they create a consolidator on the requested resolution. Instead, you'll need to create the indicator using 'new' and then call RegisterIndicator to wire the consolidator and indicator together.

Have a peek at the attached project and let me know if this helps!

This demo algorithm may also help explain some things. I encourage everyone to take a peek at what's available as far as demo algorithms go in the github repository.
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, it works! Not yielding desired results, but I'll experiment from here. One thing I'd ideally like to do is to make this work with CL data and make that run between 9 am and 2 pm everyday. I tried the latter with this code below but I see buys and sells outside of this time interval as well. Wondering if thats because of a timezone where the server is or the code itself is incorrect


(System.DateTime.Now.Hour <= 9 && System.DateTime.Now.Hour > 14) return;
0

@Vats,

You should never really being using the DateTime.Now property in your algorithm. Use the Time property instead. DateTime.Now will give you the system's current local time in the system's time zone. This is rarely useful for the algorithm since it depends on where your physical server is located! :). Also, in backtests, we 'create' time instead of using the real time.

// in OnData, returns outside of 9am - 2pm
if (Time.Hour <= 9 || Time.Hour > 14) return;
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, make sense, I should have known better!
0

Ok, I'm also not a programmer, I'm fascinated on magic Michael does :)

You can feeed into my version anything as bar time depends on what time bars are based. I can quickly construct any custom indicator with data from any symbol without registering it.
1

Magic Michael! I like it :)

@Tadas, yes setting up indicators manually can certainly be quick and easy. In my opinion, the registed indicators are best for building complex indicator machines, or for sending the indicator objects to other classes who need their data but don't have access to the raw TradeBars data.

A simple example is determining something like the EMA of the RSI. var rsi = RSI("SPY", 14, Resolution.Daily);
var smoothedRsi = new ExponentialMovingAverage("SPY_EMA_RSI", 7).Of(rsi);
So now the smoothedRsi will receive auto updates from the rsi.
Now, just for your knowledge Tadas, you can make the same thing work without auto updates. Say ou define the following:// in initialize
rsi = new RelativeStrengthIndex("SPY_RSI", 14);
smoothedRsi = new ExponentialMovingAverage("SPY_EMA_RSI", 7).Of(rsi);
// in OnData
rsi.Update(Time, data["SPY"].Close);

The smoothedRsi variable will still receive the automatic updates from the rsi variable. So each time an indicator produces new data, he fires an event. Using the 'Of' method allows other indicators to receive their data from other indicators!
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.


Yes, may be better in cases where a lot of indicators used. That's cool it's so flexible :)
1

Thanks @Tadas, @MikeH
0

I like the idea of consolidator, just wondering can it be done in events or delegate?

So I can switch my behavior (Bear/BuLL)

Since I use those indicator as a trend line, not really sure the proper way to use it but it kind of help me out
0

Consolidators expose the DataConsolidated event and indicators expose an Updated event that you can bind to.
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