├── README.md └── lambda_function.py /README.md: -------------------------------------------------------------------------------- 1 | # algoconvention 2 | AWS Lambda code to automate algo strategy using Chartink 3 | 4 | ## PASTE THE CODE IN AWS Lambda code editor - Python version 3.7 5 | 6 | 7 | -------------------------------------------------------------------------------- /lambda_function.py: -------------------------------------------------------------------------------- 1 | import json 2 | import urllib 3 | 4 | import requests 5 | import redis 6 | from kiteconnect import KiteConnect 7 | 8 | kite = KiteConnect(api_key="", debug=True) 9 | redis_con = redis.Redis(host="", port=6379, db=11, charset="utf-8", decode_responses=True, password="") 10 | 11 | # def send_message_bot(chat_id, text): 12 | # url = 'https://api.telegram.org//sendMessage' 13 | # r = requests.post(url, {'chat_id': chat_id, 'text': text}).json() 14 | 15 | def set_token(request_token): 16 | 17 | data = kite.generate_session(request_token, api_secret="") 18 | redis_con.set("my_token", data["access_token"]) 19 | return data["access_token"] 20 | 21 | def place_order(received_data, txn_type="BUY"): 22 | 23 | stock_id = [urllib.parse.quote_plus(ts) for ts in received_data['stocks'].split(',')] 24 | place_at = [float(o) for o in received_data['trigger_prices'].split(',')] 25 | stop_at, book_at = zip(*[(round(_ - _*0.01, 1), round(_ + _*0.01, 1)) for _ in place_at]) 26 | 27 | if txn_type == "SELL": 28 | stop_at, book_at = book_at, stop_at 29 | 30 | qty_to_buy = [int(500/abs(order_price - stop_loss)) for order_price, stop_loss in zip(place_at, stop_at)] 31 | kite.set_access_token(redis_con.get("my_token")) 32 | 33 | for tradingsymbol, quantity, execute_at, sl, tg in zip(stock_id, qty_to_buy, place_at, stop_at, book_at): 34 | order_id = kite.place_order(tradingsymbol=tradingsymbol, exchange="NSE", transaction_type=txn_type, quantity=quantity, 35 | order_type="LIMIT", price=execute_at, trigger_price=sl, product="MIS", validity="DAY", disclosed_quantity=0, 36 | squareoff=0, stoploss=0, trailing_stoploss=0, variety='amo') 37 | # send_message_bot(, "{} Order placed for {} at {}, SL: {}, TG: {}".format(txn_type, tradingsymbol, execute_at, sl, tg)) 38 | 39 | def lambda_handler(event, context): 40 | 41 | query_dict = event.get('queryStringParameters', {}) 42 | if event.get('rawPath') == '/set-token': 43 | return {'access_token': set_token(query_dict.get('request_token')), 'msg': 'Token was set successfully.'} 44 | 45 | if event.get('rawPath') == '/webhook': 46 | received_data = json.loads(event['body']) 47 | 48 | if received_data['scan_url'] == "near-day-high-108": 49 | place_order(received_data, txn_type="BUY") 50 | --------------------------------------------------------------------------------