Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I was coding a script that calculates percentage differnce of binance's close data and upbit's close data.
Python
  1  import time
  2  import pandas as pd
  3  import pyupbit
  4  import ccxt
  5  import requests
  6  time.sleep(3)
  7  
  8  binanceX = ccxt.binance(config={
  9      'enableRateLimit': True,
 10      'options': {
 11          'defaultType': 'future'
 12      }
 13  })
 14  
 15  
 16  def GetOhlcv(binance, Ticker, period):
 17      ohlcv = binance.fetch_ohlcv(Ticker, period)
 18      df = pd.DataFrame(ohlcv, columns=['datetime', 'open', 'high', 'low', 'close', 'volume'])
 19      df['datetime'] = pd.to_datetime(df['datetime'], unit='ms')
 20      df.set_index('datetime', inplace=True)
 21      return df
 22  
 23  
 24  def get_exchange_rate(base_currency, target_currency):
 25      url = f'http://www.floatrates.com/daily/{base_currency.lower()}.json'
 26      response = requests.get(url)
 27  
 28      if response.status_code == 200:
 29          data = response.json()
 30  
 31          if target_currency.lower() in data:
 32              exchange_rate = data[target_currency.lower()]['rate']
 33              return exchange_rate
 34          else:
 35              print(f'Error: {target_currency} not found in the conversion rates.')
 36              return None
 37      else:
 38          print(f"Error: HTTP status code {response.status_code} received.")
 39          return None
 40  
 41  
 42  upbit_coin_list = ["KRW-BTC", "KRW-DOGE", "KRW-ETH", "KRW-SOL", "KRW-XRP"]
 43  binance_coin_list = ["BTC/BUSD", "DOGE/BUSD", "ETH/BUSD", "SOL/BUSD", "XRP/BUSD"]
 44  binance_symbol_list = ["BTCBUSD", "DOGEBUSD", "ETHBUSD", "SOLBUSD", "XRPBUSD"]
 45  for i in range(5):
 46      upbit_coin = upbit_coin_list[i]
 47      binance_coin = binance_coin_list[i]
 48  
 49      exchange_rate_today = get_exchange_rate("USD", 'KRW')
 50      df_binance = GetOhlcv(binanceX, binance_coin, '1m')
 51      df_binance_close = df_binance["close"].tail(200)
 52      df_upbit = pyupbit.get_ohlcv(upbit_coin, interval="minute1")
 53      df_upbit_close = df_upbit["close"].tail(200)
 54  
 55      gap_series = (df_binance_close * exchange_rate_today - df_upbit_close) / (
 56                  df_binance_close * exchange_rate_today) * 100
 57      gap_df = pd.DataFrame(gap_series, columns=['now_gap'])
 58      now_gap = gap_series.iloc[-2]
 59  
 60      print(gap_series, gap_df, now_gap)

