Skip to main content
Skip to main content
Version: Next 🚧

Exit Conditions Schema

This document provides the complete schema reference for the exitConditions configuration object. Exit conditions define when and how the algorithm should close open positions.

Overview

Exit conditions are Step 4 of algorithm configuration. They control:

  • Stop loss rules (4 types)
  • Take profit rules (5 types)
  • Trailing stops
  • Break-even stops
  • Partial exits
  • Time-based exits
  • Exit on opposite signal

Schema Structure

interface ExitConditions {
stopLoss: StopLoss
takeProfit?: TakeProfit
trailingStop?: TrailingStop
breakEvenStop?: BreakEvenStop
partialExits?: PartialExit[]
timeBasedExit?: TimeBasedExit
exitOnOppositeSignal?: ExitOnOppositeSignal
}

Exit Priority Order

When multiple exit conditions trigger simultaneously, only the first one executes:

  1. Opposite Signal - Highest priority
  2. Partial Exits - Second priority
  3. Break-even Stop - Third priority
  4. Stop Loss - Fourth priority
  5. Trailing Stop - Fifth priority
  6. Take Profit - Sixth priority
  7. Time Exit - Lowest priority

Stop Loss Configuration

Stop loss is required and protects against excessive losses.

interface StopLoss {
type: 'fixed' | 'percentage' | 'atr' | 'indicator'

// Type-specific parameters
price?: number // For 'fixed'
percentage?: number // For 'percentage'
atrPeriod?: number // For 'atr'
atrMultiplier?: number // For 'atr'
indicator?: Indicator // For 'indicator'
}

Stop Loss Types

1. Fixed Stop Loss

Fixed price level for stop loss.

{
type: 'fixed',
price: number
}

Example:

{
"stopLoss": {
"type": "fixed",
"price": 490
}
}

With entry at ₹500, stop loss at ₹490 (₹10 risk per share).


2. Percentage Stop Loss

Stop loss as percentage from entry price.

{
type: 'percentage',
percentage: number // 0.1 to 50
}

Calculation:

Long:  Stop Price = Entry × (1 - percentage/100)
Short: Stop Price = Entry × (1 + percentage/100)

Example:

{
"stopLoss": {
"type": "percentage",
"percentage": 2.0
}
}

With ₹500 entry:

  • Long: Stop at ₹490 (₹500 × 0.98)
  • Short: Stop at ₹510 (₹500 × 1.02)

Recommended Values:

  • Scalping: 0.5-1%
  • Day Trading: 1-2%
  • Swing Trading: 2-5%
  • Position Trading: 5-10%

3. ATR-Based Stop Loss

Dynamic stop loss based on Average True Range.

{
type: 'atr',
atrPeriod: number, // 5 to 50
atrMultiplier: number // 0.5 to 5.0
}

Calculation:

ATR = Average True Range over period
Stop Distance = ATR × Multiplier
Long: Stop Price = Entry - Stop Distance
Short: Stop Price = Entry + Stop Distance

Example:

{
"stopLoss": {
"type": "atr",
"atrPeriod": 14,
"atrMultiplier": 2.0
}
}

With ₹500 entry and ATR = ₹5:

  • Stop Distance = ₹5 × 2.0 = ₹10
  • Long: Stop at ₹490
  • Short: Stop at ₹510

Benefits: Adapts to market volatility automatically.


4. Indicator-Based Stop Loss

Use indicator value as stop loss level.

{
type: 'indicator',
indicator: Indicator
}

Supported Indicators:

  • Moving Averages (SMA, EMA, WMA)
  • Parabolic SAR
  • Keltner Channels (lower/upper band)

Example:

{
"stopLoss": {
"type": "indicator",
"indicator": {
"type": "EMA",
"period": 20
}
}
}

Stop loss follows EMA(20) - exits when price crosses below EMA.


Take Profit Configuration

Take profit is optional but recommended for profit targets.

interface TakeProfit {
type: 'fixed' | 'percentage' | 'risk_reward' | 'trailing' | 'multiple_targets'

// Type-specific parameters
price?: number // For 'fixed'
percentage?: number // For 'percentage'
ratio?: number // For 'risk_reward'
trailingPercentage?: number // For 'trailing'
targets?: ProfitTarget[] // For 'multiple_targets'
}

Take Profit Types

1. Fixed Take Profit

Fixed price level for take profit.

{
type: 'fixed',
price: number
}

Example:

{
"takeProfit": {
"type": "fixed",
"price": 520
}
}

With entry at ₹500, take profit at ₹520 (₹20 profit per share).


2. Percentage Take Profit

Take profit as percentage from entry price.

{
type: 'percentage',
percentage: number // 0.1 to 100
}

Calculation:

Long:  TP Price = Entry × (1 + percentage/100)
Short: TP Price = Entry × (1 - percentage/100)

Example:

{
"takeProfit": {
"type": "percentage",
"percentage": 4.0
}
}

With ₹500 entry:

  • Long: TP at ₹520 (₹500 × 1.04)
  • Short: TP at ₹480 (₹500 × 0.96)

