Back

RegisterIndicator HeikinAshi

Hi,

I'm trying to use the HeikinAshi indicator with multiple forex securities adapting a code to my strategy but I'm not able to use the registerindicator with HeikinAshi, below is the code, Is there any way to use the HeikinAshi indicator with multiple symbols?

namespace QuantConnect
{
/*
* QuantConnect University: Full Basic Template:
*
* The underlying QCAlgorithm class is full of helper methods which enable you to use QuantConnect.
* We have explained some of these here, but the full algorithm can be found at:
* https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Algorithm/QCAlgorithm.cs
*/
public class BasicTemplateAlgorithm : QCAlgorithm
{
private readonly List<string> Symbols = new List<string>{"USDJPY","AUDCHF"};
private readonly Dictionary<string, SymbolData> Data = new Dictionary<string, SymbolData>();

//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{

//Start and End Date range for the backtest:
SetStartDate(2013, 1, 1);
SetEndDate(2015, 1, 1);

//Cash allocation
SetCash(25000);
SetBrokerageModel(BrokerageName.OandaBrokerage);
//Add as many securities as you like. All the data will be passed into the event handler:
foreach (var symbol in Symbols)
{
AddSecurity(SecurityType.Forex, symbol, Resolution.Daily);
// create symbol data for each symbol and initialize it
Data.Add(symbol, new SymbolData(symbol, this));
}
}

//Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
public void OnData(TradeBars data)
{
foreach (var symbolData in Data.Values)
{
// do something with our symbol data
if (symbolData.Security.Close > symbolData.EMA_Long)
{
SetHoldings(symbolData.Symbol, .75m*symbolData.Security.Leverage/(decimal)Symbols.Count);
}
}
}
}

public class SymbolData
{
public readonly string Symbol;
public readonly Security Security;
public readonly ExponentialMovingAverage EMA_Short;
public readonly ExponentialMovingAverage EMA_Long;
public readonly HeikinAshi HA;

public SymbolData(string symbol, QCAlgorithm algorithm)
{
Symbol = symbol;
Security = algorithm.Securities[symbol];
var consolidator = new QuoteBarConsolidator(TimeSpan.FromDays(1));
EMA_Short = new ExponentialMovingAverage(21);
EMA_Long = new ExponentialMovingAverage(55);
HA = new HeikinAshi();
//algorithm.RegisterIndicator(symbol, SMA, consolidator, Field.Close);
algorithm.RegisterIndicator(symbol, EMA_Long, consolidator, Field.Close);
algorithm.RegisterIndicator(symbol, EMA_Short, consolidator, Field.Close);
algorithm.RegisterIndicator(symbol, HA, consolidator, Field.Close);
}
}
}
Update Backtest







It is possible to use the HeikenAshi indicator with multiple symbols. The reason as to why the indicator was not working in the code above was because Field.Close was passed as a parameter when the indicator actually requires data from the entire TradeBar. The consolidator was also never subscribed to the data. The algorithm below has the error fixed, along with a few other bugs that came as a result of the fixes:

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.


Hi Gurumeher,

thanks for the explanation, it worked and I'm now stuck with something else. Now I can use the HeikinAshi close and open values of the current bar, but I don't see how I can access the previous heikin ashi bar, I'm able to use rolling windows with the single symbol code that I have, but I just can't figure it out how to use the rolling windows on this code with multiple sysmbols. Is there a simpler way? what do you recommend?

Thanks in advance for your help

0

In order to use rolling windows for multiple symbols try creating a SymbolData class like this code here. This object will contain the required data in a dictionary, keyed by its symbol. In this sample code, a Rolling window and a Simple Moving Average indicator are created for each security.

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.


Hi Gurumeher,

Thanks for your help, I'm having trouble to understand how to use the CreateIndicatorName method to HeikinAshi indicator, the example you shared shows:

symbolData.SMA = new SimpleMovingAverage(CreateIndicatorName(symbolData.Symbol, "SMA" + SimpleMovingAveragePeriod, Resolution.Minute), SimpleMovingAveragePeriod);

But the following attemps show errors that I do not find a way to fix:

symbolData.HA = new HeikinAshi(CreateIndicatorName(symbolData.Symbol), Resolution.Daily);symbolData.HA = new HeikinAshi(CreateIndicatorName(symbolData.Symbol, "HA", Resolution.Daily), Resolution.Daily);

I already declared public HeikinAshi HA; in the symbolData class.

Thanks in advance!

0

Gurumeher,

  I looked at the code.  What I don't understand is where is the "Registration" happening in there.

1

I continue to have trouble using the rolling windows on multiple symbols with HeikinAshi, I think I can now use the latest bar but I want to use also the previous bar of the Heikin Ashi indicator but can't find a way to use it with the rolling windows:

if ((symbolData.HA.Open < symbolData.HA.Close) && (symbolData.HA[1].Open > symbolData.HA[1].Close))
{
MarketOrder(symbolData.Symbol, 1);
}

The backtest has the previous bar commented, any guidance is appretiated...

0


Jorge,

  Please look at the attached code.  Please look at how HA_History is used everywhere in the code,  I think added about 5 lines of code to your existing code.  Hope it helps.

1


Hi Pranav,

I saw the code and everything makes sense to me, but it doesn't make any trades. If I only use the conditions of the symbolData.HA.Open and .Close the code triggers the trades, also if I use the symbolData.HA_History[1].Open and .Close will trigger some trades, but using both conditions doesn't trigger any trade.

I also tried using only the symbolData.HA_history[0] and [1] but it doesn't trigger trades, like if all values of the history are equal to the current or something like that.

Thanks!

0


Jorge,

  Have you verified if the data in History make sense?  Maybe we need to check if HA.IsReady, before adding to History.

  I would recommend running this for a smaller period and verifying that the output in the history is what you expect it to be.  

1

Pranav,

I think it finally works, I added the HA.IsReady as you suggested and changed the rolling window of HA_history to IBaseData instead of Indicator to be able to update with the HA.CurrentBar, I run the backtest in smaller time frame and less symbols and now the trades make sense with what I expect.

I still see some bogus trades but I believe are caused for different time zones that make some canldes in different color depending on the open and close time, I added the market.oanda to the addforex function but I still see this but the difficult issue is solved.

Thanks for your help Pranav and Gurumeher.

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