-
Notifications
You must be signed in to change notification settings - Fork 38
/
austrian-quant-3-pseudo.py
62 lines (38 loc) · 2.28 KB
/
austrian-quant-3-pseudo.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
PSEUDOCODE
# todo read and do the following tutorials
# https://www.quantopian.com/posts/simple-machine-learning-example
# 1. https://www.quantopian.com/tutorials/pipeline
# 2. https://www.quantopian.com/posts/machine-learning-on-quantopian (do parts 1-3)
# 3. https://www.quantopian.com/posts/simple-machine-learning-example
run this function once a year:
def value_find():
for stock in sp500[1:500]:
""" Turn each stock in the SP 500 into a feature set using financial metrics from Morning star"""
# input data should be randomized to prevent over learning a particular segment
X = X.append(get_features(stock))
y = y.append(get_performance(stock))
# todo use ensemble method add more classifiers, 4 classifiers in total should be used.
X = X.preprocessing.scale(X)
clf = RandomForestClassifier()
clf.learn(X,y)
stock_dict = {stock_ticker : 'probability',} # a dictionary of stock tickers and their corresponding buy probabilities
for stock in sp500[1:500]: # Top 250 stocks by Market Cap, why top 250?
performance = clf.predict(get_features(stock))
prob = clf.predict_proba(get_features(stock))
if performance == 1: # algorithm thinks it is a buy
stock_dict['ticker'] = prob
# https://docs.python.org/dev/library/collections.html#ordereddict-examples-and-recipes
stock_dict_ordered = OrderedDict(sorted(stock_dict.items(), key=lambda t: t[0]))
allocation = 0.1 # Set how much of the full portfolio we will allocate to the value fund
sum_prob = sum(stock_dict_ordered.items()) # todo how to get sum of values in a dictionary
for stock in context.value_stocks:# if it is not a top ten stock, sell our position
if stock not in stock_dict_ordered[1:10]:
order_target_percent(sid(stock, 0)
for stock in stock_dict_ordered[1:10]: #todo how to access keys and value of dictionary when iterating
sum_prob = stock.value/sum_prob # the stronger the probability, the more money allocated to that stock
if stock not in context.value_stocks:
order_target_percent(sid(stock.key), sum_prob * allocation)
def get_features(stock = 'SPY'):
""" Use Morningstart metrics to generate a feature set for the given stock"""
def get_performance((stock = 'SPY'):
""" Compare the annual % change in stock price to annual % change of sp500, if >sp500, return 1 (buy)"""