Issue
I have the following two batches of code; the first one pulls in data from the Binance API:
while True:
await socket.__aenter__()
msg = await socket.recv()
frame = createFrame(msg)
frame.to_sql("BTCUSDT", engine, if_exists = 'append', index = False)
print(frame)
The output of the above looks something like this:
With every second, a new line of data is generated.
The above will of course run continuously, unless interrupted.
Then I have the code below, which pulls data from the above table. But the issue I have is that the above WILL NOT stop running, so we will never get to the code below. I can of course stop the above after a certain number of lines, but what I want is to pull live data into the below code. Any ideas how I can rearrange my code to achieve this?
x_axis = []
y_axis = []
def animate(i):
x_axis.append(frame.iloc[-1][1])
y_axis.append(frame.iloc[-1][2])
plt.cla()
plt.plot(x_axis,y_axis)
ani = FuncAnimation(plt.gcf(),animate,interval = 1000)
plt.show()
Solution
I just implemented this code and found your question. In this code you can see how I mixed matplot getting data from Binance. What I do with the data is not much of a deal, it's a demo.
By the way, you have to use the FuncAnimation to display live data, no matter the source is coming from. This implementation has possible flows, the time interval of the requests is tied to the refresh of the animation.
import requests
import pandas as pd
import matplotlib.pyplot as plt
from time import sleep
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
I kept my "properties" outside of the function to update the animation.
# Capital initial.
initial_capital = 100
# Montant du capital à investir.
operation_invest_rate = 0.1
# Capital cumulatif investi pendant la durée de l'opération.
invest_capital = initial_capital*operation_invest_rate
# Frais de transaction.
fee_rate = 0.001
# Interval de transaction.
trade_time_interval = 2000
# Ticker choisi.
token_selected = "ATOKEN"
# Source de données.
data_door = "https://api.binance.com/api/v3/depth"
In addition I instantiate the graph. In this case a scatter plot. I also used arrays to store the future values in the function for the following update.
# Stockage pour visualisation.
prix_achat = []
prix_vente = []
fig = plt.figure()
ax = plt.axes()
ax.set_xlabel("Prix d'achat", fontsize=15)
ax.set_ylabel("Prix de vente", fontsize=15)
ax.set_title("Prix d'achat vs Prix de vente", fontsize=20)
ax.grid(True)
ax.scatter(prix_achat, prix_vente, color='blue', alpha=0.3)
Here you can observe the implementation of the update function where I had to embed all the querying and data manipulation because its the only function which is going to run in the animation process. My "props" are called in as globals (bad practice) so they are accessible inside and outside.
def update(i):
global initial_capital
global operation_invest_rate
global invest_capital
global fee_rate
global trade_time_interval
global token_selected
global data_door
Here I do "stuff" with the data. It is not important, go to the next comment.
r = requests.get(data_door,params=dict(symbol=token_selected))
results = r.json()
# Lier les données de réponse et définir les métadonnées.
frames ={
side: pd.DataFrame(
data=results[side],
columns=["price", "quantity"],
dtype=float)
for side in ["bids", "asks"]
}
# Assigner les colonnes.
frames_list = [frames[side].assign(side=side) for side in frames]
# Definir les axes.
data = pd.concat(frames_list, axis="index", ignore_index=True, sort=True)
# Groupes de données par niveau de prix.
price_summary = data.groupby("side").price.describe()
print(price_summary)
spread = price_summary._get_value("asks","mean") - price_summary._get_value("bids","mean")
spread_percent = spread / price_summary._get_value("asks","mean")
Here is the answer to your question. Once the new data is available you can add it to your original data and call the graph a new time.
# Mise a jour du graphique.
prix_achat.append(buy_unit_price)
prix_vente.append(sell_unit_price)
ax.scatter(prix_achat, prix_vente, color='blue', alpha=0.5)
Once your function defined you can call the animation and show the graph.
animation = FuncAnimation(plt.gcf(), update, interval=trade_time_interval)
plt.tight_layout()
plt.show()
Answered By - WolftrotDev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.