-
Notifications
You must be signed in to change notification settings - Fork 8
/
First Algorithm v2.py
68 lines (45 loc) · 1.84 KB
/
First Algorithm v2.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
def initialize(context):
context.max_stocks = 100
context.leverage = 0.99
schedule_function(func = adjust_portfolio,
date_rule = date_rules.month_start())
set_universe(universe.DollarVolumeUniverse \
(floor_percentile = 98.0,ceiling_percentile = 100.0))
def adjust_portfolio(context, data):
stockcount = 0
stocks_to_buy = []
for stocks in context.stock_selection:
if data.can_trade(stocks) and stockcount < context.max_stocks:
stocks_to_buy.append(stocks)
stockcount = stockcount + 1
for pos in context.portfolio.positions:
if context.portfolio.positions[pos].amount > 0 and pos not in stocks_to_buy:
order_target_value(pos, 0)
for stock in stocks_to_buy:
order_target_percent(stock, context.leverage / stockcount)
def before_trading_start(context, data):
"""
Called before the start of each trading day.
It updates our universe with the
securities and values found from fetch_fundamentals.
"""
stock_selection = get_fundamentals(
query(
fundamentals.valuation_ratios.pe_ratio,
fundamentals.valuation.enterprise_value,
)
.filter(fundamentals.valuation_ratios.pe_ratio > 25)
.filter(fundamentals.valuation.enterprise_value > 100000000)
)
context.stock_selection = stock_selection
def record_account_info(context, data):
P = context.portfolio
counter = 0
for stock in data:
amount = P.positions[stock].amount
if amount > 0:
counter = counter + 1
record(stock_count = counter)
record(cash = context.portfolio.cash)
def handle_data(context, data):
record_account_info(context, data)