Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close OpenPosition after X minutes.

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

    Close OpenPosition after X minutes.

    Hello,

    I try to find a way to close my position X minutes after that I opened it.

    for exemple, in mt4, command is: if(TimeCurrent()-OrderOpenTime()>=900) close short/long.

    is there something similar in ninjatrader 7?

    Thanks.

    #2

    Hello Frederic,



    You could use the method ToTime() to compare the time you want to set to the current Time. You use the CancelOrder() method to cancel any orders when your conditions are true.


    I have included the ToTime() and Time Help Guides to assist you further.
    Shawn B.NinjaTrader Customer Service

    Comment


      #3
      Hello,
      can you please post me an exemple of code?

      as I said, I want something like if(TimeCurrent()-OrderOpenTime()>=900) close short/long ...

      I was thinking for ninja to write something like:
      if
      { Time[0] > Time.OpenPosition[0]+2*minutes
      ExitLong()

      but sadly ninja in backtest show me only irrelevant results.

      Comment


        #4


        Hello Frederic,

        If you are using minute bars, you could use the BarsSinceEntry() method, which returns the number of bars that have elapsed since the last specified entry.
        Code:
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2][COLOR=#0000FF]if[/SIZE] [FONT=Courier New][SIZE=2](BarsSinceEntry() >[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]2[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2])[/SIZE][/FONT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2]             EnterLong();[/SIZE][/FONT][/LEFT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [/LEFT][/FONT][/COLOR]


        If you are not using minute bars, you would have to keep track of the time since the last entry and compare that time to the current time. I have included an example using the ToTime() method.
        Code:
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2][COLOR=#0000FF]double[/SIZE][FONT=Courier New][SIZE=2] timeSinceEntry = [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2];[/SIZE][/FONT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2]                 [/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#0000FF]double[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] timePassed     = ToTime(DateTime.Now) - timeSinceEntry;[/SIZE][/FONT][/LEFT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2]                  [/SIZE][/FONT][/LEFT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2]                 [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000FF]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (ToTime(Time[[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]]) >= [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]90000[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] )[/SIZE][/FONT][/LEFT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2]                 {[/SIZE][/FONT][/LEFT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2]                   EnterLong();[/SIZE][/FONT][/LEFT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2]                   timeSinceEntry = ToTime(DateTime.Now);[/SIZE][/FONT][/LEFT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2]                 }[/SIZE][/FONT][/LEFT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2]                        [/SIZE][/FONT][/LEFT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2]                  [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000FF]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2](timePassed >= [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]200[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] )[/SIZE][/FONT][/LEFT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2]                  {[/SIZE][/FONT][/LEFT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2]                   ExitLong();[/SIZE][/FONT][/LEFT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2]                  }[/SIZE][/FONT][/LEFT][/FONT][LEFT][/LEFT][/COLOR][LEFT][/LEFT]
        [FONT=Courier New][SIZE=2][COLOR=#0000FF][/COLOR][/SIZE][/FONT]
        [/LEFT][/FONT][/COLOR]
        Shawn B.NinjaTrader Customer Service

        Comment


          #5
          Hello,

          thanks for reply.

          if (BarsSinceEntry() > 2 )
          EnterLong();

          this is working.. BUT
          double timeSinceEntry = 0 ;
          double timePassed = ToTime(DateTime.Now) - timeSinceEntry;
          if (ToTime(Time[ 0]) >= 90000 )
          {
          EnterLong();
          timeSinceEntry = ToTime(DateTime.Now);
          }
          if(timePassed >= 200 )
          {
          ExitLong();
          }
          this is not working as ninjatrader close automatically my trades after 2 bars.

          all I want was a code that close position after 2 or 3 minutes. on mt4, this is a trivial solution, but looks like that in ninja the code is not really working on backtest.

          now lets me explain more.. you are 15 minutes chart, you get a signal going long, and you want signal to close after 3 or 2 minutes. you know what I mean?

          Comment


            #6
            Hello Frederic,

            Thank you for your response.

            You would want to use the second option I detailed alone.

            Below is the example you would want to use:
            Code:
            [COLOR=#000000][FONT=Tahoma]double timeSinceEntry = 0 ;[/FONT][/COLOR]
            [COLOR=#000000][FONT=Tahoma]double timePassed      = ToTime(DateTime.Now) - timeSinceEntry;[/FONT][/COLOR]
            [COLOR=#000000][FONT=Tahoma]if (ToTime(Time[0]) >= 90000 )[/FONT][/COLOR]
            [COLOR=#000000][FONT=Tahoma]                 {[/FONT][/COLOR]
            [COLOR=#000000][FONT=Tahoma]                   EnterLong();[/FONT][/COLOR]
            [COLOR=#000000][FONT=Tahoma]                   timeSinceEntry = ToTime(DateTime.Now);[/FONT][/COLOR]
            [COLOR=#000000][FONT=Tahoma]                 }[/FONT][/COLOR]
            [COLOR=#000000][FONT=Tahoma]if(timePassed >= 200 )[/FONT][/COLOR]
            [COLOR=#000000][FONT=Tahoma]                  {[/FONT][/COLOR]
            [COLOR=#000000][FONT=Tahoma]                   ExitLong();[/FONT][/COLOR]
            [COLOR=#000000][FONT=Tahoma]                  }[/FONT][/COLOR]
            Keep in mind you will need to tailor this example to your strategy specifically.
            Shawn B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by JoMoon2024, Today, 06:56 AM
            0 responses
            6 views
            0 likes
            Last Post JoMoon2024  
            Started by Haiasi, 04-25-2024, 06:53 PM
            2 responses
            19 views
            0 likes
            Last Post Massinisa  
            Started by Creamers, Today, 05:32 AM
            0 responses
            6 views
            0 likes
            Last Post Creamers  
            Started by Segwin, 05-07-2018, 02:15 PM
            12 responses
            1,786 views
            0 likes
            Last Post Leafcutter  
            Started by poplagelu, Today, 05:00 AM
            0 responses
            3 views
            0 likes
            Last Post poplagelu  
            Working...
            X