Dynamic Trade Duration and Position Sizing Strategy

14 Jun 2018
0 comment
0 attachment

This ProBuilder code snippet demonstrates a trading strategy that dynamically adjusts the duration a trade remains open based on its performance, and optionally manages position sizing for risk control. The strategy increases the time in the market after a profitable trade and resets it after a loss. It also includes an option for averaging down on losing positions.


AverageDown = 1 //0=Off 1=On Turn on or off averaging down
PositionSizing = 0 //0=Off 1=On Turn on or off position sizing
StartPositionSize = 1 //Starting Position Size
mind = 2 //Minimum candles on market
maxd = 6 //Maximum candles on market
LongConditions = (Your Long Conditions)
ShortConditions = (Your Short Conditions)

once d = mind //exit if in profit
if longonmarket and close > positionprice then
    sell at market
    d = min(d + 1,maxd)//extend time allowed in market after a win
endif

if shortonmarket and close < positionprice then
    exitshort at market
    d = min(d + 1,maxd)//extend time allowed in market after a win
endif

//exit if time is up
if longonmarket and barindex - startindex = d then
    sell at market
endif

if shortonmarket and barindex - startindex = d then
    exitshort at market
endif

//Reset time allowed on market to minimum after a loss
if strategyprofit < strategyprofit[1] then
    d = mind
endif

//Buy first long position
if not longonmarket and LongConditions then
    positionsize = StartPositionSize
    buy positionsize contract at market
    startindex = barindex
endif

//Buy extra long position if in a loss and conditions met again
if longonmarket and LongConditions and close < positionprice and averagedown then
    if positionsizing then
        positionsize = positionsize + (startpositionsize * d)
    endif
    buy positionsize contract at market
endif

//Buy first short position
if not shortonmarket and ShortConditions then
    positionsize = StartPositionSize
    sellshort positionsize contract at market
    startindex = barindex
endif

//Buy extra short position if in a loss and conditions met again
if shortonmarket and ShortConditions and close > positionprice and averagedown then
    if positionsizing then
        positionsize = positionsize + (startpositionsize * d)
    endif
    sellshort positionsize contract at market
endif

graph positionsize

This code snippet includes several key components:

  • Dynamic Duration Adjustment: The variable d represents the number of candles a trade can stay open. It starts at mind and increases up to maxd after each profitable trade, or resets to mind after a loss.
  • Profit and Loss Handling: Trades are closed either when they are in profit or when the maximum duration d is reached. If a trade is profitable, the duration for the next trade is increased. If the strategy’s overall profit decreases, the duration resets.
  • Averaging Down: If enabled, the strategy opens additional positions at a loss if the entry conditions are met again, potentially increasing the position size based on the current maximum duration d.
  • Position Sizing: This optional feature adjusts the size of new positions based on the duration variable d, which can increase the financial risk.

This strategy is designed for educational purposes to illustrate how trade management and risk exposure can be dynamically adjusted based on trading performance.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/adjusted-time-in-market-based-on-performance/#post-103621

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
Vonasi Master
V-oyaging ON A S-mall I-ncome
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...