-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathdva.py
65 lines (56 loc) · 2.35 KB
/
dva.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
import logging
import datetime
from positions import *
from algorithm import Algorithm
from indicators import *
from plot import *
import pprint
stocks = ['SPY']
class DVATest(Algorithm):
def __init__(self, symbols, start_date, end_date):
self.one_day = datetime.timedelta(days=1)
self.target_value = 0
self.expected_monthly_return = 1.006
self.initial_investment = 500
self.initial_investment_made = False
super(DVATest, self).__init__(symbols, start_date, end_date)
def handle_data(self, dt, symbols, keys, data):
for symbol in symbols:
px = data[(symbol, 'close')]
if self.initial_investment_made == False:
shares_to_order = round((self.initial_investment - 9.99) / px)
self.oms.add(Transaction(symbol, dt, px, shares_to_order))
print('Initial investment: we buy %s shares for %s on %s' % (shares_to_order, px, dt))
self.initial_investment_made = True
self.target_value += self.initial_investment * self.expected_monthly_return
next_trade_day = None
while next_trade_day is None or next_trade_day.weekday() > 4:
if next_trade_day is None:
next_trade_day = dt
next_trade_day = next_trade_day + self.one_day
current_month = dt.month
cost = self.oms.portfolio.positions[symbol].amount * px
if next_trade_day.month != current_month:
if cost < self.target_value:
budget = self.target_value - cost - 9.99
shares_to_order = round(budget / px)
if shares_to_order >= 2:
self.oms.add(Transaction(symbol, dt, px, shares_to_order))
print('Target value (%s) exceeds portfolio value (%s). We buy %s shares for %s on %s' % (self.target_value, cost, shares_to_order, px, dt))
else:
print("Minimum buying threshold not reached")
elif self.oms.portfolio.positions[symbol].is_open():
budget = cost - self.target_value - 9.99
shares_to_sell = round(budget / px)
if shares_to_sell >= 2:
self.oms.add(Transaction(symbol, dt, px, -shares_to_sell))
print('Portfolio value (%s) is below target value (%s). We sell %s shares for %s on %s' % (cost, self.target_value, shares_to_sell, px, dt))
else:
print("Minimum selling threshold not reached")
self.target_value += 500
def post_run(self):
self.results()
for symbol in self.symbols:
self.plot(symbol=symbol).show()
test = DVATest(stocks, '20130103', '20131021')
test.run()