forked from amor71/trading_strategies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswing_mama_fama.py
146 lines (124 loc) · 4.69 KB
/
swing_mama_fama.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 asyncio
from datetime import datetime
from typing import Dict, List, Tuple
import alpaca_trade_api as tradeapi
from liualgotrader.common import config
from liualgotrader.common.tlog import tlog
from liualgotrader.common.trading_data import (buy_indicators,
last_used_strategy,
latest_cost_basis, open_orders,
sell_indicators, stop_prices,
target_prices)
from liualgotrader.strategies.base import Strategy, StrategyType
from pandas import DataFrame as df
from talib import MAMA
class SwingMamaFama(Strategy):
name = "SwingMamaFama"
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.SWING,
batch_id=batch_id,
ref_run_id=ref_run_id,
schedule=schedule,
)
async def buy_callback(self, symbol: str, price: float, qty: int) -> None:
latest_cost_basis[symbol] = price
async def sell_callback(self, symbol: str, price: float, qty: int) -> None:
pass
async def create(self) -> None:
await super().create()
tlog(f"strategy {self.name} created")
async def is_buy_time(self, now: datetime):
return True
async def is_sell_time(self, now: datetime):
return True
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]:
data = minute_history.iloc[-1]
fama, mama = MAMA(minute_history["close"])
if fama[-1] > mama[-1]:
stop_price = 0.0
target_price = 0.0
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_indicators[symbol] = {
"mama": mama[-5:].tolist(),
"fama": fama[-5:].tolist(),
}
tlog(
f"[{self.name}][{now}] Submitting buy for {shares_to_buy} shares of {symbol} at market target {target_prices[symbol]} stop {stop_prices[symbol]}"
)
return (
True,
{"side": "buy", "qty": str(shares_to_buy), "type": "market",},
)
if (
await self.is_sell_time(now)
and position
and last_used_strategy[symbol].name == self.name
and not open_orders.get(symbol)
):
fama, mama = MAMA(minute_history["close"])
if fama[-1] < mama[-1]:
tlog(
f"[{self.name}][{now}] Submitting sell for {position} shares of {symbol} at market {data.close}"
)
sell_indicators[symbol] = {
"mama": mama[-5:].tolist(),
"fama": fama[-5:].tolist(),
}
return (
True,
{"side": "sell", "qty": str(position), "type": "market",},
)
return False, {}