Back

I'm trying to implement an algorithm based on two ema.

Tell me what steps I still need to do to complete the algorithm.

Input:       ema_s[1]>ema_b[1] && ema_s[2]<ema_b[2]
Output:     ema_s[1]<ema_b[1] && ema_s[2]>ema_b[2]
+

trailing stop

using QuantConnect.Indicators;

namespace QuantConnect { 

public class BasicTemplateAlgorithm : QCAlgorithm
{
    DateTime startDate = new DateTime(2014, 01, 01);
    DateTime endDate = new DateTime(2014, 12, 31);
    ExponentialMovingAverage ema_s;
    ExponentialMovingAverage ema_b;
    
    public override void Initialize() 
    {
       SetStartDate(startDate);
       SetEndDate(endDate);    
       SetCash(100000);
       AddEquity("IBM", Resolution.Minute);
       Securities["IBM"].SetDataNormalizationMode(DataNormalizationMode.SplitAdjusted);
       ema_s = EMA("IBM", 14, Resolution.Minute);
       ema_b = EMA("IBM", 21, Resolution.Minute);
    }

    public override void OnData(Slice data) {
       if (!ema_b.IsReady) return;      
    }
}
}
Update Backtest







Please check out the docs under the Indicators and Rolling Window sections.
The indicator part is right. You just need to add and plug in the rolling windows.
For the trailing stop, you can get inspiration from the Trailing Stop Risk Management Model

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.


It's a little hard for me to understand, as the second day I try to do something.
Did I create the indicators correctly in the code?
Is it possible without trading just to display the price and indicators in the chart to understand what was done correctly?

0

Take your time to learn the API. :-)
The indicators are fine. Now read the docs section on Combining Rolling Windows With Indicators:

private RollingWindow<IndicatorDataPoint> smaWin;

// In Initialize, create the rolling windows
public override void Initialize()
{
// Creates an indicator and adds to a rolling window when it is updated
SMA("SPY", 5).Updated += (sender, updated) => smaWin.Add(updated);
smaWin = new RollingWindow<IndicatorDataPoint>(5);
}

Please avoid price logging. For charting, you can use:

# In Initialize
ema_s = EMA("IBM", 14, Resolution.Minute);
ema_b = EMA("IBM", 21, Resolution.Minute);
PlotIndicator("EMA", ema_s, ema_b);

The last statement will plot the indicators in the same chart.

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.


Read the "Indicators" page:

1.

SetWarmUp(TimeSpan.FromDays(10));

10 calendar days are used to prepare the indicator (including weekends when there is no trading)?

SetWarmUp(10, Resolution.Daily)

Used 10 trading days excluding weekends?

2.The example has:

var averageGain = _rsi.AverageGain;
var averageLoss = _rsi.AverageLoss;

What is it? Intermediate values for rsi calculation?

3.What you need"Custom Period Indicators"?
If the indicator is rarely used (not all bars need values) Or to access an example of the maximum value in a sliding window?

4.At the end of the page there is an example to draw the indicator.
Where " Plot ("RSI", _rsi, _rsi SMA);"

public class IndicatorTests : QCAlgorithm
{
//Save off reference to indicator objects
RelativeStrengthIndex _rsi;
public override void Initialize()
{
AddEquity("SPY", Resolution.Hour);
//In addition to other initialize logic:
_rsi = RSI("SPY", 14,Resolution.Hour); // Creating a RSI
}

public override void OnData(Slice data)
{
Plot("RSI", _rsi);
}
}

Throws the following error:
Build Error: File: BasicTemplateAlgorithm.cs Line:11 Column:28 - Argument 3: cannot convert from 'QuantConnect.Resolution' to 'QuantConnect.Indicators.MovingAverageType'

0

If I use it "_rsi = RSI("SPY", 14);", everything is fine
The indicator data is drawn on the hourly chart.
And here is himself "SPY" on day. We need both on the hourly chart.

And why " RSI " is calculated from daily data?

0

 

In the previous post there was a link to "4.At".
Request moderators to remove its! I don't know where it came from.

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