Counter isn’t working

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #214226 quote
    Andres
    Participant
    New

    Hello,

    I’m working on a code that should only buy when the condition occurs for the second or more times. Unfortunately, I’m facing an issue where the counter doesn’t seem to increase and remains at 1, causing the “IF counter>x” condition to always return the same result, regardless of x.

    // Reset for each day
    if Date[1] <> Date[0] then
    counter = 0
    endif
    
    Divg = DivergenceMACD[12,26,9,20](close)
    
    //counter
    IF Divg>0 THEN
    counter=counter+1
    ENDIF
    
    // Conditions to enter long positions
    IF counter>3 and Divg>0 THEN
    BUY 1 SHARES AT MARKET
    ENDIF
    #214227 quote
    fifi743
    Participant
    Master

    et comme ceci

     

     

    // Reset for each day
    if Date[1] <> Date[0] then
    counter = 0
    endif
     Divg=0
    Divg = DivergenceMACD[12,26,9,20](close)
     
    //counter
    IF Divg>0 THEN
    counter=counter+1
    ENDIF
     
    // Conditions to enter long positions
    IF counter>3 and Divg>0 THEN
    BUY 1 SHARES AT MARKET
    ENDIF
    #214232 quote
    PeterSt
    Participant
    Master

    Hi Andres,

    Start out with this :

    Once counter = 0   // counter must have an initial value.
    if Date[1] <> Date[0] then
      counter = 0
    endif

    If you don’t do this, counter will have an undefined value which probably is a huge negative number. So it will add up, but only when a new day has started, it will reset to 0 for the first time.
    Maybe what fifi added helps too ( I don’t readily see why he does it) and possibly it is so that both amendments are required to get it going.

    You can let your code end with

    graph counter
    graph Divg

    Then you can see what values these variables receive.

    #214239 quote
    Andres
    Participant
    New

    Thank you very much fifi743 and PeterSt for your answers. Thanks PeterSt specially for the command “graph” I didn’t know it.

    Actually the code was working I could see it with “graph counter”. The problem is that it jumps from 0 to 4, or to 0 to 20, etc. So I’m going to adjust it to start from 4, or something else.

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

Counter isn’t working


General trading discussions

New Reply
Author
author-avatar
Andres @paltrow Participant
Summary

This topic contains 3 replies,
has 3 voices, and was last updated by Andres
2 years, 8 months ago.

Topic Details
Forum: General trading discussions
Language: English
Started: 05/06/2023
Status: Active
Attachments: 1 files
Logo Logo
Loading...