-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathapp.py
41 lines (34 loc) · 1.13 KB
/
app.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
import requests, json
from chalice import Chalice
app = Chalice(app_name='tradingview-webhook-alerts')
API_KEY = 'your-alpaca-api-key'
SECRET_KEY = 'your-alpaca-secret-key'
BASE_URL = "https://paper-api.alpaca.markets"
ORDERS_URL = "{}/v2/orders".format(BASE_URL)
HEADERS = {'APCA-API-KEY-ID': API_KEY, 'APCA-API-SECRET-KEY': SECRET_KEY}
@app.route('/buy_stock', methods=['POST'])
def buy_stock():
request = app.current_request
webhook_message = request.json_body
data = {
"symbol": webhook_message['ticker'],
"qty": 1,
"side": "buy",
"type": "limit",
"limit_price": webhook_message['close'],
"time_in_force": "gtc",
"order_class": "bracket",
"take_profit": {
"limit_price": webhook_message['close'] * 1.05
},
"stop_loss": {
"stop_price": webhook_message['close'] * 0.98,
}
}
r = requests.post(ORDERS_URL, json=data, headers=HEADERS)
response = json.loads(r.content)
return {
'webhook_message': webhook_message,
'id': response['id'],
'client_order_id': response['client_order_id']
}