I'm trying to use ta-lib to find candlestick patterns. At the moment when I run it over a year of data for a security at minute resolution it isn't showing that it's finding any patterns. Anyone know what the issue could be? I've attached the relevant part of my code.
import numpy as np
import talib
class EMACrossover(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 4, 1) # Set Start Date
self.SetEndDate(2021, 4, 30)
self.SetCash(10000) # Set Strategy Cash
self.SetWarmUp(150)
self.AddEquity("GME", Resolution.Minute)
self.sym ="GME"
def OnData(self, data):
holdings = self.Portfolio[self.sym].Quantity
price = self.Securities[self.sym].Price
#talib values
open1 = np.array(data[self.sym].Open).flatten()
close = np.array(data[self.sym].Close).flatten()
high = np.array(data[self.sym].High).flatten()
low = np.array(data[self.sym].Low).flatten()
buyquantity = self.CalculateOrderQuantity(self.sym, .1)
#Buy
pattern1 = talib.CDL3LINESTRIKE(open1, high, low, close)
pattern2 = talib.CDL3BLACKCROWS(open1, high, low, close)
pattern3 = talib.CDL2CROWS(open1, high, low, close)
pattern4 = talib.CDL3OUTSIDE(open1, high, low, close)
pattern5 = talib.CDL3INSIDE(open1, high, low, close)
pattern6 = talib.CDL3STARSINSOUTH(open1, high, low, close)
pattern7 = talib.CDL3WHITESOLDIERS(open1, high, low, close)
pattern8 = talib.CDLADVANCEBLOCK(open1, high, low, close)
pattern9 = talib.CDLBELTHOLD(open1, high, low, close)
pattern10 = talib.CDLBREAKAWAY(open1, high, low, close)
if pattern1 or pattern2 or pattern3 or pattern4 or pattern5 or pattern6 or pattern7 or pattern8 or pattern9 or pattern10:
self.Debug('Pattern found')
if holdings <= 0:
if pattern1 or pattern2 or pattern3 or pattern4 or pattern5 or pattern6 or pattern7 or pattern8 or pattern9 or pattern10:
self.Buy(self.sym, buyquantity)
self.StopMarketOrder(self.sym, -buyquantity, price * (1 - 0.01))
Varad Kabade
Hi Luke Algo,
The algorithm above calculates various patterns using just the current value of the asset in
OnData
. Perhaps it will need to use past values to detect a pattern. Thus we need to store past values along with current value using, for example, aRollingWindow
(it holds a set of the most recent entries of data). Refer to Boot Camp for further information.Best,
Varad Kabade
Vladimir
Varad kabade,
Your algorithm only traded on the first day.
Here is my attempt at solving the puzzle.
Vladimir
Luke Algo,
Here is even more productive version.
Enjoy.
Luke Algo
Thanks for the replies guys. It makes sense that I would need a rolling window. When I uses the candlestick indicators that are already part of QuantConnect I don't need to create a rolling window for those to work though right? They seem to work without the rolling window, but would just like to confirm?
Luke Algo
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!