When I was done, I ran the code. Instead of it printing out the dataframe of percentage difference of binance's close data and upbit's close data, it printed out this:
  1  2023-08-21 17:37:00   NaN
  2  2023-08-21 17:38:00   NaN
  3  2023-08-21 17:39:00   NaN
  4  2023-08-21 17:40:00   NaN
  5  2023-08-21 17:41:00   NaN
  6                         ..
  7  2023-08-22 05:52:00   NaN
  8  2023-08-22 05:53:00   NaN
  9  2023-08-22 05:54:00   NaN
 10  2023-08-22 05:55:00   NaN
 11  2023-08-22 05:56:00   NaN
 12  Name: close, Length: 400, dtype: float64 Empty DataFrame
 13  Columns: [now_gap]
 14  Index: [] nan
 15  2023-08-21 17:37:00   NaN
 16  2023-08-21 17:38:00   NaN
 17  2023-08-21 17:39:00   NaN
 18  2023-08-21 17:40:00   NaN
 19  2023-08-21 17:41:00   NaN
 20                         ..
 21  2023-08-22 05:49:00   NaN
 22  2023-08-22 05:50:00   NaN
 23  2023-08-22 05:53:00   NaN
 24  2023-08-22 05:54:00   NaN
 25  2023-08-22 05:56:00   NaN
 26  Name: close, Length: 400, dtype: float64 Empty DataFrame
 27  Columns: [now_gap]
 28  Index: [] nan
 29  2023-08-21 17:37:00   NaN
 30  2023-08-21 17:38:00   NaN
 31  2023-08-21 17:39:00   NaN
 32  2023-08-21 17:40:00   NaN
 33  2023-08-21 17:41:00   NaN
 34                         ..
 35  2023-08-22 05:52:00   NaN
 36  2023-08-22 05:53:00   NaN
 37  2023-08-22 05:54:00   NaN
 38  2023-08-22 05:55:00   NaN
 39  2023-08-22 05:56:00   NaN
 40  Name: close, Length: 400, dtype: float64 Empty DataFrame
 41  Columns: [now_gap]
 42  Index: [] nan
 43  2023-08-21 17:37:00   NaN
 44  2023-08-21 17:38:00   NaN
 45  2023-08-21 17:39:00   NaN
 46  2023-08-21 17:40:00   NaN
 47  2023-08-21 17:41:00   NaN
 48                         ..
 49  2023-08-22 05:49:00   NaN
 50  2023-08-22 05:50:00   NaN
 51  2023-08-22 05:51:00   NaN
 52  2023-08-22 05:52:00   NaN
 53  2023-08-22 05:53:00   NaN
 54  Name: close, Length: 400, dtype: float64 Empty DataFrame
 55  Columns: [now_gap]
 56  Index: [] nan
 57  2023-08-21 17:37:00   NaN
 58  2023-08-21 17:38:00   NaN
 59  2023-08-21 17:39:00   NaN
 60  2023-08-21 17:40:00   NaN
 61  2023-08-21 17:41:00   NaN
 62                         ..
 63  2023-08-22 05:52:00   NaN
 64  2023-08-22 05:53:00   NaN
 65  2023-08-22 05:54:00   NaN
 66  2023-08-22 05:55:00   NaN
 67  2023-08-22 05:56:00   NaN
 68  Name: close, Length: 400, dtype: float64 Empty DataFrame
 69  Columns: [now_gap]
 70  Index: [] nan

Thank you!

What I have tried:

I have tried to make the length of the binance and upbit's close dataframe the same, but it didn't work.
Posted
Updated 23-Aug-23 2:07am
v3
Comments
Richard MacCutchan 22-Aug-23 3:34am    
Either your input data is not correct, or some calculations are not working. Use the debugger to find out where things a re going wrong.

1 solution

Your issue might be related to the indexing of your 'gap_series DataFrame'. The '.tail(200)' method you are using is used to select the last 200 rows of your 'DataFrame', which might not be available for all coins at all times. This could result in 'NaN' values returned in your 'DataFrame' as you have shown, leading to the incorrect output.

Make sure that you have valid data for both 'df_binance_close' and 'df_upbit_close' before calculating the 'gap_series'. You can use the 'dropna()' method to remove rows with 'NaN' values from both DataFrames - Pandas DataFrame dropna() Method | W3Schools[^]

Your code will then look like -
Python
for i in range(5):
    upbit_coin = upbit_coin_list[i]
    binance_coin = binance_coin_list[i]

    exchange_rate_today = get_exchange_rate("USD", 'KRW')
    df_binance = GetOhlcv(binanceX, binance_coin, '1m')
    df_binance_close = df_binance["close"].tail(200).dropna()  # Remove NaN values
    df_upbit = pyupbit.get_ohlcv(upbit_coin, interval="minute1")
    df_upbit_close = df_upbit["close"].tail(200).dropna()  # Remove NaN values

    if not df_binance_close.empty and not df_upbit_close.empty:
        gap_series = (df_binance_close * exchange_rate_today - df_upbit_close) / (
                  df_binance_close * exchange_rate_today) * 100
        gap_df = pd.DataFrame(gap_series, columns=['now_gap'])
        now_gap = gap_series.iloc[-2]

        print(gap_series, gap_df, now_gap)
    else:
        print("Insufficient data for calculations.")
 
Share this answer
 
Comments
Maciej Los 16-Nov-23 14:15pm    
5ed!
Andre Oosthuizen 16-Nov-23 14:29pm    
Thanks Maciej Los!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900