-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSPY-Arbitrage.py
69 lines (60 loc) · 1.97 KB
/
SPY-Arbitrage.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
import quantopian.algorithm as algo
import numpy as np
def initialize(context):
"""
Called once at the start of the algorithm.
"""
# Rebalance every day, 1 hour after market open.
algo.schedule_function(
rebalance,
algo.date_rules.every_day(),
algo.time_rules.market_close(hours=1),
)
context.aapl = symbol('AAPL')
context.xom = symbol('XOM')
context.bhp = symbol('BHP')
context.spy = symbol('SPY')
context.inpos = 0
def rebalance(context, data):
X1 = context.aapl
X2 = context.xom
X3 = context.bhp
Y = context.spy
P1 = data.history(X1,'price',200,'1d')
P2 = data.history(X2,'price',200,'1d')
P3 = data.history(X3,'price',200,'1d')
N = data.history(Y,'price',200,'1d')
w = [0.92919341, 0.8350131 , 0.02745906]
theo = w[0]*P1 + w[1]*P2 + w[2]*P3 - N + 35
sd_theo = np.std(theo)
if theo[-1] > 5 and context.inpos==0:
order_target_percent(X1,-w[0]/sum(w))
order_target_percent(X2,-w[1]/sum(w))
order_target_percent(X3,-w[2]/sum(w))
order_target_percent(Y,1)
context.inpos = -1
print "Enter Short"
print context.portfolio.positions
elif theo[-1] < 0 and context.inpos==-1:
order_target_percent(X1,0)
order_target_percent(X2,0)
order_target_percent(X3,0)
order_target_percent(Y,0)
context.inpos = 0
print "Exit Short"
print context.portfolio.positions
elif theo[-1] < -5 and context.inpos==0:
order_target_percent(X1,w[0]/sum(w))
order_target_percent(X2,w[1]/sum(w))
order_target_percent(X3,w[2]/sum(w))
order_target_percent(Y,-1)
context.inpos = 1
print "Enter Long"
elif theo[-1] > 0 and context.inpos==1:
order_target_percent(X1,0)
order_target_percent(X2,0)
order_target_percent(X3,0)
order_target_percent(Y,0)
context.inpos = 0
print "Exit Long"
record('theo',theo[-1])