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

How to Properly Create a Variable for Exiting Trade Based off Time Elapsed

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

    How to Properly Create a Variable for Exiting Trade Based off Time Elapsed

    I am looking to create an extra condition for exiting a trade if we have been in the trade over a certain period if we have not reached my breakeven level.

    I am generally using non-time based bars. Would the best course of action be to reference ToTime, Timer, Stopwatch, orTriggerCustomEvent(), or something else?

    Is there a way to store the time when I enter my trade, Then calculate the elapsed time. Once elapsed time is reached, exit trade if my other conditions are not met.

    Entry Condtions.... (Insert)
    Start Counting Elapsed Time (Variable or time or whatever is needed)
    If time since stored entry time is greater than 240 minutes....


    Then,

    If (InTrade)
    && (Close[0]<Breakeven)
    &&Close[0}>EntryPrice);
    {
    ExitLong();

    Reset(Variable or time or whatever is needed)
    }

    #2
    Hello durdcash,

    Thank you for your post.

    You could keep track of the entry and once the entry order is executed, you could start a timer with a TimerEventProcessor. Then, inside of the timer, you could check if the other conditions are met or not, and if they are not call TriggerCustomEvent() to exit the trade. The following page has an example of a timer event and the use of TriggerCustomEvent():


    This is likely the best approach, especially if you are not using time-based bars.

    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      NinjaTrader_Emily thanks for your response. Do I need to enter all of the parts of code in the " Using TriggerCustomEvent() in simple timer event​" or is there more/less I need to incorporate?

      Then once I eventually figure out all the locations that code goes, what is the best way to see if we have reached 240 minutes with the timer? Would it just be If(InTrade) && (myTimer.Elapsed >=240) Then.... do something?

      Comment


        #4
        Hello durdcash,

        Thank you for your reply.

        The timer used in that example is a general C# concept and is not specific to NinjaScript. You may find more information about the System.Timers at the following publicly available link:
        Generates an event after a set interval, with an option to generate recurring events.


        The code comments in that example help to explain what each piece of code is doing. For example, here is the State.DataLoaded section from that help guide example:
        Code:
        else if (State == State.DataLoaded)
        {
            // Instantiates the timer and sets the interval to 5 seconds.
            myTimer = new System.Timers.Timer(5000);
            // Adds the event handler for the method that will
            // process the timer event to the timer.
            myTimer.Elapsed += TimerEventProcessor;
            // Starts the timer
            myTimer.Enabled = true;
        }​
        In your own script, if you want the timer to be set to 240 minutes, you would need to set it to that value in milliseconds. 240 minutes is 14400000 milliseconds. That means you would need to set it up as follows:
        Code:
            // Instantiates the timer and sets the interval to 240 minutes.
            myTimer = new System.Timers.Timer(14400000);
            // Adds the event handler for the method that will
            // process the timer event to the timer.
            myTimer.Elapsed += TimerEventProcessor;​
        Then, you would not start the timer until a position is opened. At that point when InTrade becomes true, you would add the myTimer.Enabled = true; line of code.
        You do need to the TimerEventProcessor method in your script, and inside of that method you should call TriggerCustomEvent() which would contain the logic to close out the position if it is still open.

        Thank you for your time and patience.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          NinjaTrader_Emily Can the TriggerCustomEvent() be called within on bar update if I use internal conditional order placement logic(if that is the proper term?) to place my orders? Or do I need to call it after on bar update is closed with brackets?

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Fitspressorest, Today, 01:38 PM
          0 responses
          2 views
          0 likes
          Last Post Fitspressorest  
          Started by Jonker, Today, 01:19 PM
          0 responses
          2 views
          0 likes
          Last Post Jonker
          by Jonker
           
          Started by futtrader, Today, 01:16 PM
          0 responses
          6 views
          0 likes
          Last Post futtrader  
          Started by Segwin, 05-07-2018, 02:15 PM
          14 responses
          1,791 views
          0 likes
          Last Post aligator  
          Started by Jimmyk, 01-26-2018, 05:19 AM
          6 responses
          844 views
          0 likes
          Last Post emuns
          by emuns
           
          Working...
          X