-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBacktesting.py
More file actions
52 lines (37 loc) · 1.48 KB
/
Backtesting.py
File metadata and controls
52 lines (37 loc) · 1.48 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
import Global
from Exporting import chopDateFrame
from Parameters import parameters
def betaReturns():
percReturns = 100 * (Global.marketDataFrame["c"][Global.rowsTotal - 1] - Global.marketDataFrame["c"][0])/Global.marketDataFrame["c"][0]
return percReturns
def backtest(orders, balance, propInitBuy, propBuy, propSell):
df = chopDateFrame(parameters=parameters)
dfRows = df.shape[0]
initialBalance = balance
sharesOwned = 0
sharesOwned += (balance * propInitBuy)/df["c"].iloc[0]
balance -= (balance*propInitBuy)
for order in orders:
num = order[1]
if order[0] == "buy":
sharesOwned += (balance * propBuy)/df["c"].iloc[num]
balance -= balance * propBuy
if order[0] == "sell":
sharesValue = sharesOwned * df["c"].iloc[num]
sharesOwned -= (sharesValue * propSell)/df["c"].iloc[num]
balance += sharesValue * propSell
assetTotal = balance + sharesOwned * df["c"][dfRows - 1]
percReturns = 100*(assetTotal - initialBalance)/initialBalance
return percReturns
def alphaCalc(betaReturns, Returns):
alpha = Returns - betaReturns
return alpha
def andOrderLists(orders1, orders2):
# 1. Convert inner lists to tuples
set1 = set(map(tuple, orders1))
set2 = set(map(tuple, orders2))
# 2. Perform set operation
common = set1 & set2 # {(3, 4)}
# 3. Convert back to list of lists
result = [list(t) for t in common]
return result