Hey guys I'm trying to pass a custom selector so I can use the midpoint value of the candle to calculate the EMA indicator. ((bar.Open + bar.Close) / 2.0)
I have tried both passing a Lambda and a custom function that takes one parameter (dataPoint), but nothing it keeps giving me errors.
With custom function:
# at Initialize:
def Initialize(self)
self.lambda_func = lambda x: (x.Open + x.Close) / 2.0
self.ema= self.EMA('BTCUSD', 9, Resolution.Minute, selector=self.func)
def func(self, dataPoint):
midpoint = (dataPoint.Open + dataPoint.Close) / 2.0
return midpoint
And lambda function:
def Initialize(self)
self.lambda_func = lambda x: (x.Open + x.Close) / 2.0
self.ema= self.EMA('BTCUSD', 9, Resolution.Minute, selector=self.lambda_func)
I get the same error for both:
During the algorithm initialization, the following exception has occurred: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the 'int'>) method. Please checkout the API documentation.
at Initialize
self.ind2 = self.EMA('BTCUSD' in main.py: line 18
No method matches given arguments for EMA: (<class 'str'>, <class 'int'>, <class 'int'>)
Fred Painchaud
Hi Shner,
Yeah, from the error and from what I posted on Discord, it looks like the version of EMA with the selector function is not callable from Python.
Did you try, just for the sake of it, without adding the named param “selector=”, so:
self.EMA('BTCUSD', 9, Resolution.Minute, self.func)
Fred
Vladimir
Shner
→ use the midpoint value of the candle to calculate the EMA indicator. ((bar.Open + bar.Close) / 2.0)
We can do this using QC Indicators and IndicatorExtensions:
high = self.Identity(self.crypto, res, Field.High)
low = self.Identity(self.crypto, res, Field.Low)
midpoint = IndicatorExtensions.Over(IndicatorExtensions.Plus(high, low), 2)
self.midpoint_ema = IndicatorExtensions.EMA(midpoint, EMA)
If you are satisfied with my answer, please accept it and don't forget to like it.
Shner
Thanks Vladimir, I tried it and its working great,
I just wanted to know why did you set the warmup to the lookback period by 5, is there a reason for that ?
Vladimir
Shner
-→ just wanted to know why did you set the warmup to the lookback period by 5, is there a reason for that ?
The Exponential Moving Average (EMA) from Digital Signal Processing view is an Infinite Impulse Response (IIR) filter.
The latter value of IIR filter substantially depends on the initial value of the time series.
To reduce the dependence on the initial value, it is recommended increasing the size of the rolling window of WarmUp
to 5-6 times of the EMA period for it to mature or use a Finite Impulse Response (FIR) filter - Simple Moving Average (SMA).
Also
Midpoint of candle = (H + L) / 2
If you are satisfied with my answer, please accept it and don't forget to like it
Shner
Hey Vladimir, I was testing the code that you sent me for consolidating the High + Low / 2 and it works great but only on the default resolution (Resolution.Minute),
once I try to use this custom indicator on Consolidated data (2 min candles), when I compare the results to :
Then the results are totally off, also it gets updated every Minute instead of just every 2 mins.
Here is an example of that:
Now I was wondering if its just the way that i registered the custom_ema wrong, and if there is another way that we can pass the custom Fileds directly so I can do different operations and not just: val1 + val2 / 2 in the future ?
Thanks again for all the help, Shneor
Vladimir
Shner
“EMA of the Midpoint of the Resampled Bar” can help you.
BTW, I'm wondering if you had a chance to see what I'm asking you about:
If you are satisfied with my answer, please accept it.
Shner
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!