-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerfectSetup
executable file
·135 lines (111 loc) · 3.97 KB
/
PerfectSetup
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
#!/usr/bin/python3
#
# Shows indicators, scores them or filters them based on "The Perfect Setup"
# as given by "The 5 Secrets To Highly Profitable Swing Trading" by Ivaylo Ivanov.
#
#
# [ The "Perfect" Setup ]
# Previous uptrend
# Near 10-day high (within 3% of it)
# High relative strength on weekly, monthly, quarterly or half year basis
# Tight side-ways consolidation on below average volume, lasting anywhere between 2 and 20 trading days.
# The closing prices of the past few trading days are very near to each other
# Coiled near its 5, 10 or 20-day moving average
# Trading above its 5-day moving average
# 5-day moving average is above its 20-day moving average
#
#
# [ Good Potential ]
# Up >10% over week
# Up >20% over month (or >10% for large cap)
# Up >30% over quarter (or >20% for large cap)
# Up >40% over 6 months (or >30% for large cap)
#
#
# [ Breakout Setup ]
# Low-volume range contraction (2 to 20 days)
# Up from the open >2%
# Daily change >2%
# New 10-day high
# 5-day MA >20-day MA
# Price is above its 5-day MA
# Average daily volume >50k
# Relative volume >1
# Price >2
# Set stop at the lows of the 2% breakout day
#
#
# [ Anticipation Setup (i.e. buying early for a breakout) ]
# Works only in strong up-trending markets, where most setups eventually break out
# May not trigger immediately
# Be aware of strongest industries in the market at the time
#
#
# [Don't Buy If ...]
# ... there is no range contraction
# ... the stock is already up 3-4 days in a row
import argparse
import fetcher
import printing
CLOSE = fetcher.DataSource.CLOSE
OPEN = fetcher.DataSource.OPEN
HIGH = fetcher.DataSource.HIGH
LOW = fetcher.DataSource.LOW
VOLUME = fetcher.DataSource.VOLUME
class ComputedResult(object):
"""Bag of data to keep the results of some computed values."""
def __init__(self, **kwargs):
self.good = False
self.value = None
self.print_value = str(self.value)
self.print_value_color = str(self.value)
self.data = {}
for k,v in kwargs.items():
setattr(self, k, v)
def __nonzero__(self):
return self.good
class ThePerfectSetup(object):
def __init__(self, ticker):
self.ticker = ticker
self.df = fetcher.DataFetcher().FetchData(ticker)
def PreviousUptrend(self):
"""Previous uptrend."""
def NearTenDayHigh(self):
"""Near 10-day high (within 3% of it)."""
todays = float(self.df.tail(1)[HIGH])
highest = float(self.df.tail(10)[HIGH].max())
percent = (todays - highest) / highest
result = ComputedResult(good=False,
value=percent,
print_value='{:.1f}%'.format(percent * 100),
print_value_color='{:.1f}%'.format(percent * 100),
data={'todays_highest': todays,
'ten_day_highest': highest,
'percent': percent})
if percent >= -0.03 and percent <= 0.03:
result.good = True
return result
def HighRelativeStrength(self):
"""High relative strength on weekly, monthly, quarterly or half year basis."""
def TightConsolidation(self):
"""Tight side-ways consolidation on below average volume, lasting anywhere between 2 and 20 trading days."""
def CloseClosingPrices(self):
"""The closing prices of the past few trading days are very near to each other."""
def CoiledNearSMAs(self):
"""Coiled near its 5, 10 or 20-day moving average."""
def TradingAbove5SMA(self):
"""Trading above its 5-day moving average."""
def FiveSMAAbove20SMA(self):
"""5-day moving average is above its 20-day moving average."""
def main(args):
headers = ['Ticker', 'Near 10d High']
printer = printing.TabularPrinter(headers=headers)
rows = []
for ticker in args.tickers:
ps = ThePerfectSetup(ticker)
rows.append((ticker, bool(ps.NearTenDayHigh())))
printer.print(rows)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('tickers', nargs='+')
main(parser.parse_args())