Hi,
I fetched ohlc from future account in the below code, but my RSI and some indicators is different with Binance app. It may be comed from fetching the data not correctly, may wrong with endpoint, pls check and advise.
-
The code for fetch data:
current_timestamp_utc = datetime.datetime.now(pytz.utc)#current_timestamp_utc = datetime.now(pytz.utc) current_timestamp = current_timestamp_utc.astimezone(pytz.utc) since_timestamp_pre_unix = current_timestamp - timedelta(seconds=limit * 5 * 60) since_timestamp = int(since_timestamp_pre_unix.timestamp() * 1000) print(f"Fetching data for {symbol}...") print(f"Timeframe: {timeframe}") print(f"Since: {since_timestamp}") print(f"Exchange: {exchange}") symbol_without_slash = symbol.replace('/', '') # Replace '/' with '' headers = { 'X-MBX-APIKEY': api_key } url = f"https://api.binance.com/api/v3/klines" params = { 'symbol': symbol_without_slash, 'interval': timeframe, 'limit': limit, 'startTime': since_timestamp } #print("Before fetching data") response = requests.get(url, headers=headers, params=params) #print("After fetching data") if response.status_code == 200: data = response.json() #print(f"Data: {data}") if not data: print(f"No data found for {symbol}. Skipping...") return print(f"Fetched data for {symbol}: {data}") #print("Before processing data") df = pd.DataFrame(data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'qav', 'num_trades', 'taker_base_vol', 'taker_quote_vol', 'ignore']) df['close'] = pd.to_numeric(df['close'], errors='coerce') df = df[['timestamp', 'open', 'high', 'low', 'close', 'volume']] df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms') df.set_index('timestamp', inplace=True) if not isinstance(df.index, pd.DatetimeIndex): raise ValueError("Index is not a DatetimeIndex") print(f"Data fetched successfully for {symbol}...")
-
The code for RSI:
Calculate RSI
def calculate_rsi(df, n=14):
deltas = df['close'].diff(1)
up = deltas.where(deltas > 0, 0)
down = -deltas.where(deltas < 0, 0)
roll_up = up.ewm(span=n, min_periods=n).mean()
roll_down = down.ewm(span=n, min_periods=n).mean()
rs = roll_up / roll_down
rsi = 100.0 - (100.0 / (1.0 + rs))
return rsi
Thank you