-
Notifications
You must be signed in to change notification settings - Fork 4
/
trading_utils_simple.py
105 lines (71 loc) · 3.34 KB
/
trading_utils_simple.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
import numpy as np
# check when stop loss is hit, when profit taking is hit and when liquidation is hit.
def checkLongExitSimple(row, prices, factors,interest):
long_liq = factors[0]
short_liq = factors[1]
leverage = factors[2]
liq_price = row.open*(1-long_liq)
#check if liquidated
if row.low < liq_price:
trading_returns = -interest
tag = 'liquidated'
else:
trading_returns = interest*leverage*row.price_change #in-range long return
tag = 'in_range'
return trading_returns, tag
def checkShortExitSimple(row, prices, factors,interest):
long_liq = factors[0]
short_liq = factors[1]
leverage = factors[2]
liq_price = row.open*(1+short_liq)
#check if liquidated
if row.high > liq_price:
trading_returns = -interest
tag = 'liquidated'
else:
trading_returns = -1*interest*leverage*row.price_change #in-range short return
tag = 'in_range'
return trading_returns, tag
def checkEntrySimple(hourlyData, location, row, leverage, mm_min):
location = hourlyData.index.get_loc(location)
moving_avg = hourlyData.close.iloc[location-24*5:location:24].mean() #average of previous 3 days close
if row.close > moving_avg:
position = 1
else:
position = -1
short_liq = (1+leverage)/(leverage*(mm_min+1))-1+0.005 #adjustment from ftx high to perpV2
long_liq = 1-(1-leverage)/(leverage*(mm_min-1))+0.005 #adjustment from ftx low to perpV2
factors = [long_liq, short_liq, leverage]
return position, factors
def backtestMoonshotSimple(hourlyData, weeklyData, capital, stables_yield, freq, leverage, mm_min):
position = 0
returns = 0
interest = 0
bench = capital
for i,row in weeklyData.iterrows():
weeklyData.loc[i,'capital'] = capital
weeklyData.loc[i,'position'] = position
weeklyData.loc[i,'interest'] = interest
weeklyData.loc[i,'benchmark'] = bench
#have a position from previous week, calc returns
if abs(position) > 0:
#get all hourly prices for the current trading week
location = hourlyData.index.get_loc(i)
weeksPrices = hourlyData[['close','high','low']].iloc[location-24*freq:location]
if position == 1:
trading_returns, tag = checkLongExitSimple(row, weeksPrices, factors, interest)
elif position == -1:
trading_returns, tag = checkShortExitSimple(row, weeksPrices, factors, interest)
weeklyData.loc[i,'flag'] = tag
weeklyData.loc[i,'trading_returns'] = trading_returns
weeklyData.loc[i,'returns'] = trading_returns + interest
weeklyData.loc[i,'leverage'] = factors[2]
capital += trading_returns + interest
#signal logic for next weeks trade
if weeklyData.index.get_loc(i) > 0: #only starts after 1 week of accruing interest
position,factors = checkEntrySimple(hourlyData,i,row,leverage,mm_min)
#interest for next weeks trade
interest = capital * stables_yield/365*freq
bench += bench*stables_yield/365*freq
finalCapital = weeklyData.loc[i,'capital'] + trading_returns + interest #add last period returns and current weeks interest
return weeklyData, finalCapital