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

DateTime Issues

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

    DateTime Issues

    I have written a rather fast trading strategy that requires I wait 'x' number of seconds between an exit order and an entry order. To do this, I have used the following code.

    protected override void OnBarUpdate()
    {
    if (ToTime(Time[0])<opentime)
    exittime=0;

    if (ToTime(Time[0])>opentime && ToTime(Time[0])<stoptime)
    {
    if (mysignal && !init && Positions[0].MarketPosition==MarketPosition.Flat && ToTime(DateTime.Now)>=exittime)
    {
    LE= EnterLong(tradesize,"LE");
    init = true;
    }
    }
    }

    protected override void OnExecution(IExecution execution)
    {
    // check for fill on long entry order
    if (LE != null && LE.Token == execution.Order.Token)
    {
    // place long profit GTC
    LETarget=ExitLongLimit(0,true,LE.Quantity,Position s[0].AvgPrice+pt,"LPTX","LE");
    init=false
    }
    else
    {
    if (LETarget!=null && LETarget.Token==execution.Order.Token)
    exittime=ToTime(DateTime.Now.AddSeconds(delaysecon ds));
    }

    }

    So basically what I am doing is setting my exittime variable to 0 when we are outside of trading hours, and setting it to the time my exit order was filled + 'x' number of seconds during the trading day. I then check that the current DateTime>my exittime, which would hold a value of 0 at the beginning of the session, or my exittime + 'x' number of seconds if I have been filled on a profit target order. This code is working probably about 80% of the time, and everything seems correct when I debug with visual studio. Anyone else have any ideas on this?

    #2
    toptrader,

    You are comparing against timestamps of your bars which is not necessarily correlated to seconds. The timestamps of your bars are the ending times. For instance, on a 1 minute chart you could be at 9:30:30, but your bar timestamp would be 9:31:00.

    If you are trying to run off a very specific number of seconds I suggest you use a timer event of sorts. http://www.ninjatrader-support2.com/...ead.php?t=5965
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      My exittime variable is only accessing DateTime, Not Time[0]. Therefore that should have no affect on the program.

      Comment


        #4
        Then you will need to debug and try to isolate the cases where it doesn't work. I suggest you print all of your variables used in conditions and run checks by hand to see why conditions are or are not evaluating to true.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          I have tried that along with many other things. I guess I will keep digging. Thanks for your help!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by AttiM, 02-14-2024, 05:20 PM
          11 responses
          184 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by fernandobr, Today, 09:11 AM
          1 response
          3 views
          0 likes
          Last Post NinjaTrader_Erick  
          Started by timmbbo, Today, 08:59 AM
          1 response
          3 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by KennyK, 05-29-2017, 02:02 AM
          2 responses
          1,281 views
          0 likes
          Last Post marcus2300  
          Started by itrader46, Today, 09:04 AM
          1 response
          6 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Working...
          X