Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indexing a Bar Count on Entry (Fixing Repainting Entries)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Indexing a Bar Count on Entry (Fixing Repainting Entries)

    Hello
    I'm not sure if this is even possible??
    I'm using a Variable0 for an entry condition and switching it from (Variable0 == 1) when true to (Variable0 == 0) when its not true.
    Although here comes the kicker, I want to index or declare that this event happened one bar ago,
    So that I can turn off the entry condition (true) on the Close[1] of the next bar after the condition becomes true.

    And/Or I want to limit the entry condition somehow to only one bar, like this:

    if (Variable0 == 1) [ previousBar ] // This line will throw an error code though
    {
    (Variable0 == 0)
    }

    Until another event happens inbetween like a downtrend indicator, maybe something like this:

    if (Variable1 == 1) || (Variable2 == 1)
    {
    }

    Please help!
    I need this strategy to be more verbose!!

    #2
    Hello FBraun,

    Thank you for your post.

    I'm not clear on what you're asking, are you simply looking to count 1 bar since an event occurred?

    If so you can simply save CurrentBar to a variable, then in another condition check if CurrentBar - your saved value is equal to 1.

    Comment


      #3
      Hi Gaby
      Yes exactly!
      I just want to start a bar count at the close of the bar when (Variable0 == 1)
      That way i can turn the variable off until another event.

      Can you elaborate on the code needed for your example? (Saving CurrentBar to a variable)

      I have tried CurrentBar but it says a bool and int cannot be combined or something. IDK what I'm doing wrong.
      I have even tried adding a "private string" to the "public class" :strategy just to count bars but that didn't work either.

      The line above the picture is basically:

      if ( (Variable0 == 1) && (Close[0] > Close[1]) )
      Click image for larger version  Name:	Screenshot (177).png Views:	0 Size:	17.4 KB ID:	1320247
      Edit: Added a picture to elaborate
      I keep getting this "Only assignment, call, etc, etc" error too.
      Last edited by FBraun; 10-02-2024, 11:25 AM.

      Comment


        #4
        Hello,

        You can declare your int variable on the class level.

        HTML Code:
        //at the class level
        int mySavedBar;
        
        //in OnBarUpdate
        if (conditions for event)
        mySavedBar = CurrentBar;

        Comment


          #5
          Thank You for your timely response Gaby,


          I am now trying to call mySavedBar like this:

          if (mySavedBar) // I want to say one bar ago here but it gives an indexing error
          {
          Variable2 = 0; // Turns off the variable signal from one bar ago
          }


          if ( mySavedBar > 2 ) // I also tried this as greater than 2 but that it doesn't give any signal at all
          {
          Variable2 = 0; // Turns off the variable signal from one bar ago
          }


          if ( mySavedBar[2] ) // I also tried this as an index but then I get error code CS0021 - cannot apply indexing to type 'int'
          {
          Variable2 = 0; // Turns off the variable signal from one bar ago
          }​

          ​These three don't seem to work. Hopefully you can help me with this logic.


          Before the above logic I typed:

          if ( Variable0 == 1 ) && ( Close[0] > Close[1] )
          {
          Variable2 = 1;
          mySavedBar = CurrentBar;
          }

          Thanks in advance
          Last edited by FBraun; 10-02-2024, 03:53 PM.

          Comment


            #6
            Hello,

            If you are trying to check if one bar has passed since your event, you would need to check if CurrentBar - yourSavedBar = 1.

            mySavedBar is an int variable, so you can't apply indexing to it. You can only apply indexing to series, arrays, lists etc.

            Comment


              #7
              Hi,
              This logic does work to turn off the signal while the trade is running but as soon as profit target is hit then the strategy re-paints the entry logic and it re-enters again (usually for a loss).
              This is the code I used.

              if (CurrentBar - mySavedBar2 == 1)
              {
              Variable2 = 0;
              }​

              I wish there was a way to get CS to understand 'until' another event happens. Like Variable2 = 0; ( Until-> Variable0 = 1; )
              Any ideas are much appreciated. Maybe I need to figure out how to index this code IDK.

              Comment


                #8
                Hello FBraun,

                Code:
                if (CurrentBar - mySavedBar2 == 1)
                {
                Variable2 = 0;
                }​


                This condition is just checking if 1 bar has elapsed since your event.

                I wish there was a way to get CS to understand 'until' another event happens. Like Variable2 = 0; ( Until-> Variable0 = 1; )
                I'm not exactly understanding what you mean by "Variable2 = 0; ( Until-> Variable0 = 1". Can you please explain in more detail?

                Comment


                  #9
                  Hello again,
                  This is what I'm trying to avoid (Blue drawn arrows)
                  The purple background is a viable trade but it just keeps re-entering until there's a loss.
                  Click image for larger version  Name:	Screenshot (178).png Views:	0 Size:	42.7 KB ID:	1320429

                  This is what I meant in my previous post.
                  Is there anyway to turn off Variable0 (by giving it a value of 0) until maybe Current Close[0] is less than Close[1] one bar ago (A short candle) ?

                  Code:
                  if ( Close[0] < Close[1] )



                  What's happening is that with OnBarUpdate when the bar closes, the Variable turns back on because it's logic has been met in order for it to turn on again when the bar updates. Which then enters a trade.

                  The mySavedBar logic turns off the variable logic while in a trade (the red bars) but when profit target is reached and the next bar after that is updated, the Variable turns back on; which then enters into another trade. The Variable logic is re-painting.

                  For example lets say the Variable logic is like this:

                  Code:
                  if ( Close[0] > Close[1] && (Variable0 == 1) )
                  {
                  mySavedBar = CurrentBar;​
                  EnterLong;
                  }

                  if (CurrentBar - mySavedBar == 1)
                  {
                  Variable0 = 0;
                  }​​

                  What happens with this logic is once profit target is reached and trade is exited then the next bar (if it's still long) is re-painted as a valid entry.
                  I would be super happy with any solution suggested tbh, it doesn't have to be a variable. Or even the same code honestly, just some way to turn off re-paint indicators used in a strategy.

                  I really thought it would work but IDK, back to the drawing board.
                  Last edited by FBraun; 10-03-2024, 06:53 PM. Reason: Added photo to illustrate my frustration lol

                  Comment


                    #10
                    Hello,

                    Is there anyway to turn off Variable0 (by giving it a value of 0) until maybe Current Close[0] is less than Close[1] one bar ago (A short candle) ?

                    Code:
                    if ( Close[0] < Close[1] )​
                    You would need to explicitly code this logic into the script. You would simply not assign this variable a value until that condition becomes true.

                    The mySavedBar logic turns off the variable logic while in a trade (the red bars) but when profit target is reached and the next bar after that is updated, the Variable turns back on; which then enters into another trade. The Variable logic is re-painting.
                    If the variable is turning back on earlier than you expect it to, you'll need to add additional logic to prevent it from becoming true until you want it to. We can't suggest what logic that would be, whatever criteria you have in mind for the strategy would be appropriate.

                    Comment


                      #11
                      Thank You for brainstorming with me Gaby.
                      You've been very helpful and patient. I think I have additional entry code that is making the entries re-paint. But I did come up with a solution.
                      I do wish there was an 'until' object (or command) in CS though.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Today, 05:17 AM
                      0 responses
                      50 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      126 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      69 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      42 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      46 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X