Skip to content

Risk Management in DirectionalScalper

This document explains the risk management strategies implemented in the trading bot, focusing on four key parameters from the configuration.

Example Key Configuration Parameters

  1. wallet_exposure_limit_long: 0.006 (0.6%)
  2. wallet_exposure_limit_short: 0.002 (0.2%)
  3. max_qty_percent_long: 30%
  4. max_qty_percent_short: 30%

Understanding the Parameters

Wallet Exposure Limits

The wallet_exposure_limit_long and wallet_exposure_limit_short parameters define the maximum percentage of the total equity that can be allocated to long and short positions, respectively.

  • Long positions: Up to 0.6% of total equity
  • Short positions: Up to 0.2% of total equity

These limits help prevent overexposure to any single trading direction and maintain a balanced risk profile.

Maximum Quantity Percentages

The max_qty_percent_long and max_qty_percent_short parameters set the maximum allowed position size as a percentage of the total equity for long and short positions, respectively.

  • Both long and short positions: Up to 30% of total equity

These limits act as an additional safety measure to prevent any single position from becoming too large relative to the total equity.

Risk Management Implementation

The bot implements risk management through the check_and_manage_positions function. Here's how it works:

  1. Calculate Maximum Allowed Positions:

    • For long positions: max_qty_long = (total_equity * wallet_exposure_limit_long) / current_price
    • For short positions: max_qty_short = (total_equity * wallet_exposure_limit_short) / current_price
  2. Calculate Position Exposure Percentages:

    • Long exposure: (long_pos_qty * current_price / total_equity) * 100
    • Short exposure: (short_pos_qty * current_price / total_equity) * 100
  3. Check Against Maximum Quantity Percentages:

    • If long exposure > max_qty_percent_long:
      • Clear long grid
      • Remove symbol from active grids
      • Add symbol to max_qty_reached_symbol_long set
    • If short exposure > max_qty_percent_short:
      • Clear short grid
      • Remove symbol from active grids
      • Add symbol to max_qty_reached_symbol_short set
  4. Recovery Mechanism:

    • If a symbol's exposure falls below the maximum allowed percentage, it is removed from the respective max_qty_reached_symbol set, allowing trading to resume.

Additional Risk Management Features

Proper DCA (Dollar Cost Averaging) Implementation

The bot uses two functions for implementing a proper DCA strategy:

  1. calculate_total_amount_notional_ls_properdca:

    • Calculates the total notional amount for a grid based on the wallet exposure limits and current position values.
    • Ensures that the total position value (including existing positions) doesn't exceed the maximum allowed exposure.
  2. calculate_order_amounts_notional_properdca:

    • Distributes the total notional amount across multiple orders in the grid.
    • Uses a strength parameter to adjust the distribution, allowing for more aggressive or conservative strategies.
    • Ensures that each order meets minimum notional and quantity requirements set by the exchange.

Drawdown Behavior

The bot supports different drawdown behaviors, which define how capital is allocated during drawdown scenarios:

Default Drawdown Behavior: maxqtypercent

  1. Behavior Explanation:

    • In the default maxqtypercent behavior, the bot gradually allocates capital to grid levels as the market moves against the position. The allocation is influenced by the strength parameter, which controls the distribution of capital across levels.
    • This approach aims to dollar cost average (DCA) into the position, spreading the risk and allowing the position to recover as the market retraces.
  2. Impact on Order Sizing:

    • Capital is distributed across the specified number of grid levels based on the strength parameter. Higher strength values will allocate more capital to levels further from the current price.
    • The bot adjusts the total amount to ensure that the position size remains within the defined wallet exposure limits.

New Drawdown Behavior: Aggressive Full Distribution

  1. Behavior Explanation:

    • When drawdown_behavior is set to aggressive_full_distribution, the bot aggressively allocates the full remaining allowable capital to a specified number of grid levels.
    • This approach aims to quickly recover from a drawdown by placing large orders that can bring the open position back to the current price and into profit.
  2. Impact on Order Sizing:

    • The entire adjusted maximum position value, calculated by subtracting the current position value from the maximum position value (based on wallet exposure limits), is distributed across the grid levels.
    • The strength parameter influences how the capital is distributed among the levels. Higher strength values will allocate more capital to levels further from the current price, while lower strength values will result in a more even distribution.
    • Unlike the default DCA approach, this behavior does not adjust based on the current position size within the grid, focusing instead on maximizing the deployment of available capital.
  3. Risk Considerations:

    • The aggressive_full_distribution behavior can lead to very large orders, especially if the wallet exposure limit is high (e.g., set to 1.0). This approach increases the potential for quick recovery but also raises the risk of significant drawdowns if the market moves unfavorably.
    • It is crucial to carefully manage and understand the implications of using this behavior, as it prioritizes aggressive recovery over conservative risk management.

Grid Management

  • The bot clears grids when position sizes exceed the maximum allowed percentages.
  • It uses sets (max_qty_reached_symbol_long and max_qty_reached_symbol_short) to track symbols that have reached their maximum allowed quantities.
  • Trading is paused for symbols in these sets until their exposure falls below the maximum allowed percentage.

Conclusion

This risk management system provides a comprehensive approach to controlling exposure and managing position sizes. By using wallet exposure limits and maximum quantity percentages, the bot maintains a balanced risk profile while allowing for effective trading across multiple symbols and strategies.

The implementation of proper DCA, dynamic grid management, and the new aggressive drawdown behavior further enhances the bot's ability to adapt to market conditions while adhering to predefined risk parameters.