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

Time filter but closes on exit

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

    Time filter but closes on exit

    I have a time filter that I want to exit all trades when trading time ends. For example, if trading time is between 83000 and 143000, all open trades should exit immediately after 143000. I know the time filter works because it changes BackColor depending on whether or not the current bar is between the trading times. Plus, no trades are entered until after the start time.

    However, some trades start during trading times and do not end until well after end time. Specifically, at close; Exit on close = true.

    Can someone explain why this is happening? Here's the code:
    Code:
     
    if (!bInTimeRange) // if within desired start/end times
    {
      ExitLong(); // not time to trade so exit if long
      ExitShort(); // not time to trade so exit if short
    }

    #2
    Hi ricktek,

    Thanks for your note.

    Are there any error messages that appear in the Log tab of the Control Center?

    Is there still market data incoming when trying to exit your positions?

    I'd also like you to add trace orders to the script and open the output window. This will let us know if any of the orders are getting ignored.

    A print in this if statement would be helpful as well.

    To turn on trace orders add the following line to the Initialize() part of your script.
    TraceOrders = true;

    To open the output window click Tools -> Output Window

    Please also let me know the output in the Output window.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      There are no errors in the log. I am only testing on historical data at this time.
      I'm not sure what you mean by: Is there still market data incoming when trying to exit your positions?
      In what if statement do you want the Print statement? And what do you want printed?
      I really don't want to post my strategies output. Can you tell me what to look for?

      Comment


        #4
        Hi ricktek,

        I understand that you are backtesting this strategy so please ignore my comment about needing incoming live data.

        The if statement is to ensure that the code to exit is being run.
        For example:
        Code:
        if (!bInTimeRange) // if within desired start/end times
        {
          Print("exiting trades");
          ExitLong(); // not time to trade so exit if long
          ExitShort(); // not time to trade so exit if short
        }
        In the output window you will want to watch for the print statement as well as any mentions of orders being ignored. For example something like:
        7/24/2013 1:56:00 PM Ignored PlaceOrder() method at 7/24/2013 1:56:00 PM: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Buy' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          This is the trace from a problematic trade:
          DateTime: 17-Jun-13 09:30:26; iState: 1
          17-Jun-13 09:30:26 Entered internal PlaceOrder() method at 17-Jun-13 09:30:26: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='BuyEntry1' FromEntrySignal=''
          17-Jun-13 09:30:26 Entered internal SetStopTarget() method: Type=Target FromEntrySignal='BuyEntry1' Mode=Ticks Value=23 Currency=0 Simulated=False
          17-Jun-13 09:30:26 Entered internal SetStopTarget() method: Type=TrailStop FromEntrySignal='BuyEntry1' Mode=Ticks Value=25 Currency=0 Simulated=True
          17-Jun-13 14:56:45 Cancelled order due to end of session handling: BarsInProgress=0: Order='NT-01601/Sim101' Name='Stop loss' State=Working Instrument='CL 07-13' Action=Sell Limit price=0 Stop price=95.49 Quantity=1 Strategy='MyStrategy' Type=Stop Tif=Gtc Oco='NT-01020-75' Filled=0 Fill price=0 Token='7386bf72b8d44f589692ee26d8b0d0aa' Gtd='01-Dec-99 00:00:00'
          17-Jun-13 14:56:45 Cancelled order due to end of session handling: BarsInProgress=0: Order='NT-01602/Sim101' Name='Profit target' State=Working Instrument='CL 07-13' Action=Sell Limit price=98.51 Stop price=0 Quantity=1 Strategy='MyStrategy' Type=Limit Tif=Gtc Oco='NT-01020-75' Filled=0 Fill price=0 Token='46eb1245c8e144fc9693bf050456c2a1' Gtd='01-Dec-99 00:00:00'

          This is output that displayed a Print statement:
          DateTime: 03-Jun-13 12:29:57; iState: 1
          03-Jun-13 12:29:57 Entered internal PlaceOrder() method at 03-Jun-13 12:29:57: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='BuyEntry1' FromEntrySignal=''
          03-Jun-13 12:29:57 Ignored PlaceOrder() method at 03-Jun-13 12:29:57: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='BuyEntry1' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
          03-Jun-13 12:29:57 Entered internal SetStopTarget() method: Type=Target FromEntrySignal='BuyEntry1' Mode=Ticks Value=23 Currency=0 Simulated=False
          03-Jun-13 12:29:57 Entered internal SetStopTarget() method: Type=TrailStop FromEntrySignal='BuyEntry1' Mode=Ticks Value=25 Currency=0 Simulated=True
          03-Jun-13 12:30:29 Entered internal PlaceOrder() method at 03-Jun-13 12:30:29: BarsInProgress=0 Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
          ExitLong

          Comment


            #6
            Hi ricktek,

            Just to be sure I understand the issue, the problem is that the entry order is not being exited after your set hours, and is also not being exited at the close of the session, is this correct?

            Is ExitLong the print statement that you put in the if statement?

            If so, is there nothing that prints after this?

            May I have a simplified version of this script to test on my end?

            To export your script do the following:
            1. Click File -> Utilities -> Export NinjaScript
            2. Enter a unique name for the file in the value for 'File name:'
            3. Select the strategy from the objects list on the left -> click the right facing arrow ">" to add the strategy to the export
            4. Click the 'Export' button -> click 'yes' to add any referenced indicators to the export -> click OK to clear the export location message


            By default your exported file will be in the following location:
            • (My) Documents/NinjaTrader 7/bin/Custom/ExportNinjaScript/<export_file_name.zip>


            Below is a link to the help guide on Exporting NinjaScripts.
            http://www.ninjatrader.com/support/h...nt7/export.htm

            If you do not feel comfortable posting this in your reply. Please send the exported script to support[at]ninjatrader[dot]com. In the body of the email please write Attn: Chelsea #888229.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              No. On 17 June, it did not exit at the end of trading time; it did exit at close.

              ExitLong and ExitShort are in separate print statements. I modified the code so the print statements would be more useful:
              if (!bInTimeRange) // if within desired start/end times
              {
              if (Position.MarketPosition == MarketPosition.Long)
              {
              ExitLong();
              // not time to trade so exit if long
              Print("ExitLong");
              }
              if (Position.MarketPosition == MarketPosition.Short)
              {
              ExitShort();
              // not time to trade so exit if short
              Print("ExitShort");
              }
              }

              There were six ExitLong & one ExitShort prints but most trades exited before end of trading times. The 17 June trade is one of two instances where I noticed the failure to exit on time.

              The strategy and several indicators are all proprietary and I would rather not share the scripts. If you are not able to help, just let me know and I'll try to work it out on my own.

              Comment


                #8
                Hello ricktek,

                Looking again at the output in your previous post I do see that the ExitLong worked correctly.

                It seems that as long as the ExitLong or ExitShort print is printed the order exits correctly.

                I would recommend that you print the value of bInTimeRange one line before the if (!bInTimeRange) if statement.

                This will let you know why the ExitLong and ExitShort prints are not printing.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  I did that. The ExitLong printed at the next bar after end time in all cases except 17 June. However, while communicating with you, I made changes to the conditions for entering trades and the problem seems to have disappeared. If it comes back, I'll be back. Thanks for your help and have a good evening.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Rogers101, 05-05-2024, 11:30 AM
                  7 responses
                  28 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by knowmad, Today, 03:52 AM
                  1 response
                  19 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by llanqui, Yesterday, 03:51 PM
                  5 responses
                  23 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by MSerag, Yesterday, 11:52 PM
                  1 response
                  17 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by tradingnasdaqprueba, Today, 03:42 AM
                  1 response
                  18 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Working...
                  X