def get_ind_and_signals(self, prices): short_MA = ind.moving_average(prices['close'], self.short_period) long_MA = ind.moving_average(prices['close'], self.long_period) signal = long_MA.copy() signal[short_MA > long_MA] = 1 signal[short_MA < long_MA] = -1 signal[short_MA == long_MA] = 0 signal[long_MA.isnull()] = 0 indicator = pd.DataFrame({'Timestamp': signal.index, 'short_MA': short_MA, 'long_MA': long_MA}) indicator = indicator.set_index('Timestamp') indicator = indicator.tz_localize(pytz.timezone('UTC')) return indicator, signal
def update(path): df = pd.read_csv(path) df = indicators.moving_average(df, 15) df = indicators.exponential_moving_average(df, 30) df = indicators.relative_strength_index(df, 14) df = indicators.macd(df, 12, 26) # print(df) df = df.astype(float) f = lambda x: mdates.date2num(datetime.datetime.fromtimestamp(x)) df['Date'] = df['Date'].apply(f) # print(df['Date']) return df
def main(instrument, periods, granularity, outfile, daily_alignment): print(f'Periods : {periods}') print(f'Instrument : {instrument}') print(f'Granularity : {granularity}') print(f'daily alignment: {daily_alignment}') print(f'Outfile : {outfile}') if instrument.upper() not in INSTRUMENTS: raise Exception(f'Invalid instrument {instrument}') if granularity.upper() not in GRANULARITY: raise Exception(f'Invalid granularity {granularity}') instrument = instrument.upper() granularity = granularity.upper() debug = True if not debug: pd.set_option('display.max_columns', 50) # pd.set_option('display.max_rows', 500000) pd.set_option('display.width', 1000) stock = get_oanda_api( [instrument], granularity=granularity, count=periods, daily_alignment=daily_alignment, ) nsize = len(stock[instrument]['Close']) # Calculate MACD stock[instrument] = stock[instrument].join( macd(stock[instrument]['Close']) ) # Calculate RSI for n = 14 stock[instrument] = stock[instrument].join( rsi(stock[instrument]['Close']) ) # Calculate Profile and Loss stock[instrument] = stock[instrument].join( pnl(stock[instrument]['Close']) ) # Calculate MACD Percentile stock[instrument] = stock[instrument].join( macd_percentile(stock[instrument]['MACD']) ) # Calculate RSI Percentile stock[instrument] = stock[instrument].join( rsi_percentile(stock[instrument]['RSI']) ) # Calculate Profile and Loss Percentile stock[instrument] = stock[instrument].join( pnl_percentile(stock[instrument]['Profit/Loss']) ) # Calculate Divergence factor 1 and 2 stock[instrument] = stock[instrument].join( pd.Series( ( stock[instrument]['MACD Percentile'] + 0.1 - stock[instrument]['RSI Percentile'] ) / 2.0, name='Divergence Factor 1' ) ) stock[instrument] = stock[instrument].join( pd.Series( stock[instrument]['Divergence Factor 1'] - stock[instrument]['PNL Percentile'], name='Divergence Factor 2' ) ) # Calculate Divergence factor 3 n = 19 for i in range(nsize): stock[instrument].loc[i: nsize, 'Macd_20'] = ( stock[instrument]['MACD'].iloc[i] - stock[instrument]['MACD'].iloc[i - n] ) stock[instrument].loc[i: nsize, 'Prc_20'] = ( (stock[instrument]['Close'].iloc[i] - stock[instrument]['Close'].iloc[i - n]) ) / stock[instrument]['Close'].iloc[i - n] stock[instrument].loc[i: nsize, 'Divergence Factor 3'] = ( stock[instrument]['Macd_20'].iloc[i] / stock[instrument]['Close'].iloc[i] ) - stock[instrument]['Prc_20'].iloc[i] stock[instrument] = stock[instrument].join( rsi(stock[instrument]['Close'], 20, name='RSI_20') ) # Calculate the momentum factors stock[instrument] = stock[instrument].join( pnl_n(stock[instrument]['Close'], 10) ) stock[instrument] = stock[instrument].join( pnl_n(stock[instrument]['Close'], 30) ) stock[instrument]['Close_fwd'] = stock[instrument]['Close'].shift(-2) stock[instrument].loc[-1: nsize, 'Close_fwd'] = stock[instrument]['Close'].iloc[-1] stock[instrument].loc[-2: nsize, 'Close_fwd'] = stock[instrument]['Close'].iloc[-2] stock[instrument] = stock[instrument].join( macd( stock[instrument]['Close_fwd'], name='MACD_fwd' ) ) n = 19 stock[instrument] = stock[instrument].join( pd.Series( stock[instrument]['MACD_fwd'].diff(n) - stock[instrument]['MACD'], name='M_MACD_CHANGE' ) ) stock[instrument] = stock[instrument].join( rsi(stock[instrument]['Close_fwd'], n=20, name='RSI_20_fwd') ) stock[instrument] = stock[instrument].join( pd.Series( stock[instrument]['RSI_20_fwd'] - stock[instrument]['RSI_20'], name='M_RSI_CHANGE' ) ) # Calculate the ADX, PDI & MDI _adx, _pdi, _mdi = adx(stock[instrument]) stock[instrument] = stock[instrument].join(_adx) stock[instrument] = stock[instrument].join(_pdi) stock[instrument] = stock[instrument].join(_mdi) # Calculate the Moving Averages: 5, 10, 20, 50, 100 for period in [5, 10, 20, 50, 100]: stock[instrument] = stock[instrument].join( moving_average( stock[instrument]['Close'], period, name=f'{period}MA' ) ) # Calculate the Williams PCTR stock[instrument] = stock[instrument].join( williams(stock[instrument]) ) # Calculate the Minmax Range n = 17 for i in range(nsize): maxval = stock[instrument]['High'].iloc[i - n: i].max() minval = stock[instrument]['Low'].iloc[i - n: i].min() rng = abs(maxval) - abs(minval) # where is the last price in the range of minumimn to maximum pnow = stock[instrument]['Close'].iloc[i - n: i] if len(pnow.iloc[-1: i].values) > 0: whereinrng = ( (pnow.iloc[-1: i].values[0] - abs(minval)) / rng ) * 100.0 stock[instrument].loc[i: nsize, 'MinMaxPosition'] = whereinrng stock[instrument].loc[i: nsize, 'High_Price(14)'] = maxval stock[instrument].loc[i: nsize, 'Low_Price(14)'] = minval headers = [ 'Close', 'adx', 'pdi', 'mdi', 'MACD', 'RSI', 'Divergence Factor 1', 'Divergence Factor 2', 'Divergence Factor 3', ] stock[instrument].to_csv( outfile, columns=headers, mode='w', sep=',', date_format='%d-%b-%Y %r', )
def Function_for_file_generation(): global specific_insts global ggpath ggpath = gpath debug = False if not debug: pd.set_option('display.max_columns', 50) pd.set_option('display.width', 1000) # user_input=input("Do You Want Select Instrument Manually:y or n") # man_inst_names=None # if user_input=="y": # man_inst_names=input("Enter Instrument names Separated by Space:") # man_inst_names = man_inst_names.split() instruments = INSTRUMENTS # # instruments = ['AUD_CAD',] # # instruments = ['AUD_CHF',] # instruments = ['AUD_CHF', 'AUD_CAD'] # instruments = ['AUD_CAD',] #data = get_file_data() stock = get_oanda_api(instruments, granularity='D') # print (stock['AUD_CAD']) # stock = get_ig_api(instruments) # print (stock['AUD_CAD']) # instruments = data['Close'].columns.values # # Initialize all assign all instrument data to dataframes # stock = {} # for instrument in instruments: # values = {} # for key in COLUMNS: # values[key] = data.get(key, {}).get(instrument) # values['Date'] = data.get('Date').iloc[:len(values[key]), 0] # stock[instrument] = pd.DataFrame(values, columns=COLUMNS) # print(stock[SELECTED_INSTRUMENT]) # return # Calculate the MACD, RSI and Profit and Loss for all instrument paid # Also, Calculate the MACD, RSI and Profit and Loss percentile for all # instruments instruments_list = [] CCI_list = [] dic_for_all_cci = {} for instrument in instruments: nsize = len(stock[instrument]['Close']) # Calculate MACD stock[instrument] = stock[instrument].join( macd(stock[instrument]['Close'])) # Calculate RSI for n = 14 stock[instrument] = stock[instrument].join( rsi(stock[instrument]['Close'])) #changeInPrice stock[instrument]["Change In Price"] = change_in_price( stock[instrument]["Close"].values) # Calculate Profile and Loss stock[instrument] = stock[instrument].join( pnl(stock[instrument]['Close'])) # Calculate MACD Percentile stock[instrument] = stock[instrument].join( macd_percentile(stock[instrument]['MACD'])) # Calculate RSI Percentile stock[instrument] = stock[instrument].join( rsi_percentile(stock[instrument]['RSI'])) # Calculate Profile and Loss Percentile stock[instrument] = stock[instrument].join( pnl_percentile(stock[instrument]['Profit/Loss'])) #Calculate CCI high = stock[instrument]["High"].values close = stock[instrument]["Close"].values low = stock[instrument]["Low"].values #create instrument dataframe ccis = talib.CCI(high, low, close, timeperiod=14) #ccis=list(ccis) instruments_list.append(instrument) CCI_list.append(ccis[-1]) dic_for_all_cci[instrument] = ccis stock[instrument]["CCI"] = ccis # Calculate Divergence factor 1 and 2 stock[instrument] = stock[instrument].join( pd.Series((stock[instrument]['MACD Percentile'] + 0.1 - stock[instrument]['RSI Percentile']) / 2.0, name='Divergence Factor 1')) stock[instrument] = stock[instrument].join( pd.Series(stock[instrument]['Divergence Factor 1'] - stock[instrument]['PNL Percentile'], name='Divergence Factor 2')) # Calculate Divergence factor 3 n = 19 for i in range(nsize): stock[instrument].loc[i:nsize, 'Macd_20'] = ( stock[instrument]['MACD'].iloc[i] - stock[instrument]['MACD'].iloc[i - n]) stock[instrument].loc[i:nsize, 'Prc_20'] = ( (stock[instrument]['Close'].iloc[i] - stock[instrument]['Close'].iloc[i - n]) ) / stock[instrument]['Close'].iloc[i - n] stock[instrument].loc[i:nsize, 'Divergence Factor 3'] = ( stock[instrument]['Macd_20'].iloc[i] / stock[instrument]['Close'].iloc[i] ) - stock[instrument]['Prc_20'].iloc[i] stock[instrument] = stock[instrument].join( rsi(stock[instrument]['Close'], 20, name='RSI_20')) # Calculate the momentum factors stock[instrument] = stock[instrument].join( pnl_n(stock[instrument]['Close'], 10)) stock[instrument] = stock[instrument].join( pnl_n(stock[instrument]['Close'], 30)) stock[instrument]['Close_fwd'] = stock[instrument]['Close'].shift(-2) stock[instrument].loc[ -1:nsize, 'Close_fwd'] = stock[instrument]['Close'].iloc[-1] stock[instrument].loc[ -2:nsize, 'Close_fwd'] = stock[instrument]['Close'].iloc[-2] stock[instrument] = stock[instrument].join( macd(stock[instrument]['Close_fwd'], name='MACD_fwd')) n = 19 stock[instrument] = stock[instrument].join( pd.Series(stock[instrument]['MACD_fwd'].diff(n) - stock[instrument]['MACD'], name='M_MACD_CHANGE')) stock[instrument] = stock[instrument].join( rsi(stock[instrument]['Close_fwd'], n=20, name='RSI_20_fwd')) stock[instrument] = stock[instrument].join( pd.Series(stock[instrument]['RSI_20_fwd'] - stock[instrument]['RSI_20'], name='M_RSI_CHANGE')) # Calculate the ADX, PDI & MDI _adx, _pdi, _mdi = adx(stock[instrument]) stock[instrument] = stock[instrument].join(_adx) stock[instrument] = stock[instrument].join(_pdi) stock[instrument] = stock[instrument].join(_mdi) # Calculate the Moving Averages: 5, 10, 20, 50, 100 for period in [5, 10, 20, 50, 100]: stock[instrument] = stock[instrument].join( moving_average(stock[instrument]['Close'], period, name=f'{period}MA')) # Calculate the Williams PCTR stock[instrument] = stock[instrument].join(williams(stock[instrument])) # Calculate the Minmax Range n = 17 for i in range(nsize): maxval = stock[instrument]['High'].iloc[i - n:i].max() minval = stock[instrument]['Low'].iloc[i - n:i].min() rng = abs(maxval) - abs(minval) # where is the last price in the range of minumimn to maximum pnow = stock[instrument]['Close'].iloc[i - n:i] if len(pnow.iloc[-1:i].values) > 0: whereinrng = ( (pnow.iloc[-1:i].values[0] - abs(minval)) / rng) * 100.0 stock[instrument].loc[i:nsize, 'MinMaxPosition'] = whereinrng stock[instrument].loc[i:nsize, 'High_Price(14)'] = maxval stock[instrument].loc[i:nsize, 'Low_Price(14)'] = minval stock[instrument]['Divergence factor Avg'] = ( stock[instrument]['Divergence Factor 1'] + stock[instrument]['Divergence Factor 2'] + stock[instrument]['Divergence Factor 3']) / 3.0 stock[instrument]['Momentum Avg'] = ( stock[instrument]['M_MACD_CHANGE'] + stock[instrument]['M_RSI_CHANGE'] + stock[instrument]['Profit/Loss_10'] + stock[instrument]['Profit/Loss_30']) / 4.0 df_instrument = pd.DataFrame() df_instrument["Open"] = stock[instrument]["Open"] df_instrument["High"] = stock[instrument]['High'] df_instrument["Low"] = stock[instrument]['Low'] df_instrument["Close"] = stock[instrument]['Close'] df_instrument["Volume"] = stock[instrument]['Volume'] df_instrument["Price"] = stock[instrument]['Close'] df_instrument["Change In Price"] = change_in_price( stock[instrument]['Close'].values) df_instrument["CCI"] = stock[instrument]['CCI'] df_instrument["PNL Percentile"] = stock[instrument]['PNL Percentile'] df_instrument["Divergence Factor 1"] = stock[instrument][ 'Divergence Factor 1'] df_instrument["Divergence Factor 2"] = stock[instrument][ 'Divergence Factor 2'] df_instrument["Divergence Factor 3"] = stock[instrument][ 'Divergence Factor 3'] df_instrument["Momentum Factor 1"] = stock[instrument]["M_MACD_CHANGE"] df_instrument["Momentum Factor 2"] = stock[instrument]['M_RSI_CHANGE'] df_instrument["Momentum Factor 3"] = stock[instrument][ 'Profit/Loss_10'] df_instrument["Momentum Factor 4"] = stock[instrument][ 'Profit/Loss_30'] df_instrument["RSI"] = stock[instrument]["RSI"] df_instrument["MACD"] = stock[instrument]["MACD"] df_instrument["WPCTR"] = stock[instrument]["Williams PCTR"] df_instrument["pdi"] = stock[instrument]["pdi"] df_instrument["mdi"] = stock[instrument]["mdi"] df_instrument["adx"] = stock[instrument]["adx"] #df_instrument= df_instrument[pd.notnull(df_instrument['CCI'])] df_instrument = df_instrument.dropna(how="any") df_instrument["CCI Percentile"] = cci_percentile(df_instrument["CCI"]) df_instrument["Divergence Factor 4"] = df_instrument[ "CCI Percentile"] - df_instrument["PNL Percentile"] df_instrument['Divergence Factor 1 Rank'] = rank_formulation( df_instrument['Divergence Factor 1'].values) df_instrument['Divergence Factor 2 Rank'] = rank_formulation( df_instrument['Divergence Factor 2'].values) df_instrument['Divergence Factor 3 Rank'] = rank_formulation( df_instrument['Divergence Factor 3'].values) df_instrument['Divergence Factor 4 Rank'] = rank_formulation( df_instrument['Divergence Factor 4'].values) df_instrument['DF Avg Rank'] = ( df_instrument['Divergence Factor 1 Rank'] + df_instrument['Divergence Factor 2 Rank'] + df_instrument['Divergence Factor 3 Rank'] + df_instrument['Divergence Factor 4 Rank']) / 4.0 df_instrument['Momentum Factor 1 Rank'] = rank_formulation( df_instrument['Momentum Factor 1'].values) df_instrument['Momentum Factor 2 Rank'] = rank_formulation( df_instrument['Momentum Factor 2'].values) df_instrument['Momentum Factor 3 Rank'] = rank_formulation( df_instrument['Momentum Factor 3'].values) df_instrument['Momentum Factor 4 Rank'] = rank_formulation( df_instrument['Momentum Factor 4'].values) df_instrument['MF Avg Rank'] = ( df_instrument['Momentum Factor 1 Rank'] + df_instrument['Momentum Factor 2 Rank'] + df_instrument['Momentum Factor 3 Rank'] + df_instrument['Momentum Factor 4 Rank']) / 4.0 df_instrument["% Rank of DF Avgs"] = rank_formulation( df_instrument['DF Avg Rank'].values) df_instrument["% Rank of MF Avgs"] = rank_formulation( df_instrument['MF Avg Rank'].values) df_instrument = df_instrument[[ 'Open', 'High', 'Low', 'Close', 'Volume', 'Price', 'Change In Price', 'Divergence Factor 1', 'Divergence Factor 2', 'Divergence Factor 3', 'Divergence Factor 4', 'DF Avg Rank', '% Rank of DF Avgs', 'Divergence Factor 1 Rank', 'Divergence Factor 2 Rank', 'Divergence Factor 3 Rank', 'Divergence Factor 4 Rank', 'Momentum Factor 1', 'Momentum Factor 2', 'Momentum Factor 3', 'Momentum Factor 4', 'Momentum Factor 1 Rank', 'Momentum Factor 2 Rank', 'Momentum Factor 3 Rank', 'Momentum Factor 4 Rank', 'MF Avg Rank', '% Rank of MF Avgs', 'RSI', 'MACD', 'WPCTR', 'CCI', 'CCI Percentile', 'PNL Percentile', 'pdi', 'mdi', 'adx', ]] df_instrument.to_csv(gpath + "all_folders/" + instrument + ".csv") ccis_df = pd.DataFrame(dic_for_all_cci) cci_percentile_list = [] dic = {"Instrument": instruments_list, "CCI": CCI_list} new_df = pd.DataFrame(dic) cci_percentile_list = cci_percentile(new_df["CCI"]).to_list() #sys.exit() # calculate the aggregrate for each oeruod # calculate the Divergence_Macd_Prc_Rank for nrow in range(nsize): row = [ stock[instrument]['Divergence Factor 3'].iloc[nrow] for instrument in instruments ] series = pd.Series(row).rank() / len(row) for i, instrument in enumerate(instruments): stock[instrument].loc[nrow:nsize, 'Divergence_Macd_Prc_Rank'] = series.iloc[i] # calculate the Divergence and Momentum average rank indices = [instrument for instrument in instruments] columns = [ 'Price', "Change In Price", 'Divergence Factor 1', 'Divergence Factor 2', 'Divergence Factor 3', 'Divergence Factor 1 Rank', 'Divergence Factor 2 Rank', 'Divergence Factor 3 Rank', 'M_MACD_CHANGE', 'M_RSI_CHANGE', 'Profit/Loss_10', 'Profit/Loss_30', 'M_MACD_CHANGE Rank', 'M_RSI_CHANGE Rank', 'Profit/Loss_10 Rank', 'Profit/Loss_30 Rank', 'MF Avg Rank', '% Rank of MF Avgs', 'MinMaxPosition', 'RSI', 'WPCTR', 'pdi', 'mdi', 'adx', 'High_Price(14)', 'Low_Price(14)', '5MA', '10MA', '20MA', '50MA', '100MA', "MACD", 'PNL Percentile', "DF Avg Rank", "% Rank of DF Avgs", ] periods = [] for i in range(nsize): period = [] for instrument in instruments: period.append([ stock[instrument]['Close'].iloc[i], stock[instrument]["Change In Price"].iloc[i], stock[instrument]['Divergence Factor 1'].iloc[i], stock[instrument]['Divergence Factor 2'].iloc[i], stock[instrument]['Divergence Factor 3'].iloc[i], None, None, None, stock[instrument]['M_MACD_CHANGE'].iloc[i], stock[instrument]['M_RSI_CHANGE'].iloc[i], stock[instrument]['Profit/Loss_10'].iloc[i], stock[instrument]['Profit/Loss_30'].iloc[i], None, None, None, None, None, None, stock[instrument]['MinMaxPosition'].iloc[i], stock[instrument]['RSI'].iloc[i], stock[instrument]['Williams PCTR'].iloc[i], stock[instrument]['pdi'].iloc[i], stock[instrument]['mdi'].iloc[i], stock[instrument]['adx'].iloc[i], stock[instrument]['High_Price(14)'].iloc[i], stock[instrument]['Low_Price(14)'].iloc[i], stock[instrument]['5MA'].iloc[i], stock[instrument]['10MA'].iloc[i], stock[instrument]['20MA'].iloc[i], stock[instrument]['50MA'].iloc[i], stock[instrument]['100MA'].iloc[i], stock[instrument]["MACD"].iloc[i], stock[instrument]['PNL Percentile'].iloc[i], None, None, ]) df = pd.DataFrame(data=period, index=indices, columns=columns) df['Divergence Factor 1 Rank'] = rank_formulation( df["Divergence Factor 1"].values) df['Divergence Factor 2 Rank'] = rank_formulation( df["Divergence Factor 2"].values) df['Divergence Factor 3 Rank'] = rank_formulation( df["Divergence Factor 3"].values) df['Momentum Factor 1 Rank'] = rank_formulation( df['M_MACD_CHANGE'].values) df['Momentum Factor 2 Rank'] = rank_formulation( df['M_RSI_CHANGE'].values) df['Momentum Factor 3 Rank'] = rank_formulation( df['Profit/Loss_10'].values) df['Momentum Factor 4 Rank'] = rank_formulation( df['Profit/Loss_30'].values) df['MF Avg Rank'] = ( df['Momentum Factor 1 Rank'] + df['Momentum Factor 1 Rank'] + df['Momentum Factor 1 Rank'] + df['Momentum Factor 1 Rank']) / 4.0 df['% Rank of MF Avgs'] = rank_formulation(df['MF Avg Rank'].values) #df.to_excel("target_data.xlsx") periods.append(df) pnl_percentile_nparaay = np.array(df["PNL Percentile"].values) cci_percentile_nparray = cci_percentile_list divergent_factor_4 = cci_percentile_nparray - pnl_percentile_nparaay df["CCI"] = CCI_list df["CCI Percentile"] = cci_percentile_list df["Divergence Factor 4"] = divergent_factor_4 df['Divergence Factor 4 Rank'] = rank_formulation( df['Divergence Factor 1'].values) df['DF Avg Rank'] = ( df['Divergence Factor 1 Rank'] + df['Divergence Factor 2 Rank'] + df['Divergence Factor 3 Rank'] + df['Divergence Factor 4 Rank']) / 4.0 df["% Rank of DF Avgs"] = rank_formulation(df['DF Avg Rank'].values) df = df[[ 'Price', 'Change In Price', 'Divergence Factor 1', 'Divergence Factor 2', 'Divergence Factor 3', 'Divergence Factor 4', 'Divergence Factor 1 Rank', 'Divergence Factor 2 Rank', 'Divergence Factor 3 Rank', 'Divergence Factor 4 Rank', 'DF Avg Rank', '% Rank of DF Avgs', #'Momentum Factor 1','Momentum Factor 2','Momentum Factor 3','Momentum Factor 4', #'Momentum Factor 1 Rank','Momentum Factor 2 Rank','Momentum Factor 3 Rank','Momentum Factor 4 Rank','MF Avg Rank', '% Rank of MF Avgs', 'M_MACD_CHANGE', 'M_RSI_CHANGE', 'Profit/Loss_10', 'Profit/Loss_30', 'M_MACD_CHANGE Rank', 'M_RSI_CHANGE Rank', 'Profit/Loss_10 Rank', 'Profit/Loss_30 Rank', 'MF Avg Rank', '% Rank of MF Avgs', 'MinMaxPosition', 'RSI', 'MACD', 'WPCTR', 'CCI', 'CCI Percentile', 'PNL Percentile', 'pdi', 'mdi', 'adx', 'High_Price(14)', 'Low_Price(14)', '5MA', '10MA', '20MA', '50MA', '100MA' ]] df.to_excel(gpath + "all_folders/" + "target_data.xlsx") df.sort_values(by="% Rank of DF Avgs", inplace=True) df.to_excel(gpath + "all_folders/" + "ordered_target_data.xlsx") top5, last5 = instrument_selection_rules(df) specific_insts = None specific_insts = top5 + last5 dflist1 = [] if specific_insts is not None: '''dflist1=Graph_Plots_For_Individual_Instrument(specific_insts,False) dflist1.to_csv(gpath+"all_folders"+"/"+"ins_ind_flag.csv") dfDiverge=dflist.copy() dfDiverge=dfDiverge.loc[dfDiverge['imp_var'].isin(['Divergence Factor 1','Divergence Factor 2','Divergence Factor 3','Divergence Factor 4'])] dfDiverge.reset_index(drop=True,inplace=True) dfDiverge.to_csv(gpath+"all_folders"+"/"+"selected_ins_ind_flag.csv")''' for rule_instrument in specific_insts: data = pd.read_csv(gpath + "all_folders/" + rule_instrument + ".csv", index_col="Date") data.to_csv(gpath + "rule_select_inst/" + rule_instrument + ".csv")
def plot_graph(self): from_ = self.date_picker.date() from_ = datetime.date(day=from_.day(), month=from_.month(), year=from_.year()) to = from_ + datetime.timedelta(days=1) from_ = from_.strftime("%Y%m%d") to = to.strftime("%Y%m%d") query = self.db.trades.find({'code': self.code, 'datetime': {'$gte': from_, '$lt': to}}).sort('datetime') trades = [(datetime.datetime.strptime(trade['datetime'], "%Y%m%d%H%M%S"), float(trade['open'])) for trade in query] if len(trades) < 2: self.clear() self.axes1.plot() self.axes2.plot() self.axes3.plot() self.redraw() return heads = ['DateTime', 'Open',] r = indicators.get_records(heads, trades) self.clear() for ax in self.axes1, self.axes2, self.axes3: if ax != self.axes3: for label in ax.get_xticklabels(): label.set_visible(False) else: for label in ax.get_xticklabels(): label.set_rotation(40) label.set_horizontalalignment('right') ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) try: rsi = indicators.relative_strength(r.open) fillcolor = 'darkgoldenrod' textsize = 9 self.axes1.plot(r.datetime, rsi, fillcolor) self.axes1.fill_between(r.datetime, rsi, 70, where=(rsi>=70), facecolor=fillcolor, edgecolor=fillcolor) self.axes1.fill_between(r.datetime, rsi, 30, where=(rsi<=30), facecolor=fillcolor, edgecolor=fillcolor) except: self.axes1.plot(r.datetime, r.open, color='black', label='Open') self.axes1.axhline(70, color=fillcolor) self.axes1.axhline(30, color=fillcolor) self.axes1.text(0.6, 0.9, '>70 = overbought', va='top', transform=self.axes1.transAxes, fontsize=textsize) self.axes1.text(0.6, 0.1, '<30 = oversold', transform=self.axes1.transAxes, fontsize=textsize) self.axes1.set_ylim(0, 100) self.axes1.set_yticks([30,70]) self.axes1.text(0.025, 0.95, 'RSI (14)', va='top', transform=self.axes1.transAxes, fontsize=textsize) try: ma = indicators.moving_average(r.open, 10, type_='simple') self.axes2.plot(r.datetime, ma, color='blue', lw=2, label='MA (10)') except: pass try: ma = indicators.moving_average(r.open, 20, type_='simple') self.axes2.plot(r.datetime, ma, color='red', lw=2, label='MA (20)') except: pass self.axes2.plot(r.datetime, r.open, color='black', label='Open') props = font_manager.FontProperties(size=8) self.axes2.legend(loc='best', shadow=True, fancybox=True, prop=props) fillcolor = 'darkslategrey' nslow, nfast, nema = 26, 12, 9 try: emaslow, emafast, macd = indicators.moving_average_convergence(r.open, nslow=nslow, nfast=nfast) ema9 = indicators.moving_average(macd, nema, type_='exponential') self.axes3.plot(r.datetime, macd, color='black', lw=2) self.axes3.plot(r.datetime, ema9, color='blue', lw=1) self.axes3.fill_between(r.datetime, macd-ema9, 0, alpha=0.5, facecolor=fillcolor, edgecolor=fillcolor) except: self.axes3.plot(r.datetime, r.open, color='black', label='Open') self.axes3.text(0.025, 0.95, 'MACD (%d, %d, %d)'%(nfast, nslow, nema), va='top', transform=self.axes3.transAxes, fontsize=textsize) self.redraw()
def get_mov_avg(close_price): mov_avg_20 = indicators.moving_average(close_price, window=20) mov_avg_60 = indicators.moving_average(close_price, window=60) mov_avg_100 = indicators.moving_average(close_price, window=100) return mov_avg_20, mov_avg_60, mov_avg_100