Hi, I'm working on an algorithm and there are two things I want to do that I couldn't figure out yet (new to C#):
I wanted to use a previous bar's low/high as a stop, i.e. stop when data[symbol].Close <= (data[symbol].Low)[1]
The other thing I wanted to do was a way to determine if a bar is abnormal size, what Ive done on my charting platform is something to the tune of
close-open <= 2*(sma(absval(close-open)),1000)
Any help would be greatly appreciated!
Alexandre Catarino
Please checkout the QuantConnect University example "How Do I Create a Rolling Window of Data?"
If we want to compare the current price with the previous OHLC, we need to create a RollingWindow of data and populate it with the data that comes in at OnData:
// Saving 3600 points of data RollingWindow<TradeBar> _window = new RollingWindow<TradeBar>(3600); //In OnData //Inject data into the rolling window. _window.Add(data["SPY"]); // Previous index 1, current index 0, last index _window.Count - 1 var previousLow = _window[1].Low;
We can create a moving average of abs(close-open) with the self updating SMA helper method:
// In Initialize _smaRange = SMA("SPY", 10, Resolution.Minute, x => Math.Abs(((TradeBar)x).Close - ((TradeBar)x).Open)); // In OnData var range = Math.Abs(data["SPY"].Close - data["SPY"].Open); if (range > 2 * _smaRange) { Log("Abnormal bar size: " + range + ". Moving average: " + _smaRange); }
William Patterson
Thank you!
William Patterson
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!