forked from Viandoks/python-crypto-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbotchart.py
More file actions
92 lines (71 loc) · 2.99 KB
/
botchart.py
File metadata and controls
92 lines (71 loc) · 2.99 KB
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
from botapi import BotApi
from botcandlestick import BotCandlestick
from datetime import datetime, timedelta
import pandas as pd
import time
import shared
import sys
from botlog import BotLog
class BotChart(object):
'Draws a classic trading chart, humanely readable'
def __init__(self,timeframe,startTime,endTime,backTest=True):
self.pair = shared.exchange['pair']
self.timeframe = str(timeframe)
self.startTime = str(startTime)
self.endTime = str(endTime)
self.backTest = bool(backTest)
self.output = BotLog()
self.tempCandle = None
self.data = []
# API
self.api = BotApi()
if backTest:
from_timestamp = self.api.parse8601(self.startTime)
try:
print(self.api.milliseconds(), 'Fetching candles starting from', self.api.iso8601(from_timestamp))
ohlcvs = self.api.fetch_ohlcv(shared.exchange['pair'], timeframe=self.timeframe, since=from_timestamp)
print(self.api.milliseconds(), 'Fetched', len(ohlcvs), 'candles')
for ohlcv in ohlcvs:
self.data.append(BotCandlestick(float(ohlcv[0]), float(ohlcv[1]), float(ohlcv[2]), float(ohlcv[3]), float(ohlcv[4]), float(ohlcv[5])))
except (ccxt.ExchangeError, ccxt.AuthenticationError, ccxt.ExchangeNotAvailable, ccxt.RequestTimeout) as error:
print('Got an error', type(error).__name__, error.args)
exit(2)
def getPoints(self):
return self.data
def getCurrentPrice(self):
if not self.backTest:
ticker = self.api.fetchTicker(self.pair)
price = ticker["last"]
return float(price)
def drawChart(self, candlesticks, orders, movingAverages):
# googlecharts
output = open("./output/data.js",'w')
output.truncate()
# candlesticks
candlesticks = pd.DataFrame.from_records([c.toDict() for c in candlesticks])
ma = pd.DataFrame(movingAverages)
if len(ma) > 0:
candlesticks['ma'] = ma
else:
candlesticks['ma'] = 0
candlesticks['date'] = candlesticks['date']/1000
candlesticks.set_index('date', inplace=True)
# orders
orders = pd.DataFrame.from_records([o.toDict() for o in orders])
orders['date'] = orders['date']/1000
if len(orders)>1:
orders.set_index('date', inplace=True)
else :
orders['orderNumber'] = 0
orders['rate'] = 0
orders['direction'] = 'None'
orders['stopLoss'] = 0
orders['takeProfit'] = 0
orders['exitRate'] = 0
# concat all to one dataframe
data = pd.concat([candlesticks, orders], axis=1)
data['direction'].fillna('None', inplace=True)
data.fillna(0, inplace=True)
# add to data.js
output.write("var dataRows = "+data.to_json(orient='index')+";")
output.write("var lastcall = '"+str(time.ctime())+"'")