Problem with array code trying to find best MA period.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #131044 quote
    zilliq
    Participant
    Master

    Hi Guys,

    I try to determine what is the best period for average crossing =Bigger pike after a crossing, but I don’t know why there is a problem with my array code

    defparam CALCULATEONLASTBARS=1000
    
    once reference=close
    
    for a=1 to 10
    
    EMA1=exponentialaverage[a](close)
    
    EMA2=exponentialaverage[2*a](close)
    
    
    if (EMA1 crosses over EMA2)   then
    reference=close
    endif
    
    if EMA1>EMA2 then
    
    ecart1=(close-reference)/pipsize
    
    $ecart1[lastset($ecart1)+1]=ecart1
    else
    ecart1=0
    
    endif
    
    maxi=arraymax($ecart1)
    
    if ecart1=maxi then
    periodedelamoyenne=a
    endif
    
    next
    
    return ecart1 as "Ecart ",maxi,periodedelamoyenne

    In fact it don’t determine the best period but only the best period for 10 (in this code)

    Does someone have an idea of my mistake ?

    Thanks guys

    #131057 quote
    Vonasi
    Moderator
    Master

    I’ve edited your topic title – please try to use titles that are not something like ‘Coding help needed’ otherwise we get a forum full of them.

    The problem is that you are checking for the price difference and the cross in the same loop for all averages.

    I would code it like this with a separate array location for each MA. It will still eventually end up going for the highest number because that is the slowest moving average and so takes longer to catch up with price so the gap is bigger. Perhaps you should reset the ‘biggest’ variable at each bar and then check back over the last x bars in a loop to find the best MA.

    defparam CALCULATEONLASTBARS=1000
    
    for a = 1 to 20
    if exponentialaverage[a](close) crosses over exponentialaverage[a*2](close) then
    $ema[a] = close
    endif
    next
    
    for a = 1 to 20
    if $ema[a] <> 0 then
    $difference[a] = close - $ema[a]
    biggest = max(biggest,$difference[a])
    if biggest = $difference[a] then
    bestma = a
    endif
    endif
    next
    
    return bestma
    #131090 quote
    zilliq
    Participant
    Master

    Thanks @vonasi 😉

    I solve the problem in a different way (code to find the “best” period)

    defparam CALCULATEONLASTBARS=1000
    
    for a=10 to 20
    
    ema1=average[a](close)
    wma1=average[2*a](close)
    
    ecart1=(ema1-wma1)/pipsize
    
    $ecart1[lastset($ecart1)+1]=ecart1
    
    $top[lastset($top)+1]=arraymax($ecart1)
    
    if $top[lastset($top)]=ecart1 then
    periode=a
    // Période où la valeur est la plus grande
    endif
    
    
    next
    
    return ecart1 as "Ecart ",arraymax($top) as "Maximum",periode as "Période"

    Thanks and have a nice day

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

Problem with array code trying to find best MA period.


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
zilliq @zilliq Participant
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by zilliq
5 years, 9 months ago.

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