Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

One Order Per Bar

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

    #16
    So I started from scratch and I still get orders entered after previous exit on the same bar. Not sure what to do at this point. Is it because I use Profit Targets? I am at a loss. Thanks for any help. Here's what I have:
    {
    #region Variables
    private bool OneBarSinceExit = true;
    #endregion

    protected override void Initialize()
    {
    CalculateOnBarClose = false;
    SetProfitTarget("Long 1a", CalculationMode.Ticks, 5);
    SetProfitTarget("Short 1a", CalculationMode.Ticks, 5);

    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < 1)
    return;
    if (BarsSinceExit("Long 1a") > 1)
    OneBarSinceExit = true;

    if (BarsSinceExit("Short 1a") > 1)
    OneBarSinceExit = true;

    if (OneBarSinceExit = true && SMA(9)[0] > SMA(20)[0])
    EnterLong(1, "Long 1a");
    OneBarSinceExit = false;

    if (OneBarSinceExit = true && SMA(9)[0] < SMA(20)[0])
    EnterShort(1, "Short 1a");
    OneBarSinceExit = false;
    }

    #region Properties
    #endregion
    }
    }

    Comment


      #17
      cre8it8, it appears as if some of your code is missing brackets to help separate the different sections. For example, this:
      Code:
      if (OneBarSinceExit = true && SMA(9)[0] > SMA(20)[0])
              EnterLong(1, "Long 1a");
              OneBarSinceExit = false;
      Should be this:
      Code:
      if (OneBarSinceExit = true && SMA(9)[0] > SMA(20)[0])
      {
                  EnterLong(1, "Long 1a");
                  OneBarSinceExit = false;
      }
      Since the code is missing brackets, your statements to set OneBarSinceExit to false will be executed each and every time OnBarUpdate() is called, no matter what it should actually be set to.
      AustinNinjaTrader Customer Service

      Comment


        #18
        Hi Austin, I tried out adding the brackets that you show and it still is putting orders in on the exit of the previous order on the same bar. It's like it is never getting a false.

        Comment


          #19
          I found this reference sample Strategy: Separating logic to either calculate once on bar close or on every tick so I am going to see if I can build what I need out of following its' example.

          Comment


            #20
            cre8it8, please try something like this - work with a bool flag to only place one historical entry for example if the market position is flat, then you should be able to work with BarsSinceEntry since there's at least one trade historical in to reference, the same bool flag could also be used to enter once per bar, you could reset it to 'trade ok' state for example on the first tick of the new bar then.

            Comment


              #21
              I tried to use the reference code that I mentioned but it turned out to operate like calculate on bar close because it waited until the bar that created the moving avg. to cross was finished before placing an order on the first tick of the next bar so not much help. I would try what you suggested but I don't really know how to use a bool series unless I have an example to copy and change to the parameters that I need. I am pretty novice at programming. Thanks anyway.

              Comment


                #22
                cre8it8, you would not need a series here, just a basic bool variable would help. You can define it in variables -

                Code:
                 
                private bool tradeOnce = false;
                Then you let the strategy trade once to give BarsSinceEntry a reference -

                Code:
                 
                if (Position.MarketPosition == MarketPosition.Flat && tradeOnce == false)
                {
                EnterLong();
                tradeOnce = true;
                }
                After this you can use BarsSinceEntry in your new condition -

                Code:
                 
                if (YourEntryConditions && BarsSinceEntry() > 0)
                {
                EnterLong();
                }
                I believe this should do what you seek then.

                Comment


                  #23
                  I did that but I had to divide it up into two separate strategies one for long and one for short in order for it to enter and exit consistently. When I had the SMA 9 > SMA 20 for long and the SMA 9 < SMA 20 for short on the same strategy sometimes it would put in an order for the bool long as a live order right away instead of it being a historical order. Also, sometimes I would end up with 2 live orders. Very inconsistent so my work around was the separate strategies which appears to work fine. There's probably a better way but it is beyond my skills.

                  Thanks for all your guys help on this thread.

                  Comment


                    #24
                    When I say "I did that" I meant that I tried what you suggested after reading your suggestion not before, so thank you for it.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    558 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    324 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    101 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    545 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    547 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X