-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathplot_pnl_live.py
95 lines (67 loc) · 2.93 KB
/
plot_pnl_live.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import pandas as pd
import time
import datetime
import plotly.graph_objects as go
from telegram import Bot
from plotly.subplots import make_subplots
def send_image_bot():
bot = Bot(token='2109482112343214324:alkfjaslkfjasfoiwar')
return bot
def chart_pnl():
#Reading the pnl stored in the logs folder with the current date
#csv file contains 4 columns in a sequence "timestamp", "pnl","pts","roc"
df = pd.read_csv("logs"+"//"+'ts_pnl_'+str(datetime.datetime.now().date())+'.csv', header=None,error_bad_lines=False,engine='python')
df = df.dropna()
df =df[pd.to_numeric(df[2], errors='coerce').notnull()]
df.rename(columns = {0:"timestamp",1:"pnl",2:"pts",3:"roc"},inplace = True)
df['pnl']= df.pnl.apply(lambda x:float(x))
df['pts'] = df.pts.apply(lambda x:float(x))
df['roc'] = df.roc.apply(lambda x:float(x))
#removing the pnl values that contains zero
df = df[df['pnl'] !=0]
df['timestamp']= df['timestamp'].apply(lambda x:datetime.time(int(x[:2]),int(x[3:5])))
df['time'] = df['timestamp'].apply(lambda x: datetime.datetime.combine(datetime.datetime.now().date(), x) )
df.index = pd.to_datetime(df.time, unit='s')
#taking last pnl stored for every minute
df = df[~df.index.duplicated(keep='last')]
# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])
# Add traces
fig.add_trace(
go.Scatter(x=df.index, y=df.pts.values,name = 'Points', line=dict( width=3)),
secondary_y=False,
)
fig.add_trace(
go.Scatter(x=df.index, y=df.roc.values, name = 'Roc',line=dict( width=3)),
secondary_y=True,
)
fig.add_annotation(
x=df.index[-1],
y=df.pts[-1],
text = " "+str(round(df.pts[-1],1)),
secondary_y=False,
showarrow=True)
# Add figure title
fig.update_layout(
title_text="Ts Stats",
font=dict(
family="Courier New, monospace",
size=33,
color="RebeccaPurple"
))
# Set x-axis title
fig.update_xaxes(title_text="time")
# Set y-axes titles
fig.update_yaxes(title_text="<b>Points</b>", secondary_y=False)
fig.update_yaxes(title_text="<b>Roc</b>", secondary_y=True)
# fig.show()
#storing the files in "charts" folder
try:
os.makedirs("charts"+'//'+str(datetime.datetime.now().date()))
except FileExistsError:
pass
chart_name = "charts"+'//'+str(datetime.datetime.now().date())+'//'+str(time.time()).split('.')[0] +"-"+str('rs_pnl')+'.png'
fig.write_image(chart_name,width = 2400, height = 1500)
#bot that sends image to telegram
send_image_bot().send_photo(chat_id='@abc_1234', photo=open(chart_name, "rb"))