• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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:
        if (BarsSinceEntry() > 2 )
        EnterLong();


        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:
        double timeSinceEntry = 0 ;
        double timePassed = ToTime(DateTime.Now) - timeSinceEntry;
        if (ToTime(Time[ 0]) >= 90000 )
        {
        EnterLong();
        timeSinceEntry = ToTime(DateTime.Now);
        }
        if(timePassed >= 200 )
        {
        ExitLong();
        }
        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:
            double timeSinceEntry = 0 ;
            double timePassed      = ToTime(DateTime.Now) - timeSinceEntry;
            if (ToTime(Time[0]) >= 90000 )
                             {
                               EnterLong();
                               timeSinceEntry = ToTime(DateTime.Now);
                             }
            if(timePassed >= 200 )
                              {
                               ExitLong();
                              }
            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 Sh00tHerMcGavin, Today, 07:39 PM
            0 responses
            3 views
            0 likes
            Last Post Sh00tHerMcGavin  
            Started by Mberg, Today, 05:13 PM
            0 responses
            1 view
            0 likes
            Last Post Mberg
            by Mberg
             
            Started by Barry Milan, Today, 01:33 PM
            2 responses
            9 views
            0 likes
            Last Post marcus2300  
            Started by NW Trader, Today, 03:42 PM
            2 responses
            8 views
            0 likes
            Last Post NW Trader  
            Started by ralett, Today, 01:42 PM
            1 response
            5 views
            0 likes
            Last Post NinjaTrader_ShawnB  
            Working...
            X