Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Exit Position n bars since entry

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

    Exit Position n bars since entry

    Hello,

    I'm testing a strategy and I've got a couple of questions:

    1) Once in position, I'm looking to exit at the close of n bars since my entry bar.
    2) I need to choose a different order, depending on the position of the next opening bar.
    Let's say, for example, that if the next bars opens in a gap up, I want to enter short at
    today's high and exit at the close of the bar.

    Could someone help me with the code?

    Thanks.

    #2
    Hi MAX, for #1, you could use BarsSinceEntry(). For #2, you can specify which entry signal to look for with BarsSinceEntry():
    Code:
    // if the signal name you're looking for is "longEntry"
    int barsSinceEntry = BarsSinceEntry("longEntry");
    AustinNinjaTrader Customer Service

    Comment


      #3
      For #2, maybe I wasn't so clear.
      What I need to translate is:

      If open of next bars is higher than actual high, then sell next bar (stop) at actual high and close the position at the end of the bar.

      Hope this clarify.

      Thanks for your kind help.

      Comment


        #4
        Max, I'm not sure I follow. It would almost seem like the strategy you have envisioned "looks into the future". How would you sell at the high of the next bar? Anyways, unless you add intrabar granularity to your backtest, the backtester can't be told to buy and sell at the same bar.

        If next bar means current bar and if actual means previous bar, then I think this code would do what you want:
        Code:
        // this code is untested and incomplete
        // currentbarentry is a bool
        protected override void OnBarUpdate()
        {
             if (Open[0] > High[1])
             {
                 EnterShortStop(High[1]);
                 currentbarentry = true;
             }
        
        if (currentbarentry == true)
        {
                 currentbarentry = false;
                 ExitShort();
        }
        AustinNinjaTrader Customer Service

        Comment


          #5
          Hi,

          I still didn't test your code, although I don't want to look into the future, but at the present bar (doesn't matter the time frame).

          If, on the next bar, the open is in gap, I want to sell the market at this bar high in stop.
          So I already know which my entry level is (this bar high). I just need to tell the code : If open of next bar is higher than actual bar's high then sell at actual bar's high stop.

          I'm just giving an order based on my actual setup, depending on next opening.

          Hope this clear.

          Thanks.

          Comment


            #6
            Max, thanks for clarifying - then I would suggest testing out the sample code Austin did for you.
            BertrandNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by gemify, 11-11-2022, 11:52 AM
            6 responses
            803 views
            2 likes
            Last Post ultls
            by ultls
             
            Started by ScottWalsh, Today, 04:52 PM
            0 responses
            4 views
            0 likes
            Last Post ScottWalsh  
            Started by ScottWalsh, Today, 04:29 PM
            0 responses
            7 views
            0 likes
            Last Post ScottWalsh  
            Started by rtwave, 04-12-2024, 09:30 AM
            2 responses
            22 views
            0 likes
            Last Post rtwave
            by rtwave
             
            Started by tsantospinto, 04-12-2024, 07:04 PM
            5 responses
            70 views
            0 likes
            Last Post tsantospinto  
            Working...
            X