Advanced Position Types
Advanced position types for complex trading scenarios and system operations.
CHANGE_DIRECTION
Close all opposite-side positions and open a new position in the specified direction.
Example
{
"strategy_key": "your_key",
"position_side": "CHANGE_DIRECTION",
"action": "BUY",
"entry_price": 50000,
"stop_loss_price": 48000,
"take_profit_price": 55000
}
How It Works
- Closes all positions on opposite side (if any)
- Opens new position in specified direction
- Useful for trend reversals
Use Cases
Trend Reversal:
// Was short, now going long
{
"position_side": "CHANGE_DIRECTION",
"action": "BUY"
}
Strategy Flip:
// Close all longs, open short
{
"position_side": "CHANGE_DIRECTION",
"action": "SELL"
}
VALIDATE_ALERT
Validate that positions in OptAlgo match positions in external systems (TradingView, etc.). Used for safety checks.
Example
{
"strategy_key": "your_key",
"position_side": "VALIDATE_ALERT",
"in_position": true,
"safety_check": true,
"safety_check_interval_minutes": 480
}
Parameters
in_position: Boolean indicating if external system has open positionsafety_check: Enable/disable safety checkingsafety_check_interval_minutes: How often to check (240-1440 minutes)
Validation Scenarios
Case 1: Positions Match ✅
External: in_position = true
OptAlgo: Has open trade
Result: Check passed
Case 2: Missing OptAlgo Position ⚠️
External: in_position = true
OptAlgo: No open trade
Result: Warning logged (if strategy not verified)
Case 3: Extra OptAlgo Position 🔴
External: in_position = false
OptAlgo: Has open trade
Result: Position automatically closed
TradingView Integration
// PineScript example
inPosition = strategy.position_size != 0
if (hour % 8 == 0 and minute == 0)
alertJson = '{"strategy_key":"' + strategyKey + '","position_side":"VALIDATE_ALERT","in_position":' + str.tostring(inPosition) + ',"safety_check":true,"safety_check_interval_minutes":480}'
alert(alertJson)
INTRA_ORDER
Check if a position exists; if not, create one with UPDATE_ORDER logic. Useful for ensuring position exists before updating.
Example
{
"strategy_key": "your_key",
"position_side": "INTRA_ORDER",
"action": "BUY",
"entry_price": 50000
}
How It Works
- Checks if user has open position on strategy
- If position exists → signal is ignored
- If no position → converts to
UPDATE_ORDERand creates one - Sets
skip_open_trade_check = true
Use Cases
Conditional Order Update:
// Only update if position exists
{
"position_side": "INTRA_ORDER",
"stop_loss_price": 49000
}
Safe Entry Fallback:
// Try to enter if not already in
{
"position_side": "INTRA_ORDER",
"action": "BUY",
"entry_price": 50000
}
REFRESH_TRIGGER_STATE
Refresh internal trigger states for the position without executing trades.
Example
{
"strategy_key": "your_key",
"position_side": "REFRESH_TRIGGER_STATE",
"trade_id": "abc123"
}
Details
- System-level operation
- Resets internal state machines
- No orders placed or cancelled
- Used for debugging or state recovery
When to Use
- After manual exchange operations
- Recovering from error states
- Support-directed troubleshooting
Complete Examples
Change Direction Example
// Currently SHORT, flip to LONG
{
"strategy_key": "your_key",
"exchange": "BINANCE",
"ticker": "BTCUSDT.P",
"position_side": "CHANGE_DIRECTION",
"action": "BUY",
"entry_price": 50000,
"stop_loss_price": 48000,
"take_profit_price": 55000,
"leverage": 10,
"reason": "Trend reversal detected"
}
Safety Validation Example
{
"strategy_key": "your_key",
"position_side": "VALIDATE_ALERT",
"in_position": true,
"safety_check": true,
"safety_check_interval_minutes": 720,
"reason": "12-hour safety check"
}
Conditional Update Example
{
"strategy_key": "your_key",
"position_side": "INTRA_ORDER",
"action": "BUY",
"entry_price": 50000,
"stop_loss_price": 48000,
"reason": "Ensure position exists before updating"
}
Best Practices
1. Use VALIDATE_ALERT Regularly
// Every 8 hours
{
"position_side": "VALIDATE_ALERT",
"in_position": true,
"safety_check": true,
"safety_check_interval_minutes": 480
}
2. CHANGE_DIRECTION for Clear Reversals
// Strong reversal signal
{
"position_side": "CHANGE_DIRECTION",
"action": "BUY",
"reason": "Bull trend confirmed"
}
3. INTRA_ORDER for Defensive Updates
// Only update if position exists
{
"position_side": "INTRA_ORDER",
"stop_loss_price": 49000
}
4. Include Descriptive Reasons
{
"position_side": "VALIDATE_ALERT",
"reason": "End of day position reconciliation"
}
Next Steps
- Safety Checks - Detailed safety check documentation
- Examples - Safety check examples
- Position Management - Regular position updates