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 NeverDownMoney, Today, 02:42 PM
            3 responses
            11 views
            0 likes
            Last Post NinjaTrader_ChrisL  
            Started by localappleseed, Today, 02:20 PM
            1 response
            13 views
            0 likes
            Last Post NinjaTrader_Jim  
            Started by asmmbillah, Today, 02:48 PM
            1 response
            19 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by KacemRhaz, Today, 01:31 PM
            3 responses
            15 views
            0 likes
            Last Post NinjaTrader_ChristopherD  
            Started by ronaldgreene828, Today, 12:08 PM
            1 response
            9 views
            0 likes
            Last Post NinjaTrader_Kate  
            Working...
            X