Trend Break System

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #72643 quote
    GraHal
    Participant
    Master

    Anybody willing to code / help me code / share (what is nearest example on here?) a System which would enter and exit trades at Trend Breaks determined by failures to exceed previous high, Lows / Failure to get back in trend??

    Is this even possible on PRT??

    Many Thanks for any Help
    GraHal

    Trend-Break-System-1.jpg Trend-Break-System-1.jpg Trend-Break-System-2-1.jpg Trend-Break-System-2-1.jpg
    #72653 quote
    Eric
    Participant
    Master

    Maybe you can use fractals?

    if last fractal > than previous fractal

    and sell if under last fractal

    https://www.prorealcode.com/prorealtime-indicators/fractal-williams-indicator/

    GraHal thanked this post
    #72657 quote
    Vonasi
    Moderator
    Master

    I have removed the images you did not want for you and removed the part of your post referring to them so as not to confuse future readers.  🙂

    Are you sure you listed the correct ones as now images 2 and 3 look identical!?

    GraHal thanked this post
    #72659 quote
    GraHal
    Participant
    Master

    Yes I agree image 3 needs deleting now also, thank you so much Vonasi … it’s good to have a mate on the inside! 🙂
    And who clearly shares a similar sense of humour! 😀

    #72661 quote
    Vonasi
    Moderator
    Master

    Image 3 now deleted.

    Not so sure about the being an insider thing. Being a moderator basically means that Nicolas gives you a broom and asks you to try to keep the place a bit tidy if you happen to be hanging around the place! Moderators are just the same as all the other forum users except they have some basic cleaning equipment.

    GraHal and Nicolas thanked this post
    #72664 quote
    Nicolas
    Keymaster
    Master

    Basic idea:

    You can use a simple moving average and a test if the price has not breached the last highest high or lowest low within the last X periods.

    Example for a new buy order:

    1/ a new lowest low was set at bar X while price was under a 20 periods moving average.

    2/ close is now above the SMA and the current bar is X+Y periods

    In preliminary, you would have count if the last bearish trend was well established by counting how many times the SMA20 has decreased in the same row.

    GraHal thanked this post
    #72830 quote
    juanj
    Participant
    Master

    Something I worked on at some stage:

    Calculating Diagonal Trend Lines Using Fractals and Trigonometry

    Code I am using:

    //Stategy: Trend Breakout
    //Author: JuanJ
    //Market: Neutral (Optimized for EURUSD)
    //Timeframe: 1Hr
    //Timezone: UTC +2
    //Timeframe: 1Hr but not 'completely' timeframe dependant
    
    Defparam cumulateorders = False
    
    If hour < 9 or hour > 22 then //outside market hours
    possize = 0
    If longonmarket Then
    sell at market
    Bear = 0
    Bull = 0
    ElsIf shortonmarket Then
    exitshort at market
    Bear = 0
    Bull = 0
    EndIf
    Else
    possize = 1 //position contract size
    EndIf
    
    //Calculate Fractal
    
    once FracH = 0 //initialize fractal count
    once FracL = 0 //initialize fractal count
    
    For i = 0 to 4 Do
    If high[2] > high[i] Then //look for high fractal with 2 lower highs on either side
    FracH = FracH + 1
    EndIf
    If low[2] < low[i] Then //look for low fractal with 2 higher lows on either side
    FracL = FracL + 1
    EndIf
    Next
    
    If FracH = 4 Then //High Fractal Identified
    If Bear = 0 and Bull = 0 Then
    Bear = 1 //Initialize first trend direction
    EndIf
    FractalH = high[2] //High Fractal Price Level
    FractalP = barindex - 2 //High Fractal Bar Position
    EndIf
    If FracL = 4 Then //Low Fractal Identified
    If Bear = 0 and Bull = 0 Then
    Bull = 1 //Initialize first trend direction
    EndIf
    FractalL = low[2] //Low Fractal Price Level
    FractalP = barindex - 2 //Low Fractal Bar Position
    EndIf
    
    FracH = 0 //reset fractal count
    FracL = 0 //reset fractal count
    
    //Calculate trendline using widest angle from extreme of last fractal to curent extreme (Trigonometry Function)
    
    If Bear = 1 Then //Down trend
    Height = FractalH - high //Calcululate height between high fractal and high of current bar
    HeightB = FractalH - close //Calcululate height between high fractal and close of current bar (used to determine trend violation)
    ElsIf Bull = 1 Then //Up trend
    Height = Low - FractalL //Calcululate height between low fractal and low of current bar
    HeightB = close - FractalL //Calcululate height between high fractal and close of current bar (used to determine trend violation)
    EndIf
    
    If barindex - 2 = FractalP Then //Initialize angle using high of last bar of fractal set
    MaxAngle = Tan(2/Height) //Initial value of the angle between the fractal and the last high
    EndIf
    
    once Trendbreak = 0
    
    If barindex - 2 > FractalP Then //If current bar greater than end of fractal set:
    If Bear = 1 Then //For Down Trend
    If (Tan((barindex-FractalP)/Height) > MaxAngle) and (((close < open) or (close < close[1]))) Then
    MaxAngle = Tan((barindex-FractalP)/Height) //calculate new max trend if down trend rules are valid
    Trendbreak = 0 //No Trend Violation Present
    EndIf
    If Trendbreak = 0 and (Tan((barindex-FractalP)/HeightB) > MaxAngle) and close > open Then
    TrendBreak = 1 //Trend violation potentially present, wait for confirmation on close of next bar
    ElsIf Trendbreak = 1 and (Tan(((barindex-1)-FractalP)/HeightB) > MaxAngle) and close > close[1] Then
    Trendbreak = 2 //Trend violation confirmed, there was a close outside the trend line (close angle > trend)
    EndIf
    ElsIf Bull = 1 Then //For Up Trend
    If (Tan((barindex-FractalP)/Height) > MaxAngle) and (((close > open) or (close > close[1]))) Then
    MaxAngle = Tan((barindex-FractalP)/Height) //calculate new max trend if up trend rules are valid
    Trendbreak = 0 //No Trend Violation Present
    EndIf
    If Trendbreak = 0 and (Tan((barindex-FractalP)/HeightB) > MaxAngle) and close < open Then
    TrendBreak = 1 //Trend violation potentially present, wait for confirmation on close of next bar
    ElsIf Trendbreak = 1 and (Tan(((barindex-1)-FractalP)/HeightB) > MaxAngle) and close < close[1] Then
    Trendbreak = 2 //Trend violation confirmed, there was a close outside the trend line (close angle < trend)
    EndIf
    EndIf
    EndIf
    
    If Trendbreak = 2 Then //Trend violation is confirmed:
    If Bear = 1 Then //Down Trend Present (but now broken):
    BreakoutL = Highest[2](high)[0] //Determine Highest point of violation (including last candle before violation)
    Buy possize contract at BreakoutL + (AverageTrueRange[7](close)*0.3) stop //Enter into long position at specified price level
    //N.B. We need to ensure the current position is obviously closed?
    ElsIf Bull = 1 Then //Up Trend Present (but now broken):
    BreakoutL = Lowest[2](low)[0] //Determine Lowest point of violation (including last candle before violation)
    Sellshort possize contract at BreakoutL - (AverageTrueRange[7](close)*0.3) stop //Enter into short position at specified price level
    //N.B. We need to ensure the current position is obviously closed?
    EndIf
    
    If Bear = 1 and longonmarket Then //If position is opened counter last trend, reset variables
    Trendbreak = 0
    Bear = 0
    Bull = 1
    ElsIf Bull = 1 and shortonmarket Then //If position is opened counter last trend, reset variables
    Trendbreak = 0
    Bear = 1
    Bull = 0
    EndIf
    EndIf
    
    //Trade Management (Note that Trend Direction need to be reset with exits's)
    
    Deviations = 1.618
    periods = 34
    
    PRICE  = LOG(customclose)
    alpha  = 2/(PERIODS+1)
    
    if barindex < PERIODS then
    EWMA = AVERAGE[3](PRICE)
    else
    EWMA = alpha * PRICE + (1-alpha)*EWMA
    endif
    
    error = PRICE - EWMA
    dev   = SQUARE(error)
    if barindex < PERIODS+1 then
    var  = dev
    else
    var   = alpha * dev + (1-alpha) * var
    endif
    ESD   = SQRT(var)
    
    BollU = EXP(EWMA + (DEVIATIONS*ESD))
    BollL = EXP(EWMA - (DEVIATIONS*ESD))
    
    RS2 = ROUND(RSI[2](close))
    
    PSH = Highest[100](high)[1]
    PSL = Lowest[100](low)[1]
    
    If longonmarket and ((close[1] > BollU and close < BollU) or (high[1] > BollU and high < BollU) or (close > PSH or close < PSL)) Then
    LE = 1
    ElsIf shortonmarket and ((close[1] < BollL and close > BollL) or (low[1] < BollL and low > BollL) or (close < PSL or close > PSH)) Then
    SE = 1
    EndIf
    
    If longonmarket and LE = 1 and RS2 >= 85 and close < BollU Then
    Sell at market
    Bear = 0
    Bull = 0
    ElsIf shortonmarket and SE = 1 and RS2 <= 15 and close > BollL Then
    Exitshort at market
    Bear = 0
    Bull = 0
    EndIf
    
    #72842 quote
    GraHal
    Participant
    Master

    Hey great @juanj thanks, I will check this out later.  I do recall now seeing this a while back and thinking it was very clever.

    Not being a career coder and only starting coding on here about 2 years ago I’ll never emulate such ingenuity and innovation.

    A big thank you!
    GraHal

    PS to any readers:
    I am always glad of any help from anybody with trading stragegies / ideas I put forward, even if you can produce only a few lines of the code.
    I have more ideas, but tend not to post them as it seems that with me being active here for so long then folks may think I am a capable coder and so not offer actual code?
    Not complaining, just opening up a bit!? 🙂

    #72844 quote
    Nicolas
    Keymaster
    Master

    Ideas are always welcome, GraHal. Even if they are not coded immediately, it is a very good starting point for further interesting discussions.

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.

Trend Break System


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 8 replies,
has 5 voices, and was last updated by Nicolas
7 years, 8 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 06/08/2018
Status: Active
Attachments: 2 files
Logo Logo
Loading...