Please help with Multiple MA Crossing Loop

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #183643 quote
    KhaledKhaled
    Participant
    Veteran

    Hello,

    I’d like to have a Signal whenever one of three MAs Fast crosses Slow. The code below gives Signal=1 on every single bar.

    Can you please help spot where the error is?

    Thank you!
    PeriodMASlow = 21
    PeriodMAFast = 8
    
    Signal=0
    MAType=0
    
    for MAType= 1 to 3 do
    
    if MAType = 1 then
    MASlow1  = Average[PeriodMASlow](WeightedClose)
    MAFast1  = Average[PeriodMAFast](close)
    elsif MAType = 2 then
    MASlow2  = ExponentialAverage[PeriodMASlow](WeightedClose)
    MAFast2  = ExponentialAverage[PeriodMAFast](close)
    elsif MAType = 3 then
    MASlow3 = EndPointAverage[PeriodMASlow](WeightedClose)
    MAFast3 = EndPointAverage[PeriodMAFast](close)
    endif
    
    if MAType=1 then
    cond1 = (MAFast1 crosses over MASlow1)
    
    a1= 0
     
    for i1= 0 to 10 do
    if cond1[i1] then
    a1=1
    break
    else
    a1=0
    endif
    next
    endif
    
    if MAType=2 then
    cond2 = (MAFast2 crosses over MASlow2)
    
    a2= 0
     
    for i2= 0 to 10 do
    if cond2[i2] then
    a2=1
    break
    else
    a2=0
    endif
    next
    endif
    
    if MAType=3 then
    cond3 = (MAFast3 crosses over MASlow3)
    
    a3= 0
     
    for i3= 0 to 10 do
    if cond3[i3] then
    a3=1
    break
    else
    a3=0
    endif
    next
    endif
    
    if a1=1 or a2=1 or a3=1 then
    signal=+1
    break
    else
    signal=0
    endif
    next
    
    return signal as "Signal MA Cross"
    #183666 quote
    robertogozzirobertogozzi
    Moderator
    Legend
    There you go:
    PeriodMASlow = 21
    PeriodMAFast = 8
     
    Signal=0
    MAType=0
    x     =0
    
    for MAType= 1 to 3 do
    
       a1=0
       a2=0
       a3=0
    
       if MAType = 1 then
          MASlow1  = Average[PeriodMASlow](WeightedClose)
          MAFast1  = Average[PeriodMAFast](close)
       elsif MAType = 2 then
          MASlow2  = ExponentialAverage[PeriodMASlow](WeightedClose)
          MAFast2  = ExponentialAverage[PeriodMAFast](close)
       elsif MAType = 3 then
          MASlow3 = EndPointAverage[PeriodMASlow](WeightedClose)
          MAFast3 = EndPointAverage[PeriodMAFast](close)
       endif
     
       if MAType=1 then
          cond1 = (MAFast1 crosses over MASlow1)
          a1= 0
          for i1= 0 to 10 do
             if cond1[i1] then
                a1=1
                break
             else
                a1=0
             endif
          next
       endif
     
       if MAType=2 then
          cond2 = (MAFast2 crosses over MASlow2)
          a2= 0
          for i2= 0 to 10 do
             if cond2[i2] then
                a2=1
                break
             else
                a2=0
             endif
          next
       endif
    
       if MAType=3 then
          cond3 = (MAFast3 crosses over MASlow3)
          a3= 0
          for i3= 0 to 10 do
             if cond3[i3] then
                a3=1
                break
             else
                a3=0
             endif
          next
       endif
    
       x = 0
       if a1=1 or a2=1 or a3=1 then
          signal=+1
          x     = signal
          IF signal = signal[1] THEN
             x = 0
          endif
          break
       else
          x     =0
          signal=0
       endif
    next
    
    return x as "Signal MA Cross"
    I added lines 6, 10-12, 64, 67-70, 73, then modified line 78 (the last one).
    Khaled thanked this post
    #183673 quote
    KhaledKhaled
    Participant
    Veteran
    Thanks a million times Roberto! it works exactly as I wanted. Have a nice week and happy holidays season.
    robertogozzi thanked this post
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Please help with Multiple MA Crossing Loop


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Khaled @khaled Participant
Summary

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

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 12/20/2021
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...