//@version=5
indicator(“Binance Order Book Volume Ratio”, overlay=true)
// Binance API keys
api_key = “your_api_key_here”
api_secret = “your_api_secret_here”
// Define input options
lookback_percent = input.float(1, title=“Lookback %”, minval=0.1, maxval=10.0, step=0.1)
// Define volume ratio thresholds for buy and sell signals
buy_volume_ratio_threshold = input.float(1.5, title=“Buy Volume Ratio Threshold”, minval=0.1, maxval=10.0, step=0.1)
sell_volume_ratio_threshold = input.float(1.5, title=“Sell Volume Ratio Threshold”, minval=0.1, maxval=10.0, step=0.1)
// Get current ticker symbol and exchange name
symbol = syminfo.tickerid
exchange = syminfo.exchange
// Define Binance API endpoint URLs
base_url = “https://api.binance.com”
depth_endpoint = “/api/v3/depth”
// Define function to fetch order book data from Binance API
get_order_book_data() =>
depth = “5” // fetch order book depth for top 5 bids and asks
limit = “1000” // fetch up to 1000 entries for each bid and ask
url = base_url + depth_endpoint + “?symbol=” + symbol + “&limit=” + limit + “&depth=” + depth
headers = [“X-MBX-APIKEY”: api_key ]
response = request(url=url, headers=headers, timeout=5000)
json_data = from_json(response.content)
json_data
// Get order book data
order_book_data = get_order_book_data()
// Get current last price
last_price = close
// Calculate price range for order book data
price_range = last_price * lookback_percent / 100
// Calculate volume ratios for buy and sell signals
buy_volume = 0.0
sell_volume = 0.0
for bid in order_book_data.bids
if bid[0] >= last_price - price_range and bid[0] <= last_price + price_range
buy_volume := buy_volume + bid[1]
for ask in order_book_data.asks
if ask[0] >= last_price - price_range and ask[0] <= last_price + price_range
sell_volume := sell_volume + ask[1]
buy_volume_ratio = buy_volume / sell_volume
sell_volume_ratio = sell_volume / buy_volume
// Plot volume ratio lines
plot(buy_volume_ratio, color=color.green, title=“Buy Volume Ratio”)
plot(sell_volume_ratio, color=color.red, title=“Sell Volume Ratio”)
// Plot buy and sell signals
buy_signal = buy_volume_ratio > buy_volume_ratio_threshold
sell_signal = sell_volume_ratio > sell_volume_ratio_threshold
plotshape(series=buy_signal, style=shape.arrowup, location=location.belowbar, color=color.green, text=“Buy”, size=size.small)
plotshape(series=sell_signal, style=shape.arrowdown, location=location.abovebar, color=color.red, text=“Sell”, size=size.small)
when i try to apply it on my charts it says no viable alternative at character ‘{’ . any help would be highly appreciated