Back

How to lag indicators

Tried using max to find a previous period high but got an error saying I could not apply indexing

How do I find previous values of indicators?
Update Backtest








Set up a RollingWindow and .Add an Indicator.Current.Value to it on each bar. Then you can refer to the rolling window using the normal array notation. For example, [2] refers to 2 periods ago and [0] refers to the value you just added.

You have to be careful about which index you use because the Add method adds a new IndicatorDataPoint at the beginning of the RollingWindow and shifts all the other values up 1 index. So there is a difference between the two following pieces of code.

rw.Add(new IndicatorDataPoint(Time, value).
var x = rw[1].Value;

and

var x = rw[1].Value;
rw.Add(new IndicatorDataPoint(Time, value):

Nick
0

Thanks

I have tried to add your code suggestion but I don't think I have used it correctly.
0


If you want a fixed lag indicator I would recommend using the Delay indicator. It sounds like it does exactly what you want, it lags the value by the specified number of periods.var sma = SMA("SPY", 14);
// create a 5 period lag of our sma
var laggedSma = new Delay(5).Of(sma);
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.


@max Here is some code which corrects some of your mistakes. I tried to keep as close to your original logic as possible and added comments where I made changes.

However, this algorithm has a serious problem which I did not address. When I ran it in back test on my local machine, I got 157 errors similar to the following.

Transactions.GetSufficientCapitalForOrder(): Id: 9, Initial Margin: 7912.75000, Free Margin: 3150.370000000000000000000000
2016-02-01T18:04:26.7047617Z ERROR:: 6/8/2010 12:00:00 AM: Error >> Order Error: id: 9, Insufficient buying power to complete order (Value:15823.5).

In other words you are trying to buy or sell shares for which you have insufficient capital in your account. I put a stop in on June 8 2010 and took a look at the variables.

At that time you calculate quantity as -150. Portfolio.Cash of $-15000.98 / Close of 105.49. Your portfolio cash is negative because you own 373 shares already and have borrowed the 15k on margin. The quantity is then negative and your holdings > 0, so even though your comment says "Short", you put in an order for (-)-150 or a buy of 150. When the platform calls GetSufficientCapitalForOrder, it throws an Exception that you have insufficient capital.

When I run a back test, the log shows that the algorithm is rate limited to prevent browser flooding so I cannot get to the logs.
2


One more thing in addition to Michael's comment. There is already a DonchianChannel indicator in Lean. You might want to take a look at it.
0

Delay looks useful

Really appreciate you guys taking the time to give feedback :)
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