Back

How to "look-back" at previous bars?

Hello,

I am trying to construct an algorithm that compares the chosen indicator/metric values of the current bar with those of the previous bars.
Example:

I would like to check
if (10 period EMA > 30 period EMA for 3 bars in a row)
-------
if ( close of the previous bar > low of the bar that occurred 2 bars ago)

How would I code these sorts of criteria in C#?
I appreciate any and all help.
Update Backtest








Hey @Nicholas!

Check out this algorithm I've thrown together for you. It makes use of our RollingWIndow class to allow for easy index like you want. Take a peak at the code and let me know if you have any questions or need clarification with anything!
1

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.


Quick question, I am not too familiar with C#. Could you explain to me
"public RollingWindow FastEmaHistory = new RollingWindow(10);"

What is the significance behind the that is attached to Rolling window and what is the significance behind attaching (10) to the second RollingWindow?

Thank you.
0

***Meant
"public RollingWindow FastEmaHistory = new RollingWindow(10)"
0

@MichaelH

Also if you could explain your reasoning behind having the tolerance 0.005m in the code that would be swell.
0

@Nicholas, QuantConnect would be much easier with some foundations of programming. Check out the tutorials linked in the documentation --

The following line:
public RollingWindow FastEmaHistory = new RollingWindow(10);

Roughly translates to this:
[Scope of Variable] TypeOfVariable NameOfVariable = new TypeVariable(initial value / settings)

In this particular use the rolling window saves the last 10 values.

The 0.005 tolerance is to avoid trading thousands of times when the values are very close together. Check out the video EMA Cross Video in the documentation which guides you through this implementation.
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.


@Jared Broad

Sorry Jared, I meant to ask what is the significance behind the " " because I am new to C# and unaware of certain notations/syntax
public RollingWindow FastEmaHistory = new RollingWindow(10);
0

Sorry didn't notice that this wasn't displaying,
what is the significance behind the

<
decimal
>

notation next to the RollingWindow Class type because I see that in your code you use that notation multiple times such as writing

<
TradeBar
>

<
Decision
>
0

That notation is for the generic type parameter. It specifies the type of data the RollingWindow will hold. So in this case, we're storing decimal values in the window. Have a look at the MSDN page on generic types for more information!

Edit: And yes, you can specify any type there, so < TradeBar > or < Tick > to hold a window of trade bars or tick 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.


Hi Michael,

I've been debugging an algorithm which uses rolling windows(5) to determine when one value crosses above or below another.  I've noticed when debugging locally in Visual Studio that the most recent value (or previous value) is not at index[0] but rather moves down the list with each time iteration and can be referenced at index[tail # - 1].  When it gets to the end of the list, it is saved back to the index[0] position.

Is this "by design" or should I always be able to reference the previous value at index[0]?

Thanks

0

Hi Brad,

Yes, this is by design. The RollingWindow indicator uses indexers (check it here).
With this design, we don't need to add and remove elements from the list, we just replace the oldest item for the new one and update the tail position used by the indexer.

You can always reference reference the previous item in the window at this[0] and the last on at this[Count - 1]

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