Calculate Support and Resistance
Tags: #ccxt #bitcoin #trading
Author: Jeremy Ravenel

Input

1
!pip install trendln matplotlib==3.1.3 --user
Copied!
1
import naas
2
import ccxt
3
import pandas as pd
4
from datetime import datetime
5
import naas_drivers
6
import trendln
7
import plotly.tools as tls
8
import plotly.graph_objects as go
Copied!

Setup Binance

1
binance_api = ""
2
binance_secret = ""
Copied!

Variables

1
symbol = 'BTC/USDT'
2
limit = 180
3
timeframe = '4h'
Copied!

Model

Get data

1
binance = ccxt.binance({
2
'apiKey': binance_api,
3
'secret': binance_secret
4
})
5
6
data = binance.fetch_ohlcv(symbol=symbol,
7
limit=limit,
8
timeframe=timeframe)
Copied!

Data cleaning

1
df = pd.DataFrame(data, columns=["Date", "Open", "High", "Low", "Close", "Volume"])
2
df['Date'] = [datetime.fromtimestamp(float(time)/1000) for time in df['Date']]
3
df
Copied!

Output

Plotting figure

1
fig = trendln.plot_support_resistance(
2
df[-1000:].Close, #as per h for calc_support_resistance
3
xformatter = None, #x-axis data formatter turning numeric indexes to display output
4
# e.g. ticker.FuncFormatter(func) otherwise just display numeric indexes
5
numbest = 1, #number of best support and best resistance lines to display
6
fromwindows = True, #draw numbest best from each window, otherwise draw numbest across whole range
7
pctbound = 0.1, # bound trend line based on this maximum percentage of the data range above the high or below the low
8
extmethod = trendln.METHOD_NUMDIFF,
9
method=trendln.METHOD_PROBHOUGH,
10
window=125,
11
errpct = 0.005,
12
hough_prob_iter=50,
13
sortError=False,
14
accuracy=1)
Copied!
1
plotly_fig = tls.mpl_to_plotly(fig)
Copied!
1
layout = dict(
2
dragmode="pan",
3
xaxis_rangeslider_visible=False,
4
showlegend=True,
5
)
6
new_data = list(plotly_fig.data)
7
new_data.pop(2)
8
new_data.pop(2)
9
new_data.pop(1)
10
new_data.pop(1)
11
fig = go.Figure(data=new_data, layout=layout)
12
fig
Copied!
Copy link
Edit on GitHub