(May-07-2023, 01:44 PM)SuchUmami Wrote: In future, I will not put too many functions in my code.I think you misunderstand a little here.
I mean that you could make function then it will isolate code,
so can have what you have written in one function and other stuff needed in a other function.
Example.
# kr3.py import krakenex import pandas as pd api = krakenex.API() def calc_pair_price(): # Define currency pairs and their corresponding names currency_pairs = [ ('XETHXXBT', 'ETH/BTC'),('XXBTZEUR', 'BTC/EUR'), ('XXBTZUSD', 'BTC/USD'),('XXBTZGBP', 'BTC/GBP'), ('XETHZEUR', 'ETH/EUR'),('XETHZGBP', 'ETH/GBP'), ('XETHZUSD', 'ETH/USD'),('EURGBP', 'EURGBP'), ('ZEURZUSD', 'EUR/USD'),('ZGBPZUSD', 'GBP/USD'), ('PAXGXBT', 'PAXG/BTC'),('PAXGETH', 'PAXG/ETH'), ('PAXGEUR', 'PAXG/EUR'),('PAXGUSD', 'PAXG/USD'), ('XXRPXXBT', 'XRP/BTC'),('XRPETH', 'XRP/ETH'), ('XXRPZEUR', 'XRP/EUR'),('XRPGBP', 'XRP/GBP'), ('XXRPZUSD', 'XRP/USD'), ] time_frames = ['1', '5', '15', '30', '60', '240', '1440', '10080'] for pair, pair_name in currency_pairs: for tf in time_frames: print(f'{tf} minute m/a ({pair_name})') # Fetch OHLC data for the given pair and time frame ohlc_data = api.query_public('OHLC', {'pair': pair, 'interval': tf}) # Convert the data to a pandas dataframe df = pd.DataFrame( ohlc_data['result'][pair], columns=['time', 'open', 'high', 'low', 'close', 'vwap', 'volume', 'count',] ) df['time'] = pd.to_datetime(df['time'], unit='s') df.set_index('time', inplace=True) # Calculate the moving averages last_20_candles = df.iloc[-21:-1] last_20_close_prices = last_20_candles['close'].astype(float).tolist() avg_last_20_close_price = sum(last_20_close_prices) / len(last_20_close_prices) print(f'20 period m/a: {avg_last_20_close_price}') last_50_candles = df.iloc[-51:-1] last_50_close_prices = last_50_candles['close'].astype(float).tolist() avg_last_50_close_price = sum(last_50_close_prices) / len(last_50_close_prices) print(f'50 period m/a: {avg_last_50_close_price}') last_200_candles = df.iloc[-201:-1] last_200_close_prices = (last_200_candles['close'].astype(float).tolist()) avg_last_200_close_price = sum(last_200_close_prices) / len(last_200_close_prices) print(f'200 period m/a: {avg_last_200_close_price}') print('\n') def calc_max(): pair = 'BTC/USD' interval = 15 # make the API request for OHLC data ohlc_data = api.query_public('OHLC', {'pair': pair, 'interval': interval}) # Convert the data to a pandas dataframe df = pd.DataFrame(ohlc_data['result'][pair], columns=['time', 'open', 'high', 'low', 'close', 'vwap', 'volume', 'count']) df['time'] = pd.to_datetime(df['time'], unit='s') df.set_index('time', inplace=True) max_price = df['high'].max() print(f"Maximum price in the <high column>: {max_price}") if __name__ == '__main__': calc_max() #calc_pair_price()
(dl_env) G:\div_code\dl_env λ python kr3.py Maximum price in the <high column>: 29975.0So now have added a function as eg calculate Maximum price in high high column(as i don't understand what you want to add).
If uncomment
calc_pair_price()
it will run your code,so a way organize code that make it easier to add stuff.Also is normal that a function
return
code for further processing,then can to smaller task and code get easier to read.