-
Notifications
You must be signed in to change notification settings - Fork 1
/
trailing_stop.py
59 lines (49 loc) · 1.44 KB
/
trailing_stop.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
from db import config
import tulipy
from helpers import calc_qty
from timezone import is_dst
import alpaca_trade_api as tradeapi
from alpaca_trade_api.rest import TimeFrame
api = tradeapi.REST(config.API_KEY, config.SECRET_KEY, base_url=config.API_URL)
# symbols = ['AMC', 'SPY', 'DIA']
# for symbols in symbols:
# api.submit_order()
# quote = api.get_last_quote(symbol)
# api.submit_order(
# symbol=symbol,
# side='buy',
# type='market',
# qty=calc_qty(quote.bidprice),
# 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,
# )
# )
# orders = api.list_orders()
# positions = api.list_positions()
# api.submit_order(
# symbol='AMC',
# side='sell',
# qty=200,
# time_in_force='day',
# type='trailing_stop',
# trail_price='0.2'
# )
# api.submit_order(
# symbol='AMC',
# side='sell',
# qty=200,
# time_in_force='day',
# type='trailing_stop',
# trail_percent='2'
# )
# Use Average True Range for Stop loss based on daily price action
daily_bars = api.get_bars('TSLA', TimeFrame.Day, '2021-05-01', '2021-06-01').df
print(daily_bars)
atr = tulipy.atr(daily_bars.high.values, daily_bars.low.values, daily_bars.close.values, 14)
print(atr)