Strategy vault / Tools & templates
Dynamic Balancing Strategy with 50 Funds and 50 Positions
This strategy dynamically balances between 50% funds and 50% positions to control risk. By continuously adjusting the ratio between funds and positions, it manages risk for investors who cannot monitor the market in…

Strategy Overview
This strategy dynamically balances between 50% funds and 50% positions to control risk. By continuously adjusting the ratio between funds and positions, it manages risk for investors who cannot monitor the market in real-time.
Strategy Logic
Initialize capital at 1 million, divided equally into 50% funds and 50% positions.
During the trading period, if remaining funds exceed unrealized profit/loss by 1.05 times at each open, use 2.5% of remaining funds to add positions.
If unrealized profit/loss exceeds remaining funds by 1.05 times, sell partial positions to restore balance.
Close all positions at end of trading period.
Advantages
Effective risk control by dynamically balancing funds and positions, avoiding huge losses in extreme market conditions.
Simple to operate for busy investors, only need to adjust fund/position ratios.
Customizable parameters to meet varying risk appetites.
Risks
Unable to capitalize on short-term fluctuations, profit potential limited.
Long one-sided run may result in insufficient position size.
Improper parameter tuning leads to excess position flipping or low capital utilization.
Enhancement Opportunities
Introduce more parameters for finer fund/position control.
Incorporate stop loss/profit taking for larger positions.
Test different trading period parameters to improve adaptability.
Conclusion
This strategy achieves risk control by dynamically balancing between funds and positions. Simple to implement compared to other strategies. Can be further improved by introducing more adjustable parameters and combining with other strategy concepts.
Source Pine Script
/*backtest
start: 2023-12-17 00:00:00
end: 2023-12-18 19:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("00631L Trading Simulation", shorttitle="Sim", overlay=true, initial_capital = 1000000)
// Set principal
capital = 1000000
// Set purchase and sale date range
start_date = timestamp(2020, 11, 4)
next_date = timestamp(2020, 11, 5)
sell_date = timestamp(2023, 10, 24)
end_date = timestamp(2023, 10, 25) // End date changed to October 25, 2023
// Determine if it is during trading hours
in_trade_period = true
// Realized profit and loss
realized_profit_loss = strategy.netprofit
plot(realized_profit_loss, title="realized_profit_loss", color=color.blue)
// Unrealized profit and loss
open_profit_loss = strategy.position_size * open
plot(open_profit_loss, title="open_profit_loss", color=color.red)
// Remaining funds
remaining_funds = capital + realized_profit_loss - (strategy.position_size * strategy.position_avg_price)
plot(remaining_funds, title="remaining_funds", color=color.yellow)
// Total equity
total_price = remaining_funds + open_profit_loss
plot(total_price, title="remaining_funds", color=color.white)
// Buying logic: buy on each trading day during the trading period daily_investment Amount of products
first_buy = time >= start_date and time <= next_date
buy_condition = in_trade_period and dayofmonth != dayofmonth[1]
// Selling logic : Sell all commodities on the expiration date during the trading period.
sell_all = time >= sell_date
// Buy on the first day of the trading period50%Principal
if first_buy
strategy.order("First", strategy.long, qty = capital/2/open)
// in eachKBuy at the opening of the line
// Adding logic : Remaining funds > Unrealized profit and loss * 1.05
add_logic = remaining_funds > open_profit_loss * 1.05
if buy_condition
strategy.order("Buy", strategy.long, when = add_logic, qty = remaining_funds * 0.025 / open)
//
// Subtraction logic : Remaining funds > Unrealized profit and loss * 1.05
sub_logic = open_profit_loss > remaining_funds * 1.05
if buy_condition
strategy.order("Sell", strategy.short, when = sub_logic, qty = open_profit_loss * 0.025/open)
//
strategy.order("Sell_all", strategy.short, when = sell_all, qty = strategy.position_size)
// Draw a rectangular area during the trading period
bgcolor(in_trade_period ? color.green : na, transp=90)
Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-25. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a tested, production-ready module from strategies/Dynamic-Balancing-Strategy-with-50-Funds-and-50-Positions.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- CSO Custom Signal Oscillator Strategy CSOPine Script · Tools & templates · 2h
- Mechanical Trading StrategyPine Script · Tools & templates · 4h
- Multi Indicator Configurable Strategy GeneratorPine Script · Tools & templates · 5m
- MA MACD BB Multi Indicator Trading Strategy Backtesting ToolPine Script · Tools & templates · 1d
- ML Alerts TemplatePine Script · Tools & templates · 2h
- Monthly Returns Strategy with BenchmarkPine Script · Tools & templates · 2h