forked from amor71/trading_strategies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvwap_short.py
259 lines (230 loc) · 8.63 KB
/
vwap_short.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import asyncio
from datetime import datetime, timedelta
from typing import Dict, List, Tuple
import alpaca_trade_api as tradeapi
import numpy as np
from liualgotrader.common import config
from liualgotrader.common.tlog import tlog
from liualgotrader.common.trading_data import (buy_indicators, buy_time,
cool_down, last_used_strategy,
latest_cost_basis,
latest_scalp_basis, open_orders,
sell_indicators, stop_prices,
target_prices)
from liualgotrader.fincalcs.support_resistance import find_stop
from liualgotrader.fincalcs.vwap import add_daily_vwap
from liualgotrader.strategies.base import Strategy, StrategyType
from pandas import DataFrame as df
from pandas import Series
from pandas import Timestamp as ts
from pandas import concat
from talib import BBANDS, MACD, RSI
class VWAPShort(Strategy):
name = "vwap_short"
was_above_vwap: Dict = {}
def __init__(
self,
batch_id: str,
schedule: List[Dict],
ref_run_id: int = None,
check_patterns: bool = False,
):
self.check_patterns = check_patterns
super().__init__(
name=self.name,
type=StrategyType.DAY_TRADE,
batch_id=batch_id,
ref_run_id=ref_run_id,
schedule=schedule,
)
async def buy_callback(self, symbol: str, price: float, qty: int) -> None:
pass
async def sell_callback(self, symbol: str, price: float, qty: int) -> None:
latest_cost_basis[symbol] = price
async def create(self) -> None:
await super().create()
tlog(f"strategy {self.name} created")
async def run(
self,
symbol: str,
shortable: bool,
position: int,
minute_history: df,
now: datetime,
portfolio_value: float = None,
trading_api: tradeapi = None,
debug: bool = False,
backtesting: bool = False,
) -> Tuple[bool, Dict]:
if not shortable:
return False, {}
data = minute_history.iloc[-1]
if data.close > data.average:
self.was_above_vwap[symbol] = True
if (
await super().is_buy_time(now)
and not position
and not open_orders.get(symbol, None)
):
day_start = ts(config.market_open)
try:
day_start_index = minute_history["close"].index.get_loc(
day_start, method="nearest"
)
except Exception as e:
tlog(
f"[ERROR]{self.name}[{now}]{symbol} can't load index for {day_start} w/ {e}"
)
return False, {}
close = (
minute_history["close"][day_start_index:-1]
.dropna()
.between_time("9:30", "16:00")
.resample("5min")
.last()
).dropna()
open = (
minute_history["open"][day_start_index:-1]
.dropna()
.between_time("9:30", "16:00")
.resample("5min")
.first()
).dropna()
high = (
minute_history["high"][day_start_index:-1]
.dropna()
.between_time("9:30", "16:00")
.resample("5min")
.max()
).dropna()
low = (
minute_history["low"][day_start_index:-1]
.dropna()
.between_time("9:30", "16:00")
.resample("5min")
.min()
).dropna()
volume = (
minute_history["volume"][day_start_index:-1]
.dropna()
.between_time("9:30", "16:00")
.resample("5min")
.sum()
).dropna()
df = concat(
[
open.rename("open"),
high.rename("high"),
low.rename("low"),
close.rename("close"),
volume.rename("volume"),
],
axis=1,
)
if not add_daily_vwap(df):
tlog(f"[{now}]{symbol} failed in add_daily_vwap")
return False, {}
vwap_series = df["average"]
if (
data.close < vwap_series[-1] * 0.99
and self.was_above_vwap.get(symbol, False)
and close[-1]
< open[-1]
<= close[-2]
< open[-2]
<= close[-3]
< open[-3]
):
stop_price = vwap_series[-1]
target_price = data.close - 3 * (stop_price - data.close)
stop_prices[symbol] = stop_price
target_prices[symbol] = target_price
if portfolio_value is None:
if trading_api:
retry = 3
while retry > 0:
try:
portfolio_value = float(
trading_api.get_account().portfolio_value
)
break
except ConnectionError as e:
tlog(
f"[{symbol}][{now}[Error] get_account() failed w/ {e}, retrying {retry} more times"
)
await asyncio.sleep(0)
retry -= 1
if not portfolio_value:
tlog(
"f[{symbol}][{now}[Error] failed to get portfolio_value"
)
return False, {}
else:
raise Exception(
f"{self.name}: both portfolio_value and trading_api can't be None"
)
shares_to_buy = (
portfolio_value
* config.risk
// (data.close - stop_prices[symbol])
)
if not shares_to_buy:
shares_to_buy = 1
buy_price = data.close
tlog(
f"[{self.name}][{now}] Submitting buy short for {-shares_to_buy} shares of {symbol} at {buy_price} target {target_prices[symbol]} stop {stop_prices[symbol]}"
)
sell_indicators[symbol] = {
"vwap_series": vwap_series[-5:].tolist(),
"vwap": data.vwap,
"avg": data.average,
}
return (
True,
{
"side": "sell",
"qty": str(-shares_to_buy),
"type": "market",
},
)
if (
await super().is_sell_time(now)
and position
and last_used_strategy[symbol].name == self.name
and not open_orders.get(symbol)
):
day_start = ts(config.market_open)
day_start_index = minute_history["close"].index.get_loc(
day_start, method="nearest"
)
close = (
minute_history["close"][day_start_index:-1]
.dropna()
.between_time("9:30", "16:00")
.resample("5min")
.last()
).dropna()
to_sell: bool = False
reason: str = ""
if data.close >= stop_prices[symbol]:
to_sell = True
reason = "stopped"
elif data.close <= target_prices[symbol]:
to_sell = True
reason = "target reached"
elif close[-1] > close[-2] > close[-3] < close[-4]:
to_sell = True
reason = "reversing direction"
if to_sell:
buy_indicators[symbol] = {
"close_5m": close[-5:].tolist(),
"reason": reason,
}
tlog(
f"[{self.name}][{now}] Submitting sell short for {position} shares of {symbol} at market {data.close} with reason:{reason}"
)
return (
True,
{"side": "buy", "qty": str(-position), "type": "market",},
)
return False, {}