Skip to main content

Safety Check Examples

Examples of position validation and safety checks.

How it works: a VALIDATE_ALERT with in_position compares TradingView's position state with OptAlgo's. If TradingView is flat but OptAlgo has an open trade, the trade is closed. With safety_check: true, OptAlgo also expects the next validation within safety_check_interval_minutes (240-1440, default 1440). If none arrives for twice the interval while a trade is open, OptAlgo closes the trade.

Enable Safety Checks (Position Open)​

{
"strategy_key": "your_strategy_key_here",
"exchange": "BINANCE",
"ticker": "BTCUSDT.P",
"position_side": "VALIDATE_ALERT",
"in_position": true,
"safety_check": true,
"safety_check_interval_minutes": 480,
"reason": "8-hour position validation"
}

Safety Check (No Position)​

{
"strategy_key": "your_strategy_key_here",
"exchange": "BINANCE",
"ticker": "BTCUSDT.P",
"position_side": "VALIDATE_ALERT",
"in_position": false,
"safety_check": true,
"safety_check_interval_minutes": 720,
"reason": "Confirm no open positions"
}

Disable Safety Checks​

{
"strategy_key": "your_strategy_key_here",
"exchange": "BINANCE",
"ticker": "BTCUSDT.P",
"position_side": "VALIDATE_ALERT",
"in_position": false,
"safety_check": false
}

in_position is required: without it, the safety-check settings are not changed. Send the real position state (true or false).

TradingView PineScript Integration​

//@version=5
// Paste your bot's strategy key into this input in the script settings.
strategyKey = input.string("", "OptAlgo strategy key")

// Send a validation every 8 hours. Create the TradingView alert with the
// condition "Any alert() function call" and the OptAlgo webhook URL.
inPosition = strategy.position_size != 0
if barstate.isconfirmed and hour % 8 == 0 and minute == 0
alert('{"strategy_key":"' + strategyKey + '","exchange":"BINANCE","ticker":"' + syminfo.ticker + '","position_side":"VALIDATE_ALERT","in_position":' + str.tostring(inPosition) + ',"safety_check":true,"safety_check_interval_minutes":480}', alert.freq_once_per_bar_close)

TradingView placeholders such as {{ticker}} are not filled in inside alert() messages, so the script builds the JSON from syminfo.ticker and the key input.

Daily Reconciliation​

{
"strategy_key": "your_strategy_key_here",
"exchange": "BINANCE",
"ticker": "BTCUSDT.P",
"position_side": "VALIDATE_ALERT",
"in_position": true,
"safety_check": true,
"safety_check_interval_minutes": 1440,
"reason": "Daily end-of-day position sync"
}