Recommended Values:

  • Scalping: 0.5-2%
  • Day Trading: 2-5%
  • Swing Trading: 5-15%
  • Position Trading: 15-50%

3. Risk-Reward Take Profit

Calculate take profit based on risk-reward ratio.

{
type: 'risk_reward',
ratio: number // 1.0 to 10.0
}

Calculation:

Risk = Entry - Stop Loss
Reward = Risk × Ratio
Long: TP Price = Entry + Reward
Short: TP Price = Entry - Reward

Example:

{
"takeProfit": {
"type": "risk_reward",
"ratio": 2.0
}
}

With ₹500 entry, ₹490 stop (₹10 risk):

  • Reward = ₹10 × 2.0 = ₹20
  • Long: TP at ₹520
  • Short: TP at ₹480

Recommended Ratios:

  • Minimum: 1:1 (break-even after costs)
  • Good: 2:1 or 3:1
  • Aggressive: 5:1 or higher

4. Trailing Take Profit

Take profit that moves with price.

{
type: 'trailing',
trailingPercentage: number // 0.1 to 10
}

Behavior:

  • Profit target moves up as price increases (long)
  • Profit target moves down as price decreases (short)
  • Never moves against favorable direction

Example:

{
"takeProfit": {
"type": "trailing",
"trailingPercentage": 2.0
}
}

