I deleted a few lines and added some new, but basically, the mistake is the same
class Trendchange(QCAlgorithm):
def Initialize(self):
self.length = 50
self.SetStartDate(2013, 12, 26) # Set Start Date
self.SetEndDate(2020, 12, 26) # Set Start Date
self.SetCash(10000) # Set Strategy Cash
self.SetWarmUp(self.length)
self.symbol = "EURUSD"
self.AddForex(self.symbol, Resolution.Hour)
#self.AddCrypto("BTCUSD", Resolution.Hour)
self.SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Cash)
self.SetBenchmark(self.symbol)
self.longOnly = True
self.tradeBarWindow = RollingWindow[TradeBar](self.length) # Store the last 100 values
self.support = 0.0
self.resistance = 0.0
self.high = 0.0
self.low = 9999999999.9
self.newLL = False
self.newHH = False
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
'''
def findMax(self):
high = 0.0
i = self.length-1
while i > 0:
if self.tradeBarWindow[i].High > high:
high = self.tradeBarWindow[i].High
i -= 1
return high
def findMin(self):
low = 999999999.9
i = self.length-1
while i > 0:
if self.tradeBarWindow[i].Low < low:
low = self.tradeBarWindow[i].Low
i -= 1
return low
# Update our rolling windows
self.tradeBarWindow.Add(data[self.symbol])
if data[self.symbol].Close > data[self.symbol].Open:
self.high = data[self.symbol].High
elif data[self.symbol].Close < data[self.symbol].Open:
self.low = data[self.symbol].Low
# Wait for windows to be ready.
if not self.tradeBarWindow.IsReady: return
if self.IsWarmingUp: return
holdings = self.Portfolio[self.symbol].Quantity
currOpen = self.tradeBarWindow[0].Open
currHigh = self.tradeBarWindow[0].High
currLow = self.tradeBarWindow[0].Low
currClose = self.tradeBarWindow[0].Close
tp = 6
sl = 3
##### RUN SOME LOGIC HERE...
longCondition = False
shortCondition = False
if currLow < findMin(self):
self.resistance = self.high
self.newLL = True
if self.newLL:
if currClose > self.resistance:
longCondition = True
self.newLL = False
if currHigh > findMax(self):
self.support = self.low
self.newHH = True
if self.newHH:
if currClose < self.support:
shortCondition = True
self.newHH = False
##### END OF LOGIC
#### TRADE EXECUTION
if (holdings <= 0.0) and longCondition:
self.LimitOrder(self.symbol, 1.0, currClose)
self.LimitOrder(self.symbol, -1.0, currClose*(1+(tp/100)))
elif (holdings >= 0.0) and shortCondition:
self.LimitOrder(self.symbol, -1.0, currClose)
self.LimitOrder(self.symbol, 1.0, currClose*(1-(tp/100)))
Â
Â