├── README.md ├── app.py └── requirements.txt /README.md: -------------------------------------------------------------------------------- 1 | # tradingview-webhooks 2 | 3 | AWS Lambda endpoint for executing Alpaca bracket orders. 4 | 5 | ## Uses AWS Chalice Framework for Serverless Python: 6 | 7 | https://github.com/aws/chalice 8 | 9 | ## Demo Video 10 | 11 | https://youtu.be/TKAo_Z-hzQs 12 | 13 | ## Sign Up For TradingView with this link to support this channel/project 14 | 15 | https://tradingview.go2cloud.org/SH4Gl 16 | 17 | ## TradingView message format 18 | 19 | ``` 20 | { 21 | "open": {{open}}, 22 | "high": {{high}}, 23 | "low": {{low}}, 24 | "close": {{close}}, 25 | "exchange": "{{exchange}}", 26 | "ticker": "{{ticker}}", 27 | "volume": {{volume}}, 28 | "time": "{{time}}", 29 | "timenow": "{{timenow}}" 30 | } 31 | ``` -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import requests, json 2 | from chalice import Chalice 3 | 4 | app = Chalice(app_name='tradingview-webhook-alerts') 5 | 6 | API_KEY = 'your-alpaca-api-key' 7 | SECRET_KEY = 'your-alpaca-secret-key' 8 | BASE_URL = "https://paper-api.alpaca.markets" 9 | ORDERS_URL = "{}/v2/orders".format(BASE_URL) 10 | HEADERS = {'APCA-API-KEY-ID': API_KEY, 'APCA-API-SECRET-KEY': SECRET_KEY} 11 | 12 | @app.route('/buy_stock', methods=['POST']) 13 | def buy_stock(): 14 | request = app.current_request 15 | webhook_message = request.json_body 16 | 17 | data = { 18 | "symbol": webhook_message['ticker'], 19 | "qty": 1, 20 | "side": "buy", 21 | "type": "limit", 22 | "limit_price": webhook_message['close'], 23 | "time_in_force": "gtc", 24 | "order_class": "bracket", 25 | "take_profit": { 26 | "limit_price": webhook_message['close'] * 1.05 27 | }, 28 | "stop_loss": { 29 | "stop_price": webhook_message['close'] * 0.98, 30 | } 31 | } 32 | 33 | r = requests.post(ORDERS_URL, json=data, headers=HEADERS) 34 | 35 | response = json.loads(r.content) 36 | 37 | return { 38 | 'webhook_message': webhook_message, 39 | 'id': response['id'], 40 | 'client_order_id': response['client_order_id'] 41 | } 42 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests --------------------------------------------------------------------------------