Creating a date range/region with script

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

  • This topic has 4 replies, 2 voices, and was last updated 2 years ago by Dean.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #51939
    Dean
    • Topics: 13
    • Replies: 25
    • Posts: 38

    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
    • Topics: 5
    • Replies: 481
    • Posts: 486

    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
    Dean
    • Topics: 13
    • Replies: 25
    • Posts: 38

    Sure thing, thanks Matthew.

    #52003
    Matthew
    • Topics: 5
    • Replies: 481
    • Posts: 486

    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

    • This reply was modified 2 years ago by Matthew.
    1 user thanked author for this post.
    #52015
    Dean
    • Topics: 13
    • Replies: 25
    • Posts: 38

    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