Skip to content

Instantly share code, notes, and snippets.

@hanchiang
Last active May 28, 2023 12:46
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hanchiang/23a6ddf08d41c9aad337b13f6742990d to your computer and use it in GitHub Desktop.
PineScript for sending market data via webhook
//@version=5
indicator("Market data notification stocks test")
array_start = '['
array_end = ']'
json_start = '{'
json_end = '}'
get_series_array(data, n) =>
float[] res = array.new_float()
if (n > 0)
for i = 0 to n-1
array.push(res, data[i])
res
build_json_array(res_json, symbol, exchange_symbol, timeframe, num_close_prices, num_volumes, num_ema20s) =>
[closee, ema20, volumee] = request.security(exchange_symbol, timeframe, [close, ta.ema(close, 20), volume])
float[] close_prices = get_series_array(closee, num_close_prices)
float[] volumes = get_series_array(volumee, num_volumes)
float[] ema20s = get_series_array(ema20, num_ema20s)
string close_prices_formatted = array_start + array.join(close_prices, ',') + array_end
string volumes_formatted = array_start + array.join(volumes, ',') + array_end
string ema20s_formatted = array_start + array.join(ema20s, ',') + array_end
res = res_json
res := res + json_start + '"symbol": ' + '"' + symbol + '"' + ', "timeframe": ' + '"' + timeframe + '"'
+ ', "close_prices": ' + close_prices_formatted
if (num_ema20s > 0)
res := res + ', "ema20s": ' + ema20s_formatted
if (num_volumes > 0)
res := res + ', "volumes": ' + volumes_formatted
res := res + json_end + ','
res
init_json_key_values() =>
string[] json_key_values = array.new_string()
array.push(json_key_values, '"secret": "<your secret here>"')
array.push(json_key_values, '"type": "stocks"')
array.push(json_key_values, '"test_mode": "true"')
array.push(json_key_values, '"unix_ms": ' + str.tostring(timenow))
json_key_values
send_alert_for_stocks() =>
int num_close_prices = 7
int num_volumes = 7
int num_ema20s = 1
string timeframe = "1D"
data_payload = '"data": ' + array_start
data_payload := build_json_array(data_payload, "SPY", "AMEX:SPY", timeframe, num_close_prices, num_volumes, num_ema20s)
data_payload := build_json_array(data_payload, "QQQ", "NASDAQ:QQQ", timeframe, num_close_prices, num_volumes, num_ema20s)
data_payload := build_json_array(data_payload, "DJIA", "AMEX:DJIA", timeframe, num_close_prices, num_volumes, num_ema20s)
data_payload := build_json_array(data_payload, "IWM", "AMEX:IWM", timeframe, num_close_prices, num_volumes, num_ema20s)
data_payload := build_json_array(data_payload, "AAPL", "NASDAQ:AAPL", timeframe, num_close_prices, num_volumes, num_ema20s)
data_payload := build_json_array(data_payload, "AMD", "NASDAQ:AMD", timeframe, num_close_prices, num_volumes, num_ema20s)
data_payload := build_json_array(data_payload, "AMZN", "NASDAQ:AMZN", timeframe, num_close_prices, num_volumes, num_ema20s)
data_payload := build_json_array(data_payload, "BABA", "NYSE:BABA", timeframe, num_close_prices, num_volumes, num_ema20s)
data_payload := build_json_array(data_payload, "GOOGL", "NASDAQ:GOOGL", timeframe, num_close_prices, num_volumes, num_ema20s)
data_payload := build_json_array(data_payload, "META", "NASDAQ:META", timeframe, num_close_prices, num_volumes, num_ema20s)
data_payload := build_json_array(data_payload, "MSFT", "NASDAQ:MSFT", timeframe, num_close_prices, num_volumes, num_ema20s)
data_payload := build_json_array(data_payload, "NFLX", "NASDAQ:NFLX", timeframe, num_close_prices, num_volumes, num_ema20s)
data_payload := build_json_array(data_payload, "NVDA", "NASDAQ:NVDA", timeframe, num_close_prices, num_volumes, num_ema20s)
data_payload := build_json_array(data_payload, "TSLA", "NASDAQ:TSLA", timeframe, num_close_prices, num_volumes, num_ema20s)
// remove trailing comma from array
data_payload := str.substring(data_payload, 0, str.length(data_payload) - 1)
data_payload := data_payload + array_end
string[] json_key_values = init_json_key_values()
array.push(json_key_values, data_payload)
result_json = json_start + array.join(json_key_values, ',') + json_end
alert(result_json, alert.freq_once_per_bar_close)
send_alert_for_stocks()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment