-
Notifications
You must be signed in to change notification settings - Fork 1
/
orbo.py
113 lines (90 loc) · 3.92 KB
/
orbo.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
from main import strategy
from db import config
import sqlite3
from datetime import date, datetime, timedelta, tzinfo
from timezone import is_dst
from helpers import calc_qty
import alpaca_trade_api as tradeapi
from alpaca_trade_api.rest import TimeFrame
connection = sqlite3.connect(config.DB_FILE)
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
cursor.execute("""
SELECT id FROM strategy WHERE name = 'opening_range_breakout'
""")
strategy_id = cursor.fetchone()['id']
cursor.execute("""
SELECT symbol, name
FROM stock
JOIN stock_strategy on stock_strategy.stock_id = stock.id
WHERE stock_strategy.strategy_id = ?
""", (strategy_id,))
stocks = cursor.fetchall()
symbols = [stock['symbol'] for stock in stocks]
print(symbols)
api = tradeapi.REST(config.API_KEY, config.SECRET_KEY, config.API_URL, api_version='v2')
current_date = '2021-05-28'
if is_dst():
# current_date = datetime.today().isoformat()
start_minute_bar = f"{current_date}T09:30:00-05:00"
end_minute_bar = f"{current_date}T09:45:00-05:00"
else:
# current_date = datetime.today().isoformat()
start_minute_bar = f"{current_date}T09:30:00-04:00"
end_minute_bar = f"{current_date}T09:45:00-04:00"
orders = api.list_orders(status='all', limit=500, after=f"{current_date}T09:30:00-05:00)")
existing_order_symbols = [order.symbol for order in orders if orders.status != 'canceled']
# print(
# start_minute_bar,
# end_minute_bar,
# )
for symbol in symbols:
# minute_bars = api.get_barset(symbol, 'minute', start=start_minute_bar, end=end_minute_bar).df
# minute_bars = api.get_bars(symbol, TimeFrame.Minute, start_minute_bar, end_minute_bar).df
minute_bars = api.get_bars(symbol, TimeFrame.Minute, current_date, current_date).df
print(symbol)
opening_range_mask = (minute_bars.index >= start_minute_bar) & (minute_bars.index < end_minute_bar)
opening_range_bars = minute_bars.loc[opening_range_mask]
print(opening_range_bars)
if not minute_bars.empty:
opening_range_low = opening_range_bars['low'].min()
opening_range_high = opening_range_bars['high'].max()
opening_range = opening_range_high - opening_range_low
# print(
# opening_range_low,
# opening_range_high,
# opening_range
# )
after_opening_range_mask = minute_bars.index >= end_minute_bar
after_opening_range_bars = minute_bars.loc[after_opening_range_mask]
# print(
# after_opening_range_bars
# )
after_opening_range_breakout = after_opening_range_bars[after_opening_range_bars['close'] > opening_range_high]
if not after_opening_range_breakout.empty:
if symbol not in existing_order_symbols:
limit_price = after_opening_range_breakout.iloc[0]['close']
message = (f"buying long {symbol} at {limit_price}, closed_above {opening_range_high} at {after_opening_range_breakout.iloc[0]['close']}")
# messages.append(message)
print(message)
try:
# submit order
api.submit_order(
symbol='symbol',
side='buy',
type='limit',
qty=calc_qty(limit_price),
time_in_force='day',
order_class='bracket',
take_profit=dict(
limit_price=limit_price + opening_range,
),
stop_loss=dict(
stop_price=limit_price - opening_range,
limit_price=limit_price,
)
)
except Exception as e:
print(f"could not submit order {e}")
else:
print(f"Buy order for {symbol} exists, skipping place order!!")