-
Notifications
You must be signed in to change notification settings - Fork 2
/
support_resist.py
84 lines (65 loc) · 2.57 KB
/
support_resist.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
import pandas as pd
import numpy as np
import math
import matplotlib.dates as mpl_dates
import matplotlib.pyplot as plt
from mplfinance.original_flavor import candlestick_ohlc
from get_data import get_data_without_date
import cufflinks as cf
df = get_data_without_date()
#method 1: fractal candlestick pattern
# determine bullish fractal
def is_support(df,i):
cond1 = df['low'][i] < df['low'][i-1]
cond2 = df['low'][i] < df['low'][i+1]
cond3 = df['low'][i+1] < df['low'][i+2]
cond4 = df['low'][i-1] < df['low'][i-2]
return (cond1 and cond2 and cond3 and cond4)
# determine bearish fractal
def is_resistance(df,i):
cond1 = df['high'][i] > df['high'][i-1]
cond2 = df['high'][i] > df['high'][i+1]
cond3 = df['high'][i+1] > df['high'][i+2]
cond4 = df['high'][i-1] > df['high'][i-2]
return (cond1 and cond2 and cond3 and cond4)
# to make sure the new level area does not exist already
def is_far_from_level(value, levels, df):
ave = np.mean(df['high'] - df['low'])
return np.sum([abs(value-level)<ave for _,level in levels])==0
# a list to store resistance and support levels
levels_supp = []
levels_resist = []
for i in range(2, df.shape[0] - 2):
if is_support(df, i):
low = df['low'][i]
if is_far_from_level(low, levels_supp, df):
levels_supp.append((i, low))
elif is_resistance(df, i):
high = df['high'][i]
if is_far_from_level(high, levels_resist, df):
levels_resist.append((i, high))
cf.set_config_file(offline = True)
ohlc = df.set_index('date')
qf = cf.QuantFig(ohlc)
num_resist = len(levels_resist)
num_supp = len(levels_supp)
list_support=[]
# for x in range(0 , num_supp):
# list_support.append ( df.loc[levels_supp[x][0]].low)
# date =df.loc[levels_supp[x][0]].date
# qf.add_support(date= date,to_strfmt='%Y-%m-%d %H:%M:%S')
for x in range(0 , num_resist):
list_support.append ( df.loc[levels_resist[x][0]].low)
date =df.loc[levels_resist[x][0]].date
qf.add_resistance(date= date,to_strfmt='%Y-%m-%d %H:%M:%S')
qf.iplot(title="btc-usdt")
def plot_all(levels, df):
fig, ax = plt.subplots(figsize=(16, 9))
candlestick_ohlc(ax,df.values,width=0.6, colorup='green',
colordown='red', alpha=0.8)
date_format = mpl_dates.dateFormatter('%d %b %Y')
ax.xaxis.set_major_formatter(date_format)
for level in levels:
plt.hlines(level[1], xmin = df['date'][level[0]], xmax =
max(df['date']), colors='blue', linestyle='--')
fig.show()