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:
    [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.

    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:
    [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]

    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 DJ888, 04-16-2024, 06:09 PM
6 responses
18 views
0 likes
Last Post DJ888
by DJ888
 
Started by Jon17, Today, 04:33 PM
0 responses
1 view
0 likes
Last Post Jon17
by Jon17
 
Started by Javierw.ok, Today, 04:12 PM
0 responses
6 views
0 likes
Last Post Javierw.ok  
Started by timmbbo, Today, 08:59 AM
2 responses
10 views
0 likes
Last Post bltdavid  
Started by alifarahani, Today, 09:40 AM
6 responses
41 views
0 likes
Last Post alifarahani  
Working...
X