Implementing Take-Profit Strategy Using Pivot Points and Fractals in Trading

10 Sep 2020
0 comment
0 attachment

This code snippet demonstrates how to implement a trading strategy that uses pivot points and fractals to determine buy and sell levels, and subsequently set take-profit levels based on these pivot points. The strategy does not include a stop-loss mechanism and is designed to be always in the market by taking every fractal breakout.


DEFPARAM Cumulateorders=false // Prevents accumulation of orders
Arrow = 0 // Indicator for fractal direction
Ncandles = 4 // Total number of candles in the fractal
Nside = (Ncandles) / 2 // Number of candles on each side of the extreme candle

// Definition of the upper fractal
IF high[Nside] >= highest[Ncandles](high) THEN
    Arrow = 1
    buylevel = high[Nside]
ENDIF

// Definition of the lower fractal
IF low[Nside] <= lowest[Ncandles](low) THEN
    Arrow = -1
    selllevel = low[Nside]
ENDIF

// Compute take-profit based on pivot points
Ht = DHigh(1)
Bs = DLow(1)
C = DClose(1)
Pivot = (Ht + Bs + C) / 3
Res1 = (2 * Pivot) - Bs
Res2 = Pivot + (Ht - Bs)
Res3 = Res1 + (Ht - Bs)
Sup1 = (2 * Pivot) - Ht
Sup2 = Pivot - (Ht - Bs)
Sup3 = Sup1 - (Ht - Bs)

// Determine the trading range based on pivot levels
if close > pivot then
    i=1
    while i<=3 do
        if i=1 then
            floor=pivot
            ceil=res1
        elsif i=2 then
            floor=res1
            ceil=res2
        elsif i=3 then
            floor=res2
            ceil=res3
        endif
        if close > floor and close < ceil then
            break
        endif
        i=i+1
    wend
elsif close < pivot then
    i=1
    while i<=3 do
        if i=1 then
            floor=sup1
            ceil=pivot
        elsif i=2 then
            floor=sup2
            ceil=sup1
        elsif i=3 then
            floor=sup3
            ceil=sup2
        endif
        if close > floor and close < ceil then
            break
        endif
        i=i+1
    wend
endif

// Execute trades based on fractal breakouts
IF Arrow = 1 and not longonmarket THEN
    BUY 1 CONTRACT AT buylevel stop
    sell at ceil limit
ENDIF
IF Arrow = -1 and not shortonmarket THEN
    SELLSHORT 1 CONTRACT AT selllevel stop
    exitshort at floor limit
ENDIF

if longonmarket then
    sell at ceil limit
endif
if shortonmarket then
    exitshort at floor limit
endif

// Visual representation of trading levels
graph floor coloured(0,200,200)
graph ceil coloured(200,0,0)

Explanation of the Code:

  • The code starts by setting up parameters for the trading strategy, including disabling order accumulation and defining the structure of fractals used for trading signals.
  • It then defines buy and sell levels based on the highest and lowest points within the fractal structure.
  • Pivot points are calculated using the high, low, and close prices of the previous day. These points are used to define potential resistance (Res) and support (Sup) levels.
  • The script checks where the current close price stands in relation to these pivot levels to determine the appropriate trading range.
  • Based on the direction indicated by the fractals (stored in the Arrow variable), the script either places a buy or sell order with a take-profit level set at the nearest resistance or support level.
  • Graphical lines are drawn at the floor and ceiling levels for visual representation of the current trading range.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/fractal-systeme/page/2/#post-88943

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.
Nicolas Legend
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
Author’s Profile

Comments

Search Snippets

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

Snippets Categories

global
35
indicator
135
strategy
171

Recent Snippets

A multi indicator Dashboard for ProRealTime
indicator
This snippet creates a compact on-chart dashboard that aggregates multiple indicator states into a single, readable [...]
How to Track Up/Down Cumulative Volume Inside Each Candle (Tick Volume Delta)
indicator
This snippet separates the real-time, intra-candle volume into up-volume and down-volume based on whether the last [...]
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 [...]
Logo Logo
Loading...