-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scanner.py
69 lines (55 loc) · 2.69 KB
/
Scanner.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
from AppConstants import StrategyCode
from AppConstants import INDICATORS
class Scanner:
def __init__(self, param):
self.param = param
self.functions = {
StrategyCode.ALL_NEGATIVE.name: self.all_negative,
StrategyCode.FIB_RETRACE.name: self.fibonacci_retracement,
StrategyCode.VWAP_REV.name: self.vwap_rev
}
def scan(self, asset_list):
strategy = self.param['strategy']
executor = self.functions.get(strategy['name'], lambda a, b: True)
for asset in asset_list:
if len(asset.klines) > 5:
asset.is_displayed = executor(asset, strategy)
if asset.is_displayed:
print(f"Output => {asset.symbol}")
def vwap_rev(self, asset, strategy) -> bool:
vwap_df = asset.indicators[INDICATORS.VWAP.name]
if asset.klines.iloc[-3].high < asset.klines.iloc[-1].high < vwap_df.iloc[-1, 0]:
return True
def macd_hist_negative(self, asset) -> bool:
macd_df = asset.indicators[INDICATORS.MACD.name]
idx = -1
hist = []
while macd_df.iloc[idx].macdhist <= 0:
hist.append(macd_df.iloc[idx].macdhist) #stored in reversed order
idx = idx - 1
if len(hist) > 4:
min_index = hist.index(min(hist))
if 1 < min_index < len(hist) - 2:
if hist[min_index-2] > hist[min_index] and \
hist[min_index-1] > hist[min_index] and \
hist[min_index] < hist[min_index+1] and \
hist[min_index] < hist[min_index+2]:
return True
def all_negative(self, asset, strategy) -> bool:
sar = asset.indicators[INDICATORS.SAR.name]
bb = asset.indicators[INDICATORS.BBANDS.name]
if self.macd_hist_negative(asset):
high = asset.klines.iloc[-1].high
if high < bb.iloc[-1].middle and high < sar.iloc[-1].real:
return True
def fibonacci_retracement(self, asset, strategy) -> bool:
fibonacci = asset.indicators[INDICATORS.FIBONACCI.name]
if fibonacci is None:
return False
last_bar = asset.klines.iloc[-1]
target_level = strategy["fibonacciLevel"]
fib_direction = "down" if fibonacci[0]['percent'] < fibonacci[-1]['percent'] else "up"
if fib_direction == "down" and fibonacci[target_level - 1]["price"] >= last_bar.low >= fibonacci[target_level]["price"]:
return True
elif fib_direction == "up" and fibonacci[target_level - 1]["price"] >= last_bar.high >= fibonacci[target_level]["price"]:
return True