Long position with ₹500 entry:

  • Price reaches ₹520: TP at ₹509.60 (₹520 × 0.98)
  • Price reaches ₹530: TP at ₹519.40 (₹530 × 0.98)
  • Price drops to ₹525: TP stays at ₹519.40 (doesn't move down)

5. Multiple Targets (Partial Exits)

Multiple profit targets with partial position exits.

{
type: 'multiple_targets',
targets: ProfitTarget[]
}

interface ProfitTarget {
percentage: number // Profit percentage
exitPercentage: number // Position percentage to exit
moveStopTo?: 'entry' | 'breakeven' | number
}

Example:

{
"takeProfit": {
"type": "multiple_targets",
"targets": [
{
"percentage": 2.0,
"exitPercentage": 33,
"moveStopTo": "breakeven"
},
{
"percentage": 4.0,
"exitPercentage": 33
},
{
"percentage": 6.0,
"exitPercentage": 34
}
]
}
}

With 100 shares at ₹500 entry:

  1. At ₹510 (2% profit): Exit 33 shares, move stop to breakeven
  2. At ₹520 (4% profit): Exit 33 shares
  3. At ₹530 (6% profit): Exit remaining 34 shares

Trailing Stop Configuration

Optional trailing stop that locks in profits.

interface TrailingStop {
enabled: boolean
type: 'percentage' | 'atr'
percentage?: number // For 'percentage'
atrPeriod?: number // For 'atr'
atrMultiplier?: number // For 'atr'
activationThreshold: number // Profit % to activate
}

enabled

  • Type: boolean
  • Required: Yes
  • Description: Whether trailing stop is enabled
  • Example: true

type

  • Type: enum
  • Required: Yes (if enabled)
  • Values: percentage, atr
  • Example: "percentage"

percentage

  • Type: number
  • Required: Yes (if type is 'percentage')
  • Range: 0.1 to 10
  • Description: Trailing distance as percentage
  • Example: 1.5

atrPeriod and atrMultiplier

  • Required: Yes (if type is 'atr')
  • Description: ATR-based trailing distance
  • Example: atrPeriod: 14, atrMultiplier: 2.0

activationThreshold

  • Type: number
  • Required: Yes (if enabled)
  • Range: 0.1 to 20
  • Description: Profit percentage required to activate trailing
  • Example: 1.0 (activate after 1% profit)

Example:

{
"trailingStop": {
"enabled": true,
"type": "percentage",
"percentage": 1.5,
"activationThreshold": 1.0
}
}

Long position at ₹500:

  • Price reaches ₹505 (1% profit): Trailing activates
  • Stop placed at ₹497.43 (₹505 × 0.985)
  • Price reaches ₹510: Stop moves to ₹502.35 (₹510 × 0.985)
  • Price drops to ₹508: Stop stays at ₹502.35
  • Price drops to ₹502.35: Position exits

Break-Even Stop Configuration

Optional break-even stop that moves stop to entry after profit threshold.

interface BreakEvenStop {
enabled: boolean
triggerProfit: number // Profit % to trigger
offset: number // Offset from entry (%)
}

enabled

  • Type: boolean
  • Required: Yes
  • Description: Whether break-even stop is enabled
  • Example: true

triggerProfit

  • Type: number
  • Required: Yes (if enabled)
  • Range: 0.1 to 10
  • Default: 1.0
  • Description: Profit percentage to trigger break-even
  • Example: 1.5

offset

  • Type: number
  • Required: Yes (if enabled)
  • Range: 0 to 1
  • Default: 0.1
  • Description: Offset from entry for safety (%)
  • Example: 0.1

Example:

{
"breakEvenStop": {
"enabled": true,
"triggerProfit": 1.0,
"offset": 0.1
}
}

Long position at ₹500 with ₹490 stop:

  • Price reaches ₹505 (1% profit): Break-even triggers
  • Stop moves to ₹500.50 (entry + 0.1% offset)
  • Now risk-free trade (small profit locked in)

Partial Exits Configuration

Optional array of partial exit rules (alternative to multiple_targets take profit).

interface PartialExit {
profitPercentage: number // Profit % to trigger
exitPercentage: number // Position % to exit
moveStopTo?: 'entry' | 'breakeven' | number
}

Example:

{
"partialExits": [
{
"profitPercentage": 2.0,
"exitPercentage": 50,
"moveStopTo": "breakeven"
},
{
"profitPercentage": 4.0,
"exitPercentage": 50
}
]
}

With 100 shares:

  1. At 2% profit: Exit 50 shares, move stop to breakeven
  2. At 4% profit: Exit remaining 50 shares

Time-Based Exit Configuration

Optional time-based exit rules.

interface TimeBasedExit {
enabled: boolean
maxHoldMinutes?: number // Maximum hold time
partialExits?: TimePartialExit[]
}

interface TimePartialExit {
minutes: number
exitPercentage: number
}

enabled

  • Type: boolean
  • Required: Yes
  • Description: Whether time-based exit is enabled
  • Example: true

maxHoldMinutes

  • Type: number
  • Required: No
  • Range: 1 to 43200 (30 days)
  • Description: Maximum minutes to hold position
  • Example: 1440 (24 hours)

partialExits

  • Type: TimePartialExit[]
  • Required: No
  • Description: Partial exits at specific times

Example:

{
"timeBasedExit": {
"enabled": true,
"maxHoldMinutes": 1440,
"partialExits": [
{
"minutes": 480,
"exitPercentage": 50
}
]
}
}
  • After 8 hours: Exit 50% of position
  • After 24 hours: Exit remaining 50%

Exit on Opposite Signal Configuration

Optional exit when opposite entry signal triggers.

interface ExitOnOppositeSignal {
enabled: boolean
closeImmediately: boolean
}

enabled

  • Type: boolean
  • Required: Yes
  • Description: Whether to exit on opposite signal
  • Example: true

closeImmediately

  • Type: boolean
  • Required: Yes (if enabled)
  • Description: Whether to close immediately or wait for next candle
  • Example: true

Example:

{
"exitOnOppositeSignal": {
"enabled": true,
"closeImmediately": true
}
}

Long position with EMA crossover strategy:

  • Entry: EMA(9) crosses above EMA(21)
  • Exit: EMA(9) crosses below EMA(21) (opposite signal)

Complete Examples

Conservative with Break-Even

{
"stopLoss": {
"type": "percentage",
"percentage": 2.0
},
"takeProfit": {
"type": "risk_reward",
"ratio": 3.0
},
"breakEvenStop": {
"enabled": true,
"triggerProfit": 1.0,
"offset": 0.1
}
}

Aggressive with Trailing

{
"stopLoss": {
"type": "atr",
"atrPeriod": 14,
"atrMultiplier": 1.5
},
"takeProfit": {
"type": "percentage",
"percentage": 10.0
},
"trailingStop": {
"enabled": true,
"type": "percentage",
"percentage": 2.0,
"activationThreshold": 2.0
}
}

Scalping with Partial Exits

{
"stopLoss": {
"type": "percentage",
"percentage": 0.5
},
"takeProfit": {
"type": "multiple_targets",
"targets": [
{
"percentage": 0.5,
"exitPercentage": 50,
"moveStopTo": "breakeven"
},
{
"percentage": 1.0,
"exitPercentage": 50
}
]
},
"timeBasedExit": {
"enabled": true,
"maxHoldMinutes": 30
}
}

Swing Trading with Indicator Stop

{
"stopLoss": {
"type": "indicator",
"indicator": {
"type": "EMA",
"period": 20
}
},
"takeProfit": {
"type": "risk_reward",
"ratio": 2.0
},
"trailingStop": {
"enabled": true,
"type": "atr",
"atrPeriod": 14,
"atrMultiplier": 2.0,
"activationThreshold": 3.0
},
"exitOnOppositeSignal": {
"enabled": true,
"closeImmediately": false
}
}

Validation Rules

General Rules

  • Stop loss is required
  • At least one exit method should be configured
  • Percentages must be positive
  • Ratios must be >= 1.0

Stop Loss Validation

  • Fixed: price must be specified
  • Percentage: must be between 0.1 and 50
  • ATR: period (5-50), multiplier (0.5-5.0)
  • Indicator: must be supported indicator type

Take Profit Validation

  • Fixed: price must be specified
  • Percentage: must be between 0.1 and 100
  • Risk-Reward: ratio must be between 1.0 and 10.0
  • Multiple Targets: sum of exitPercentage must equal 100

Trailing Stop Validation

  • Percentage: must be between 0.1 and 10
  • ATR: period (5-50), multiplier (0.5-5.0)
  • Activation threshold: must be between 0.1 and 20