forked from UNSW-Fintech-Society/algothon24-starter-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
72 lines (62 loc) · 2.03 KB
/
eval.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
70
71
72
#!/usr/bin/env python
import numpy as np
import pandas as pd
from Cutiepatooties import getMyPosition as getPosition
nInst = 0
nt = 0
commRate = 0.0010
dlrPosLimit = 10000
def loadPrices(fn):
global nt, nInst
df = pd.read_csv(fn, sep='\s+', header=None, index_col=None)
(nt, nInst) = df.shape
return (df.values).T
pricesFile = "./prices.txt"
prcAll = loadPrices(pricesFile)
print("Loaded %d instruments for %d days" % (nInst, nt))
def calcPL(prcHist):
cash = 0
curPos = np.zeros(nInst)
totDVolume = 0
totDVolumeSignal = 0
totDVolumeRandom = 0
value = 0
todayPLL = []
(_, nt) = prcHist.shape
for t in range(250, 501):
prcHistSoFar = prcHist[:, :t]
newPosOrig = getPosition(prcHistSoFar)
curPrices = prcHistSoFar[:, -1]
posLimits = np.array([int(x) for x in dlrPosLimit / curPrices])
newPos = np.clip(newPosOrig, -posLimits, posLimits)
deltaPos = newPos - curPos
dvolumes = curPrices * np.abs(deltaPos)
dvolume = np.sum(dvolumes)
totDVolume += dvolume
comm = dvolume * commRate
cash -= curPrices.dot(deltaPos) + comm
curPos = np.array(newPos)
posValue = curPos.dot(curPrices)
todayPL = cash + posValue - value
todayPLL.append(todayPL)
value = cash + posValue
ret = 0.0
if (totDVolume > 0):
ret = value / totDVolume
print("Day %d value: %.2lf todayPL: $%.2lf $-traded: %.0lf return: %.5lf" %
(t, value, todayPL, totDVolume, ret))
pll = np.array(todayPLL)
(plmu, plstd) = (np.mean(pll), np.std(pll))
annSharpe = 0.0
if (plstd > 0):
annSharpe = np.sqrt(250) * plmu / plstd
return (plmu, ret, plstd, annSharpe, totDVolume)
(meanpl, ret, plstd, sharpe, dvol) = calcPL(prcAll)
score = meanpl - 0.1*plstd
print("=====")
print("mean(PL): %.1lf" % meanpl)
print("return: %.5lf" % ret)
print("StdDev(PL): %.2lf" % plstd)
print("annSharpe(PL): %.2lf " % sharpe)
print("totDvolume: %.0lf " % dvol)
print("Score: %.2lf" % score)