-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathperp_utils.py
149 lines (96 loc) · 6.32 KB
/
perp_utils.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import pandas as pd
import numpy as np
def runPerpBacktest(weeklyInputs,hourlyInputs,freq,leverage,mm_min,interest,currency,strategy,take_profit=0.15,stop_loss=0.05):
# weeklyInputData Pandas dataframe, weekly frequency
# Columns required, open,close,high,low,price_change,position
outputData = weeklyInputs.copy()
short_liq = (1+leverage)/(leverage*(mm_min+1))-1
long_liq = 1-(1-leverage)/(leverage*(mm_min-1))
if short_liq < stop_loss or long_liq < stop_loss:
print("warning stop loss to low for leverage")
stop_loss = min(short_liq,long_liq)/1.1
if strategy == 'simple':
outputData['liq_price'] = np.where(weeklyInputs.position>0,weeklyInputs.open*(1-long_liq),weeklyInputs.open*(1+short_liq))
outputData['perpReturns'] = 0
maskLongLiqs = ((weeklyInputs.position > 0) & (weeklyInputs.low < outputData.liq_price) )
maskShortLiqs = ((weeklyInputs.position < 0) & (weeklyInputs.high > outputData.liq_price) )
maskElse = (maskLongLiqs == False) & (maskShortLiqs == False)
if currency == 'USD':
outputData.loc[(maskLongLiqs | maskShortLiqs),'perpReturns'] = -interest
outputData.loc[maskElse,'perpReturns'] = interest*leverage*weeklyInputs.loc[maskElse,'position']*weeklyInputs.loc[maskElse,'price_change']
elif currency == 'ETH':
outputData.loc[(maskLongLiqs | maskShortLiqs),'perpReturns'] = -interest
marginReturn = interest * (weeklyInputs.loc[maskElse,'open']/weeklyInputs.loc[maskElse,'close']-1)
tradingProfitETH = interest * leverage * weeklyInputs.loc[maskElse,'position'] * (1-weeklyInputs.loc[maskElse,'open']/ weeklyInputs.loc[maskElse,'close'])
outputData.loc[maskElse,'perpReturns'] = marginReturn + tradingProfitETH
elif strategy == 'enhanced':
#enhanced perp strategy returns
outputData['take_profit'] = np.where(weeklyInputs.position>0,weeklyInputs.open*(1+take_profit),weeklyInputs.open*(1-take_profit))
outputData['stop_loss'] = np.where(weeklyInputs.position>0,weeklyInputs.open*(1-stop_loss),weeklyInputs.open*(1+stop_loss))
#find exit flag
outputData['exit_flag'] = 0
exit_flags = checkPerpExit(outputData, hourlyInputs,take_profit,stop_loss, long_liq,short_liq,freq)
outputData.loc[outputData.index,'exit_flag'] = exit_flags.copy()
maskTakeProfit = (outputData.exit_flag == 'take_profit')
maskStopLoss = (outputData.exit_flag == 'stop_loss')
maskInRange = (outputData.exit_flag == 'in_range')
if currency == 'USD':
outputData.loc[maskTakeProfit,'perpReturns'] = interest*leverage*take_profit
outputData.loc[maskStopLoss,'perpReturns'] = -interest*leverage*stop_loss
outputData.loc[maskInRange,'perpReturns'] = interest*leverage*weeklyInputs.loc[maskInRange,'position']*weeklyInputs.loc[maskInRange,'price_change']
elif currency == 'ETH':
marginReturn = interest * (weeklyInputs['open']/weeklyInputs['close']-1)
outputData.loc[maskTakeProfit,'perpReturns'] = interest*leverage*take_profit*weeklyInputs.loc[maskTakeProfit,'open']/weeklyInputs.loc[maskTakeProfit,'close']+marginReturn.loc[maskTakeProfit]
outputData.loc[maskStopLoss,'perpReturns'] = -interest*leverage*stop_loss*weeklyInputs.loc[maskStopLoss,'open']/weeklyInputs.loc[maskStopLoss,'close']+marginReturn.loc[maskStopLoss]
outputData.loc[maskInRange,'perpReturns'] = interest * leverage * weeklyInputs.loc[maskInRange,'position'] * (1-weeklyInputs.loc[maskInRange,'open']/ weeklyInputs.loc[maskInRange,'close']) + marginReturn.loc[maskInRange]
else:
print("Incorrect strategy name")
alpha = outputData.perpReturns.sum() / (outputData.shape[0]/52)
return outputData,alpha
def checkPerpExit(exitData, hourlyData,take_profit,stop_loss, long_liq,short_liq,freq):
for i,row in exitData.iloc[0:,:].iterrows():
flag = 0
loc = hourlyData.index.get_loc(i)
weeksPrices = hourlyData.close.iloc[loc-freq*24:loc]
if row.position < 0:
take_profitCount = np.sum( weeksPrices < row.open*(1-take_profit) , axis = 0)
stop_lossCount = np.sum( weeksPrices > row.open*(1+stop_loss) , axis = 0)
if take_profitCount + stop_lossCount == 0:
flag = 'in_range'
elif (take_profitCount+stop_lossCount) == take_profitCount:
flag = 'take_profit'
exitPrice = row.open*(1-take_profit)
elif (take_profitCount+stop_lossCount) == stop_lossCount:
flag = 'stop_loss'
exitPrice = row.open*(1+stop_loss)
else: #stop loss and takeprofit hit
tp = (weeksPrices < row.take_profit).argmax(axis = 0)
sl = (weeksPrices > row.stop_loss).argmax(axis = 0)
if tp < sl:
flag = 'take_profit'
exitPrice = row.open*(1-take_profit)
else:
flag = 'stop_loss'
exitPrice = row.open*(1+stop_loss)
elif row.position > 0:
take_profitCount = np.sum( weeksPrices > row.open*(1+take_profit) , axis = 0)
stop_lossCount = np.sum( weeksPrices < row.open*(1-stop_loss) , axis = 0)
if take_profitCount + stop_lossCount == 0:
flag = 'in_range'
elif (take_profitCount+stop_lossCount) == take_profitCount:
flag = 'take_profit'
exitPrice = row.open*(1+take_profit)
elif (take_profitCount+stop_lossCount) == stop_lossCount:
flag = 'stop_loss'
exitPrice = row.open*(1-stop_loss)
else: #stop loss and takeprofit hit
tp = (weeksPrices > row.take_profit).argmax(axis = 0)
sl = (weeksPrices < row.stop_loss).argmax(axis = 0)
if tp < sl:
flag = 'take_profit'
exitPrice = row.open*(1+take_profit)
else:
flag = 'stop_loss'
exitPrice = row.open*(1-stop_loss)
exitData.loc[i,'exit_flag'] = flag
return exitData['exit_flag']