Skip to content

Commit fd7ca34

Browse files
committed
edit position tracking
1 parent f71e352 commit fd7ca34

File tree

1 file changed

+44
-78
lines changed

1 file changed

+44
-78
lines changed

algorithms/backtrader algos/bt algo v1/algorithm.py

Lines changed: 44 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,57 @@
1111
from ta.momentum import RSIIndicator
1212

1313
# Define position, pnl and pnl_counter
14-
position = 0
15-
pnl = 0
16-
profit_counter = 0
1714

1815
# Define conditions
1916
long_condition = True
2017
short_condition = False
2118

2219
# Define lists
23-
open_long = []
24-
open_short = []
25-
close_long = []
26-
close_short = []
2720

2821
class Algorithm:
2922

3023
def __init__(self, symbol, interval):
3124
self.symbol = symbol
3225
self.interval = interval
3326
self.data = deque([], maxlen=500)
27+
self.tmp = deque([], maxlen=500)
3428
self.position = 0
35-
self.pnl = 0
29+
self.pnl = []
3630
self.profit_counter = 0
31+
self.open_long = []
32+
self.open_short = []
33+
self.close_long = []
34+
self.close_short = []
3735

3836
historical_data = futures_get_hist(
3937
symbol = symbol,
4038
interval = interval
4139
)
4240

4341
for candle in historical_data:
44-
4542
self.data.append(float(candle[4]))
46-
# print(self.data[-1])
4743

4844
def get_ticks(self, candle):
4945

50-
# print(close)
51-
52-
# Implement strategy if candle is closed
53-
if candle['x'] == True:
54-
self.data.pop(0)
55-
self.data.append(float(candle['c']))
5646

57-
# Implement strategy if candle is open
58-
else:
59-
self.data[-1] = float(candle['c'])
60-
# Dataframe transform
47+
# When candle is open, append all data from self.data in tmp list and append tick data candle['c'] to tmp
48+
if candle['x'] == False:
49+
for close in list(self.data):
50+
self.tmp.append(close)
51+
self.tmp.append(float(candle['c']))
52+
6153
df = pd.DataFrame(
62-
data = list(self.data),
54+
data = list(self.tmp),
6355
columns = ['close']
6456
)
65-
# Bollinger bands
57+
58+
# Bollinger Bands
6659
indicator_bb = BollingerBands(
6760
close=df['close'],
6861
window=20,
6962
window_dev=2
7063
)
64+
7165
# Relative Strength Index
7266
indicator_rsi = RSIIndicator(
7367
close=df['close'],
@@ -78,103 +72,75 @@ def get_ticks(self, candle):
7872
df['bb_bbh'] = indicator_bb.bollinger_hband()
7973
df['bb_bbl'] = indicator_bb.bollinger_lband()
8074
df['rsi'] = indicator_rsi.rsi()
81-
# print(df.iloc[-1])
75+
8276
price = float(df['close'].iloc[-1])
8377
bb_high = float(df['bb_bbh'].iloc[-1])
8478
bb_low = float(df['bb_bbl'].iloc[-1])
8579
last_rsi = float(df['rsi'].iloc[-1])
8680

8781
# Positions
82+
8883
if self.position == 0:
8984

9085
# Long condition is met
91-
if price < bb_low and last_rsi < 35:
92-
86+
if price < bb_low or last_rsi < 30:
9387
print('GO LONG')
94-
position = 1
95-
96-
open_long.append(price)
88+
self.position = 1
89+
self.open_long.append(price)
9790

9891
# Short condition is met
99-
elif price > bb_high and last_rsi > 65:
100-
92+
elif price > bb_high or last_rsi > 70:
10193
print('GO SHORT')
102-
position = -1
103-
104-
open_short.append(price)
94+
self.position = -1
95+
self.open_short.append(price)
10596

10697
else:
107-
10898
pass
10999

110100
elif self.position == 1:
111101

112-
# Long condition no longer met
113-
if price > bb_low and rsi > 35:
114-
115-
print('CLOSE LONG')
116-
position = 0
117-
118-
close_long.append(price)
119-
120-
pnl.append(open_long[-1] - close_long[-1])
121-
profit_counter += 1
122-
123102
# Short condition is met
124-
elif price > bb_high and last_rsi > 65:
125-
103+
if price > bb_high or last_rsi > 70:
126104
print('CLOSE LONG AND GO SHORT')
127-
position = -1
128-
129-
close_long.append(price)
130-
open_short.append(price)
131-
132-
pnl.append(open_long[-1] - close_long[-1])
133-
profit_counter += 1
105+
self.position = -1
106+
self.close_long.append(price)
107+
self.open_short.append(price)
108+
self.pnl.append(open_long[-1] - close_long[-1])
109+
self.profit_counter += 1
134110

135111
else:
136-
137112
pass
138113

139114
elif self.position == -1:
140115

141-
# Short condition no longer met
142-
if price < bb_high and last_rsi < 65:
143-
144-
print('CLOSE SHORT')
145-
position = 0
146-
147-
close_short.append(price)
148-
149-
pnl.append(close_short[-1] - open_short[-1])
150-
profit_counter += 1
151-
152116
# Long condition is met
153-
elif price < bb_low and last_rsi < 35:
154-
117+
if price < bb_low or last_rsi < 30:
155118
print('CLOSE SHORT AND GO LONG')
156-
position = 1
157-
158-
close_short.append(price)
159-
open_long.append(price)
160-
161-
pnl.append(close_short[-1] - open_short[-1])
162-
profit_counter += 1
119+
self.position = 1
120+
self.close_short.append(price)
121+
self.open_long.append(price)
122+
self.pnl.append(close_short[-1] - open_short[-1])
123+
self.profit_counter += 1
163124

164125
else:
165-
166126
pass
167127

168128
else:
169129

170130
print('Error')
171131

132+
# When candle is closed, append close price to data
133+
else:
134+
# print('candle is closed')
135+
self.data.append(float(candle['c']))
136+
172137
print(
173-
self.symbol, ': price: {} ; bb_h: {} ; bb_l: {} ; rsi: {}\nPnl: {}'.format(
138+
self.symbol, ': price: {} ; bb_h: {} ; bb_l: {} ; rsi: {}\nPosition: {} ; Pnls: {}'.format(
174139
round(price, 1),
175140
round(bb_high, 1),
176141
round(bb_low, 1),
177142
round(last_rsi, 1),
178-
round(pnl, 1)
143+
self.position,
144+
round(sum(self.pnl), 4)
179145
)
180146
)

0 commit comments

Comments
 (0)