📘Smart Position Control Tools! Freqtrade’s Two Key Functions to Say Goodbye to Rigid Buying

This article was produced by the Quantitative Trading Lab at https://www.itrade.icu. Visit for more benefits. In quantitative trading, position management is a key factor in controlling risk and improving returns. Freqtrade provides two powerful and flexible functions to help us intelligently adjust positions:

  • custom_stake_amount: Dynamically calculate the position size for each trade.

  • adjust_trade_position: Automatically increase or decrease positions based on market and current holdings.

This article will explain the functionality and use cases of these two functions, with complete code examples to help you master the core techniques of intelligent position control.


🔍 1. custom_stake_amount: Dynamically Calculate Buy Position

Function Overview

custom_stake_amount allows the strategy to dynamically calculate the amount or coin quantity for the current buy, replacing the fixed stake_amount in the configuration file. You can flexibly adjust the buy size based on market trend, account balance, volatility, and other factors.


Typical Use Cases

Scenario
Description

Strong market trend

Increase investment, enlarge position

Market volatility or unclear trend

Reduce investment, open positions cautiously

Changes in account balance

Dynamically adjust position according to current balance


Function Signature Example

def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: float,
                        proposed_stake: float, min_stake: float, max_stake: float, **kwargs) -> float:
    """
    Return the actual amount to invest in this buy.
    """

📘 Example: Dynamically Adjust Stake Based on Account Balance


🔍 2. adjust_trade_position: Intelligent Position Adjustment During Holdings

Function Overview

adjust_trade_position handles position adjustments while holding, including:

  • Automatically adding to positions based on market changes

  • Identifying risk signals to reduce positions promptly

  • Implementing partial take-profit or stop-loss


Typical Use Cases

Scenario
Adjustment Action
Description

Profit exceeds threshold

Add position

Capture trend and increase returns

Loss exceeds warning threshold

Reduce position

Control risk and prevent further loss

Sudden market reversal

Partial exit

Protect profit and reduce drawdown


Function Signature Example


Example: Adjust Position Based on Profit/Loss Ratio


📈 3. Intelligent Position Control Flow Diagram


🔔 4. Recommendations and Notes

  • Boundary limits: Always limit minimum/maximum buy amount to prevent extreme orders.

  • Risk control: Combine with overall position allocation to avoid overexposure and liquidation risk.

  • Logging: Record each position adjustment for easier backtesting and tuning.

  • Test coverage: Backtest across multiple scenarios to ensure robust logic and prevent overfitting.


5. Summary

custom_stake_amount and adjust_trade_position are the two key tools in Freqtrade for intelligent position management.

  • custom_stake_amount dynamically decides the investment amount for opening a position.

  • adjust_trade_position allows position adjustments during holding.

Using them properly makes your strategy more flexible, aligned with real market conditions, and able to improve profits while controlling risk.

Last updated