-
Notifications
You must be signed in to change notification settings - Fork 0
/
squeeze momentum(BBband,Keltner ch , ATR ).py
282 lines (216 loc) · 10.1 KB
/
squeeze momentum(BBband,Keltner ch , ATR ).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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# { import the libraries
import ccxt
from datetime import datetime
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas_ta as ta
# }
# { 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
# }
# Bollinger Bands® are a technical analysis tool developed by John Bollinger for generating oversold or overbought signals.
# There are three lines that compose Bollinger Bands: A simple moving average(middle band) and an upper and lower band.
# The upper and lower bands are typically 2 standard deviations + /- from a 20-day simple moving average, but they can be modified.
# {Here is this Bollinger Band® formula:
# BOLU=MA(TP,n)+m∗σ[TP,n]
# BOLD=MA(TP,n)−m∗σ[TP,n]
# where:
# BOLU=Upper Bollinger Band
# BOLD=Lower Bollinger Band
# MA=Moving average
# TP (typical price)=(High+Low+Close)÷3
# n=Number of days in smoothing period (typically 20)
# m=Number of standard deviations (typically 2)
# σ[TP,n]=Standard Deviation over last n periods of TP
# } https://www.investopedia.com/terms/b/bollingerbands.asp
# { bollinger band
def SMA(data: str = 'BTC', src: str = 'close', length: int = 20): # sma for middle band
return data[src].rolling(window=length).mean()
def TP(data: str = 'BTC'): # hlc/3 (typical price)
return(data['high']+data['low']+data['close'])/3
def BOLU(data: str = "BTC", src: str = 'close', length: int = 20, m: int = 1.5): # upperband
return SMA(data, src, 20)+((m)*data[src].rolling(window=length).std())
def BOLD(data: str = 'BTC', src: str = 'close', length: int = 20, m: int = 1.5): # lower band
return SMA(data, 'close', 20)-((m)*data[src].rolling(window=length).std())
# }
# { ATR (averge true range)
def ATR(data: str = 'BTC', length: int = 20):
tr = abs(data['high']-data['low'])
atr = tr.rolling(window=length).mean()
return atr
# }
# Keltner Channels are volatility-based bands that are placed on either side of an asset's price and can aid in determining the direction of a trend.
# The exponential moving average (EMA) of a Keltner Channel is typically 20 periods, although this can be adjusted if desired.
# The upper and lower bands are typically set two times the average true range (ATR) above and below the EMA, although the multiplier can also be adjusted based on personal preference.
# Price reaching the upper Keltner Channel band is bullish, while reaching the lower band is bearish.
# The angle of the Keltner Channel also aids in identifying the trend direction. The price may also oscillate between the upper and lower Keltner Channel bands, which can be interpreted as resistance and support levels.
# Keltner Channel Calculation :
# Keltner Channel Middle Line=EMA
# Keltner Channel Upper Band=EMA+2∗ATR
# Keltner Channel Lower Band=EMA−2∗ATR
# where:
# EMA=Exponential moving average (typically over 20 periods)
# ATR=Average True Range (typically over 10 or 20 periods)
# } https://www.investopedia.com/terms/k/keltnerchannel.asp
# { Keltner channel (this calculetion is difrent and like Squeeze Momentum Indicator [LazyBear] in tradingview )
def UPPERKC(data: str = 'BTC', src: str = 'close', length: int = 20, multKC: float = 1.5):
ma = SMA(data, src, length)
rangema = ATR(data, length)
upperKC = ma + rangema * multKC
return upperKC
def LOWERKC(data: str = 'BTC', src: str = 'close', length: int = 20, multKC: float = 1.5):
ma = SMA(data, src, length)
rangema = ATR(data, length)
lowerKC = ma - rangema * multKC
return lowerKC
# }
# { Squeeze finder
def SQZON(data: str = 'BTC', upperband: str = 'upperband', lowerband: str = 'lowerband', upperKC: str = 'upperKC', lowerKC: str = 'lowerKC'):
return (data[lowerband] > data[lowerKC]) and (data[upperband] < data[upperKC])
def SQZOFF(data: str = 'BTC', upperband: str = 'upperband', lowerband: str = 'lowerband', upperKC: str = 'upperKC', lowerKC: str = 'lowerKC'):
return (data[lowerband] < data[lowerKC]) and (data[upperband] < data[upperKC])
# }
# { set the symbol for data function
BTC = fetch('BTC/USDT', '1h', 1000)
# }
# { use bollinger band functions to calculate bbband
BTC['middelband'] = SMA(BTC, 'close', 20)
BTC['tp'] = TP(BTC)
BTC['upperband'] = BOLU(BTC, 'close')
BTC['lowerband'] = BOLD(BTC, 'close')
# }
# { use keltner channel functions for calculate upper and lower kc channel
BTC['upperKC'] = UPPERKC(BTC)
BTC['lowerKC'] = LOWERKC(BTC)
# }
# { use squeez finder functions
BTC['in_sqz'] = BTC.apply(SQZON, axis=1)
BTC['out_sqz'] = BTC.apply(SQZOFF, axis=1)
# }
# { for set marker color
BTC['squeez'] = np.where(BTC['in_sqz'] == True,
BTC['lowerKC']*0.9, np.NAN) # gray
BTC['bullish_momentum'] = np.where((BTC['in_sqz'] == False) & (
BTC['close'] > BTC['middelband']), BTC['lowerKC']*0.9, np.NAN) # green
BTC['berish_momentum'] = np.where((BTC['in_sqz'] == False) & (
BTC['close'] < BTC['middelband']), BTC['lowerKC']*0.9, np.NAN) # red
# }
# { for candlestick color (set new dataframe)
berish = BTC[(BTC['in_sqz'] == False) & (
BTC['close'] < BTC['middelband'])]
not_berish = BTC[BTC.index.isin(berish.index)].index
bulish = BTC[(BTC['in_sqz'] == False) & (
BTC['close'] > BTC['middelband'])]
not_bulish = BTC[BTC.index.isin(bulish.index)].index
sqz = BTC[BTC['in_sqz'] == True]
not_sqz = BTC[BTC.index.isin(sqz.index)].index
# }
# print(BTC)
# { plot the data
fig = go.Figure()
fig.add_trace(go.Candlestick(x=BTC.index,
open=BTC['open'],
high=BTC['high'],
low=BTC['low'],
close=BTC['close'],
name='Candlestick',
visible='legendonly'))
fig.add_traces(go.Candlestick(x=berish.index,
open=berish['open'], high=berish['high'],
low=berish['low'], close=berish['close'],
increasing_line_color='red',
decreasing_line_color='DarkRed',
name='Berish momentum(+/-)'))
fig.add_traces(go.Candlestick(x=bulish.index,
open=bulish['open'], high=bulish['high'],
low=bulish['low'], close=bulish['close'],
increasing_line_color='SpringGreen',
decreasing_line_color='DarkGreen',
name='Bulish momentum(+/-)'))
fig.add_traces(go.Candlestick(x=sqz.index,
open=sqz['open'], high=sqz['high'],
low=sqz['low'], close=sqz['close'],
increasing_line_color='Silver',
decreasing_line_color='DimGrey',
name='Squeez(+/-)'))
fig.add_trace(go.Scatter(x=BTC.index,
y=BTC['middelband'],
opacity=1,
line=dict(color='orange', width=2),
name='middelband',
visible='legendonly'))
fig.add_trace(go.Scatter(x=BTC.index,
y=BTC['upperband'],
fill=None,
mode='lines',
line_color='rgba(0, 0, 255, 0.5)',
line=dict(width=2),
name='upperband',
visible='legendonly'))
fig.add_trace(go.Scatter(x=BTC.index,
y=BTC['lowerband'],
opacity=0.3,
fill='tonexty',
line=dict(width=2),
name='lowerband',
line_color='rgba(0, 0, 255, 0.5)',
mode='lines', fillcolor='rgba(0, 0, 255, 0.1)',
visible='legendonly'))
fig.add_trace(go.Scatter(x=BTC.index,
y=BTC['upperKC'],
opacity=1,
line=dict(color='blue', width=2),
name='upperKC',
visible='legendonly'))
fig.add_trace(go.Scatter(x=BTC.index,
y=BTC['lowerKC'],
opacity=1,
line=dict(color='blue', width=2),
name='lowerKC',
visible='legendonly'))
fig.add_trace(go.Scatter(x=BTC.index,
y=BTC['squeez'],
mode='markers',
marker=dict(color='gray', size=5,
opacity=1),
marker_symbol=203,
name='squeez',
visible='legendonly'))
fig.add_trace(go.Scatter(x=BTC.index,
y=BTC['bullish_momentum'],
mode='markers',
marker=dict(color='green', size=5,
opacity=1),
marker_symbol=5,
name='bullish momentum',
visible='legendonly'))
fig.add_trace(go.Scatter(x=BTC.index,
y=BTC['berish_momentum'],
mode='markers',
marker=dict(color='red', size=5,
opacity=1),
marker_symbol=6,
name='berish momentum',
visible='legendonly'))
fig.show()