State machine vs expressions

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #86685 quote
    craigb
    Participant
    New

    Hi

    I am developing an indicator for the following entry setup:

     

    1. significant high = touch of the upper Bollinger (high >= BollingerUp)

    2. one one or more lower closes (than the close in 1)

    3. One or more higher closes (than the close in 2)

    This setup is invalid after X bars.  Where X is 5 but would like to test different values.

     

    I am struggling with 2 and 3 as it is a variable number of bars so I want to avoid hardcoding bar indexes…

     

    Any suggestions?

    #86687 quote
    robertogozzi
    Moderator
    Master

    You have to use variables following the five steps (see attached pic) while in ProBuilder:

    1. click on the spanner (upper left corner)
    2. assign a name to your variable (one each time, but you can add many), that name will appear automatically mext to the sparrow  after confirmation
    3. assign a type and default value to your variable
    4. close the window
    5. confirm your indicator by validating it

    you’ll then be able to use the indicator properties to change their value anytime.

    GraHal, Nicolas and craigb thanked this post
    x-4.jpg x-4.jpg
    #86693 quote
    GraHal
    Participant
    Master

    I learnt something entirely new there … using a function I never knew existed, thank you Roberto!

    Just hope I remember for when I need it! 🙂

    craigb thanked this post
    #86818 quote
    craigb
    Participant
    New

    Thank you Roberto for the quick and helpful response.

    To clarify, I’m looking for coding insights to detect candlestick patterns over a variable range of bars.

    As I see it – based on minor experience so far with this platform – there are two generalised ways to approach solving this:

    1. use in-built mathematical and statistical functions to express the conditions.  This leads to a common pattern of decomposing the condition into elements (commonly named c1, c2, c3…) and then testing the truth of the conditions combined using logical operators.
    2. write a state machine with lots of if/then and hand coded counters.

     

    In my view, the first approach is a lot more elegant but more importantly to me, leads to less bugs as you avoid manual handling of state.  I believe this approach is advocated in the manual for performance reasons (can’t find the reference now).  There is no “problem” per se with 2, but I do find it a lot more error prone when creating the code.

    So I’m pondering how to codify ‘one or more higher closes (than the prior bar)’ followed by ‘one or more lower closes (than the prior bar)’ in the spirit of approach 1 and looking for code ideas to point me in the right direction.  Perhaps a test of the closing price causing a TRUE value and then testing an array variable for consecutive TRUE values…but in a one-two liner :).

    I’m stuck so would really welcome ideas – however off the wall they may be.

    #86823 quote
    robertogozzi
    Moderator
    Master

    If you need to detect whether an ENGULFING (Bullish and Bearish) pattern can be detected (currently or previously) you can write:

    ONCE n           = 10                                        //lookback bars
    Bullish          = close > open
    Bearish          = close < open
    Body             = abs(close - open)
    BullishEngulfing = Bullish AND Bearish[1] AND Body > Body[1] //TRUE if Bullish engulfing just occurred
    BearishEngulfing = Bullish[1] AND Bearish AND Body > Body[1] //TRUE if Bearish engulfing just occurred
    PrevBullishEngulf= summation[n](BullishEngulfing)            //TRUE if Bullish engulfing occurred within the last "n" bars
    PrevBearishEngulf= summation[n](BearishEngulfing)            //TRUE if Bearish engulfing occurred within the last "n" bars

    is that something that might help you?

    craigb thanked this post
    #86832 quote
    craigb
    Participant
    New

    Thanks again Roberto.  This is the style of solution I am thinking of.  Converting your engulfing candle to my “higher/lower closes” is simple enough:

    ONCE n = 5 //lookback bars
    
    HigherClose = close > close[1]
    LowerClose = close < close[1]
    
    OneorMoreHigherCloses = summation[n](HigherClose)
    OneorMoreLowerCloses = summation[n](LowerClose)
    
    RETURN OneorMoreHigherCloses as "OneorMoreHigherCloses", OneorMoreLowerCloses as "OneorMoreLowerCloses"
    

    So I am trying to figure out how to go from “count how many times the condition appears in the previous X bars” to “tell me when the condition is true in a repeated sequence up to X times”….

    #86834 quote
    robertogozzi
    Moderator
    Master

    This

    OneorMoreHigherCloses = summation[n](HigherClose) = 2
    OneorMoreLowerCloses  = summation[n](LowerClose)  = 2

    will be true when, within the last “n” bars, the condition occurred twice.

    craigb thanked this post
    #86835 quote
    robertogozzi
    Moderator
    Master

    Sorry, this is true even when not consecutive.

    #86836 quote
    craigb
    Participant
    New

    We are now in the same ballpark 🙂

    #86837 quote
    robertogozzi
    Moderator
    Master
    OneorMoreHigherCloses = summation[n](HigherClose) = n

    this will be true when for ALL “n” bars the condition was true.

    To spot any 2 consecutive occurrences within n bars:

    n = 5
    c = 2
    OneorMoreHigherCloses = summation[c](HigherClose) = c
    x = summation[n](OneorMoreHigherCloses)
    craigb thanked this post
    #86841 quote
    robertogozzi
    Moderator
    Master

    Actually in my last example, 6 bars will be used, since if the condition is true on bar 5 it’s because it joins bar 6 to make the pair 6-5.

    To make sure you have the exact number of bars you should insert this line between 2 and 3:

    n = round((n / c) - 0.5)

    in this case n will return 4, which scans n+1 bars.

    craigb thanked this post
    #86842 quote
    craigb
    Participant
    New

    That’s a clever approach but I think we’ve moved away from the original requirement (my post 86832 confused things – sorry for that).

    Here’s the pattern description:

    1. significant high = touch of the upper Bollinger (high >= BollingerUp)

    2. one one or more lower closes (than the close in 1)

    3. One or more higher closes (than the close in 2)

    This setup is invalid after X bars.  Where X is 5 but would like to test different values.

    So counting or specifying a “number of bars requirement” for the “more closes” is not so relevant (but helpful for parameter experimentation in backtesting).

    I imagined something like this:

    // pseudo code
    
    ONCE SigIndex = 0
    SetupMaxBars = 5
    
    SH = high >= BollingerUp[20](close) AND high >= highest[10](high)
    SL = low <= BollingerDown[20](close) AND low <= Lowest[10](low)
    
    if (SH OR SL) AND SigIndex = 0 then
        SigIndex = BarIndex
    endif
    
    OneorMoreHigherCloses = ...
    OneorMoreLowerCloses = ...
    
    // test the combined conditions 
    if SH AND OneorMoreLowerCloses AND OneorMoreHigherCloses AND (BarIndex - SigIndex) =< SetupMaxBars then
        SellSignal = 1
        SigIndex = SH = SL = 0
    fi
    
    if SL AND OneorMoreHigherCloses AND OneorMoreLowerCloses AND (BarIndex - SigIndex) =< SetupMaxBars then
        BuySignal = 1
        SigIndex = SH = SL = 0
    fi
    

    Does this make sense?

    #86845 quote
    robertogozzi
    Moderator
    Master
    OneorMoreHigherCloses = summation[SetupMaxBars](SH)
    OneorMoreLowerCloses  = summation[SetupMaxBars](SL)
Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.

State machine vs expressions


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
craigb @craigb Participant
Summary

This topic contains 12 replies,
has 3 voices, and was last updated by robertogozzi
7 years, 3 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 12/10/2018
Status: Active
Attachments: 1 files
Logo Logo
Loading...