Reputation: 1
I am trading on binance, I have a code that pulling data for 5 minutes candle (for example), when I click on run code, it will collect data, but how to continue pulling also for new candles ? this is my code:
import binance.client
from binance.client import Client
import pandas as pd
import numpy as np
import time
import datetime
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
Pkey = 'xxxxxxxx'
Skey = 'ccccccccccccc'
client = Client(api_key=Pkey, api_secret=Skey)
ticker = 'BTCUSDT'
interval = Client.KLINE_INTERVAL_5MINUTE
depth = '13 hours ago'
raw = client.get_historical_klines(ticker, interval, depth)
raw = pd.DataFrame(raw)
print(raw)
thanks
Upvotes: 0
Views: 384
Reputation: 11342
As @Selcuk mentioned is his comment, you can loop the binance read and pause between each read. In your case, you are retrieving data at 5 minute intervals, so you can wait 5 minutes before reading again and request the previous 5 minutes. You can append to the initial dataframe using append
.
Try this code:
import ......
Pkey = 'xxxxxxxx'
Skey = 'ccccccccccccc'
client = Client(api_key=Pkey, api_secret=Skey)
ticker = 'BTCUSDT'
interval = Client.KLINE_INTERVAL_5MINUTE
depth = '13 hours ago'
raw = client.get_historical_klines(ticker, interval, depth)
raw = pd.DataFrame(raw)
alldata = raw
print(raw) # intial load
depth = '5 minutes ago'
while True: # loop forever
time.sleep(300) # wait 5 minutes
raw = client.get_historical_klines(ticker, interval, depth) # 5 minutes of data
raw = pd.DataFrame(raw)
alldata.append(raw) # add to main dataset
Upvotes: 1