Reputation: 35
I am trying to check the price change of all the crypto at a 15-minute interval. Till now, I have been able to fetch all coins traded with USDT pair, get their price change percentage, and sort then descending order. Now the plan is to wait for 15 min, again fetch the data, and compare it with the old one. This will keep on happening every 15 minutes. (For example, let's say I fetched data for 5.30, at 5.45 again data will be fetched - comparison will be made and the most volatile coin will be chosen. At 6.00, the data will be fetched again, now it has to be compared with data of 5.45 and so on.)
This is what I have till now (This is based on CCXT library for python)This code works for one-time fetch, all I need is some guidance on how to do comparison and store/update new values.Should I create new file pricechange2
and keep comparing with it or should I keep on adding and deleting columns in dataframe?
markets=exchange.fetchTickers()
with open('pricechange1.txt', 'w') as json_file:
json.dump(markets, json_file)
with open('pricechange1.txt') as json_file:
data = json.load(json_file)
symbol_list = [value['symbol'] for value in data.values()]
r = re.compile(".*/USDT")
filtered_list = list(filter(r.match, symbol_list))
fl = list([x for x in filtered_list if "DOWN" not in x and "UP" not in x and "BULL" not in x and "BEAR" not in x])
pp1 = []
for i in fl:
pp = data[i]['info']['priceChangePercent']
pp1.append(float(pp))
df = pd.DataFrame({'coinpair': fl})
df['change1'] = pp1
df = df.sort_values(by='change1', ascending=False)
print(df['coinpair'].head(10).iloc[0])
print(df.head(10))
Current output of above code
coinpair change1
90 FTT/USDT 29.677
48 FTM/USDT 24.615
246 TORN/USDT 24.508
259 MINA/USDT 23.265
156 YFII/USDT 20.739
37 CELR/USDT 18.435
179 INJ/USDT 17.601
207 FIRO/USDT 15.848
206 TWT/USDT 15.715
126 VTHO/USDT 15.634
Upvotes: 1
Views: 536
Reputation: 2692
I would recommend putting those things into separate functions and using schedule
library to run them periodically (in your case in every 15 mins). First of all, you need to install it using:
pip install schedule
Then import it in your script:
import schedule
import time
...
Now, put everything into a separate function:
markets = None
def fetch_data():
global markets
markets = exchange.fetchTickers()
return markets
def write_to_file(data):
with open('pricechange1.txt', 'w') as json_file:
json.dump(data, json_file)
previous_pp = None
def form_df(data, previous_pp):
global previous_pp
symbol_list = [value['symbol'] for value in data.values()]
r = re.compile(".*/USDT")
filtered_list = list(filter(r.match, symbol_list))
fl = list([x for x in filtered_list if "DOWN" not in x and "UP" not in x and "BULL" not in x and "BEAR" not in x])
pp1 = []
for i in fl:
if previous_pp:
current_pp = data[i]['info']['priceChangePercent']
if previous_pp > current_pp:
# do something
# update previous_pp value
else:
# do something
# update previous_pp value
else:
previous_pp = current_pp = data[i]['info']['priceChangePercent']
pp1.append(float(current_pp))
df = pd.DataFrame({'coinpair': fl})
df['change1'] = pp1
df = df.sort_values(by='change1', ascending=False)
print(df['coinpair'].head(10).iloc[0])
print(df.head(10))
And run them periodically as below:
schedule.every(15).minutes.do(fetch_data)
schedule.every(15).minutes.do(write_to_file, data=markets)
schedule.every(15).minutes.do(form_df, data=markets, previous_p=previous_pp)
while True:
schedule.run_pending()
time.sleep(1)
Upvotes: 1