Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exit position 1 second after buy/sell

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

    Exit position 1 second after buy/sell

    Hello,

    I would like to know if it's possible exit a position 1 second after enter it.

    I tried something like below but seems to work only in minute frame:


    private double enterPrice
    private int startTime

    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
    EnterLongLimit(1, enterPrice, "Buy");
    startTime = ToTime(Time[0]);

    if (ToTime(Time[0]) >= startTime + 000001 && Position.MarketPosition == MarketPosition.Long)
    {
    ExitLong(@"exit_TPM_Buy", @"Buy");
    }
    }


    Could anyone help me with correct code for second frame?
    Last edited by diorfo; 12-24-2022, 06:23 PM.

    #2
    Hello diorfo,

    You have the right idea but you need a condition for the long limit entry and the setting of startTime ​. You need to stop setting startTime once the limit entry fills so it has a specific fixed time. Once you do that the condition you have would be the way to do that type of action after 1 second however there is a easier way to add 1 second. You also need to consider the time that you used.

    To check 1 second from a given time you can do something like the following. I added a Flat condition so that the setting of startTime stops once the entry has filled.

    Code:
    private DateTime startTime;
    ​
    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
        if(Position.MarketPosition == MarketPosition.Flat) // or some condition for your entry that remains true while the limit order is unfilled so it does not expire
        {
            EnterLongLimit(1, enterPrice, "Buy");
            startTime = Time[0].AddSeconds(1);
        }
        else if(Position.MarketPosition == MarketPosition.Long)
        {
             if (Time[0] >= startTime)
             {
                  ExitLong(@"exit_TPM_Buy", @"Buy");
              }  
        }​
    }

    There are a few considerations to make here.

    Time[0] is your bar series time. If you used a 1 minute bar then adding 1 second would be the last bar close time plus 1 second. The times you are checking are in 1 minute intervals. If you want the latest time you can use the incoming market data time in OnMarketData:

    Code:
    startTime = marketDataUpdate.Time.AddSeconds(1);
    Code:
     if (marketDataUpdate.Time >= startTime)
    https://ninjatrader.com/support/help...aeventargs.htm

    When using OnMarketData that is realtime only, to be able to backtest this script you would need to use TickReplay.

    https://ninjatrader.com/support/help...sub=tickreplay


    If you wanted to use times from a secondary series you can use the Times series.


    Code:
     startTime = Times[1][0].AddSeconds(1);
    Code:
    if (Times[1][0] >= startTime)

    https://ninjatrader.com/support/help...hlightsub=mult i​
    Last edited by NinjaTrader_Jesse; 12-27-2022, 08:18 AM.

    Comment


      #3
      Great, thank you.

      Your idea worked as I need.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      93 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      138 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      68 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      123 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      73 views
      0 likes
      Last Post PaulMohn  
      Working...
      X