Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscribeClick #343

Open
sbgmicromax opened this issue Apr 29, 2024 · 1 comment
Open

subscribeClick #343

sbgmicromax opened this issue Apr 29, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@sbgmicromax
Copy link

Description

Need to get the value and time of the series from the point of click, for further calculation purpose in the strategy

Code example

No response

Method of implementation

No response

@sbgmicromax sbgmicromax added the enhancement New feature or request label Apr 29, 2024
@emma-uw
Copy link

emma-uw commented May 1, 2024

Hey, this has been posted previously somewhere around here and I used it since then.

def subscribe_click(chart, *, callback):
    js = (
        'function clickHandler(param) {'
        'if (!param.point) {'
        'return;'
        '}'
        f'const time = {chart.id}.chart.timeScale().coordinateToTime(param.point.x);'
        f'const price = {chart.id}.series.coordinateToPrice(param.point.y);'
        'const data = JSON.stringify({time: time, price: price});'
        'window.callbackFunction(`on_click_~_${data}`);'
        '}'
        f'{chart.id}.chart.subscribeClick(clickHandler);'
    )

    def decorated_callback(data):
        data = json.loads(data)
        data = {
            'timestamp': pd.to_datetime(data['time'], unit='s'),
            'price': data['price'],
        }
        return callback(data, chart, chart.candle_data)

    chart.win.handlers['on_click'] = decorated_callback
    chart.win.run_script(js)

def on_chart_click(data, chart, candle_data): print(data['timestamp'])

subscribe_click(chart, callback=on_chart_click)

@louisnw01 louisnw01 added documentation Improvements or additions to documentation and removed enhancement New feature or request labels May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants