• 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

  • NinjaTrader_ShawnB
    replied
    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.

    Leave a comment:


  • Frederic
    replied
    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?

    Leave a comment:


  • NinjaTrader_ShawnB
    replied


    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();
    }

    Leave a comment:


  • Frederic
    replied
    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.

    Leave a comment:


  • NinjaTrader_ShawnB
    replied

    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.

    Leave a comment:


  • Frederic
    started a topic Close OpenPosition after X minutes.

    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.

Latest Posts

Collapse

Topics Statistics Last Post
Started by ntdev, Today, 07:31 AM
1 response
9 views
0 likes
Last Post NinjaTrader_Kate  
Started by Kabua, Today, 07:19 AM
1 response
4 views
0 likes
Last Post NinjaTrader_Jim  
Started by brianshih, Today, 02:40 AM
1 response
8 views
0 likes
Last Post NinjaTrader_PatrickG  
Started by ubreun, Yesterday, 10:27 PM
1 response
39 views
0 likes
Last Post NinjaTrader_PatrickG  
Started by 7robert, Yesterday, 06:19 PM
1 response
19 views
0 likes
Last Post NinjaTrader_Marco  
Working...
X