-
Notifications
You must be signed in to change notification settings - Fork 724
Money Management
- Introduction
- MoneyManagerBase Interface
- Fixed Capital Strategy
- Fixed Count Strategy
- Fixed Percent Strategy
- Fixed Risk Strategy
- Williams Fixed Risk Strategy
- Implementation and Configuration Examples
- Performance Considerations
- Conclusion
The Hikyuu money management system provides a comprehensive framework for position sizing and risk management in trading systems. This documentation details the available money management strategies including MM_FixedCapital, MM_FixedCount, MM_FixedPercent, MM_FixedRisk, and WilliamsFixedRisk. Each strategy implements the MoneyManagerBase interface and determines trade size based on different parameters such as account equity, risk percentage, or fixed units. The system is designed to integrate seamlessly with trading systems and supports both C++ and Python interfaces.
The MoneyManagerBase class serves as the foundation for all money management strategies in Hikyuu. It defines the core interface that all concrete implementations must follow.
classDiagram
class MoneyManagerBase {
+string name()
+void name(string)
+void reset()
+void setTM(TradeManagerPtr)
+TradeManagerPtr getTM()
+void setQuery(KQuery)
+KQuery getQuery()
+void buyNotify(TradeRecord)
+void sellNotify(TradeRecord)
+double getSellNumber(Datetime, Stock, price_t, price_t, SystemPart)
+double getSellShortNumber(Datetime, Stock, price_t, price_t, SystemPart)
+double getBuyShortNumber(Datetime, Stock, price_t, price_t, SystemPart)
+double getBuyNumber(Datetime, Stock, price_t, price_t, SystemPart)
+size_t currentBuyCount(Stock)
+size_t currentSellCount(Stock)
+virtual double _getBuyNumber(Datetime, Stock, price_t, price_t, SystemPart) = 0
+virtual double _getSellNumber(Datetime, Stock, price_t, price_t, SystemPart)
+virtual double _getSellShortNumber(Datetime, Stock, price_t, price_t, SystemPart)
+virtual double _getBuyShortNumber(Datetime, Stock, price_t, price_t, SystemPart)
+virtual void _reset()
+virtual MoneyManagerPtr _clone() = 0
}
class FixedCapitalMoneyManager {
+double _getBuyNumber(Datetime, Stock, price_t, price_t, SystemPart)
}
class FixedCountMoneyManager {
+double _getBuyNumber(Datetime, Stock, price_t, price_t, SystemPart)
}
class FixedPercentMoneyManager {
+double _getBuyNumber(Datetime, Stock, price_t, price_t, SystemPart)
}
class FixedRiskMoneyManager {
+double _getBuyNumber(Datetime, Stock, price_t, price_t, SystemPart)
}
class WilliamsFixedRiskMoneyManager {
+double _getBuyNumber(Datetime, Stock, price_t, price_t, SystemPart)
}
MoneyManagerBase <|-- FixedCapitalMoneyManager
MoneyManagerBase <|-- FixedCountMoneyManager
MoneyManagerBase <|-- FixedPercentMoneyManager
MoneyManagerBase <|-- FixedRiskMoneyManager
MoneyManagerBase <|-- WilliamsFixedRiskMoneyManager
Diagram sources
- MoneyManagerBase.h
- FixedCapitalMoneyManager.cpp
- FixedCountMoneyManager.cpp
- FixedPercentMoneyManager.cpp
- FixedRiskMoneyManager.cpp
- WilliamsFixedRiskMoneyManager.cpp
Section sources
- MoneyManagerBase.h
The MM_FixedCapital strategy implements a fixed capital position sizing approach where the number of units to buy is determined by dividing the current cash by a fixed capital amount.
The FixedCapitalMoneyManager calculates position size using the formula: Buy Quantity = Current Cash / Capital. This strategy is useful when you want to allocate a fixed portion of your capital to each trade.
- capital: The fixed capital amount used for position sizing (default: 10000.00)
flowchart TD
Start([Start]) --> GetCash["Get current cash from TradeManager"]
GetCash --> Calculate["Calculate position size = cash / capital"]
Calculate --> Return["Return position size"]
Return --> End([End])
Diagram sources
- FixedCapitalMoneyManager.cpp
Section sources
- MM_FixedCapital.h
- FixedCapitalMoneyManager.cpp
The MM_FixedCount strategy implements a fixed quantity position sizing approach where each trade buys a predetermined number of units regardless of account size or risk.
The FixedCountMoneyManager returns a constant value for the number of units to buy, making it useful for testing and comparison purposes. This strategy does not consider account equity or risk parameters.
- n: The fixed number of units to trade (default: 100)
flowchart TD
Start([Start]) --> Return["Return fixed count parameter n"]
Return --> End([End])
Diagram sources
- FixedCountMoneyManager.cpp
Section sources
- MM_FixedCount.h
- FixedCountMoneyManager.cpp
The MM_FixedPercent strategy implements a percentage risk model based on Van K. Tharp's position sizing methods, where position size is determined by the percentage of total equity at risk.
The FixedPercentMoneyManager calculates position size using the formula: Position Size = (Current Cash × Risk Percentage) / Risk per Unit. This approach ensures that each trade risks a consistent percentage of the trading account.
- p: The percentage of total equity to risk per trade (default: 0.02, representing 2%)
flowchart TD
Start([Start]) --> GetCash["Get current cash from TradeManager"]
GetCash --> Calculate["Calculate position size = (cash × p) / risk"]
Calculate --> Return["Return position size"]
Return --> End([End])
Diagram sources
- FixedPercentMoneyManager.cpp
Section sources
- MM_FixedPercent.h
- FixedPercentMoneyManager.cpp
The MM_FixedRisk strategy implements a fixed monetary risk approach where each trade risks a predetermined dollar amount regardless of account size.
The FixedRiskMoneyManager calculates position size using the formula: Position Size = Fixed Risk Amount / Risk per Unit. This strategy ensures that each trade has the same absolute dollar risk.
- risk: The fixed monetary amount to risk per trade (default: 1000.00)
flowchart TD
Start([Start]) --> Calculate["Calculate position size = risk / risk_per_unit"]
Calculate --> Return["Return position size"]
Return --> End([End])
Diagram sources
- FixedRiskMoneyManager.cpp
Section sources
- MM_FixedRisk.h
- FixedRiskMoneyManager.cpp
The WilliamsFixedRisk strategy implements a modified fixed risk approach inspired by Larry Williams' trading methods, combining percentage risk with a maximum loss parameter.
The WilliamsFixedRiskMoneyManager calculates position size using the formula: Position Size = (Account Balance × Risk Percentage) / Maximum Loss. This approach combines elements of percentage risk with a fixed maximum loss parameter.
- p: The risk percentage of account balance (default: 0.1, representing 10%)
- max_loss: The maximum loss amount per unit (default: 1000.00)
flowchart TD
Start([Start]) --> GetCash["Get current cash from TradeManager"]
GetCash --> Calculate["Calculate position size = (cash × p) / max_loss"]
Calculate --> Validate["Validate max_loss > 0"]
Validate --> Return["Return position size or 0 if invalid"]
Return --> End([End])
Diagram sources
- WilliamsFixedRiskMoneyManager.cpp
Section sources
- MM_WilliamsFixedRisk.h
- WilliamsFixedRiskMoneyManager.cpp
sequenceDiagram
participant User as "User Code"
participant MM as "MoneyManager"
participant TM as "TradeManager"
User->>MM : MM_FixedCapital(5000.0)
MM-->>User : MoneyManagerPtr
User->>MM : setParam("capital", 10000.0)
User->>MM : getBuyNumber(datetime, stock, price, risk, from)
MM->>TM : m_tm->cash(datetime, kType)
TM-->>MM : Current cash amount
MM-->>User : Calculated position size
Diagram sources
- FixedCapitalMoneyManager.cpp
sequenceDiagram
participant User as "Python Script"
participant PyWrapper as "Python Wrapper"
participant CppMM as "C++ MoneyManager"
User->>PyWrapper : MM_FixedPercent(0.03)
PyWrapper->>CppMM : Create FixedPercentMoneyManager
CppMM-->>PyWrapper : MoneyManagerPtr
PyWrapper-->>User : MoneyManager instance
User->>CppMM : getBuyNumber(datetime, stock, price, risk, from)
CppMM->>TradeManager : Query current cash
TradeManager-->>CppMM : Cash value
CppMM-->>User : Position size calculation
Diagram sources
- trade_sys_main.cpp
- FixedPercentMoneyManager.cpp
Section sources
- test_money_manager.py
When implementing money management strategies in high-frequency trading scenarios, several performance factors should be considered:
All money management strategies in Hikyuu are designed for efficient calculation with O(1) time complexity for position sizing operations. The calculations involve simple arithmetic operations that can be executed quickly.
The memory footprint of money managers is minimal, primarily storing parameter values and maintaining transaction state. The FixedCapital, FixedCount, and FixedRisk strategies have the lowest memory overhead as they only store one or two parameters.
When integrating with trading systems, the performance impact is primarily determined by:
- Frequency of position size calculations
- Overhead of querying current cash balance from TradeManager
- Serialization costs when persisting state
The system is optimized to minimize these overheads through efficient data structures and caching mechanisms.
Section sources
- MoneyManagerBase.h
- FixedCapitalMoneyManager.cpp
The Hikyuu money management system provides a robust framework for implementing various position sizing strategies. The MoneyManagerBase interface ensures consistency across different strategies while allowing for specialized implementations. Each strategy serves different risk management needs:
- MM_FixedCapital for capital-based allocation
- MM_FixedCount for consistent unit trading
- MM_FixedPercent for percentage risk management
- MM_FixedRisk for fixed monetary risk
- WilliamsFixedRisk for combined percentage and maximum loss approaches
The system supports both C++ and Python interfaces, making it accessible for different development environments. When implementing custom money management logic, developers should inherit from MoneyManagerBase and implement the required virtual methods while adhering to the parameter validation and cloning interfaces.