Back

Indicators for multiple instruments

So this is a program that trades a basket of FX-pairs, However i am having truble uppdating my indicators. 

 

The algo trades if i remove the indicators, but it soes not trade when they are a part of it as they keep on giving 0. 

I belive that the reason for this, is that i am failing to suply enough datapoints for the indicators to warm up. 

Any inputs on how i can work around this, I have read the examples in the documantaition, and seen several excamples for no avail. 

 

 

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.


Hi - an easy way to tell your algorithm to warm up is to set self.SetWarmup( xx ) in the Initialize function where xx is the number of bars you need for a warmup. In your case, you'll need not only to warm up the indicators but also populate the rolling windows.

1

Thanks for the answer Douglas Stridsberg ! 

However i do have a fe followup problems. Since i hae and basket of pairs that I am trading, if i move the indicators to initialize, and use the warmup function, would that warm it up for one pair or all pairs ? 
I did put the indicators into dictionaries earlyer and used the for loop in initilaize, but that just kept on giving the same output value ( so i failed to uppdat it).

 

I have been experimenting with rolling windows, and i have used it for one pair, but i seam to loose my self while doing multiple pairs. 

0

I made an attemt with my understanding of this situation with a rolling window, but i get an error. I am not using the initialize, as i need it created for every symbol. 

0


My understanding of SetWarmup is that it simply gets and pushes x number of datapoints for each of the subscribed symbols into whatever registered consolidators you have.

In order to get faster help here, could you a) give us the error you're receiving and b) shorten your code example to only the minimum things required to produce this error? I.e. remove all the comments and superfluous functionality, focusing only on the required pieces of code to produce the error. This way we can help you much faster. Right now your code is quite long and it would probably take quite a bit of time for someone to narrow down the issue.

0

Here is an link to the example that I am inspiered from, but I do not understand the sma part of it. 
https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/MultipleSymbolConsolidationAlgorithm.py

 

I have tried to clean up the code a bit to make it easyer to read, but quant connect is not uppdating my backtest so i cant attach it right now 

 

What the program does: 

It goes through the forex pairs, and is able to take trades with sl and tp in all of them. 

The problem is that it cant make use of indicators as part of the signals, since i am not able to figure out where to intitilize and call the indicators from. 

 

The error is : 

 

Runtime Error: ArgumentOutOfRangeException : Must be between 0 and -1
Parameter name: i
Actual value was 0.
at QuantConnect.Indicators.RollingWindow`1[T].get_Item (System.Int32 i) [0x0004c] in <23543ce0cd384e128799a1ad1753a505>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <b0e1ad7573a24fd5a9f2af9595e677e7>:0
at OnData in main.py:line 126
ArgumentOutOfRangeException : Must be between 0 and -1
Parameter name: i
Actual value was 0.
at QuantConnect.Indicators.RollingWindow`1[T].get_Item (System.Int32 i) [0x0004c] in <23543ce0cd384e128799a1ad1753a505>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <b0e1ad7573a24fd5a9f2af9595e677e7>:0 (Open Stacktrace)

 

Tells me that i am feeding it somthing wrong and not managing to make the indicators work. 

0

Hi Christian, your error suggests you're trying to use a RollingWindow before it's ready (i.e. fully populated). Please, each time you intend to use the window, check whether self.<window name here>.IsReady is true. If it's false, you should wait another loop before trying to use it.

Can you go through your rolling windows, apply the above check and re-try the backtest? If you still have issues, you can use the Insert Code Snippet to attach code (second button from the right, just above the text editor).

0

def OnData(self,data):

for symbol in self.Data.keys():
symbolData = self.Data[symbol]


self.quoteBarWindow.Add(data[symbol])
self.SMA(symbol, 20).Updated += self.SmaUpdated
self.smaWindow = RollingWindow[IndicatorDataPoint](20)

self.bb_ind = self.BB(symbol, 20, 1, MovingAverageType.Simple, Resolution.Hour);
self.slow = self.SMA(symbol, 1, Resolution.Hour)
self.fast = self.SMA(symbol, 1, Resolution.Hour)


fxOpen = data[symbol].Open
fxClose = data[symbol].Close
fxHigh = data[symbol].High
fxLow = data[symbol].Low
price = data[symbol].Price
pip = (fxHigh - fxLow)*10000


if self.smaWindow.IsReady == True:
trend_sma = np.where(self.fast.Current.Value > self.slow.Current.Value,1,0)

Console.Write(self.smaWindow[-1]) # this is a test to write the sma value to the consol, this is where the error occure.

 

It never gets ready, so i think the main problem is that I am not setting the window correctly. 

I have googled for examples, but the example i linked to is the only example of multiple instruments and an indicator that I can find. 

 

ForexSymbols =["EURUSD", "GBPUSD", "USDCHF", "AUDUSD","NZDUSD"]

for symbol in ForexSymbols:
forex = self.AddForex(symbol)
self.Data[symbol] = SymbolData(forex.Symbol, BarPeriod, RollingWindowSize)
self.trades[symbol] = 0
self.price[symbol] = 0
self.short[symbol] = 0
self.long[symbol] = 0
self.sl_s[symbol] = 0
self.tp_s[symbol] = 0
self.trade_ind[symbol] = 0
self.first_stg_up[symbol] = 0
self.first_stg_down[symbol] = 0

