Loading...
Searching...
No Matches
pickstock.py
Go to the documentation of this file.
6
7if __name__ == "__main__":
8
9 # [1]
10 from gams.magic import GamsInteractive
11 gams = GamsInteractive()
12
13
18
19 # [2]
20 import pandas as pd
21 url="https://github.com/daveh19/pydataberlin2017/raw/master/notebooks/dowjones2016.csv"
22 price_data=pd.read_csv(url)
23
24 # [3]
25 m = gams.exchange_container
26 date = m.addSet('date', description='trading date')
27 symbol = m.addSet('symbol', description='stock symbol')
28 price = m.addParameter('price', [date, symbol], domain_forwarding=True, records=price_data, description='price of stock on date')
29 d = m.addAlias('d', date)
30 s = m.addAlias('s', symbol)
31
32
35
36 # [4]
37 avgprice = m.addParameter('avgprice', [symbol], description='average price of stock')
38 gams.gams('avgprice(s) = sum(d, price(d,s))/card(d);')
39 print(avgprice.records.head())
40
41
44
45 # [5]
46 weight = m.addParameter('weight', [symbol], description='weight of stock')
47 gams.gams('weight(symbol) = avgprice(symbol)/sum(s, avgprice(s));')
48 print(weight.records.head())
49
50
53
54 # [6]
55 contribution = m.addParameter('contribution', [date,symbol])
56 gams.gams('contribution(d,s) = weight(s)*price(d,s);')
57 print(contribution.records.head())
58
59
62
63 # [7]
64 index = m.addParameter('index', [date], description='Dow Jones index')
65 gams.gams('index(d) = sum(s, contribution(d,s));')
66 print(index.records.head())
67
68
71
72 # [8] - [9] - skipped
73
74
77
78 # [10]
79 trainingdays = m.addParameter('trainingdays', records = 100)
80 maxstock = m.addParameter('maxstock', records = 3, description='maximum number of stocks to select')
81 ds = m.addSet('ds', [date], description='selected dates')
82 ds.setRecords(date.records['uni'][:100])
83
84
87
88 # [11]
89 p = m.addVariable('p', 'binary', [symbol], description = 'is stock included?')
90 w = m.addVariable('w', 'positive', [symbol], description = 'what part of the portfolio')
91 slpos = m.addVariable('slpos', 'positive', [date], description = 'positive slack')
92 slneg = m.addVariable('slneg', 'positive', [date], description = 'negative slack')
93 obj = m.addVariable('obj', 'free', description = 'objective')
94
95
105
106 # [12]
107 gams.gams('''
108Equation deffit(date) 'fit to Dow Jones index';
109deffit(ds).. sum(s, price(ds,s)*w(s)) =e= index(ds) + slpos(ds) - slneg(ds);
110
111Equation defpick(symbol) 'can only use stock if picked';
112defpick(s).. w(s) =l= p(s);
113
114Equation defnumstock 'few stocks allowed';
115defnumstock.. sum(s, p(s)) =l= maxstock;
116
117Equation defobj 'absolute violation (L1 norm) from index';
118defobj.. obj =e= sum(ds, slpos(ds) + slneg(ds));
119
120Model pickStock /all/;''')
121
122
125
126 # [13]
127 ds.setRecords(date.records['uni'][:100])
128 maxstock.setRecords(3)
129
130 # [14]
131 gams.gams('option optCR=0.01, resLim=6; solve pickStock min obj using mip;')
132
133
136
137 # [15]
138 fund = m.addParameter('fund', [date], description='Index fund report parameter')
139 gams.gams('fund(d) = sum(s, price(d, s)*w.l(s));')
140 error = m.addParameter('error', [date], description='Absolute error')
141 gams.gams('error(d) = abs(index(d)-fund(d));')
142
143
146
147 # [16] - [17] - skipped
148
149 # [18]
150 gams.gams_cleanup(closedown=True)
GamsInteractive slneg
Definition: pickstock.py:92
GamsInteractive p
Declaration of the variables and equations used to formulate the optimization model.
Definition: pickstock.py:89
GamsInteractive slpos
Definition: pickstock.py:91
GamsInteractive w
Definition: pickstock.py:90
GamsInteractive price
Definition: pickstock.py:28
GamsInteractive index
Compute index values.
Definition: pickstock.py:64