-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy patheval.py
More file actions
83 lines (72 loc) · 2.53 KB
/
eval.py
File metadata and controls
83 lines (72 loc) · 2.53 KB
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
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
import numpy as np
import pandas as pd
from main 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+', names=cols, header=None, index_col=0)
df=pd.read_csv(fn, sep='\s+', header=None, index_col=None)
nt, nInst = df.values.shape
return (df.values).T
pricesFile="./prices.txt"
prcAll = loadPrices(pricesFile)
print ("Loaded %d instruments for %d days" % (nInst, nt))
currentPos = np.zeros(nInst)
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(750,1001):
prcHistSoFar = prcHist[:,:t]
newPosOrig = getPosition(prcHistSoFar)
curPrices = prcHistSoFar[:,-1] #prcHist[:,t-1]
posLimits = np.array([int(x) for x in dlrPosLimit / curPrices])
clipPos = np.clip(newPosOrig, -posLimits, posLimits)
newPos = np.array([np.trunc(x) for x in clipPos])
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
# *** Intermediate score calculation ***
pll = np.array(todayPLL)
(plmu, plstd) = (np.mean(pll), np.std(pll))
annSharpe = 0.0
if (plstd > 0):
annSharpe = np.sqrt(250) * plmu / plstd
score = plmu - 0.1 * plstd
# *** Intermediate score calculation (end) ***
print ("Day %d value: %.2lf todayPL: $%.2lf $-traded: %.0lf return: %.5lf Score: %.2lf" % (t,value, todayPL, totDVolume, ret, score))
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)