currentprice_MA crossover

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #84457 quote
    rama
    Participant
    Senior
    // Conditions to enter long positions
    indicator1 = Average[50](close)
    if (close crosses over  indicator1) then
    c2=0
    endif
    
    
    if (close crosses under   indicator1) then
    c2=0
    endif
    
    if close>=indicator1 then
    c3= close-indicator1+c2
    endif
    
    
    if close<= indicator1 then
    c3= -close+indicator1+c2
    endif
    
    
    return c3
    
    50_ma.jpg 50_ma.jpg
    #84459 quote
    rama
    Participant
    Senior

    I have written the code, it give some results but not expect

    I expect the max value should be 420

    where as I get 250 which not correct

    max.jpg max.jpg
    #84465 quote
    robertogozzi
    Moderator
    Master

    Lines 3 through 10 are useless, since you never assign C2 any value.

    C3 is the distance between CLOSE and the indicator for that single bar.

    If you want the distance from CLOSE and the crossover then you’ll have to save the value of the indicator, or its BarIndex, when the crossover occurs.

    #84468 quote
    rama
    Participant
    Senior

    I have corrected the code

     

    it works fine

    // Conditions to enter long positions
    indicator1 = Average[50](close)
    if (close crosses over indicator1) then
    c2=indicator1
    endif
    if close>Average[50](close) then
    c3=close-c2[1]
    endif
    
    return c3
    #84472 quote
    robertogozzi
    Moderator
    Master

    Line 7 should reference c2 (not c2[1]), because when there is a crossover it would return the value of the previous crossover. From the candle following the crossover on it wouldn’t make any difference (despite it is logically wrong).

    #126468 quote
    rama
    Participant
    Senior

    If I want to add number of bars in the same code what should i do. if I want to trade contrarian say if the value c3>=500 after x number of bars only. I dont want sell in very sharp trend, I want to keep some minimum number of bars

    #126481 quote
    robertogozzi
    Moderator
    Master

    There you go:

    // Conditions to enter long positions
    indicator1 = Average[50](close)
    if (close crosses over indicator1) then
       c2=indicator1
       MyBar = barindex
    endif
    if close>Average[50](close) then
       c3=close-c2
    endif
    if c3 > 500*pipsize And (barindex - MyBar) > 5 then
       MyBar = barindex
       c2=indicator1
    Else
       c3 = 0
    endif
    return c3
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.

currentprice_MA crossover


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
rama @rama Participant
Summary

This topic contains 6 replies,
has 2 voices, and was last updated by robertogozzi
5 years, 10 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 11/09/2018
Status: Active
Attachments: 2 files
Logo Logo
Loading...