forked from amor71/trading_strategies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolden_pair.py
91 lines (80 loc) · 2.83 KB
/
golden_pair.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
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.strategies.base import Strategy, StrategyType
from pandas import DataFrame as df
from talib import BBANDS, MACD, RSI
class GoldenPair(Strategy):
name = "golden_pair"
whipsawed: 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:
pass
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 should_cool_down(self, symbol: str, now: datetime):
if (
symbol in cool_down
and cool_down[symbol]
and cool_down[symbol] >= now.replace(second=0, microsecond=0) # type: ignore
):
return True
cool_down[symbol] = None
return False
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]:
tlog(f"ahm {symbol}")
if (
await super().is_buy_time(now)
and not position
and not open_orders.get(symbol, None)
and not await self.should_cool_down(symbol, now)
):
tlog(f"consider buy {symbol}")
if (
await super().is_sell_time(now)
and position > 0
and symbol in latest_cost_basis
and last_used_strategy[symbol].name == self.name
and not open_orders.get(symbol)
):
tlog(f"consider sell {symbol}")
return False, {}