-
Notifications
You must be signed in to change notification settings - Fork 0
/
backtesting.py
178 lines (141 loc) · 5.82 KB
/
backtesting.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import pandas as pd
from ta.momentum import StochasticOscillator
from ta.volume import ForceIndexIndicator, VolumeWeightedAveragePrice
btc_df = pd.read_csv('csv/BTCUSDT_May.csv', index_col=0)
stoch_indicator = StochasticOscillator(close=btc_df['Close'], high=btc_df['High'], low=btc_df['Low'])
force_index_indicator = ForceIndexIndicator(close=btc_df['Close'], volume=btc_df['Volume'])
vwap_indicator = VolumeWeightedAveragePrice(close=btc_df['Close'], high=btc_df['High'], low=btc_df['Low'], volume=btc_df['Volume'])
# Add TA indicators as columns to cloned dataframe
btc_df['Stoch K'] = stoch_indicator.stoch()
btc_df['Force Index'] = force_index_indicator.force_index()
btc_df['VWAP'] = vwap_indicator.volume_weighted_average_price()
# btc_df = btc_df.tail(1483583)
commission = 1.0006
slippage = 0.999
lastDropTrigger = 0.006
previousPrice = 99999.0
currentPrice = 99999.0
stopPrice = 99999.0
newStopPrice = 99999.0
firstRun = True
attemptPurchase = False
activeOrder = False
orderType = "b"
lastPurchasePriceAt = 99999.0
noOfOrders = 0
USDT_balance = 5000
BTC_balance = 0
BTC_cost_basis = 0
USDT_spent = 0
purchase_size = 10
buy_orders = 0
sell_orders = 0
def trail_order(tick):
# print(tick)
close = tick[3]
global firstRun, previousPrice, stopPrice, newStopPrice, attemptPurchase, activeOrder, orderType, lastPurchasePriceAt, slippage
if attemptPurchase:
if orderType == "b":
if firstRun:
previousPrice = close
# print("Initial price is " + str(previousPrice))
stopPrice = previousPrice / slippage
# print("Initial Stop Loss Price updated to {:.4f}".format(stopPrice))
firstRun = False
else:
currentPrice = close
# if currentPrice == previousPrice:
# print("The price has stayed the same")
if currentPrice < previousPrice:
# print("Current price is " + str(currentPrice))
newStopPrice = currentPrice / slippage
if newStopPrice < stopPrice:
stopPrice = newStopPrice
# print("Stop Loss Price updated to {:.4f}".format(stopPrice))
if currentPrice >= stopPrice:
# print("Bought at " + str(currentPrice))
buy(currentPrice)
lastPurchasePriceAt = currentPrice
attemptPurchase = False
activeOrder = True
firstRun = True
previousPrice = currentPrice
elif orderType == "s":
if firstRun:
previousPrice = close
# print("Initial price is " + str(previousPrice))
stopPrice = previousPrice * slippage
# print("Initial Stop Loss Price updated to {:.4f}".format(stopPrice))
firstRun = False
else:
currentPrice = close
# if currentPrice == previousPrice:
# print("The price has stayed the same")
if currentPrice > previousPrice:
# print("Current price is " + str(currentPrice))
newStopPrice = currentPrice * slippage
if newStopPrice > stopPrice:
stopPrice = newStopPrice
# print("Stop Loss Price updated to {:.4f}".format(stopPrice))
if currentPrice <= stopPrice:
# print("Sold at " + str(currentPrice))
sell(currentPrice)
attemptPurchase = False
activeOrder = False
firstRun = True
previousPrice = currentPrice
def buy(BTC_price):
global USDT_balance, BTC_balance, USDT_spent, noOfOrders, purchase_size, buy_orders, lastDropTrigger
# BTC_price = BTC_price * commission
if noOfOrders == 0:
purchase_size = 10
else:
purchase_size = purchase_size * 2
USDT_amount = purchase_size
if USDT_amount <= USDT_balance:
USDT_balance -= USDT_amount
USDT_spent += USDT_amount
BTC_balance += USDT_amount / BTC_price / commission
noOfOrders += 1
buy_orders += 1
def sell(BTC_price):
global USDT_balance, BTC_balance, USDT_spent, noOfOrders, sell_orders
# BTC_price = BTC_price / commission
USDT_balance += BTC_balance * BTC_price / commission
BTC_balance = 0
USDT_spent = 0
noOfOrders = 0
sell_orders += 1
def calcCostBasis():
global USDT_spent, BTC_balance
return USDT_spent / BTC_balance * commission * commission
def calcProfitTarget():
return calcCostBasis() * 1.003
for index, tick in btc_df.iterrows():
trail_order(tick)
# Initial entry
if not activeOrder:
lastDropTrigger = 0.004
orderType = "b"
attemptPurchase = True
# If I have any open positions
if BTC_balance > 0:
# If I'm at profit
if tick['Close'] > calcProfitTarget():
orderType = "s"
attemptPurchase = True
# If price dropped further
if tick['Close'] < (lastPurchasePriceAt * (1 - lastDropTrigger)):
lastDropTrigger = lastDropTrigger * 1
orderType = "b"
attemptPurchase = True
print("Final USDT balance is")
print(USDT_balance)
print("Final BTC balance is")
print(BTC_balance)
algo_return = (((USDT_balance + BTC_balance * btc_df['Close'][-1]) / 5000) - 1) * 100
print("Mr. Scalperman has bestowed upon you {:.2f}%".format(algo_return))
bnh_return = ((btc_df['Close'][-1] / btc_df['Close'][0]) - 1) * 100
print("If you were to just buy and hodl, you would have gotten {:.2f}%".format(bnh_return))
print("Total buy orders {:.2f}".format(buy_orders))
print("Total sell orders {:.2f}".format(sell_orders))