I'm trying to create a simple script to spot candlestick patterns using the TA-Lib library, however I'm not able to pass the correct data even if I'm following the talib instructions as per their github page. The error message I get is 'Input array has wrong dimensions' when passing ohlc prices.
import talib
import numpy as np
class GeekyYellowGreenDinosaur(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020,1,1) #Set Start Date
self.SetEndDate(2020,12,31)
self.SetCash(1000) # Set Strategy Cash
self.AddForex('EURUSD', Resolution.Hour)
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
open_ = np.array(data['EURUSD'].Open)
close_ = np.array(data['EURUSD'].Close)
high_ = np.array(data['EURUSD'].High)
low_ = np.array(data['EURUSD'].Low)
hammer = talib.CDLHAMMER(open_, high_, low_, close_)
Louis Szeto
It seems to me your input array is 1-D np array, the CDLHAMMER shall use 2-D ones. You can change
low_ = np.array(data['EURUSD'].Low)
into
low_ = np.array(data['EURUSD'].Low).reshape(-1, 1)
similar to the rest of the OHLC
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.
SIG_94
Thank you for your reply Louis,
I've tried with your instructions but unfortunately I still get the same error message:
import talib import numpy as np class GeekyYellowGreenDinosaur(QCAlgorithm): def Initialize(self): self.SetStartDate(2020,1,1) #Set Start Date self.SetEndDate(2020,12,31) self.SetCash(1000) # Set Strategy Cash self.AddForex('EURUSD', Resolution.Hour) def OnData(self, data): '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here. Arguments: data: Slice object keyed by symbol containing the stock data ''' open_ = np.array(data['EURUSD'].Open).reshape(-1, 1) close_ = np.array(data['EURUSD'].Close).reshape(-1, 1) high_ = np.array(data['EURUSD'].High).reshape(-1, 1) low_ = np.array(data['EURUSD'].Low).reshape(-1, 1) pattern = talib.CDLHAMMER(open_, high_, low_, close_) if pattern: self.Log('Pattern found')
Louis Szeto
This will work on a 1-d array
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.
Derek Melchin
Hi Louis and SIG_94,
The algorithm above doesn't log anything because the CDLHAMMER method uses historical candles to determine if the pattern has formed. The source code for the method has the following conditions:
Note: "rb near the prior candle's lows"
See the attached backtest and notebook for reference.
Best,
Derek Melchin
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.
SIG_94
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!