Implementing Dynamic Exit Strategies in Trading Algorithms

02 Aug 2018
0 comment
0 attachment

This code snippet demonstrates how to implement dynamic exit strategies in trading algorithms using the ProBuilder language. It includes two distinct strategies for exiting trades based on market conditions and price movements.

// support & resistence once supportresistance = 0 // exit [0-1-2] // timeframe(30 seconds)
if supportresistance then
    pivotbar = 20
    lookback = 20
    barlookback = pivotbar + 1
    if low[pivotbar] < lowest[lookback](low)[barlookback] then
        if low[pivotbar] = lowest[barlookback](low) then
            supportprice = low[pivotbar]
        endif
    endif
    if high[pivotbar] > highest[lookback](high)[barlookback] then
        if high[pivotbar] = highest[barlookback](high) then
            resistanceprice = high[pivotbar]
        endif
    endif
endif
timeframe(default)
if supportresistance=1 then
    if ((longonmarket and shortonmarket[1]) or (longonmarket and not onmarket[1])) then
        sup=supportprice
    endif
    if ((shortonmarket and longonmarket[1]) or (shortonmarket and not onmarket[1])) then
        res=resistanceprice
    endif
elsif supportresistance=2 then
    sup=supportprice
    res=resistanceprice
endif
if supportresistance=1 or supportresistance=2 then
    if longonmarket then
        sell at sup stop
    endif
    if shortonmarket then
        exitshort at res stop
    endif
endif
graphonprice sub
graphonprice res

// exitpivot
once exitpivot=2 // exit [0-1-2] //
if exitpivot=1 or exitpivot=2 then
    duration=360 // 720 = 1 hour lookback on 5s
    breakvalue=0
    if not onmarket then
        prell=0
        prehh=0
    endif
    if longonmarket then
        prehh=highest[max(1,(barindex-tradeindex))](high)
    endif
    if ((longonmarket and shortonmarket[1]) or (longonmarket and not onmarket[1])) then
        prell=lowest[duration](low)
    endif
    if shortonmarket then
        prell=lowest[max(1,(barindex-tradeindex))](low)
    endif
    if ((shortonmarket and longonmarket[1]) or (shortonmarket and not onmarket[1])) then
        prehh=highest[duration](high)
    endif
    if onmarket then
        fibpivot=(prehh-prell)*0.618
    else
        fibpivot=0
    endif
    if exitpivot=1 then
        if longonmarket then
            sell at (prehh-fibpivot)-breakvalue stop
        endif
        if shortonmarket then
            exitshort at (prell+fibpivot)+breakvalue stop
        endif
    endif
    if exitpivot=2 then
        if longonmarket then
            sell at prell-breakvalue stop
        endif
        if shortonmarket then
            exitshort at prehh+breakvalue stop
        endif
    endif
endif
graphonprice prehh
graphonprice prell
graphonprice prell+fibpivot coloured(255,0,0,255) as "short exit"
graphonprice prehh-fibpivot coloured(0,0,255,255) as "long exit"

The code is structured into two main parts, each defining a different exit strategy:

  • Support and Resistance Strategy: This part of the code calculates support and resistance levels based on historical price data. If the current price breaks these levels, the position is closed. This strategy is useful for markets that exhibit strong levels of support or resistance.
  • Fibonacci Pivot Strategy: This strategy uses Fibonacci retracement levels to determine exit points. It calculates the highest and lowest prices since the trade was opened and applies the Fibonacci ratio to these values to find potential reversal points. This is particularly useful in trending markets where retracements can indicate potential reversals.

Both strategies are designed to help manage risk by specifying conditions under which a trade should be exited, potentially protecting gains or minimizing losses.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/discussion-re-pure-renko-strategy/page/23/#post-136817

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.
paul New
As an architect of digital worlds, my own description remains a mystery. Think of me as an undeclared variable, existing somewhere in the code.
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...