This is from initialize function, and i think i somhow must set the indicators there. with a rolling window, but how am i able to set one set of indicators for each pair?

 

 

 

0

Hey,

Three things:

  1. You haven't attached the complete code, so it's hard to see what might be missing.
  2. You can't access [-1] of your rolling window - this entry does not exist. Your rolling window will contain entries from 0 to 19 (20-1).
  3. OnData() is called every time you get new data. Right now, on line #9, you're making a new rolling window every time there's new data. That's not what you want - you want to .Add( data here ) on every OnData() call. The creation of the rolling window should only happen once (it's best to do this in Initialize()).

 

0

hey, posting the full code here, 

 

I will try to rework what you said in point 3 into it. 

 

It runns now, but the only reason for that is that i have commentet out the "and trend_sma == 1 :" in line 128. 

Also it never reaches the amount of candles to run the indicator. 

 

Ref poit 3: the reason i did it like this was that i thought that since i iterate through the symbols in the basket each time, i would need to create a new rolling windo for each instrument each time I went throug. 
But in the example link i posted from gitHub i can see that they solved it in another way, but i do not understand the prosess behind it, and have not bean able to recreate it. 

 

Also thanks for helping me and looking at this !! 

0


Hey, there's quite a lot going on in your code, I want to point out a few things:

  • = RollingWindow[xxx](xxx) creates a new rolling window. This should be done only once per symbol. Again, in the code you sent, you're creating a new rolling window every time you get new data - this means it will always be empty!
  • Typically, one window should only contain information for one symbol. I don't know why you've done self.quoteBarWindow = RollingWindow[QuoteBar](20) but in your example that window will be hard to use because you have 5 symbols. I can't see that you're ever using self.quoteBarWindow, so I'm guessing that line doesn't need to be in your example. It's always a good idea to clean and cut out lines that don't do anything, it makes it easier to read your own code.
  • As with rolling windows, indicators should be created once per symbol. In your example, you're creating new indicators (and overwriting the old ones) every time there is new data. That's not the right way to do it.
  • OnData() runs every time you get new data. Initialize() runs once.
Maybe at this stage it might be better for you to start with one of the simpler templates and build your confidence in Python programming from there. A good template to start with is the rolling window template - it will show you the basics of how to initialise and use rolling windows.
0

Thanks for pointing it out, I have bean reworking it, and it seam to be more logical now. 

Indicator works, and the rolling window works, so thanks a lot for your pointers. 

However It only gives the values for GBPUSD, so it seems to skip or just write ower EURUSD. 

Are you able to spot something obvious that is wrong ? 

As you can see I walked away from the class with a rolling window and are now using the normal update function with a rolling window. 

0


....
for symbol, symbolData in self.Data.items():
symbolData.sma_f = self.SMA(symbol, 20)
symbolData.sma_s = self.SMA(symbol, 7)



symbolData.sma_f.Updated += self.SmaUpdated
self.smaWin_f = RollingWindow[IndicatorDataPoint](20)

symbolData.sma_s.Updated += self.SmaUpdated_s
self.smaWin_s = RollingWindow[IndicatorDataPoint](20)
self.SetWarmUp(timedelta(20))


def SmaUpdated(self, sender, updated):
self.smaWin_f.Add(updated)

def SmaUpdated_s(self, sender, updated):
self.smaWin_s.Add(updated)
....

Here you have only two rolling windows - self.smaWin_f and self.smaWin_s - but you're trying to stuff the data from two symbols into these. You will need two rolling windows per symbol to achieve what you're trying to do. I suspect instead of self.smaWin_f you meant symbolData.smaWin_f.

Also, with this setup you're going to need two update handlers per symbol, which is not pretty. I'm not a Python expert but you should look to use lambda or inline functions as binds to the .Updated event rather than have separate functions outside of the loop. In C#, for example, it could look like this:

MovingAverageWindow = new RollingWindow<decimal>( 2 );
MovingAverage.Updated += ( sender, data ) => MovingAverageWindow.Add( data.Value );

 

0

Thanks a lot for all your pointers, and it have given me a direction to keep on working with it. 


I did soe more web searches through the weekend, and it seems that creating a class, and using the rolling window throug that is the way to go about it if one would like to avoid all the functions.

But i need to keep on looking onto that. 

 

 

0

Hi Christian,

Multiple Symbol Consolidation Algorithm is a great source for you to look at. The essential idea is we create a SymbolData class which defines, initialize and/or update all fields for each symbol and maintain a mapping (self.Data) which maps from symbol to its corresponding SymbolData class. All those parameters and methods regarding a single symbol should go into that class.

Ideally your way of defining every parameter as a dictionary would work, but it is very redundant and not recommend. In your most recent work, self.smaWin_f and self.smaWin_s are rewritten each time the indicators of any symbol are updated.

Here is my attempt to rewrite your strategy with SymbolData class idea. I passed in the QCAlgorithm instance to the SymbolData in the initializer. When we need to call a method/field from QCAlgorithm, we could simply call self.algorithm.doSomething.

Hope it helps.

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.


Wow, Thanks !! 

 

That is exactly what I have been looking for. 

 

cant say that i understand it all yet, but I will play around with it, 

once again, thanks for sharing: 

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