Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Great, thank you.

      Your idea worked as I need.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by usglucofreeze, Today, 01:19 AM
      0 responses
      7 views
      0 likes
      Last Post usglucofreeze  
      Started by f.saeidi, Today, 01:12 AM
      0 responses
      7 views
      0 likes
      Last Post f.saeidi  
      Started by NinjaTrader_ChelseaB, 03-14-2017, 10:17 AM
      227 responses
      34,318 views
      7 likes
      Last Post rare312
      by rare312
       
      Started by f.saeidi, Today, 12:11 AM
      0 responses
      6 views
      0 likes
      Last Post f.saeidi  
      Started by Graci117, Yesterday, 11:40 PM
      0 responses
      7 views
      0 likes
      Last Post Graci117  
      Working...
      X