How to code no crossover/crossunder on the moving average for the past 20 bars

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #147634 quote
    jebus89
    Participant
    Master

    Hello all and sorry for such a beginners question i guess, but ive forgot how to do this and i cant easily find an answer by searching words for this so im just going to ask:

    How can i code: If there has been no crossover/crossunder on the moving average, for the past 20 bars, then K = 0

     

    x = moving average 20

    I could say:  Close > x and close[1] > x[1] ……. close[20] > x[20]

     

    and then reverse the < sign, but i know theres an easier way to write this piece of the code.

     

    Big thx for any help.

    #147635 quote
    Luciole
    Participant
    Senior

    Hello,

     

    you are talking about “no cross” moving average I guess.

     

    You can find a code snipet here :

    Codage non croisement de moyennes mobiles

    jebus89 thanked this post
    #147636 quote
    jebus89
    Participant
    Master

    Hello,

    you are talking about “no cross” moving average I guess.

    You can find a code snipet here :

    https://www.prorealcode.com/topic/codage-non-croisement-d-mm/

    Thanks, i think its the “summation” part i was looking for 🙂 Looks like my code is working now, here it is for anyone wondering:

    i have e1 and e2 = 20 at the moment, but you can try whatever numbers you want.

    Im going to use this as a filter for when “price is choppy”. As soon as there has been e2 periods (20 at the moment) with no crossover/crossunder, then K=0.

     

    x =exponentialaverage[e1](close)
    
    once k = 0
    
    if close crosses over x or close crosses under x then
    k = k+1
    endif
    
    if summation[e2](close > x) then
     k = 0
    endif
    
    if summation[e2](close < x) then
    k = 0
    endif
    
    return k
    #147744 quote
    Vonasi
    Moderator
    Master

    You must know the rules by now jebus89! Topics must have meaningful titles – I’ve changed yours from ‘Simple question’!

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

How to code no crossover/crossunder on the moving average for the past 20 bars


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
jebus89 @jebus89 Participant
Summary

This topic contains 3 replies,
has 3 voices, and was last updated by Vonasi
5 years, 5 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 10/17/2020
Status: Active
Attachments: No files
Logo Logo
Loading...