Back

Exponential Moving Average and Stop Loss Question

Hello,
Couple of newbie questions...
How do I get the Exponential moving average of the prior day / time period ?
For example, something like:=

Avg1 = XAverage(close,40);
And then we can compare Avg1 with Avg1[1]...
(Where Avg1 is current and Avg1[1] is prior)

And, how can I setup a stop loss order ? I see that there is a StopMarketOrder class,
Is that what I have to use ?
I would like to set a stop using the average true range.

I am using the clone of the exponential moving average cross-over
strategy as a starting point.

Thanks
Update Backtest








I would use a RollingWindow

private RollingWindow Avg1;

Then you can

Avg1.Add(new IndicatorDataPoint(this.time, XAverage.Current.Value);

and access it with

if (Avg[0].Value == Avg[1].Value){
// do something
}
0

We can easily define out 40 period EMA using the following: var ema = EMA("SPY", 40);We can put the old emas into a rolling window as suggested by @Nicholas:int historyLength = 10;
var window = new RollingWindow(historyLength);
// this next line says each time we get a new ema value, add it to our window
ema.Updated += (sender, args) => window.Add(args);
Now we can access and compare the way @Nicholas shows: // if today's ema is greater than yesterday's ema
if (window[0] > window[1]){
// do something magic
}
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.


Ah yes. You are subscribing the the Updated event in IndicatorBase.cs. Very cool.
1

I know this is an old thread, but would this code go in the initialize or the OnData event. Specifically for the .Updated event to fire. Does that happen no matter where you call EMA()? Thanks

0

on the right there are some examples provided by the QC team.

check them out

there is for example one file called MovingAverageCrossAlgorithm. which you can copy paste into the algo lab and play with that.

(its an ema crossover example)

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