def inform(self, df): # print("Calculating Strategy for " + str(self.tickerName) + " at " + str(timeStamp) + "...") ### TO-DO: Develop strategies to calculate ## ## Inform Analyser to do the interval Analysis first ## This is before the pseudotrades to ensure no clashes self.analyser.intervalAnalysis(df.head(1)) #1. Calculate ATR for potential trade atr = atrcalc.ATRcalc(df) indi = ind.Indicator() Results = indi.beginCalc(df, self.tickerName, atr) for i in Results: if Results[i] != 0: self.analyser.PseudoTrade(df, i, Results[i], atr) ### for testing # self.analyser.PseudoTrade(df,i, Results[i], atr) ### ### END TO-DO # print("Calculated Strategy for " + str(self.tickerName) + " at " + str(timeStamp)) self.comparator.compare(Results, atr)
def macdTRIX(self, df, pattern, patterntype): df = df.dropna() #1. Calculate ATR for potential trade atr = atrcalc.ATRcalc(df) ##b. MACD macdInput = df.head(34) macdInput = macdInput.iloc[::-1] MACDclose = macdInput['close'].values macd, macdsignal, macdhist = MACDFIX(MACDclose, signalperiod=9) # print("MACD\n", macd[-1]) # print("Signal\n", macdsignal[-1]) ##c. DelayedMACD delayedmacdInput = df.iloc[1:].head(34) delayedmacdInput = delayedmacdInput.iloc[::-1] delayedMACDclose = delayedmacdInput['close'].values delayedmacd, delayedmacdsignal, delayedmacdhist = MACDFIX( delayedMACDclose, signalperiod=9) trixInput = df.head(60) trixInput = df.iloc[::-1] trixOutput = TRIX(trixInput['close'].values, timeperiod=20) # print("EMA\n", ema[-1]) ##d. current price action priceaction = df.head(1) pricehigh = priceaction['high'].values[0] pricelow = priceaction['low'].values[0] priceclose = priceaction['close'].values[0] # print("pricehigh\n", pricehigh) # print("pricelow\n", pricelow) if delayedmacd[-1] < delayedmacdsignal[-1] and macd[-1] > macdsignal[ -1] and trixOutput[-1] < 0: position = 1 elif delayedmacd[-1] > delayedmacdsignal[-1] and macd[-1] < macdsignal[ -1] and trixOutput[-1] > 0: position = -1 else: position = 0 amount = 50 if position == 1: stoploss = priceclose - 1.05 * atr takeprofit = priceclose + 1.45 * atr # amount = priceclose / (priceclose - stoploss) if (priceclose - stoploss) * amount < 0.1: position = 0 amount = 0 stoploss = 0 takeprofit = 0 elif position == -1: stoploss = priceclose + 1.05 * atr takeprofit = priceclose - 1.45 * atr # amount = priceclose / (stoploss - priceclose) if (stoploss - priceclose) * amount < 0.1: position = 0 amount = 0 stoploss = 0 takeprofit = 0 else: stoploss = 0 takeprofit = 0 amount = 0 ##For test # position = 1 if position * pattern > 0: confidence = abs(pattern) elif position * pattern < 0: confidence = abs(1 / pattern) elif position == 0: confidence = 0 else: confidence = 1 # ##FOR TEST # position = 1 return [position, amount, priceclose, stoploss, takeprofit, confidence]
def trix200(self, df, pattern, patterntype): df = df.dropna() #1. Calculate ATR for potential trade atr = atrcalc.ATRcalc(df) trixInput = df.head(60) trixInput = trixInput.iloc[::-1] trixOutput = TRIX(trixInput['close'].values, timeperiod=14) ##c. 200EMA emaInput = df.head(200) emaInput = emaInput.iloc[::-1] EMAclose = emaInput['close'].values ema = EMA(EMAclose, timeperiod=200) # print("EMA\n", ema[-1]) ##d. current price action priceaction = df.head(1) pricehigh = priceaction['high'].values[0] pricelow = priceaction['low'].values[0] priceclose = priceaction['close'].values[0] # print("pricehigh\n", pricehigh) # print("pricelow\n", pricelow) if trixOutput[-2] < 0 and trixOutput[-1] > 0: crossover = 1 elif trixOutput[-2] > 0 and trixOutput[-1] < 0: crossover = -1 else: crossover = 0 if pricelow > ema[-1]: marketEMA = 1 elif pricehigh < ema[-1]: marketEMA = -1 else: marketEMA = 0 ##OUTPUT if marketEMA == 1 and crossover > 0: position = 1 elif marketEMA == -1 and crossover < 0: position = -1 else: position = 0 amount = 50 if position == 1: stoploss = priceclose - 1.05 * atr takeprofit = priceclose + 1.45 * atr # amount = priceclose / (priceclose - stoploss) if (priceclose - stoploss) * amount < 0.1: position = 0 amount = 0 stoploss = 0 takeprofit = 0 elif position == -1: stoploss = priceclose + 1.05 * atr takeprofit = priceclose - 1.45 * atr # amount = priceclose / (stoploss - priceclose) if (stoploss - priceclose) * amount < 0.1: position = 0 amount = 0 stoploss = 0 takeprofit = 0 else: stoploss = 0 takeprofit = 0 amount = 0 if position * pattern > 0: confidence = abs(pattern) elif position * pattern < 0: confidence = abs(1 / pattern) elif position == 0: confidence = 0 else: confidence = 1 # ##FOR TEST # position = 1 return [position, amount, priceclose, stoploss, takeprofit, confidence]
def macd200(self, df, pattern, patterntype): #1. Calculate ATR for potential trade atr = atrcalc.ATRcalc(df) #####PLACEHOLDER # df = pd.read_csv('./database/AAPL.csv') #####END_PLACEHOLDER df = df.dropna() ###1. Getting Parameters ##b. MACD macdInput = df.head(34) macdInput = macdInput.iloc[::-1] MACDclose = macdInput['close'].values macd, macdsignal, macdhist = MACDFIX(MACDclose, signalperiod=9) # print("MACD\n", macd[-1]) # print("Signal\n", macdsignal[-1]) ##c. DelayedMACD delayedmacdInput = df.iloc[1:].head(34) delayedmacdInput = delayedmacdInput.iloc[::-1] delayedMACDclose = delayedmacdInput['close'].values delayedmacd, delayedmacdsignal, delayedmacdhist = MACDFIX( delayedMACDclose, signalperiod=9) ##c. 200EMA emaInput = df.head(200) emaInput = emaInput.iloc[::-1] EMAclose = emaInput['close'].values ema = EMA(EMAclose, timeperiod=200) # print("EMA\n", ema[-1]) ##d. current price action priceaction = df.head(1) pricehigh = priceaction['high'].values[0] pricelow = priceaction['low'].values[0] priceclose = priceaction['close'].values[0] # print("pricehigh\n", pricehigh) # print("pricelow\n", pricelow) ###2. Analysing using the data provided ##macd-signal crossover type ## -1 means negative crossover ## 1 means positive crossover ## 0 means both if delayedmacd[-1] < delayedmacdsignal[-1] and macd[-1] > macdsignal[ -1]: crossover = 1 elif delayedmacd[-1] > delayedmacdsignal[-1] and macd[-1] < macdsignal[ -1]: crossover = -1 else: crossover = 0 ##market-ema type ## 1 means low > 200EMA ## -1 means high < 200EMA ## 0 means otherwise if pricelow > ema[-1]: marketEMA = 1 elif pricehigh < ema[-1]: marketEMA = -1 else: marketEMA = 0 ##OUTPUT if marketEMA == 1 and crossover > 0 and macd[-1] < 0: position = 1 elif marketEMA == -1 and crossover < 0 and macd[-1] > 0: position = -1 else: position = 0 amount = 50 if position == 1: stoploss = priceclose - 1.05 * atr takeprofit = priceclose + 1.45 * atr # amount = priceclose / (priceclose - stoploss) if (priceclose - stoploss) * amount < 0.1: position = 0 amount = 0 stoploss = 0 takeprofit = 0 elif position == -1: stoploss = priceclose + 1.05 * atr takeprofit = priceclose - 1.45 * atr # amount = priceclose / (stoploss - priceclose) if (stoploss - priceclose) * amount < 0.1: position = 0 amount = 0 stoploss = 0 takeprofit = 0 else: stoploss = 0 takeprofit = 0 amount = 0 ##For test # position = 1 if position * pattern > 0: confidence = abs(pattern) elif position * pattern < 0: confidence = abs(1 / pattern) elif position == 0: confidence = 0 else: confidence = 1 # ##FOR TEST # position = 1 return [position, amount, priceclose, stoploss, takeprofit, confidence]
def SMA200(self, df, pattern, patterntype): df = df.dropna() ###1. Getting Parameters ##a. current price action priceaction = df.head(1) pricehigh = priceaction['high'].values[0] pricelow = priceaction['low'].values[0] priceclose = priceaction['close'].values[0] ##b. previous price action prevaction = df.iloc[1:].head(1) prevhigh = prevaction['high'].values[0] prevlow = prevaction['low'].values[0] prevclose = prevaction['close'].values[0] ##d. current SMAs smaCurrentInput = df.head(20) sma20Current = SMA(smaCurrentInput['close'].values, timeperiod=20) smaCurrentInput = smaCurrentInput.head(10) sma10Current = SMA(smaCurrentInput['close'].values, timeperiod=10) ##e. previous SMAs smaPreviousInput = df.iloc[1:].head(20) sma20Previous = SMA(smaPreviousInput['close'].values, timeperiod=20) smaPreviousInput = smaPreviousInput.head(10) sma10Previous = SMA(smaPreviousInput['close'].values, timeperiod=10) ##f. 200EMA emaInput = df.head(200) emaInput = emaInput.iloc[::-1] EMAclose = emaInput['close'].values ema = EMA(EMAclose, timeperiod=200) ##trade conditions if sma10Previous[-1] < sma20Previous[-1] and sma10Current[ -1] > sma20Current[-1]: crossover = 1 elif sma10Previous[-1] > sma20Previous[-1] and sma10Current[ -1] < sma20Current[-1]: crossover = -1 else: crossover = 0 ## 200EMA filtering false signal if pricelow > ema[-1]: marketEMA = 1 elif pricehigh < ema[-1]: marketEMA = -1 else: marketEMA = 0 if crossover == 1 and marketEMA == 1: position = 1 elif crossover == -1 and marketEMA == -1: position = -1 else: position = 0 atr = atrcalc.ATRcalc(df) amount = 50 if position == 1: stoploss = priceclose - 1.05 * atr takeprofit = priceclose + 1.45 * atr # amount = priceclose / (priceclose - stoploss) if (priceclose - stoploss) * amount < 0.1: position = 0 amount = 0 stoploss = 0 takeprofit = 0 elif position == -1: stoploss = priceclose + 1.05 * atr takeprofit = priceclose - 1.45 * atr # amount = priceclose / (stoploss - priceclose) if (stoploss - priceclose) * amount < 0.1: position = 0 amount = 0 stoploss = 0 takeprofit = 0 else: stoploss = 0 takeprofit = 0 amount = 0 if position * pattern > 0: confidence = abs(pattern) elif position * pattern < 0: confidence = abs(1 / pattern) elif position == 0: confidence = 0 else: confidence = 1 # ##FOR TEST # position = 1 return [position, amount, priceclose, stoploss, takeprofit, confidence]
def bbands200(self, df, pattern, patterntype): df = df.dropna() #1. Calculate ATR for potential trade atr = atrcalc.ATRcalc(df) ##b. get BBands bband = df.head(21) bband = bband.iloc[1:] bband = bband.iloc[::-1] bbandInput = bband['close'].values upperband, middleband, lowerband = BBANDS(bbandInput, timeperiod=20, nbdevup=2, nbdevdn=2, matype=0) ##c. 200EMA emaInput = df.head(200) emaInput = emaInput.iloc[::-1] EMAclose = emaInput['close'].values ema = EMA(EMAclose, timeperiod=200) ##d. current price action priceaction = df.head(1) pricehigh = priceaction['high'].values[0] pricelow = priceaction['low'].values[0] priceclose = priceaction['close'].values[0] if pricehigh > upperband[-1]: breakBand = -1 elif pricelow < lowerband[-1]: breakBand = 1 else: breakBand = 0 if pricelow > ema[-1]: marketEMA = 1 elif pricehigh < ema[-1]: marketEMA = -1 else: marketEMA = 0 ##OUTPUT if marketEMA == 1 and pattern > 0 and patterntype == -1 and breakBand == 1: position = 1 elif marketEMA == -1 and pattern < 0 and patterntype == -1 and breakBand == -1: position = -1 else: position = 0 amount = 50 if position == 1: stoploss = priceclose - 1.05 * atr takeprofit = priceclose + 1.45 * atr # amount = priceclose / (priceclose - stoploss) if (stoploss - priceclose) * amount < 0.1: position = 0 amount = 0 stoploss = 0 takeprofit = 0 elif position == -1: stoploss = priceclose + 1.05 * atr takeprofit = priceclose - 1.45 * atr # amount = priceclose / (stoploss - priceclose) if (stoploss - priceclose) * amount < 0.1: position = 0 amount = 0 stoploss = 0 takeprofit = 0 else: stoploss = 0 takeprofit = 0 amount = 0 if position * pattern > 0: confidence = abs(pattern) elif position * pattern < 0: confidence = abs(1 / pattern) elif position == 0: confidence = 0 else: confidence = 1 # ##FOR TEST # position = 1 return [position, amount, priceclose, stoploss, takeprofit, confidence]
import pandas as pd from talib import MACDFIX, RSI, EMA, SAR, SMA, TRIX, BBANDS, CDLIDENTICAL3CROWS, CDL3BLACKCROWS, CDL3WHITESOLDIERS, CDLMORNINGSTAR, CDLEVENINGSTAR, CDL3LINESTRIKE, CDLMORNINGDOJISTAR, CDLEVENINGDOJISTAR, CDL3OUTSIDE, CDLENGULFING, CDLBELTHOLD, CDLABANDONEDBABY, CDL3INSIDE, CDLPIERCING, CDLDARKCLOUDCOVER, CDLBREAKAWAY, CDLXSIDEGAP3METHODS, CDLHAMMER, CDLSHOOTINGSTAR import indicators.ATRcalc as atrcalc import os #1. Calculate ATR for potential trade #####PLACEHOLDER df = pd.read_csv('./database/AAPL/temp2.csv') #####END_PLACEHOLDER #1. Calculate ATR for potential trade #####PLACEHOLDER # df = pd.read_csv('./database/AAPL.csv') #####END_PLACEHOLDER df = df.dropna() #1. Calculate ATR for potential trade atr = atrcalc.ATRcalc(df) ## a. checkpatterns cdlInput = df.head(20) cdlInput = cdlInput.iloc[::-1] aa = cdlInput['open'].values ab = cdlInput['high'].values ac = cdlInput['low'].values ad = cdlInput['close'].values outputI3C = CDLIDENTICAL3CROWS(aa, ab, ac, ad) output3BC = CDL3BLACKCROWS(aa, ab, ac, ad) output3WS = CDL3WHITESOLDIERS(aa, ab, ac, ad) outputMS = CDLMORNINGSTAR(aa, ab, ac, ad) outputES = CDLEVENINGSTAR(aa, ab, ac, ad) output3LS = CDL3LINESTRIKE(aa, ab, ac, ad) outputMDS = CDLMORNINGDOJISTAR(aa, ab, ac, ad)