def getData(self): # define URL base = 'https://api.binance.com' endpoint = '/api/v1/klines' params = '?&symbol=' + self.symbol + '&interval=1h' url = base + endpoint + params # download data data = requests.get(url) dictionary = json.loads(data.text) # put in dataframe and clean-up df = pd.DataFrame.from_dict(dictionary) df = df.drop(range(6, 12), axis=1) # rename columns col_names = ['time', 'open', 'high', 'low', 'close', 'volume'] df.columns = col_names # transform values from strings to floats for col in col_names: df[col] = df[col].astype(float) # add the moving averages df['fast_sma'] = sma(df['close'].tolist(), 10) df['slow_sma'] = sma(df['close'].tolist(), 30) return df
def getData(self): #get url to data from kraken api pair = self.pair interval = str(self.interval) base = 'https://api.kraken.com' endpoint = '/0/public/OHLC' params = '?pair=' + pair + '&interval=' + interval url = base + endpoint + params #download the data json_string = requests.get(url) dictionary = json.loads(json_string.text) dict_len = len(dictionary['result'][pair]) self.dflength = dict_len #creat pandas df col_names = ['time', 'open', 'high', 'low', 'close', 'volume', 'sma20', 'sma40', 'rsi', 'obv','hma20'] df = pd.DataFrame(columns = col_names) #creat df cause the import stuff would work for x in range(dict_len): temp = dictionary['result'][pair][x] df = df.append({col_names[0]: temp[0], col_names[1]: temp[1], col_names[2]: temp[2], col_names[3]: temp[3], col_names[4]: temp[4], col_names[5]: temp[5]}, ignore_index=True) #turn df into floats for col in col_names: df[col] = df[col].astype(float) #add techinical indicatiors to the df df['time'] = [datetime.fromtimestamp(x) for x in df['time']] df['sma20'] = sma(df['close'].tolist(), 20) df['sma40'] = sma(df['close'].tolist(), 50) df['hma20'] = hma(df['close'].tolist(), 200) df['rsi'] = rsi(df['close'].tolist(), 10) df['obv'] = obv(df['close'].tolist(), df['volume'].tolist()) return df
def getData(self): # get url base = 'https://api.binance.com' endpoint = '/api/v3/klines' params = '?&symbol=' + self.symbol + '&interval=1h' url = base + endpoint + params print("Fetching data from " + url) # fetch data data = requests.get(url) dictionary = json.loads(data.text) # clean up df = pd.DataFrame.from_dict(dictionary).drop(range(6, 12), axis=1) col_names = ['time', 'open', 'high', 'low', 'close', 'volume'] df.columns = col_names print(df) for col in col_names: df[col] = df[col].astype(float) # calculate SMAs df['fast_sma'] = sma(df['close'].tolist(), 10) df['slow_sma'] = sma(df['close'].tolist(), 30) return df
def collect_data(symbol, time): base = 'https://api.binance.com' endpoint = '/api/v1/klines' params = '?&symbol=' + symbol + '&interval=' + time data = requests.get(base + endpoint + params) dictionary = json.loads(data.text) dataframe = pandas.DataFrame.from_dict(dictionary) dataframe = dataframe.drop(range(6, 12), axis=1) dataframe.columns = ['time', 'open', 'high', 'low', 'close', 'volume'] for col in dataframe.columns: dataframe[col] = dataframe[col].astype(float) dataframe['fast_sma'] = sma(dataframe['close'].tolist(), 10) dataframe['slow_sma'] = sma(dataframe['close'].tolist(), 30) dataframe.plot(x='time', y='fast_sma', kind='line', title='fast sma in the last 10 hours') dataframe.plot(x='time', y='slow_sma', kind='line', title='slow sma in the last 30 hours') plt.show()
def __init__(self, symbol): self.symbol = symbol self.exchange = bi.Binance() self.df = self.exchange.GetSymbolData(symbol, '4h') self.last_price = self.df['close'][len(self.df['close'])-1] self.buy_signals = [] try: self.df['fast_sma'] = sma(self.df['close'].tolist(), 10) self.df['slow_sma'] = sma(self.df['close'].tolist(), 30) self.df['low_boll'] = lbb(self.df['close'].tolist(), 14) self.df['up_boll'] = ubb(self.df['close'].tolist(), 14) self.df['vwap'] = (self.df['volume']*(self.df['close'])).cumsum() / self.df['volume'].cumsum() #self.df['vwap'] = (self.df['volume']*(self.df['high']+self.df['low'])/2).cumsum() / self.df['volume'].cumsum() self.df['vwma'] = self.vwma(14) # From Indicators self.df = ind.macd(self.df) self.df = ind.money_flow_index(self.df) self.df = ind.rsi(self.df) #From Finta vw_macd = TA.VW_MACD(self.df) self.df['vw_macd'] = vw_macd['MACD'] self.df['vw_macd_sig'] = vw_macd['SIGNAL'] except Exception as e: print(" Exception raised when trying to compute indicators on "+self.symbol) print(e) return None
def getData(self): base = 'https://api.binance.com' endpoint = '/api/v1/klines' params = '?&symbol=' + self.symbol + '&interval=1d' url = base + endpoint + params #JSON LOADS AND DOWNLOAD DATA data = requests.get(url) dictionary = json.loads(data.text) #PUT IN DATAFRAME AND CLEAN UP df = pd.DataFrame.from_dict(dictionary) df = df.drop(range(6, 12), axis=1) #NAMING COLUMNS typeOfcol = ['time', 'open', 'high', 'low', 'close', 'volume'] df.columns = typeOfcol for col in typeOfcol: df[col] = df[col].astype(float) valueONE = 10 valueTWO = 21 valueTHREE = 50 valueFOUR = 200 #ADDING SMA'S df['first_sma'] = sma(df['close'].tolist(), valueONE) df['second_sma'] = sma(df['close'].tolist(), valueTWO) df['third_sma'] = sma(df['close'].tolist(), valueTHREE) df['fourth_sma'] = sma(df['close'].tolist(), valueFOUR) return df
def getData(self): # definition de l'URL: obtention des donnée à partir de binance.com base = 'https://api.binance.com' endpoint = '/api/v1/klines' params = '?&symbol='+self.symbol+'&interval=1h' url = base + endpoint + params # téléchargement des data data = requests.get(url) dictionary = json.loads(data.text) # on met les données dans une dataframe df = pd.DataFrame.from_dict(dictionary) df = df.drop(range(6, 12), axis=1) # On renomme correctement les colonnes col_names = ['time', 'open', 'high', 'low', 'close', 'volume'] df.columns = col_names # transform values from strings to floats for col in col_names: df[col] = df[col].astype(float) # add the moving averages df['fast_sma'] = sma(df['close'].tolist(), 10) df['slow_sma'] = sma(df['close'].tolist(), 30) return df
def __init__(self, symbol): self.symbol = symbol self.exchange = Binance() self.df = self.exchange.GetSymbolData(symbol, '4h') self.last_price = self.df['close'][len(self.df['close'])-1] self.buy_signals = [] try: self.df['fast_sma'] = sma(self.df['close'].tolist(), 10) self.df['slow_sma'] = sma(self.df['close'].tolist(), 30) self.df['low_boll'] = lbb(self.df['close'].tolist(), 14) except Exception as e: print(" Exception raised when trying to compute indicators on "+self.symbol) print(e) return None
def __init__(self, symbol, timeframe: str = '4h'): self.symbol = symbol self.exchange = Binance() self.df = self.exchange.get_symbol_data(symbol, timeframe) self.last_price = self.df['close'][len(self.df['close']) - 1] try: self.df['short_sma'] = sma(self.df['close'].tolist(), 10) self.df['long_sma'] = sma(self.df['close'].tolist(), 30) self.df['low_boll'] = lbb(self.df['close'].tolist(), 14) except Exception as e: print(' Exception when trying to computer indicators on: ' + self.symbol) print(e) return None
def __init__(self, symbol, timeframe='4h'): self.symbol = symbol self.timeframe = timeframe self.exchange = Binance() self.df = self.exchange.GetSymbolData(symbol, timeframe) self.last_price = self.df['close'][len(self.df['close']) - 1] try: self.df['fast_sma'] = sma(self.df['close'].tolist(), 10) self.df['slow_sma'] = sma(self.df['close'].tolist(), 30) self.df['low_boll'] = lbb(self.df['close'].tolist(), 14) except Exception as e: print( "Exception occurred when attempting to compute indicators on " + self.symbol) print(e) return None
def process_data(candlestick_data): """ Processes the candlestick data in order to be able to apply our trading strategy on it. First, we will rename the columns of our data frame and convert all data types from string to floats in order to be able to perform calculations. Then, we will add the slow moving average and the fast moving average to our market data. Each of them will represent a new column within our candlestick df. At last, we will another additional column holding the time in datetime format so we not only have the time or our data in timestamp format. Parameter: - candlestick_data: DataFrame - Candlestick market data that we will process Return: - df: DataFrame - The adjusted and processed candlestick data frame holding our market data """ df = candlestick_data df = df.drop(range( 6, 12), axis=1) # Drop the last six columns as they are not needed # Rename the columns col_names = ["time", "open", "high", "low", "close", "volume"] df.columns = col_names # Cast values from strings to floats for col in col_names: df[col] = df[col].astype(float) # Add the moving averages to the data frame df["fast_sma"] = sma( df["close"].tolist(), 10) # fast simple moving average follows prices closely df["slow_sma"] = sma( df["close"].tolist(), 30) # slow simple moving average follows prices less closely # Add date in datetime format to the data frame so we can se real dates on the plot and not just timestamps df["datetime"] = pd.to_datetime(df["time"] * 1000000, infer_datetime_format=True) # Return result return df
def getData(self): #get url to data from kraken api data = pd.read_csv("Kraken_BTCUSD_1h.csv") #creat pandas df col_names = [ 'time', 'symbol', 'open', 'high', 'low', 'close', 'volume', 'volume usd', 'sma20', 'sma40', 'rsi', 'obv' ] df = pd.DataFrame(data, columns=col_names) print df #turn df into floats for col in col_names: df[col] = df[col].astype(float) #add techinical indicatiors to the df #df['time'] = [datetime.fromtimestamp(x) for x in df['time']] df['sma20'] = sma(df['close'].tolist(), 20) df['sma40'] = sma(df['close'].tolist(), 40) df['rsi'] = rsi(df['close'].tolist(), 14) df['obv'] = obv(df['close'].tolist(), df['volume'].tolist()) return df
class TradingModel: def __init__(self, symbol): self.symbol = symbol self.df = self.getData() def getData(self): # define URL base_url = 'Your API URL' api_key_id = 'Your API Key' api_secret = 'Your API Secret' api = tradeapi.REST( base_url=base_url, key_id=api_key_id, secret_key=api_secret) session = requests.session() # download data data = requests.get(url) dictionary = json.loads(data.text) # put in dataframe and clean-up df = pd.DataFrame.from_dict(dictionary) df = df.drop(range(6, 12), axis=1) # rename columns col_names = ['time', 'open', 'high', 'low', 'close', 'volume'] df.columns = col_names # transform values from strings to floats for col in col_names: df[col] = df[col].astype(float) # add the moving averages df['fast_sma'] = sma(df['close'].tolist(), 10) df['slow_sma'] = sma(df['close'].tolist(), 30) return df
def add_sma(df_or_lst, period): if isinstance(df_or_lst, list) == True: return sma(df_or_lst, period) else: data_frame['sma ' + str(period)] = sma(df_or_lst['close'].tolist(), period)