This repository was archived by the owner on Jan 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathbasic_chart.py
122 lines (83 loc) · 3.55 KB
/
basic_chart.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
from asciichart import plot, plot_str
import numpy as np
import pandas as pd
import math
# -----------------------------------------------------------------------------
this_folder = os.path.dirname(os.path.abspath(__file__))
root_folder = os.path.dirname(os.path.dirname(this_folder))
sys.path.append(root_folder + '/python')
sys.path.append(this_folder)
# -----------------------------------------------------------------------------
import ccxt # noqa: E402
# -----------------------------------------------------------------------------
symbols = ['┼', '┤', '╶', '╴', '─', '└', '┌', '┐', '┘', '│']
class BasicCharts():
def __init__(self, exchange=ccxt.binance(), symbol="BTC/USDT", timeframe="5m", limit=24, index=4):
self.exchange = exchange
self.symbol = symbol
self.timeframe = timeframe
self.limit = limit
self.index = index
def set_symbol(self, symbol="BTC/USDT"):
self.symbol = symbol
def set_limit(self, limit=100):
self.limit = limit
def set_timeframe(self, timeframe="5m"):
if timeframe in ["1m", "5m", "15m", "1h", "4h"]:
self.timeframe = timeframe
def get_timeframe(self):
return self.timeframe
def get_ohlcv(self, timeframe=None, limit=None):
if timeframe is not None:
return self.exchange.fetch_ohlcv(symbol=self.symbol, timeframe=timeframe, limit=limit)
else:
return self.exchange.fetch_ohlcv(symbol=self.symbol, timeframe=self.timeframe, limit=self.limit)
def prep_ohlcv(self, ohlcv):
df = pd.DataFrame(ohlcv, columns=['date', 'Open', 'High', 'Low', 'Close', 'Volume'])
df['date'] = pd.to_datetime(df['date'], unit = 'ms', utc=True)
df.set_index('date', inplace=True)
df['Close'] = df['Close'].apply(float)
return df
def get_chart_arr(self, height=20, basic_symbols=False):
# get a list of ohlcv candles
ohlcv = self.get_ohlcv(limit=self.limit)
# get the ohlcv (closing price, index == 4)
series = [x[self.index] for x in ohlcv]
cfg = {'height': height}
if basic_symbols:
cfg['symbols'] = symbols
# print the chart
return plot(series, cfg) # return chart array
def get_chart_str(self, height=20, width=120, trades=None, basic_symbols=False):
# get a list of ohlcv candles
ohlcv = self.get_ohlcv(limit=self.limit)
# get the ohlcv (closing price, index == 4)
series = [x[self.index] for x in ohlcv]
cfg = {'height': height}
if basic_symbols:
cfg['symbols'] = symbols
outstr = plot_str(plot(series, cfg))
return outstr
def get_profit_str(self, trades, height=20, width=120, basic_symbols=False):
profit = 0
profitseries = [0]
for x in trades:
profit = profit + int(x['close_profit_abs'])
profitseries.append(profit)
cfg = {'height': height, 'min': min(profitseries)}
if basic_symbols:
cfg['symbols'] = symbols
# print the chart
outstr = plot_str(plot(profitseries[-self.limit:], cfg))
return outstr
def print_chart(self):
print(self.get_chart_str())
def main():
btcgbp_charts = BasicCharts(symbol="BTC/GBP")
btcgbp_charts.combo_chart()
if __name__ == "__main__":
main()