Using multiple MA's in Back Tester

Optuma Forums Optuma Scripting Using multiple MA's in Back Tester

This topic contains 8 replies, has 3 voices, and was last updated by Darren Darren 5 days, 18 hours ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #47987

    Brad
    • Topics: 7
    • Replies: 6
    • Posts: 13

    I’m trying to test a model based on change in slope of a short-term and mid-term MA’s as a buy condition with a sell based on a LT MA.  

    How do I script that both MA conditions have to be met in order to trigger a buy?  Individually, they have many more buys than they do combined.  Next, I only want the first buy trigger and then the first sell trigger to be used.  Is there a way to specify these conditions? 

    #47991
    Darren
    Darren
    • Topics: 28
    • Replies: 131
    • Posts: 159

    Hi Brad,

    What would be the entry condition? You could say when the 50MA is sloping up and and the 12EMA TurnsUp – in other words when the medium trend is up and the short term resumes up after a pullback.

    You can create the following boolean conditions which give a value or 1 when true and 0 when false. By adding the results together then the entry will be when the total is 2, or when both conditions are true:

    Add that formula to a Show Bar to verify that it’s doing what you want it to do. In this example the red lines show when the blue 50MA is trending up and the green 12EMA resumes upwards after a downturn:

    Capture

    With regards to the entries and exits in the backtester, if you set the criteria in the Test Rules to 100% of equity with a max position of 1 it will only take the first signal after each exit:

    Capture

    #47995

    Brad
    • Topics: 7
    • Replies: 6
    • Posts: 13

    Thanks Matthew, that’s sort of the idea. However, as I learned, you have to be more specific about what constitutes a change in slope.  So, I have one condition for the 20MA which looks like this

    m1 = MA(BARS=20, CALC=Close);

    h1 = LOWESTLOW(m1,BARS=10);

    c1 = (m1 – h1) / m1 * 100;

    c1 CrossesAbove .50 

    The script for the shorter term, 10MA, is basically the same.  So, how would I write the formula to identify those conditions both being met, as you described?

    #47999
    Matthew
    Matthew
    • Topics: 3
    • Replies: 69
    • Posts: 72

    Hi Brad,

    If you have two criteria you want to pass at the same time, you would use something similar to this script:

    #48019
    Darren
    Darren
    • Topics: 28
    • Replies: 131
    • Posts: 159

    Hi Brad,

    Just to follow up on the previous post, that will only trigger when both MAs cross 0.5 on the same day, which isn’t very likely. As such, to trigger when either one crosses 0.5 (and the other one is already above 0.5) change the last 3 lines to this:

     

    #48251

    Brad
    • Topics: 7
    • Replies: 6
    • Posts: 13

    Darren, Thank you for that last suggestion. That definitely helped.  Now, the last thing I’m trying to add combines a weekly close below one moving average, say 20MA, and a slope of another moving average, like 40MA, is downward.  How would I apply that in a similar fashion so that it doesn’t have to be a simultaneous occurrence?

    #48271
    Darren
    Darren
    • Topics: 28
    • Replies: 131
    • Posts: 159

    Hi Brad,

    It gets a bit complicated when using weekly data on a daily chart, but you could do something like this (assuming the MAs are also weekly):

    #48287

    Brad
    • Topics: 7
    • Replies: 6
    • Posts: 13

    Thanks Darren, but I don’t quite understand the last suggestion.  You suggested using (Res1 or Res2) but didn’t specificy what those conditions would be.

    Also, I have been testing the previous scripting conditions combination

    m1 = MA(BARS=10, CALC=Close);

    m2 = MA(BARS=20, CALC=Close);

    h1 = LOWESTLOW(m1,BARS=15);

    h2 = LOWESTLOW(m2,BARS=10);

    c1 = (m1 – h1) / m1 * 100; c2 = (m2 – h2) / m2 * 100; RES1= c1 > .75 and c2 CrossesAbove .50;

    RES2 = c2 > .50 and c1 CrossesAbove .75;

    RES1 or RES2

    What is odd to me is that I’m actually getting more signals with this than if I just used the singular condition which is one part of this expression 

    m1 = MA(BARS=20, CALC=Close);

    h1 = LOWESTLOW(m1,BARS=10);

    c1 = (m1 – h1) / m1 * 100;

    c1 CrossesAbove .50

    What I would expect is that the combination of 2 conditions would result in less, not more signals.

    #48295
    Darren
    Darren
    • Topics: 28
    • Replies: 131
    • Posts: 159

    Sorry Brad I thought your question was adding the condition to the previous script which included RES1 and RES2… if it’s a different one you could do something like this to trigger when the close crosses below the 20MA when the 40MA is sloping down:

    Note: if using this in a Show Bar on a weekly chart or a scan set to use the data timeframe of 1 week then you don’t need to specify the timeframe in the script.

    As for the issue with getting more results with the RES1 or RES2 rather then using just one, there are more chances of getting a trigger when using 2 scenarios rather than just one. Put the various formulas as separate Show Bar tools (change the colours to differentiate) and you will see why they trigger.

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

You must be logged in to reply to this topic.

Pin It on Pinterest

Share This

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