├── tradingview-webhook-alert ├── requirements.txt ├── .gitignore ├── __pycache__ │ └── app.cpython-38.pyc ├── .chalice │ ├── config.json │ └── deployed │ │ └── dev.json └── app.py ├── .gitattributes └── README.md /tradingview-webhook-alert/requirements.txt: -------------------------------------------------------------------------------- 1 | requests -------------------------------------------------------------------------------- /tradingview-webhook-alert/.gitignore: -------------------------------------------------------------------------------- 1 | .chalice/deployments/ 2 | .chalice/venv/ 3 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /tradingview-webhook-alert/__pycache__/app.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psriniketh57/Algorithmic-Trading-Bot/HEAD/tradingview-webhook-alert/__pycache__/app.cpython-38.pyc -------------------------------------------------------------------------------- /tradingview-webhook-alert/.chalice/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0", 3 | "app_name": "tradingview-webhook-alert", 4 | "stages": { 5 | "dev": { 6 | "api_gateway_stage": "api" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tradingview-webhook-alert/.chalice/deployed/dev.json: -------------------------------------------------------------------------------- 1 | { 2 | "resources": [ 3 | { 4 | "name": "default-role", 5 | "resource_type": "iam_role", 6 | "role_arn": "arn:aws:iam::090940016697:role/tradingview-webhook-alert-dev", 7 | "role_name": "tradingview-webhook-alert-dev" 8 | }, 9 | { 10 | "name": "api_handler", 11 | "resource_type": "lambda_function", 12 | "lambda_arn": "arn:aws:lambda:us-east-2:090940016697:function:tradingview-webhook-alert-dev" 13 | }, 14 | { 15 | "name": "rest_api", 16 | "resource_type": "rest_api", 17 | "rest_api_id": "k58b7yy6i6", 18 | "rest_api_url": "https://k58b7yy6i6.execute-api.us-east-2.amazonaws.com/api/" 19 | } 20 | ], 21 | "schema_version": "2.0", 22 | "backend": "api" 23 | } 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Algorithmic-Trading-Bot 2 | Uses AWS Chalice, Python, Pine Script, TradingView Charts, and the Alpaca API in conjunction to make a stock trading bot which runs in a virtual environment. 3 | First, we create a virtual environment then activate it. Next we install AWS chalice into the virtual environment, then create a new chalice project. We edit the 4 | code in 'app.py' to make it a POST request, so that everytime something is posted to the API link, the code is executed. We then use TradingView charts to create alerts 5 | based on our custom pine script, and within the alerts we paste the API link into the webhook url section. Now, when the alerts hit, the trades will be executed, which 6 | signifies that the bot is working. Once the bot is up and running, we can modify our strateges and backtest them. 7 | -------------------------------------------------------------------------------- /tradingview-webhook-alert/app.py: -------------------------------------------------------------------------------- 1 | import requests, json 2 | from chalice import Chalice 3 | 4 | app = Chalice(app_name='tradingview-webhook-alert') 5 | 6 | API_KEY = '' 7 | 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": 10, 20 | "side": "buy", 21 | "type": "market", 22 | #"type": "limit", 23 | #"limit_price": webhook_message['close'], 24 | "time_in_force": "gtc", 25 | #"order_class": "bracket", 26 | #"take_profit": { 27 | # "limit_price": webhook_message['close'] * 1.05 28 | #}, 29 | "stop_loss": { 30 | "stop_price": webhook_message['close'] * 0.98, 31 | } 32 | } 33 | 34 | r = requests.post(ORDERS_URL, json=data, headers=HEADERS) 35 | 36 | response = json.loads(r.content) 37 | 38 | return { 39 | 'webhook_message': webhook_message, 40 | 'id': response['id'], 41 | 'client_order_id': response['client_order_id'], 42 | 'response': response 43 | } 44 | 45 | @app.route('/sell_stock', methods=['POST']) 46 | def sell_stock(): 47 | request = app.current_request 48 | webhook_message = request.json_body 49 | 50 | data = { 51 | "symbol": webhook_message['ticker'], 52 | "qty": 10, 53 | "side": "sell", 54 | "type": "market", 55 | "time_in_force": "gtc", 56 | } 57 | 58 | r = requests.post(ORDERS_URL, json=data, headers=HEADERS) 59 | 60 | response = json.loads(r.content) 61 | 62 | return { 63 | 'webhook_message': webhook_message, 64 | 'id': response['id'], 65 | 'client_order_id': response['client_order_id'] 66 | } 67 | --------------------------------------------------------------------------------