kieran-mackle / AutoTrader

A Python-based development platform for automated trading systems - from backtesting to optimisation to livetrading.
https://kieran-mackle.github.io/AutoTrader/
GNU General Public License v3.0
945 stars 217 forks source link

Webhook support for signals like tradingview or chartink #17

Open rahulmr opened 2 years ago

rahulmr commented 2 years ago

Is your feature request related to a problem? Please describe. Signals / Alerts are currently being sent to email, if AutoTrade can send them to a webhook link it will be great.

Describe the solution you'd like Whenever a signal comes, AutoTrader must be able to send data in json format to a webhook link. Data must consist name of strategy, time when signal was generated, candle open high low close OHLCV (V if possible).

{
    "strategy_name": "SMA Momentum",
    "strategy_params": {
      "sma_period" : "44",
      "candle_lookback" : "4",
      "RR" : "1.4",
      "exit_buffer" : "0.2"
    },
    "trading_symbol" : "SBIN.NS",
    "time_frame" : "5min",
     "ohlcv" : {
       "open" : "345",
       "high" : "356",
       "low" : "334",
       "close" : "351",
       "volume" : "2409"
     }
    "triggered_at": "2021-12-24 13:35:05",
    "webhook_url": "http://your-web-hook-url.com"
}

If possible make this customizable like we have in tradingview. We can add or remove like say timeframe can be removed and we can add symbol or dataprovide like yahoo or oanda.

Describe alternatives you've considered Reference - chartink.com webhooks JSON data which is not so useful.

{
    "stocks": "SEPOWER,ASTEC,EDUCOMP,KSERASERA,IOLCP,GUJAPOLLO,EMCO",
    "trigger_prices": "3.75,541.8,2.1,0.2,329.6,166.8,1.25",
    "triggered_at": "2:34 pm",
    "scan_name": "Short term breakouts",
    "scan_url": "short-term-breakouts",
    "alert_name": "Alert for Short term breakouts",
    "webhook_url": "http://your-web-hook-url.com"
}

Additional context If webhook is provided, it will be easy to integrate a choice of broker order placement code. Even webhook can be used to send alerts to telegram channel. See below code where I am sending data to a channel using webhook coming from chartink.


from flask import Flask
from flask import jsonify, make_response, request
import requests
from time import sleep
import config_ab as config
import pandas as pd
from urllib.parse import urlencode, quote_plus
from datetime import datetime

app = Flask(__name__)

def telegram_bot_sendtext(message):
    bot_token = '<BOT_TOKEN>'
    bot_chatID = '-100<CHANNEL_ID>'
    data = {
        'text': message,
        'chat_id': bot_chatID,
        'parse_mode': 'MARKDOWN'
    }
    encodedData = urlencode(data, quote_via=quote_plus)

    url = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?' + encodedData
    response = requests.get(url)
    return response.json()

@app.route('/')
def hello_world():
    return 'Chartink testing!!'

def time_check():
    t = datetime.today()
    mis_end_time = datetime(t.year, t.month, t.day, 15, 00)
    if datetime.now() > mis_end_time:
        print("no more intraday trading")
        return True

@app.route('/alertsonly', methods=['POST'])
def alertsonly():

    if not request.is_json:
        return make_response(jsonify({"message": "Request body must be JSON"}), 400)
    body = request.get_json()
    print(body)

    df = pd.DataFrame.from_dict({'symbol': list(body['stocks'].split(
        ',')), 'price': list(body['trigger_prices'].split(','))})
    # print(df)
    table = df.to_markdown(index=False, tablefmt="grid")
    # print(table)
    test = telegram_bot_sendtext(body['alert_name']+'\n'+table)
    print(test)
    return table

I think implementing code to send json to webhook endpoint can be implemented easily since you have already coded for email.