def handler(event, context): # DynamoDb dynamodb = boto3.resource('dynamodb') tableName = os.environ['DATABASE'] table = dynamodb.Table(tableName) # Get stock data stock = yf.Ticker(event['symbol']) if (event['date']): endDate = datetime.strptime(event['date'], "%Y-%m-%d") else: endDate = datetime.utcnow() startDate = endDate - timedelta(weeks=260) stock_data = stock.history(end=endDate, start=startDate, interval="5d") # Calculate impulse system macd_hist = MACD(stock_data["Close"], fillna=True).macd_diff() ema = EMAIndicator(stock_data["Close"], n=13, fillna=True).ema_indicator() shouldBeGreen = ema.iloc[-1] > ema.iloc[-2] and macd_hist.iloc[ -1] > macd_hist.iloc[-2] shouldBeRed = ema.iloc[-1] < ema.iloc[-2] and macd_hist.iloc[ -1] < macd_hist.iloc[-2] color = 'green' if shouldBeGreen else 'red' if shouldBeRed else 'blue' dateString = endDate.strftime("%Y-%m-%d") id = str(uuid.uuid4()) entry = { 'symbol': event['symbol'], 'security': event['security'], 'sector': event['sector'], 'id': id, 'date': dateString, 'weekly': { 'type': 'impulse_system', 'result': color, } } table.put_item(Item=entry) return entry
def start_test(count): cancel_all('all') client = build_client() if count < 1: count += 1 symbols = ['CRBP', 'QD', 'WPG', 'ANY'] for symbol in symbols: ph = client.get_price_history( symbol=symbol, period_type=client.PriceHistory.PeriodType.DAY, period=client.PriceHistory.Period.ONE_DAY, frequency_type=client.PriceHistory.FrequencyType.MINUTE, frequency=client.PriceHistory.Frequency.EVERY_FIFTEEN_MINUTES, start_datetime=None, end_datetime=None, need_extended_hours_data=None).json() print(ph['symbol']) df = pd.json_normalize(ph, 'candles') # pprint(len(df)) # pprint(df) # pprint(df['close']) # pprint(df['high']) # pprint(df['low']) # pprint(df['volume']) raw_vwap = VolumeWeightedAveragePrice(high=df['high'], low=df['low'], close=df['close'], volume=df['volume'], window=14) raw_ema = EMAIndicator(close=df['close'], window=9) print("vwap:") pprint(raw_vwap.vwap[len(raw_vwap.vwap) - 1]) print("ema:") pprint(raw_ema._close[len(raw_ema._close) - 1]) print("len ema:") print(len(raw_ema._close))
def get_ema(data, days): """ Function to get the EMA of a ticker """ ema = EMAIndicator(close=data["c"], n=days) return ema.ema_indicator()
def __init__(self, symbols): # data = json.loads(symbols) # df_stock = pd.json_normalize(symbols) # df_stock = pd.read_csv(fn,names = ['sym']).drop_duplicates() df_stock = pd.DataFrame(symbols) ls_stock = df_stock['sym'].to_list() df_stock = df_stock.reset_index() df_stock.columns = ['sort', 'sym'] df_stock.head() # In[3]: start = dt.date.today() + relativedelta(days=-150) end = dt.date.today() + relativedelta(days=-0) ls_tickers = ls_stock ls_df = [] for ticker in ls_tickers: try: df = web.DataReader(ticker, 'yahoo', start, end) except Exception as e: print(str(e)) continue df['sym'] = ticker ls_df.append(df.copy()) df_price = pd.concat(ls_df).reset_index() df_price.columns = [ 'dte', 'hgh', 'low', 'opn', 'cls', 'vol', 'cls_adj', 'sym' ] df_price.sort_values(['sym', 'dte'], inplace=True) df_price = df_price[['dte', 'sym', 'hgh', 'low', 'cls', 'vol']].copy() df_price['curr'] = end df_price['curr'] = pd.to_datetime(df_price['curr']) df_price['dte'] = pd.to_datetime(df_price['dte']) df_price['ndays'] = (df_price['curr'] - df_price['dte']).dt.days df_price['ndays'] = df_price.groupby(['sym'])['ndays'].rank() df_price[df_price['sym'] == 'SPY'].head() # In[4]: ls_df = [] ls_tickers = ls_stock for ticker in ls_tickers: #df = dropna(df_price[df_price['sym']==ticker]) df = df_price[df_price['sym'] == ticker].copy() indicator_bb = BollingerBands(close=df['cls'], window=20, window_dev=2) indicator_macd = MACD(close=df['cls'], window_fast=12, window_slow=26, window_sign=9) indicator_rsi14 = RSIIndicator(close=df['cls'], window=14) indicator_cci20 = cci(high=df['hgh'], low=df['low'], close=df['cls'], window=20, constant=0.015) indicator_obv = OnBalanceVolumeIndicator(close=df['cls'], volume=df['vol'], fillna=True) indicator_vol_sma20 = SMAIndicator(close=df['vol'], window=20) indicator_ema03 = EMAIndicator(close=df['cls'], window=3) indicator_ema05 = EMAIndicator(close=df['cls'], window=5) indicator_ema08 = EMAIndicator(close=df['cls'], window=8) indicator_ema10 = EMAIndicator(close=df['cls'], window=10) indicator_ema12 = EMAIndicator(close=df['cls'], window=12) indicator_ema15 = EMAIndicator(close=df['cls'], window=15) indicator_ema30 = EMAIndicator(close=df['cls'], window=30) indicator_ema35 = EMAIndicator(close=df['cls'], window=35) indicator_ema40 = EMAIndicator(close=df['cls'], window=40) indicator_ema45 = EMAIndicator(close=df['cls'], window=45) indicator_ema50 = EMAIndicator(close=df['cls'], window=50) indicator_ema60 = EMAIndicator(close=df['cls'], window=60) # Add Bollinger Band high indicator df['bb_bbhi'] = indicator_bb.bollinger_hband_indicator() # Add Bollinger Band low indicator df['bb_bbli'] = indicator_bb.bollinger_lband_indicator() #df['macd'] = indicator_macd.macd() df['macd'] = indicator_macd.macd_diff() #df['macd_signal'] = indicator_macd.macd_signal() df['obv'] = indicator_obv.on_balance_volume() df['vol_sma20'] = indicator_vol_sma20.sma_indicator() df['ema03'] = indicator_ema03.ema_indicator() df['ema05'] = indicator_ema05.ema_indicator() df['ema08'] = indicator_ema08.ema_indicator() df['ema10'] = indicator_ema10.ema_indicator() df['ema12'] = indicator_ema12.ema_indicator() df['ema15'] = indicator_ema15.ema_indicator() df['ema30'] = indicator_ema30.ema_indicator() df['ema35'] = indicator_ema35.ema_indicator() df['ema40'] = indicator_ema40.ema_indicator() df['ema45'] = indicator_ema45.ema_indicator() df['ema50'] = indicator_ema50.ema_indicator() df['ema60'] = indicator_ema60.ema_indicator() df['rsi14'] = indicator_rsi14.rsi() df['cci20'] = indicator_cci20 ls_df.append(df.copy()) df = pd.concat(ls_df) df['score_vol_sma20'] = df[['vol', 'vol_sma20']].apply(lambda x: x[0] / x[1], axis=1) df['emash_min'] = df[[ 'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15' ]].min(axis=1) df['emash_max'] = df[[ 'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15' ]].max(axis=1) df['emash_avg'] = df[[ 'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15' ]].mean(axis=1) #df['score_short'] = df[['cls','emash_min','emash_max','emash_min']].apply(lambda x: 100 * (x[0]-x[1])/(x[2]-x[3]),axis=1) df['emalg_min'] = df[[ 'ema30', 'ema35', 'ema40', 'ema45', 'ema50', 'ema60' ]].min(axis=1) df['emalg_max'] = df[[ 'ema30', 'ema35', 'ema40', 'ema45', 'ema50', 'ema60' ]].max(axis=1) df['emalg_avg'] = df[[ 'ema30', 'ema35', 'ema40', 'ema45', 'ema50', 'ema60' ]].mean(axis=1) #df['score_long'] = df[['cls','emalg_min','emalg_max','emalg_min']].apply(lambda x: 100 * (x[0]-x[1])/(x[2]-x[3]),axis=1) df['ema_min'] = df[[ 'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15', 'ema30', 'ema35', 'ema40', 'ema45', 'ema50', 'ema60' ]].min(axis=1) df['ema_max'] = df[[ 'ema03', 'ema05', 'ema08', 'ema10', 'ema12', 'ema15', 'ema30', 'ema35', 'ema40', 'ema45', 'ema50', 'ema60' ]].max(axis=1) df['score_ovlp_ema'] = df[[ 'emash_min', 'emalg_max', 'ema_max', 'ema_min' ]].apply(lambda x: 100 * (x[0] - x[1]) / (x[2] - x[3]), axis=1) df = pd.merge(df_stock, df, on=['sym'], how='inner').sort_values(['sort', 'ndays']) decimals = pd.Series([1, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0], index=[ 'cls', 'ndays', 'vol', 'score_vol_sma20', 'bb_bbhi', 'bb_bbli', 'macd', 'obv', 'rsi14', 'cci20', 'score_ovlp_ema' ]) cols = [ 'ndays', 'dte', 'sort', 'sym', 'cls', 'vol', 'score_vol_sma20', 'bb_bbhi', 'bb_bbli', 'macd', 'obv', 'rsi14', 'cci20', 'score_ovlp_ema' ] df = df[df['ndays'] <= 10][cols].round(decimals).copy() print(df['score_ovlp_ema'].min(), df['score_ovlp_ema'].max()) df[df['sym'] == 'QQQ'].head(50) self.df = df
def handler(event, context): # DynamoDb dynamodb = boto3.resource('dynamodb') tableName = os.environ['DATABASE'] table = dynamodb.Table(tableName) # Get stock data stock = yf.Ticker('^GSPC') stock_data_weekly = stock.history(period="5y", interval="5d") stock_data_daily = stock.history(period="2y", interval="1d") # Calculate impulse system weekly macd_hist = MACD(stock_data_weekly["Close"], fillna=True).macd_diff() ema = EMAIndicator( stock_data_weekly["Close"], n=13, fillna=True).ema_indicator() shouldBeGreen = ema.iloc[-1] > ema.iloc[-2] and macd_hist.iloc[-1] > macd_hist.iloc[-2] shouldBeRed = ema.iloc[-1] < ema.iloc[-2] and macd_hist.iloc[-1] < macd_hist.iloc[-2] colorWeekly = 'green' if shouldBeGreen else 'red' if shouldBeRed else 'blue' # Calculate impulse system daily macd_hist = MACD(stock_data_daily["Close"], fillna=True).macd_diff() ema = EMAIndicator( stock_data_daily["Close"], n=13, fillna=True).ema_indicator() shouldBeGreen = ema.iloc[-1] > ema.iloc[-2] and macd_hist.iloc[-1] > macd_hist.iloc[-2] shouldBeRed = ema.iloc[-1] < ema.iloc[-2] and macd_hist.iloc[-1] < macd_hist.iloc[-2] colorDaily = 'green' if shouldBeGreen else 'red' if shouldBeRed else 'blue' # Caculate ForceIndex 13 days indicator_fi = ForceIndexIndicator( stock_data_daily["Close"], stock_data_daily["Volume"], 13, fillna=True).force_index() lastForceIndexValue = indicator_fi.iloc[-1] lastFIDecimal = Decimal(lastForceIndexValue) lastFI = round(lastFIDecimal, 0) lastCloseData = stock_data_daily.iloc[-1]["Close"] lastCloseDecimal = Decimal(lastCloseData) lastClose = round(lastCloseDecimal, 2) # daily timestamp date = datetime.utcnow() future = date + timedelta(days=14) expiryDate = round(future.timestamp() * 1000) dateString = date.strftime("%Y-%m-%d") id = str(uuid.uuid4()) entry = { 'symbol': '^GSPC', 'id': id, 'date': dateString, 'ttl': expiryDate, 'weeklyImpulse': colorWeekly, 'dailyImpulse': colorDaily, 'forceIndex13': lastFI, 'lastClose': lastClose, } table.put_item(Item=entry) return entry
def add_trend_ta( df: pd.DataFrame, high: str, low: str, close: str, fillna: bool = False, colprefix: str = "", vectorized: bool = False, ) -> pd.DataFrame: """Add trend technical analysis features to dataframe. Args: df (pandas.core.frame.DataFrame): Dataframe base. high (str): Name of 'high' column. low (str): Name of 'low' column. close (str): Name of 'close' column. fillna(bool): if True, fill nan values. colprefix(str): Prefix column names inserted vectorized(bool): if True, use only vectorized functions indicators Returns: pandas.core.frame.DataFrame: Dataframe with new features. """ # MACD indicator_macd = MACD(close=df[close], window_slow=26, window_fast=12, window_sign=9, fillna=fillna) df[f"{colprefix}trend_macd"] = indicator_macd.macd() df[f"{colprefix}trend_macd_signal"] = indicator_macd.macd_signal() df[f"{colprefix}trend_macd_diff"] = indicator_macd.macd_diff() # SMAs df[f"{colprefix}trend_sma_fast"] = SMAIndicator( close=df[close], window=12, fillna=fillna).sma_indicator() df[f"{colprefix}trend_sma_slow"] = SMAIndicator( close=df[close], window=26, fillna=fillna).sma_indicator() # EMAs df[f"{colprefix}trend_ema_fast"] = EMAIndicator( close=df[close], window=12, fillna=fillna).ema_indicator() df[f"{colprefix}trend_ema_slow"] = EMAIndicator( close=df[close], window=26, fillna=fillna).ema_indicator() # Vortex Indicator indicator_vortex = VortexIndicator(high=df[high], low=df[low], close=df[close], window=14, fillna=fillna) df[f"{colprefix}trend_vortex_ind_pos"] = indicator_vortex.vortex_indicator_pos( ) df[f"{colprefix}trend_vortex_ind_neg"] = indicator_vortex.vortex_indicator_neg( ) df[f"{colprefix}trend_vortex_ind_diff"] = indicator_vortex.vortex_indicator_diff( ) # TRIX Indicator df[f"{colprefix}trend_trix"] = TRIXIndicator(close=df[close], window=15, fillna=fillna).trix() # Mass Index df[f"{colprefix}trend_mass_index"] = MassIndex(high=df[high], low=df[low], window_fast=9, window_slow=25, fillna=fillna).mass_index() # DPO Indicator df[f"{colprefix}trend_dpo"] = DPOIndicator(close=df[close], window=20, fillna=fillna).dpo() # KST Indicator indicator_kst = KSTIndicator( close=df[close], roc1=10, roc2=15, roc3=20, roc4=30, window1=10, window2=10, window3=10, window4=15, nsig=9, fillna=fillna, ) df[f"{colprefix}trend_kst"] = indicator_kst.kst() df[f"{colprefix}trend_kst_sig"] = indicator_kst.kst_sig() df[f"{colprefix}trend_kst_diff"] = indicator_kst.kst_diff() # Ichimoku Indicator indicator_ichi = IchimokuIndicator( high=df[high], low=df[low], window1=9, window2=26, window3=52, visual=False, fillna=fillna, ) df[f"{colprefix}trend_ichimoku_conv"] = indicator_ichi.ichimoku_conversion_line( ) df[f"{colprefix}trend_ichimoku_base"] = indicator_ichi.ichimoku_base_line() df[f"{colprefix}trend_ichimoku_a"] = indicator_ichi.ichimoku_a() df[f"{colprefix}trend_ichimoku_b"] = indicator_ichi.ichimoku_b() # Schaff Trend Cycle (STC) df[f"{colprefix}trend_stc"] = STCIndicator( close=df[close], window_slow=50, window_fast=23, cycle=10, smooth1=3, smooth2=3, fillna=fillna, ).stc() if not vectorized: # Average Directional Movement Index (ADX) indicator_adx = ADXIndicator(high=df[high], low=df[low], close=df[close], window=14, fillna=fillna) df[f"{colprefix}trend_adx"] = indicator_adx.adx() df[f"{colprefix}trend_adx_pos"] = indicator_adx.adx_pos() df[f"{colprefix}trend_adx_neg"] = indicator_adx.adx_neg() # CCI Indicator df[f"{colprefix}trend_cci"] = CCIIndicator( high=df[high], low=df[low], close=df[close], window=20, constant=0.015, fillna=fillna, ).cci() # Ichimoku Visual Indicator indicator_ichi_visual = IchimokuIndicator( high=df[high], low=df[low], window1=9, window2=26, window3=52, visual=True, fillna=fillna, ) df[f"{colprefix}trend_visual_ichimoku_a"] = indicator_ichi_visual.ichimoku_a( ) df[f"{colprefix}trend_visual_ichimoku_b"] = indicator_ichi_visual.ichimoku_b( ) # Aroon Indicator indicator_aroon = AroonIndicator(close=df[close], window=25, fillna=fillna) df[f"{colprefix}trend_aroon_up"] = indicator_aroon.aroon_up() df[f"{colprefix}trend_aroon_down"] = indicator_aroon.aroon_down() df[f"{colprefix}trend_aroon_ind"] = indicator_aroon.aroon_indicator() # PSAR Indicator indicator_psar = PSARIndicator( high=df[high], low=df[low], close=df[close], step=0.02, max_step=0.20, fillna=fillna, ) # df[f'{colprefix}trend_psar'] = indicator.psar() df[f"{colprefix}trend_psar_up"] = indicator_psar.psar_up() df[f"{colprefix}trend_psar_down"] = indicator_psar.psar_down() df[f"{colprefix}trend_psar_up_indicator"] = indicator_psar.psar_up_indicator( ) df[f"{colprefix}trend_psar_down_indicator"] = indicator_psar.psar_down_indicator( ) return df
try: insert_list = list() # get all the prices for equity ticker_eod_df = eod_df[eod_df['equity_id'] == equity_id] ticker_eod_df = ticker_eod_df.sort_values(by=['trading_date']) df = dropna(ticker_eod_df) # Initialize Bollinger Bands Indicator indicator_bb = BollingerBands(close=df["adj_close"], window=20, window_dev=2) # Add Bollinger Bands features df['bb_bbm'] = indicator_bb.bollinger_mavg() df['bb_bbh'] = indicator_bb.bollinger_hband() df['bb_bbl'] = indicator_bb.bollinger_lband() # EMA Indicator indicator_ema_200 = EMAIndicator(close=df["adj_close"], window=200) df['ema_200'] = indicator_ema_200.ema_indicator() indicator_ema_100 = EMAIndicator(close=df["adj_close"], window=100) df['ema_100'] = indicator_ema_100.ema_indicator() indicator_ema_50 = EMAIndicator(close=df["adj_close"], window=50) df['ema_50'] = indicator_ema_50.ema_indicator() # SMA Indicator indicator_sma_200 = SMAIndicator(close=df["adj_close"], window=200) df['sma_200'] = indicator_sma_200.sma_indicator() indicator_sma_100 = SMAIndicator(close=df["adj_close"], window=100) df['sma_100'] = indicator_sma_100.sma_indicator() indicator_sma_50 = SMAIndicator(close=df["adj_close"], window=50) df['sma_50'] = indicator_sma_50.sma_indicator() # RSI Indicator
def applyIndicator(self, full_company_price): self.data = full_company_price high = self.data['high'] low = self.data['low'] close = self.data['close'] volume = self.data['volume'] EMA12 = EMAIndicator(close, 12, fillna=False) EMA30 = EMAIndicator(close, 20, fillna=False) EMA60 = EMAIndicator(close, 60, fillna=False) MACD1226 = MACD(close, 26, 12, 9, fillna=False) MACD2452 = MACD(close, 52, 24, 18, fillna=False) ROC12 = ROCIndicator(close, 12, fillna=False) ROC30 = ROCIndicator(close, 30, fillna=False) ROC60 = ROCIndicator(close, 60, fillna=False) RSI14 = RSIIndicator(close, 14, fillna=False) RSI28 = RSIIndicator(close, 28, fillna=False) RSI60 = RSIIndicator(close, 60, fillna=False) AROON25 = AroonIndicator(close, 25, fillna=False) AROON50 = AroonIndicator(close, 50, fillna=False) AROON80 = AroonIndicator(close, 80, fillna=False) MFI14 = MFIIndicator(high, low, close, volume, 14, fillna=False) MFI28 = MFIIndicator(high, low, close, volume, 28, fillna=False) MFI80 = MFIIndicator(high, low, close, volume, 80, fillna=False) CCI20 = CCIIndicator(high, low, close, 20, 0.015, fillna=False) CCI40 = CCIIndicator(high, low, close, 40, 0.015, fillna=False) CCI100 = CCIIndicator(high, low, close, 100, 0.015, fillna=False) WILLR14 = WilliamsRIndicator(high, low, close, 14, fillna=False) WILLR28 = WilliamsRIndicator(high, low, close, 28, fillna=False) WILLR60 = WilliamsRIndicator(high, low, close, 60, fillna=False) BBANDS20 = BollingerBands(close, 20, 2, fillna=False) KC20 = KeltnerChannel(high, low, close, 20, 10, fillna=False) STOCH14 = StochasticOscillator(high, low, close, 14, 3, fillna=False) STOCH28 = StochasticOscillator(high, low, close, 28, 6, fillna=False) STOCH60 = StochasticOscillator(high, low, close, 60, 12, fillna=False) CMI20 = ChaikinMoneyFlowIndicator(high, low, close, volume, 20, fillna=False) CMI40 = ChaikinMoneyFlowIndicator(high, low, close, volume, 40, fillna=False) CMI100 = ChaikinMoneyFlowIndicator(high, low, close, volume, 100, fillna=False) self.data['ema12'] = (close - EMA12.ema_indicator()) / close self.data['ema30'] = (close - EMA30.ema_indicator()) / close self.data['ema60'] = (close - EMA60.ema_indicator()) / close self.data['macd1226'] = MACD1226.macd() - MACD1226.macd_signal() self.data['macd2452'] = MACD2452.macd() - MACD2452.macd_signal() self.data['roc12'] = ROC12.roc() self.data['roc30'] = ROC30.roc() self.data['roc60'] = ROC60.roc() self.data['rsi14'] = RSI14.rsi() self.data['rsi28'] = RSI28.rsi() self.data['rsi60'] = RSI60.rsi() self.data['aroon25'] = AROON25.aroon_indicator() self.data['aroon50'] = AROON50.aroon_indicator() self.data['aroon80'] = AROON80.aroon_indicator() self.data['mfi14'] = MFI14.money_flow_index() self.data['mfi28'] = MFI28.money_flow_index() self.data['mfi80'] = MFI80.money_flow_index() self.data['cci20'] = CCI20.cci() self.data['cci40'] = CCI40.cci() self.data['cci100'] = CCI100.cci() self.data['willr14'] = WILLR14.wr() self.data['willr28'] = WILLR28.wr() self.data['willr60'] = WILLR60.wr() self.data['bband20up'] = (BBANDS20.bollinger_hband() - close) / close self.data['bband20down'] = (close - BBANDS20.bollinger_lband()) / close self.data['stoch14'] = STOCH14.stoch() self.data['stoch28'] = STOCH28.stoch() self.data['stoch60'] = STOCH60.stoch() self.data['cmi20'] = CMI20.chaikin_money_flow() self.data['cmi40'] = CMI40.chaikin_money_flow() self.data['cmi100'] = CMI100.chaikin_money_flow() self.data['kc20up'] = (KC20.keltner_channel_hband() - close) / close self.data['kc20down'] = (close - KC20.keltner_channel_lband()) / close return self.data
def add_trend_ta(df: pd.DataFrame, high: str, low: str, close: str, fillna: bool = False, colprefix: str = ""): """Add trend technical analysis features to dataframe. Args: df (pandas.core.frame.DataFrame): Dataframe base. high (str): Name of 'high' column. low (str): Name of 'low' column. close (str): Name of 'close' column. fillna(bool): if True, fill nan values. colprefix(str): Prefix column names inserted Returns: pandas.core.frame.DataFrame: Dataframe with new features. """ # MACD indicator_macd = MACD(close=df[close], n_fast=12, n_slow=26, n_sign=9, fillna=fillna) df[f'{colprefix}trend_macd'] = indicator_macd.macd() df[f'{colprefix}trend_macd_signal'] = indicator_macd.macd_signal() df[f'{colprefix}trend_macd_diff'] = indicator_macd.macd_diff() # EMAs df[f'{colprefix}trend_ema_fast'] = EMAIndicator( close=df[close], n=12, fillna=fillna).ema_indicator() df[f'{colprefix}trend_ema_slow'] = EMAIndicator( close=df[close], n=26, fillna=fillna).ema_indicator() # Average Directional Movement Index (ADX) indicator = ADXIndicator(high=df[high], low=df[low], close=df[close], n=14, fillna=fillna) df[f'{colprefix}trend_adx'] = indicator.adx() df[f'{colprefix}trend_adx_pos'] = indicator.adx_pos() df[f'{colprefix}trend_adx_neg'] = indicator.adx_neg() # Vortex Indicator indicator = VortexIndicator(high=df[high], low=df[low], close=df[close], n=14, fillna=fillna) df[f'{colprefix}trend_vortex_ind_pos'] = indicator.vortex_indicator_pos() df[f'{colprefix}trend_vortex_ind_neg'] = indicator.vortex_indicator_neg() df[f'{colprefix}trend_vortex_ind_diff'] = indicator.vortex_indicator_diff() # TRIX Indicator indicator = TRIXIndicator(close=df[close], n=15, fillna=fillna) df[f'{colprefix}trend_trix'] = indicator.trix() # Mass Index indicator = MassIndex(high=df[high], low=df[low], n=9, n2=25, fillna=fillna) df[f'{colprefix}trend_mass_index'] = indicator.mass_index() # CCI Indicator indicator = CCIIndicator(high=df[high], low=df[low], close=df[close], n=20, c=0.015, fillna=fillna) df[f'{colprefix}trend_cci'] = indicator.cci() # DPO Indicator indicator = DPOIndicator(close=df[close], n=20, fillna=fillna) df[f'{colprefix}trend_dpo'] = indicator.dpo() # KST Indicator indicator = KSTIndicator(close=df[close], r1=10, r2=15, r3=20, r4=30, n1=10, n2=10, n3=10, n4=15, nsig=9, fillna=fillna) df[f'{colprefix}trend_kst'] = indicator.kst() df[f'{colprefix}trend_kst_sig'] = indicator.kst_sig() df[f'{colprefix}trend_kst_diff'] = indicator.kst_diff() # Ichimoku Indicator indicator = IchimokuIndicator(high=df[high], low=df[low], n1=9, n2=26, n3=52, visual=False, fillna=fillna) df[f'{colprefix}trend_ichimoku_a'] = indicator.ichimoku_a() df[f'{colprefix}trend_ichimoku_b'] = indicator.ichimoku_b() indicator = IchimokuIndicator(high=df[high], low=df[low], n1=9, n2=26, n3=52, visual=True, fillna=fillna) df[f'{colprefix}trend_visual_ichimoku_a'] = indicator.ichimoku_a() df[f'{colprefix}trend_visual_ichimoku_b'] = indicator.ichimoku_b() # Aroon Indicator indicator = AroonIndicator(close=df[close], n=25, fillna=fillna) df[f'{colprefix}trend_aroon_up'] = indicator.aroon_up() df[f'{colprefix}trend_aroon_down'] = indicator.aroon_down() df[f'{colprefix}trend_aroon_ind'] = indicator.aroon_indicator() # PSAR Indicator indicator = PSARIndicator(high=df[high], low=df[low], close=df[close], step=0.02, max_step=0.20, fillna=fillna) df[f'{colprefix}trend_psar'] = indicator.psar() df[f'{colprefix}trend_psar_up'] = indicator.psar_up() df[f'{colprefix}trend_psar_down'] = indicator.psar_down() df[f'{colprefix}trend_psar_up_indicator'] = indicator.psar_up_indicator() df[f'{colprefix}trend_psar_down_indicator'] = indicator.psar_down_indicator( ) return df
def getDailyData(self, symbol, since, debug=False): #try: print("GET IBKR INTRADAY DATA") now = datetime.now() print("CREATE CONTRACT FOR %s" % symbol) contract = IBMarket.conn.createStockContract(symbol) dirname = os.path.dirname(__file__) filename = "data/daily/" + symbol + "_" + now.strftime( "%Y_%m_%d") + "_1_day.csv" if os.path.isfile(filename) == True: os.remove(filename) print("REQUEST HISTORICAL DATA FOR %s SINCE %s" % (symbol, since)) IBMarket.market_data_available[symbol] = False IBMarket.conn.requestHistoricalData(contract, rth=self.rth, resolution="1 day", lookback="12 M", csv_path='data/daily/') print("WAIT FOR DATA...") while IBMarket.market_data_available[symbol] == False: time.sleep(5) IBMarket.market_data_available[symbol] = False print("RENAME CSV FILE TO %s" % filename) os.rename("data/daily/" + symbol + ".csv", filename) print("READ CSV FILE:%s" % filename) self.data = pd.read_csv(filename, index_col='datetime', parse_dates=True) self.data = self.data.sort_index() if debug: print("===VALUES===") print(self.data) print("===CALCULATE ALL TA===") self.data = ta.utils.dropna(self.data) self.data['sma20'] = SMAIndicator(close=self.data['C'], n=20, fillna=True).sma_indicator() self.data['sma50'] = SMAIndicator(close=self.data['C'], n=50, fillna=True).sma_indicator() self.data['sma100'] = SMAIndicator(close=self.data['C'], n=100, fillna=True).sma_indicator() self.data['sma200'] = SMAIndicator(close=self.data['C'], n=200, fillna=True).sma_indicator() self.data['ema9'] = EMAIndicator(close=self.data['C'], n=9, fillna=True).ema_indicator() self.data['ema20'] = EMAIndicator(close=self.data['C'], n=20, fillna=True).ema_indicator() self.data = ta.add_all_ta_features(self.data, open="O", high="H", low="L", close="C", volume="V", fillna=True) self.data = self.data.loc[self.data.index >= since] print("===CALCULATE DOJI===") candle_names = talib.get_function_groups()['Pattern Recognition'] included_items = ('CDLDOJI', 'CDLHAMMER', 'CDLEVENINGSTAR', 'CDLHANGINGMAN', 'CDLSHOOTINGSTAR') candle_names = [ candle for candle in candle_names if candle in included_items ] for candle in candle_names: self.data[candle] = getattr(talib, candle)(self.data['O'], self.data['H'], self.data['L'], self.data['C']) if debug: print("===VALUES===") print(self.data[[ 'C', 'V', 'WAP', 'sma20', 'sma50', 'sma100', 'sma200', 'momentum_rsi', 'trend_cci', 'momentum_stoch_signal', 'trend_adx' ]]) #except: # print("ERROR GETTING MARKET DATA") # time.sleep(60) # self.data = None #30 request / 10 mins -> need to wait 20 secs time.sleep(20) return self.data
def getIntraDayData(self, symbol, since, period, live=False, debug=False): #try: print("GET IBKR INTRADAY DATA") if live == True: lookback = int(period * 1600 / 60 / 24) else: dtime = datetime.now() - datetime.strptime(since, '%Y-%m-%d') #lookback = dtime.total_seconds() / 60 / 60 / 24 lookback = 7 now = datetime.now() print("CREATE CONTRACT FOR %s" % symbol) contract = IBMarket.conn.createStockContract(symbol) dirname = os.path.dirname(__file__) filename = "data/intraday/" + symbol + "_" + now.strftime( "%Y_%m_%d") + "_" + str(period) + "_min.csv" if os.path.isfile(filename) == True: os.remove(filename) print( "REQUEST HISTORICAL DATA FOR %s, LOOKBACK %d DAYS, SINCE %s, RTH:%d" % (symbol, lookback, since, self.rth)) IBMarket.market_data_available[symbol] = False IBMarket.conn.requestHistoricalData(contract, rth=self.rth, resolution=str(period) + " mins", lookback=str(lookback) + " D", csv_path='data/intraday/') print("WAIT FOR DATA...") while IBMarket.market_data_available[symbol] == False: time.sleep(5) IBMarket.market_data_available[symbol] = False print("RENAME CSV FILE") os.rename("data/intraday/" + symbol + ".csv", filename) print("READ CSV DATA") self.data = pd.read_csv(filename, index_col='datetime', parse_dates=True) self.data = self.data.sort_index() self.data = self.data[:-1] if debug: print("===VALUES===") print(self.data) print("===CALCULATE SMA===") # add trend indicator. #self.data['sma20'] = self.data['C'].rolling(20, min_periods=20).mean() #self.data['sma50'] = self.data['C'].rolling(50, min_periods=50).mean() #self.data['sma100'] = self.data['C'].rolling(100, min_periods=100).mean() #self.data['sma200'] = self.data['C'].rolling(200, min_periods=200).mean() print("===CALCULATE ALL TA===") self.data = ta.utils.dropna(self.data) self.data['sma20'] = SMAIndicator(close=self.data['C'], n=20, fillna=True).sma_indicator() self.data['sma50'] = SMAIndicator(close=self.data['C'], n=50, fillna=True).sma_indicator() self.data['sma100'] = SMAIndicator(close=self.data['C'], n=100, fillna=True).sma_indicator() self.data['sma200'] = SMAIndicator(close=self.data['C'], n=200, fillna=True).sma_indicator() self.data['ema9'] = EMAIndicator(close=self.data['C'], n=9, fillna=True).ema_indicator() self.data['ema20'] = EMAIndicator(close=self.data['C'], n=20, fillna=True).ema_indicator() self.data = ta.add_all_ta_features(self.data, open="O", high="H", low="L", close="C", volume="V", fillna=True) self.data['mvwap'] = self.data['volume_vwap'].rolling( 14, min_periods=14).mean() self.data = self.data.loc[self.data.index >= since] print("===CALCULATE DOJI===") candle_names = talib.get_function_groups()['Pattern Recognition'] if debug: print(candle_names) included_items = ('CDLDOJI', 'CDLHAMMER', 'CDLEVENINGSTAR', 'CDLHANGINGMAN', 'CDLSHOOTINGSTAR') candle_names = [ candle for candle in candle_names if candle in included_items ] for candle in candle_names: self.data[candle] = getattr(talib, candle)(self.data['O'], self.data['H'], self.data['L'], self.data['C']) if debug: print("===VALUES===") print(self.data) #except: # print("ERROR GETTING MARKET DATA") # time.sleep(60) # self.data = None return self.data
def handle(self, *args, **options): # import pdb # pdb.set_trace() if not options['update']: NSETechnical.objects.all().delete() symbols = Symbol.objects.all() for symbol in symbols: nse_history_data = NSEHistoricalData.objects.filter( symbol__symbol_name=symbol).order_by('timestamp') if not nse_history_data: continue nse_technical = pd.DataFrame( list( nse_history_data.values('timestamp', 'open', 'high', 'low', 'close', 'total_traded_quantity'))) ''' Moving average convergence divergence ''' indicator_macd = MACD(close=nse_technical['close'], window_slow=26, window_fast=12, window_sign=9, fillna=False) nse_technical["trend_macd"] = indicator_macd.macd() nse_technical["trend_macd_signal"] = indicator_macd.macd_signal() nse_technical["trend_macd_diff"] = indicator_macd.macd_diff() ''' Simple Moving Average ''' nse_technical["trend_sma_fast"] = SMAIndicator( close=nse_technical['close'], window=12, fillna=False).sma_indicator() nse_technical["trend_sma_slow"] = SMAIndicator( close=nse_technical['close'], window=26, fillna=False).sma_indicator() ''' Exponential Moving Average ''' nse_technical["trend_ema_fast"] = EMAIndicator( close=nse_technical['close'], window=12, fillna=False).ema_indicator() nse_technical["trend_ema_slow"] = EMAIndicator( close=nse_technical['close'], window=26, fillna=False).ema_indicator() ''' Ichimoku Indicator ''' indicator_ichi = IchimokuIndicator( high=nse_technical['high'], low=nse_technical['low'], window1=9, window2=26, window3=52, visual=False, fillna=False, ) nse_technical[ "trend_ichimoku_conv"] = indicator_ichi.ichimoku_conversion_line( ) nse_technical[ "trend_ichimoku_base"] = indicator_ichi.ichimoku_base_line() nse_technical["trend_ichimoku_a"] = indicator_ichi.ichimoku_a() nse_technical["trend_ichimoku_b"] = indicator_ichi.ichimoku_b() indicator_ichi_visual = IchimokuIndicator( high=nse_technical['high'], low=nse_technical['low'], window1=9, window2=26, window3=52, visual=True, fillna=False, ) nse_technical[ "trend_visual_ichimoku_a"] = indicator_ichi_visual.ichimoku_a( ) nse_technical[ "trend_visual_ichimoku_b"] = indicator_ichi_visual.ichimoku_b( ) ''' Bollinger Band ''' indicator_bb = BollingerBands(close=nse_technical['close'], window=20, window_dev=2, fillna=False) nse_technical["volatility_bbm"] = indicator_bb.bollinger_mavg() nse_technical["volatility_bbh"] = indicator_bb.bollinger_hband() nse_technical["volatility_bbl"] = indicator_bb.bollinger_lband() nse_technical["volatility_bbw"] = indicator_bb.bollinger_wband() nse_technical["volatility_bbp"] = indicator_bb.bollinger_pband() nse_technical[ "volatility_bbhi"] = indicator_bb.bollinger_hband_indicator() nse_technical[ "volatility_bbli"] = indicator_bb.bollinger_lband_indicator() ''' Accumulation Distribution Index ''' nse_technical["volume_adi"] = AccDistIndexIndicator( high=nse_technical['high'], low=nse_technical['low'], close=nse_technical['close'], volume=nse_technical['total_traded_quantity'], fillna=False).acc_dist_index() ''' Money Flow Index ''' nse_technical["volume_mfi"] = MFIIndicator( high=nse_technical['high'], low=nse_technical['low'], close=nse_technical['close'], volume=nse_technical['total_traded_quantity'], window=14, fillna=False, ).money_flow_index() ''' Relative Strength Index (RSI) ''' nse_technical["momentum_rsi"] = RSIIndicator( close=nse_technical['close'], window=14, fillna=False).rsi() ''' Stoch RSI (StochRSI) ''' indicator_srsi = StochRSIIndicator(close=nse_technical['close'], window=14, smooth1=3, smooth2=3, fillna=False) nse_technical["momentum_stoch_rsi"] = indicator_srsi.stochrsi() nse_technical["momentum_stoch_rsi_k"] = indicator_srsi.stochrsi_k() nse_technical["momentum_stoch_rsi_d"] = indicator_srsi.stochrsi_d() nse_technical.replace({np.nan: None}, inplace=True) nse_technical.replace([np.inf, -np.inf], None, inplace=True) list_to_create = [] list_to_update = [] for index in range(len(nse_history_data) - 1, -1, -1): data = nse_history_data[index] if data.technicals: break technical = NSETechnical( nse_historical_data=data, trend_macd=nse_technical['trend_macd'][index], trend_macd_signal=nse_technical['trend_macd_signal'] [index], trend_macd_diff=nse_technical['trend_macd_diff'][index], trend_sma_fast=nse_technical['trend_sma_fast'][index], trend_sma_slow=nse_technical['trend_sma_slow'][index], trend_ema_fast=nse_technical['trend_ema_fast'][index], trend_ema_slow=nse_technical['trend_ema_slow'][index], trend_ichimoku_conv=nse_technical['trend_ichimoku_conv'] [index], trend_ichimoku_base=nse_technical['trend_ichimoku_base'] [index], trend_ichimoku_a=nse_technical['trend_ichimoku_a'][index], trend_ichimoku_b=nse_technical['trend_ichimoku_b'][index], trend_visual_ichimoku_a=nse_technical[ 'trend_visual_ichimoku_a'][index], trend_visual_ichimoku_b=nse_technical[ 'trend_visual_ichimoku_b'][index], volatility_bbm=nse_technical['volatility_bbm'][index], volatility_bbh=nse_technical['volatility_bbh'][index], volatility_bbl=nse_technical['volatility_bbl'][index], volatility_bbw=nse_technical['volatility_bbw'][index], volatility_bbp=nse_technical['volatility_bbp'][index], volatility_bbhi=nse_technical['volatility_bbhi'][index], volatility_bbli=nse_technical['volatility_bbli'][index], volume_adi=nse_technical['volume_adi'][index], volume_mfi=nse_technical['volume_mfi'][index], momentum_rsi=nse_technical['momentum_rsi'][index], momentum_stoch_rsi=nse_technical['momentum_stoch_rsi'] [index], momentum_stoch_rsi_k=nse_technical['momentum_stoch_rsi_k'] [index], momentum_stoch_rsi_d=nse_technical['momentum_stoch_rsi_d'] [index]) data.technicals = True list_to_update.append(data) list_to_create.append(technical) NSETechnical.objects.bulk_create(list_to_create) NSEHistoricalData.objects.bulk_update(list_to_update, ['technicals']) print(f"Technicals updated for {symbol}")
window_fast=12, window_slow=26, window_sign=9) indicator_rsi14 = RSIIndicator(close=df['cls'], window=14) indicator_cci20 = cci(high=df['hgh'], low=df['low'], close=df['cls'], window=20, constant=0.015) indicator_obv = OnBalanceVolumeIndicator(close=df['cls'], volume=df['vol'], fillna=True) indicator_vol_sma20 = SMAIndicator(close=df['vol'], window=20) indicator_ema03 = EMAIndicator(close=df['cls'], window=3) indicator_ema05 = EMAIndicator(close=df['cls'], window=5) indicator_ema08 = EMAIndicator(close=df['cls'], window=8) indicator_ema10 = EMAIndicator(close=df['cls'], window=10) indicator_ema12 = EMAIndicator(close=df['cls'], window=12) indicator_ema15 = EMAIndicator(close=df['cls'], window=15) indicator_ema30 = EMAIndicator(close=df['cls'], window=30) indicator_ema35 = EMAIndicator(close=df['cls'], window=35) indicator_ema40 = EMAIndicator(close=df['cls'], window=40) indicator_ema45 = EMAIndicator(close=df['cls'], window=45) indicator_ema50 = EMAIndicator(close=df['cls'], window=50) indicator_ema60 = EMAIndicator(close=df['cls'], window=60) # Add Bollinger Band high indicator df['bb_bbhi'] = indicator_bb.bollinger_hband_indicator()
def getDailyData(self, symbol, since, debug=False): try: print("GET DAILY DATA") print("using key [%d/%d] = %s " % (Market.keyindex, len( Market.apikeys), Market.apikeys[Market.keyindex])) data, meta_data = self.ts[Market.keyindex].get_daily( symbol=symbol, outputsize='full') Market.keyindex = (Market.keyindex + 1) % len(Market.apikeys) self.data = data self.meta_data = meta_data self.data = self.data.sort_index() # map values to standard OHLC format self.data['O'] = self.data['1. open'] self.data['H'] = self.data['2. high'] self.data['L'] = self.data['3. low'] self.data['C'] = self.data['4. close'] self.data['V'] = self.data['5. volume'] if debug: print("===VALUES===") print(self.data) # add trend indicator. self.data = ta.utils.dropna(self.data) self.data['sma20'] = SMAIndicator(close=self.data['C'], n=20, fillna=True).sma_indicator() self.data['sma50'] = SMAIndicator(close=self.data['C'], n=50, fillna=True).sma_indicator() self.data['sma100'] = SMAIndicator(close=self.data['C'], n=100, fillna=True).sma_indicator() self.data['sma200'] = SMAIndicator(close=self.data['C'], n=200, fillna=True).sma_indicator() self.data['ema9'] = EMAIndicator(close=self.data['C'], n=9, fillna=True).ema_indicator() self.data['ema20'] = EMAIndicator(close=self.data['C'], n=20, fillna=True).ema_indicator() self.data = ta.add_all_ta_features(self.data, open="O", high="H", low="L", close="C", volume="V", fillna=True) self.data = self.data.loc[self.data.index >= since] candle_names = talib.get_function_groups()['Pattern Recognition'] if debug: print(candle_names) included_items = ('CDLDOJI', 'CDLHAMMER', 'CDLEVENINGSTAR', 'CDLHANGINGMAN', 'CDLSHOOTINGSTAR') candle_names = [ candle for candle in candle_names if candle in included_items ] for candle in candle_names: self.data[candle] = getattr(talib, candle)(self.data['O'], self.data['H'], self.data['L'], self.data['C']) self.data['action'] = 0 if debug: print("===VALUES===") print(self.data) except: print("ERROR GETTING MARKET DATA") time.sleep(60) self.data = None # max 5 api calls per minutes #time.sleep(60 / (2 * len(Market.apikeys))) #time.sleep(60 / 4) return self.data
# Process json file closes = [] for candle in data: closes.append(float(candle[4])) print('Max: ', max(closes), '\nMin: ', min(closes)) df = pd.DataFrame(data=closes, columns=['close']) # Build technical indicators emas_Used = [3, 5, 8, 10, 12, 15, 30, 35, 40, 45, 50, 60] for x in emas_Used: ema = x indicator_ema = EMAIndicator(close=df['close'], window=x, fillna=False) df['ema_' + str(ema)] = indicator_ema.ema_indicator() # print(df.tail()) # Strategy position = 0 open_long = [] open_short = [] close_long = [] close_short = [] pnl = [] fees = [] for i in df.index: c_min = min( df['ema_3'][i],