Creating a date range/region with script

Optuma Forums Optuma Scripting Creating a date range/region with script

This topic contains 4 replies, has 2 voices, and was last updated by Avatar Dean 5 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #51939
    Avatar
    Dean
    • Topics: 13
    • Replies: 23
    • Posts: 36

    I’d like to able to define a continuous period of time (or a date region) using script only. The start and end of the date region are each based on their own specific conditions. All bars in between the start and end evaluate to true. Such a concept could be used to define a bear market for example. Skeleton pseudo code as follows:

    MovAvg = MA(BARS=9, CALC=Close);

    StartCondition = CLOSE(0) < MovAvg[0] and CLOSE(1) < MovAvg[1];

    EndCondition = CLOSE(0) > MovAvg[0] and CLOSE(1) > MovAvg[1];

    BearMarketStart = StartCondition;

    BearMarketEnd = SIGNALAFTER(BearMarketStart, EndCondition);

    isBarInBearMarket = IF(Bar is between BearMarketStart and BearMarketEnd, TRUE, FALSE);

    Has anyone done something similar? The above correctly defines the BearMarketStart and BearMarketEnd variables, but fails in that the isBarInBearMarket variable only evaluates to true for one bar after BearMarketStart even though there might be a dozen or more bars between BearMarketStart and BearMarketEnd.

    Many thanks in advance for any ideas or assistance.

    #51971
    Matthew
    Matthew
    • Topics: 4
    • Replies: 172
    • Posts: 176

    Hi Dean,

    Can you please provide a screen shot example of what it is you are wanting the criteria to find / highlight for reference?

    #51987
    Avatar
    Dean
    • Topics: 13
    • Replies: 23
    • Posts: 36

    Screenshot for reference

    Sure thing, thanks Matthew.

    Attachments:
    #52003
    Matthew
    Matthew
    • Topics: 4
    • Replies: 172
    • Posts: 176

    Hi Dean,

    Based on your screen shot the following script should provide the result you are after:

     

     

    The switch function will swap between 1 (True) and 0 (False) when the last 2 closes cross below a MA or above the MA.

    The shaded green zone shows where the script returns a true result…

    Ex4

    #52015
    Avatar
    Dean
    • Topics: 13
    • Replies: 23
    • Posts: 36

    Thank you!

    Wow, Matthew thank you so much. That was a nice concise solution you provided. Very smart indeed. The key function that was the ‘aha! moment’ for me was the SWITCH function. It is so useful. Thanks again, really appreciate your help.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

Pin It on Pinterest

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close