Skip to main content

Safety Check Parameters

Parameters for validating position consistency between OptAlgo and external systems.

safety_check​

  • Type: boolean
  • Required: No
  • Default: false

Enable safety position verification. Validates that OptAlgo positions match TradingView or external system positions.


safety_check_interval_minutes​

  • Type: integer
  • Required: No
  • Default: 1440 (24 hours)
  • Range: 240 - 1440 minutes (4-24 hours)

Interval between safety checks. Values below 240 are raised to 240 and values above 1440 are lowered to 1440.

If a bot has an open trade and no VALIDATE_ALERT has arrived for twice this interval, OptAlgo assumes the alert stopped working and closes the trade (a background job checks every 5 minutes).


in_position​

  • Type: boolean
  • Required: Yes, for VALIDATE_ALERT safety checks
  • Description: Indicates if position is currently open in external system

Used with VALIDATE_ALERT position side to check position consistency. Without in_position, the safety check settings are not updated and no comparison is made.

Scenarios​

Case 1: Positions Match

TradingView: in_position = true
OptAlgo: Has open trade
Result: ✅ Check passed

Case 2: Missing OptAlgo Position

TradingView: in_position = true
OptAlgo: No open trade
Result: ⚠️ Warning logged

Case 3: Extra OptAlgo Position

TradingView: in_position = false
OptAlgo: Has open trade
Result: 🔴 Position automatically closed

reason​

  • Type: string
  • Required: No
  • Description: Reason or note for the signal

Special Values​

  • "scheduler": Automated safety signal (no user notification)
  • Contains "error": Logged as error with user notification
  • Contains "warning": Logged as warning with user notification
  • Other values: Logged as info without notification

Examples​

{
"reason": "scheduler"
// Automated safety check
}

{
"reason": "error: Stop loss not set"
// User notified of error
}

{
"reason": "warning: High volatility detected"
// User notified of warning
}

{
"reason": "Manual close by user"
// Just logged for reference
}

Complete Safety Check Example​

{
"strategy_key": "your_key",
"exchange": "BINANCE",
"ticker": "BTCUSDT.P",
"position_side": "VALIDATE_ALERT",
"in_position": true,
"safety_check": true,
"safety_check_interval_minutes": 720,
"reason": "Periodic safety validation"
}

How Safety Checks Work​

1. Enable Safety Checks​

Send a VALIDATE_ALERT signal with safety_check: true:

{
"strategy_key": "your_key",
"exchange": "BINANCE",
"ticker": "BTCUSDT.P",
"position_side": "VALIDATE_ALERT",
"in_position": true,
"safety_check": true,
"safety_check_interval_minutes": 480
}

2. System Validates Position​

  • Checks if OptAlgo has an open trade
  • Compares with in_position value from signal
  • Logs warnings if mismatch
  • Auto-closes position if needed

3. Repeat Periodically​

Send validation signals at your specified interval (e.g., every 8 hours).


TradingView 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.


Best Practices​

1. Enable for All Automated Strategies​

{
"safety_check": true,
"safety_check_interval_minutes": 480
}

2. Use Reasonable Intervals​

  • Day trading: 240-480 minutes (4-8 hours)
  • Swing trading: 720-1440 minutes (12-24 hours)

3. Include Descriptive Reasons​

{
"reason": "Daily position sync at market close"
}

4. Monitor Validation Warnings​

Check logs for position mismatches and investigate causes.


Next Steps​