Hi!
Using rolling windows, I want to calculate average value of a pip for fx pairs within last 20 days. Unfortunately I wasn't able to find similar topics within this forum. I attempted to implement the code by myself, but I am stuck with this error:
During the algorithm initialization, the following exception has occurred: Loader.TryCreatePythonAlgorithm(): Unable to import python module ./cache/algorithm/project/main.pyc. AlgorithmPythonWrapper(): name 'Pair1_0D' is not defined
at
AvgPair1_Variation = (Pair1_0D + Pair1_1D + Pair1_2D + Pair1_3D + Pair1_4D +Pair1_5D + Pair1_6D) / 7
Here is my code:
def Initialize(self):
self.Pair_1 = "USDDKK"
self.Pair_2 = "USDPLN"
self.SetStartDate (2000, 3, 3)
self.SetEndDate(2022,5,24)
self.SetCash(1000000)
self.SetBrokerageModel(BrokerageName.OandaBrokerage)
self.EURSEK = self.AddForex(self.Pair_1, Resolution.Daily, Market.Oanda)
self.GBPSGD = self.AddForex(self.Pair_2, Resolution.Daily, Market.Oanda)
self.symbols = [self.Pair1, self.Pair_2]
self.prevMinPriceVariation = { symbol : RollingWindow[QuoteBar](20) for symbol in self.symbols }
def OnData(self,data):
for symbol in self.symbols:
if data.ContainsKey(symbol):
self.prevPrices[symbol].Add( data[symbol] )
if not all([ window.IsReady for window in self.prevMinPriceVariation.values() ]):
return
Pair1_Variation = self.prevMinPriceVariation[self.Pair1]
Pair1_6D = Pair1_window[6].MinimumPriceVariation
Pair1_5D = Pair1_window[5].MinimumPriceVariation
Pair1_4D = Pair1_window[4].MinimumPriceVariation
Pair1_3D = Pair1_window[3].MinimumPriceVariation
Pair1_2D = Pair1_window[2].MinimumPriceVariation
Pair1_1D = Pair1_window[1].MinimumPriceVariation
Pair1_0D = Pair1_window[0].MinimumPriceVariation
Pair2_Variation = self.prevMinPriceVariation[self.Pair_2]
Pair2_6D = Pair1_window[6].MinimumPriceVariation
Pair2_5D = Pair1_window[5].MinimumPriceVariation
Pair2_4D = Pair1_window[4].MinimumPriceVariation
Pair2_3D = Pair1_window[3].MinimumPriceVariation
Pair2_2D = Pair1_window[2].MinimumPriceVariation
Pair2_1D = Pair1_window[1].MinimumPriceVariation
Pair2_0D = Pair1_window[0].MinimumPriceVariation
AvgPair1_Variation = (Pair1_0D + Pair1_1D + Pair1_2D + Pair1_3D + Pair1_4D +Pair1_5D + Pair1_6D) / 7
AvgPair2_Variation = (Pair2_0D + Pair2_1D + Pair2_2D + Pair2_3D + Pair2_4D +Pair2_5D + Pair2_6D) / 7
self.Log("Average variation Pair1 : " + str+(AvgPair1_Variation))
Does anyone have any idea or suggestions on how to solve this?
Nico Xenox
Hey sebul
So I'm not sure if that is just a part of the code because copy pasting the code has more than just that error and is structured differently. Anyways that error occurs because the average calculation happens before Pair values get any data from the rolling windows.
Guessing your average calculation is outside of the for loop, you could add an if statement and either check if the window is ready before doing the calculation or make a bool statement.
bool statement:
Sebul
Hi Nico Xenox
I've just implemented your advice and the error no longer pops up, thank you!
Unfortunately another error doesn't allow me to continue coding. I have been looking for the solution for a while but can't get my head around this. The error looks like this:
If I understand correctly, as FX products use QuoteBars, I'm not able to immediately get minium price variation values from historical quotes.
I looked at documentation https://www.quantconnect.com/docs/v2/writing-algorithms/trading-and-orders/key-concepts
and based on their guidance I added lines 21 and 22, which were supposed to get symbol properties from the FX pair which I am using:
Unfortunately, this doesn't help. Do you have any idea on how to solve this error? Am I missing something?
Here is my code:
Derek Melchin
Hi Sebul,
The algorithm above is trying to access a MinimumPriceVariation property of QuoteBar objects, which doesn't exist. We can't exactly calculate the pip range of each bar since we don't have TradeBar objects with Forex. One way we could approximate the pip range with QuoteBar data is with
See the attached backtest for reference.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Sebul
Hi Derek Melchin
Oh now I understand, thank you !
Sebul
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!