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







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.


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

It is difficult to study your documentation.
It is not clear whether it is possible to display 15-minute data on the instrument and the indicator on the chart.
The traded asset appears on the daily chart all the time?
If that's not possible, tell me. On this I here and finish the)
The black box is not for me. I need to see clearly where the input was, where the output is on the traded timeframe.

I have not even thought about whether the optimization of the algorithm parameters is possible?

TradeBar QuoteBar what is their difference?

0

The indicator displayed successfully.
If it's not that difficult, explain how to add the hourly closing price to the chart.

public class IndicatorTests : QCAlgorithm
{
//Save off reference to indicator objects
RelativeStrengthIndex _rsi;
private RollingWindow<IndicatorDataPoint> _rsiWin;

public override void Initialize()
{
AddEquity("SPY", Resolution.Hour);

//In addition to other initialize logic:
_rsi = RSI("SPY", 14); // Creating a RSI
RSI("SPY", 14).Updated += (sender, updated) => _rsiWin.Add(updated);
_rsiWin = new RollingWindow<IndicatorDataPoint>(14);

}

public override void OnData(Slice data)
{
if (!_rsi.IsReady) return;
Plot("RSI", _rsi);
}
}
0

When we use SetWarmup(TimeSpan(days)), it doesn't exclude weekends. For instance, if the start date is a Monday and we use a lookback of one day, no past data will be used since the market is closed on Sunday. In the docs, we advise users to "to choose a lookback period that contains the required data.".

The Relative Strength Index is the ratio between the Average Gain and Average Loss. We choose to make these "sub-indicators" available.

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

There is no bug in the example. You have added the resolution where it should be the moving average type. Please check out the reference table for indicators' parameters.

I didn't understand this question:
And why " RSI " is calculated from daily data?
In that example, we have hourly RSI as we subscribed to hourly data from SPY.

It is not clear whether it is possible to display 15-minute data on the instrument and the indicator on the chart.
This thread was about indicators. Did you read the section in Charting? Please do, it addresses your questions on charting.

I have not even thought about whether the optimization of the algorithm parameters is possible?
No. At the moment, you cannot use Lean/QuantConnect for parameter optimization. 

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.


Thank You, Alexandre!
I have one more thing to decide.
How to display the closing price of the hourly chart in a separate window?
(Me well enough 4,000 points. This is to make sure visually that the inputs and outputs are where they need to be)
It would be great if you showed how to withdraw the closing price.
I can't do that.

P.S. in the future, do you plan to add the possibility of optimizing the parameters 
(periods of indicators, regulation of stops and profits....)?
I heard that the search parameters by annealing works quickly and is almost similar to the genetic algorithm, the result.

Best, regards.

0

We can create an Identity indicator to plot hourly data:

# In Initialize
Identity("SPY", Resolution.Hour).Updated += PlotHourlyData;

private void PlotHourlyData(object sender, IndicatorDataPoint data) {
Plot("SPY", "Hourly", data.Value);
}

We do have plans on adding optimization, but it is not a high priority project at the moment.

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.


Thank You, Alexandre.

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

 
";