Strategy vault / Trend following
Intelligent Time Based Long Short Rotation Balanced Trading Strategy
This is an intelligent rotation strategy based on time periods that seeks to generate returns through long-short rotation trading within specified time periods. The strategy employs a flexible position management…

Overview
This is an intelligent rotation strategy based on time periods that seeks to generate returns through long-short rotation trading within specified time periods. The strategy employs a flexible position management mechanism that can automatically adjust trading direction according to market conditions while incorporating risk control functions. It supports bi-directional trading and optional swing trading mode, demonstrating strong adaptability.
Strategy Principle
The strategy primarily controls trading through time periods and position status. First, the inActivePeriod() function determines whether trading is within the effective trading interval of the last 500 bars. Within the effective interval, the strategy decides trading actions based on variables such as position status (positionHeld), holding time (barsHeld), and pause time (barsPaused). When swing trading mode is enabled, the strategy rotates quickly between long and short directions; when disabled, positions are closed after 3 periods and wait for new trading opportunities.
Strategy Advantages
- High Flexibility: Supports pure long, pure short, or bi-directional trading modes
- Controlled Risk: Limits position holding time through time periods to avoid long-term position risks
- Good Adaptability: Automatically adjusts trading direction based on market conditions
- Simple Operation: Clear trading logic, easy to understand and execute
- High Capital Efficiency: Improves capital utilization through frequent rotation
- Adjustable Parameters: Key parameters can be flexibly adjusted according to actual needs
Strategy Risks
- Frequent trading may lead to high commission costs
- May generate frequent false signals in oscillating markets
- Fixed holding periods might miss important market opportunities
- Market trends not considered, may trade against primary trends
- Lack of stop-loss mechanism may cause significant losses in extreme market conditions
Strategy Optimization Directions
- Introduce volatility indicators (like ATR) to dynamically adjust holding periods
- Add trend judgment indicators to improve trading direction accuracy
- Include stop-loss and take-profit mechanisms to enhance risk control
- Optimize entry timing to avoid unfavorable trading periods
- Introduce money management system to optimize position sizing
- Add market sentiment indicators to improve trading accuracy
Summary
This strategy achieves market returns through time period control and long-short rotation, demonstrating strong flexibility and adaptability. While certain risks exist, the strategy's stability and profitability can be significantly improved through reasonable optimization and risk control measures. The core advantage lies in its simple yet effective trading logic, making it suitable as a foundation strategy for further optimization and expansion.
Source Pine Script
/*backtest
start: 2024-10-12 00:00:00
end: 2024-11-11 00:00:00
period: 4h
basePeriod: 4h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Tickerly Test Strategy", overlay=true)
// Inputs
longEnabled = input.bool(true, "Enable Long Trades")
shortEnabled = input.bool(true, "Enable Short Trades")
swingEnabled = input.bool(false, "Enable Swing Trading")
// Variables
var positionHeld = 0
var barsHeld = 0
var barsPaused = 0
var lastAction = "none"
// Function to determine if we're in the last 500 bars
inActivePeriod() =>
barIndex = bar_index
lastBarIndex = last_bar_index
barIndex >= (lastBarIndex - 499)
// Main strategy logic
if inActivePeriod()
if swingEnabled
if positionHeld == 0 and barstate.isconfirmed
if lastAction != "long"
strategy.entry("Long", strategy.long)
positionHeld := 1
barsHeld := 0
lastAction := "long"
else
strategy.entry("Short", strategy.short)
positionHeld := -1
barsHeld := 0
lastAction := "short"
if positionHeld != 0
barsHeld += 1
if barsHeld >= 2
if positionHeld == 1
strategy.entry("Short", strategy.short)
positionHeld := -1
barsHeld := 0
lastAction := "short"
else
strategy.entry("Long", strategy.long)
positionHeld := 1
barsHeld := 0
lastAction := "long"
else
if positionHeld == 0 and barsPaused >= 1 and barstate.isconfirmed
if longEnabled and shortEnabled
if lastAction != "long"
strategy.entry("Long", strategy.long)
positionHeld := 1
barsHeld := 0
barsPaused := 0
lastAction := "long"
else
strategy.entry("Short", strategy.short)
positionHeld := -1
barsHeld := 0
barsPaused := 0
lastAction := "short"
else if longEnabled
strategy.entry("Long", strategy.long)
positionHeld := 1
barsHeld := 0
barsPaused := 0
lastAction := "long"
else if shortEnabled
strategy.entry("Short", strategy.short)
positionHeld := -1
barsHeld := 0
barsPaused := 0
lastAction := "short"
if positionHeld != 0
barsHeld += 1
if barsHeld >= 3
strategy.close_all()
positionHeld := 0
barsHeld := 0
barsPaused := 0 // Reset pause counter when exiting a position
else
barsPaused += 1
// Plotting active period for visual confirmation
plot(inActivePeriod() ? 1 : 0, "Active Period", color=color.new(color.blue, 80), style=plot.style_areabr)Published by ChaoZhang on the FMZ strategy square · last modified 2024-11-12. 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 production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Intelligent-Time-Based-Long-Short-Rotation-Balanced-Trading-Strategy.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
- ATR Dynamic Profit Target and Stop Loss StrategyPine Script · Trend following · 1d
- ATR Dynamic Trend Following and EMA Crossover Trading StrategyPine Script · Trend following · 1d
- ATR and Moving Average Crossover Hybrid StrategyPine Script · Trend following · 1h
- ATR and T3 Moving Average StrategyPine Script · Trend following · 5m
- Adaptive ATR and RSI Trend Following Strategy with Trailing Stop LossPine Script · Trend following · 1h
- Adaptive Risk Management Strategy Based on Dynamic ATR and Moving Average CrossoverPine Script · Trend following · 2h