def isSpreading(self, currentCandle): # see if the EMA are spreading out if abs(self.M5dataSet[-1].indicators['EMA(8)'] - self.M5dataSet[-1].indicators['EMA(13)']) > 0.00012 and \ abs(self.M5dataSet[-1].indicators['EMA(13)'] - self.M5dataSet[-1].indicators['EMA(21)']) > 0.00012: # See if it's above the candlestick if currentCandle.high < self.M5dataSet[-1].indicators['EMA(8)'] < self.M5dataSet[-1].indicators['EMA(13)'] \ < self.M5dataSet[-1].indicators['EMA(21)']: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "RED", text='') return 'short' # See of it's below the candlestick elif currentCandle.low > self.M5dataSet[-1].indicators['EMA(8)'] > self.M5dataSet[-1].indicators['EMA(13)'] \ > self.M5dataSet[-1].indicators['EMA(21)']: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "BLUE", text='') return 'long' else: return self.touchTrigger(currentCandle) else: return False
def run(self, currentCandle, candlesUpToCurrent): if currentCandle.datetime == '2009-01-05 19:32:00': from HistoryAnalysis import backTest backTest.addMarker(self.backtest, '2009-01-05 19:32:00', "Simple Mark Above") return True return False
def run(self, currentCandle, candlesUpToCurrent): if len(candlesUpToCurrent) % 5 == 0: self.createM5Candle(candlesUpToCurrent) self.M5dataSet[-1].indicators['EMA(8)'] = ema().run( self.M5dataSet[-1], self.M5dataSet, 8) self.M5dataSet[-1].indicators['EMA(13)'] = ema().run( self.M5dataSet[-1], self.M5dataSet, 13) self.M5dataSet[-1].indicators['EMA(21)'] = ema().run( self.M5dataSet[-1], self.M5dataSet, 21) trigger = self.isSpreading(currentCandle) if trigger == 'long': self.spreadPatternSell = 0 self.spreadPatternBuy += 1 elif trigger == 'short': self.spreadPatternBuy = 0 self.spreadPatternSell += 1 elif trigger == 'Open Long': backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "RED") return 'long' elif trigger == 'Open Short': backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "RED") return 'short' else: self.spreadPatternSell = 0 self.spreadPatternBuy = 0 return False
def run(self, currentCandle, candlesUpToCurrent): if currentCandle.indicators['TRIX(14)'] >= 0: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "BLUE") # long return 'long' elif currentCandle.indicators['TRIX(14)'] < 0: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "RED") # short return 'short' else: return False
def run(self, currentCandle, candlesUpToCurrent): if 80 < currentCandle.indicators['STC(23, 50, 10)']: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "BLUE") # long # return True return 'long' elif 20 > currentCandle.indicators['STC(23, 50, 10)']: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "RED") # short # return True return 'short' else: return False
def run(self, currentCandle, candlesUpToCurrent): if currentCandle.indicators['BullSTR'] >= currentCandle.indicators[ 'BearSTR']: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "BLUE") # long # return True return 'NA' elif currentCandle.indicators['BullSTR'] < currentCandle.indicators[ 'BearSTR']: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "RED") # short # return True return 'NA' else: return False
def run(self, currentCandle, candlesUpToCurrent): if 80 < currentCandle.indicators['STC(23, 50, 10)']: if self.lastState is None: if currentCandle.indicators[ 'Bears'] <= currentCandle.indicators['Bulls']: self.lastState = 'bulls' else: self.lastState = 'bears' backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "BLUE") # long # return True return 'long' elif 20 > currentCandle.indicators['STC(23, 50, 10)']: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "RED") # short # return True return 'short' else: return False
def findNewSL(self, candlesUpToCurrent, position, currentCandle): l = 1000000000000 h = -1 for i in range(-15,0): if candlesUpToCurrent[i].high > h: h = candlesUpToCurrent[i].high if candlesUpToCurrent[i].low < l: l = candlesUpToCurrent[i].low if position == 'long': slTemp = round(l - 0.0003,5) if slTemp > self.varSl: if slTemp != self.varSl: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "CYAN", text=f"New SL:\n{round(slTemp,5)}") self.varSl = slTemp elif position == 'short': slTemp = round(h + 0.0003,5) if slTemp < self.varSl: if slTemp != self.varSl: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "CYAN", text=f"New SL:\n{round(slTemp,5)}") self.varSl = slTemp
def setup(self, currentCandle, candlesUpToCurrent, position): print('================================================================================') self.risk = (currentCandle.indicators['ATR(14)'] * 1.5) if position == 'long': self.sl = currentCandle.close - self.risk backTest.addHorizontalLine(self.backtest, currentCandle.datetime, "Horizontal At Price", price=self.sl) self.breakEven = currentCandle.close + self.risk self.openPrice = currentCandle.close backTest.addHorizontalLine(self.backtest, currentCandle.datetime, "Horizontal At Price", price=self.breakEven) backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "magenta", text=f"BE{round(self.breakEven,6)}\nSl{round(self.sl,6)}") elif position == 'short': self.sl = currentCandle.close + self.risk backTest.addHorizontalLine(self.backtest, currentCandle.datetime, "Horizontal At Price", price=self.sl) self.breakEven = currentCandle.close - self.risk self.openPrice = currentCandle.close backTest.addHorizontalLine(self.backtest, currentCandle.datetime, "Horizontal At Price", price=self.breakEven) backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "magenta", text=f"BE{round(self.breakEven, 6)}\nSl{round(self.sl, 6)}")
def run(self, currentCandle, candlesUpToCurrent): if self.lastState is None: if currentCandle.indicators['BullSTR'] >= currentCandle.indicators[ 'BearSTR']: self.lastState = 'bulls' else: self.lastState = 'bears' if currentCandle.indicators['BullSTR'] >= currentCandle.indicators[ 'BearSTR'] and self.lastState == 'bears': self.lastState = 'bulls' backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "BLUE") # long return 'long' elif currentCandle.indicators['BullSTR'] < currentCandle.indicators[ 'BearSTR'] and self.lastState == 'bulls': self.lastState = 'bears' backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "RED") # short return 'short' else: return False
def run(self, currentCandle, candlesUpToCurrent): from HistoryAnalysis import backTest time = currentCandle.datetime.split(':') hour = int(time[0][-2:]) min = int(time[1]) if hour == 8 and min >= 0 and self.flag: self.flag = False backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", color="Yellow") backTest.addHorizontalLine(self.backtest, currentCandle.datetime, "Horizontal At Price", price=self.hi) backTest.addHorizontalLine(self.backtest, currentCandle.datetime, "Horizontal At Price", price=self.lo) self.wasRanging = self.calculateTrendingIndex(self.rangingCandles) self.reset = True # Sleeping if 0 <= hour < 3: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", color="White") self.updateHiLo(currentCandle) self.rangingCandles.append(currentCandle) # London elif 3 <= hour < 8: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", color="Green") #return self.openPos(currentCandle) self.updateHiLo(currentCandle) self.rangingCandles.append(currentCandle) # Overlap elif 8 <= hour < 12: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", color="Brown") return self.openPos(currentCandle) # United States elif 12 <= hour < 17: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", color="Red") #return self.openPos(currentCandle) #self.updateHiLo(currentCandle) # Sleeping elif 17 <= hour: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", color="White") self.updateHiLo(currentCandle) self.rangingCandles.append(currentCandle) else: print("INVALID CANDLE") # Reset Flag if hour >= 17: self.flag = True
def run(self, currentCandle, candlesUpToCurrent, position): riskPrice = self.backtest.riskPrice self.sl = self.backtest.sl if position == 'long' and self.state == "part1": if currentCandle.open >= riskPrice: self.varSl = self.sl self.findNewSL(candlesUpToCurrent, position, currentCandle) backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "CYAN", text=f"\n\n\n\nTP Exit 50%\nNew SL: {self.varSl}") self.state = "part2" self.updateMoney(currentCandle,position,self.backtest.money, self.backtest.openPrice) self.newStart = currentCandle.open return False elif currentCandle.open < self.sl: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "CYAN", text="Stop loss") self.updateMoney(currentCandle, position,self.backtest.money, self.backtest.openPrice) self.newStart = currentCandle.open return True elif position == 'short' and self.state == "part1": if currentCandle.open <= riskPrice: self.varSl = self.sl self.findNewSL(candlesUpToCurrent, position, currentCandle) backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "CYAN", text=f"\n\n\n\nTP Exit 50%\nNew SL: {self.varSl}") self.state = "part2" self.updateMoney(currentCandle, position,self.backtest.money, self.backtest.openPrice) self.newStart = currentCandle.open return False elif currentCandle.open > self.sl: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "CYAN", text="Stop Loss") self.updateMoney(currentCandle, position,self.backtest.money, self.backtest.openPrice) self.newStart = currentCandle.open return True # PART 2 if self.state == "part2": self.findNewSL(candlesUpToCurrent, position, currentCandle) if position == 'long' and currentCandle.open < self.varSl: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "CYAN", text="\nEND TRADE") self.state = "part1" self.updateMoney(currentCandle, position, self.backtest.money/2, self.newStart) return True elif position == 'short' and currentCandle.open > self.varSl: backTest.addMarker(self.backtest, currentCandle.datetime, "Simple Mark Above", "CYAN", text="\nEND TRADE") self.state = "part1" self.updateMoney(currentCandle, position, self.backtest.money/2, self.newStart) return True return False