-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPivot(L&R)+slope(vs code).py
239 lines (187 loc) · 6.19 KB
/
Pivot(L&R)+slope(vs code).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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# { import the libraries
import ccxt
from datetime import datetime
import pandas as pd
#import pandas_ta as ta
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# }
# { show all rows and column
pd.set_option('display.max_rows', None)
#pd.set_option('display.max_column', None)
# }
# { load exchange
exchange = ccxt.binance({
'options': {
'adjustForTimeDifference': True,
},
})
# }
# { load data as function
def fetch(symbol: str, timeframe: str, limit: int):
print(f"Fetching {symbol} new bars for {datetime.now().isoformat()}")
bars = exchange.fetch_ohlcv(
symbol, timeframe=timeframe, limit=limit) # fetch ohlcv
df = pd.DataFrame(bars[:-1], columns=['timestamp',
'open', 'high', 'low', 'close', 'volume'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
#df = df.set_index(pd.DatetimeIndex(df.timestamp))
return df
# }
# { set the symbol for data function
BTC = fetch('BTC/USDT', '1h', 900)
# }
# { function for calculate povit high
def PIVOTHIGH(data: str = BTC['high'], left_bar: int = 14, right_bar: int = 14):
data_len = np.size(data)
pivothigh = []
for i in range(data_len - right_bar):
pivothigh.append(np.nan)
pivot = True
if i > left_bar:
for x in range(left_bar + 1):
if data[i-x] > data[i]:
pivot = False
for x in range(right_bar + 1):
if data[i+x] > data[i]:
pivot = False
if pivot is True:
pivothigh[np.size(pivothigh)-1] = data[i]
for i in range(right_bar):
pivothigh.append(np.nan)
return np.round(pivothigh, 2)
# }
# { function for calculate povit low
def PIVOTLOW(data: str = BTC['low'], left_bar: int = 14, right_bar: int = 14):
data_len = np.size(data)
pivotlow = []
for i in range(data_len - right_bar):
pivotlow.append(np.nan)
pivot = True
if i > left_bar:
for x in range(left_bar + 1):
if data[i-x] < data[i]:
pivot = False
for x in range(right_bar + 1):
if data[i+x] < data[i]:
pivot = False
if pivot is True:
pivotlow[np.size(pivotlow)-1] = data[i]
for i in range(right_bar):
pivotlow.append(np.nan)
return np.round(pivotlow, 2)
# }
# { ATR (averge true range)
def ATR(data: str = BTC, length: int = 14):
h_l = data['high'] - data['low']
h_pc = np.abs(data['high'] - data['close'].shift())
low_pc = np.abs(data['low'] - data['close'].shift())
tr = np.max(pd.concat([h_l, h_pc, low_pc], axis=1), axis=1)
atr = tr.rolling(length).mean()
return np.round(atr, 2)
# }
# { pivot high calclution
def SLOPE_PH(data: str, ATR: str, pivot: str, lenpivot: int = 14):
slope = np.zeros(np.size(data))
slope[:lenpivot] = np.nan
for i in range(lenpivot, np.size(data), 1):
if data[i] == 'True':
atr = ATR[i]
slope[i] = pivot[i]
if data[i] == 'False':
slope[i] = slope[i-1]-atr
return np.round(slope)
# }
# { pivot low calclution
def SLOPE_PL(data: str, ATR: str, pivot: str, lenpivot: int = 14):
slope = np.zeros(np.size(data))
slope[:lenpivot] = np.nan
for i in range(lenpivot, np.size(data), 1):
if data[i] == 'True':
atr = ATR[i]
slope[i] = pivot[i]
if data[i] == 'False':
slope[i] = slope[i-1]+atr
return np.round(slope)
# }
# { pivothigh
BTC['pivothigh'] = PIVOTHIGH()
BTC['pivothigh'][:14] = np.nan
BTC['pivothighfill'] = BTC['pivothigh'].fillna(method='ffill')
# }
# { pivotlow
BTC['pivotlow'] = PIVOTLOW()
BTC['pivotlow'][:14] = np.nan
BTC['pivotlowfill'] = BTC['pivotlow'].fillna(method='ffill')
# }
# { return bool if condition is true for add slope
BTC['pivothigh_bool'] = np.where(
BTC['pivothigh'] == BTC['high'], 'True', 'False')
BTC['pivotlow_bool'] = np.where(
BTC['pivotlow'] == BTC['low'], 'True', 'False')
# }
# { use atr function and / len(pivot) * multy
multy = 1
BTC['atr'] = np.round(ATR()/14*multy)
# }
# { use slop functions to calcluate pivot slope
BTC['slope_ph'] = SLOPE_PH(BTC['pivothigh_bool'], BTC['atr'], BTC['pivothigh'])
BTC['slope_ph'] = np.where(BTC['pivothigh_bool'] ==
'True', np.NAN, BTC['slope_ph'])
BTC['slope_pl'] = SLOPE_PL(BTC['pivotlow_bool'], BTC['atr'], BTC['pivotlow'])
BTC['slope_pl'] = np.where(BTC['pivotlow_bool'] ==
'True', np.NAN, BTC['slope_pl'])
# }
print(BTC)
# { plot the data
#fig = make_subplots(rows=2, cols=1, shared_xaxes=True)
fig = go.Figure()
fig.add_trace(go.Candlestick(x=BTC.index,
open=BTC['open'],
high=BTC['high'],
low=BTC['low'],
close=BTC['close'],
showlegend=False))
fig.add_trace(go.Scatter(x=BTC.index,
y=BTC['slope_ph'],
opacity=0.7,
line=dict(color='green', width=2, dash='dot'),
name='pivothigh'))
fig.add_trace(go.Scatter(x=BTC.index,
y=BTC['slope_pl'],
opacity=0.7,
line=dict(color='red', width=2, dash='dot'),
name='pivotlow'))
fig.add_trace(go.Scatter(
x=BTC.index,
y=BTC['pivotlow'],
mode="markers+text",
name="Markers and Text",
text=BTC['pivotlow'],
textfont=dict(
family="sans serif",
size=15,
color="red"
),
textposition="bottom center"))
fig.add_trace(go.Scatter(
x=BTC.index,
y=BTC['pivothigh'],
mode="markers+text",
name="Markers and Text",
text=BTC['pivothigh'],
textfont=dict(
family="sans serif",
size=15,
color="green"
),
textposition="top center"))
# colors = ['green' if row['open'] - row['close'] >= 0
# else 'red' for index, row in BTC.iterrows()]
# fig.add_trace(go.Bar(x=BTC.index,
# y=BTC['volume'],
# marker_color=colors
# ), row=2, col=1)
fig.show()
# }