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

Open position on Secondary instrument doesn't close on time

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

    Open position on Secondary instrument doesn't close on time

    Hello,
    I would like to close the secondary instrument before the end of session, I wrote this code, and it seems like it doesn't close the open positions within the time I want.
    I attached a picture.

    if (BarsInProgress == 1);
    if ( ToTime(Time[0]) >= ToTime( 15, 50, 00)
    && ToTime(Time[0]) <= ToTime( 16, 10, 00)
    && (Positions[1].Quantity == Quantity))

    {
    ExitShort(Positions[1].Quantity, "END","S1");
    }

    I am running the analyzer on 1 minute for primary. The secondary instrument is on 300Tick.
    Am I missing something?

    Thank you,
    Attached Files

    #2
    Hello markkm,

    Thank you for your note.

    In the code block below I see you have a semi colon after the first if statement, if you remove the semi colon does the code work as expected?
    Code:
    if (BarsInProgress == 1[B]);[/B]
    if ( ToTime(Time[0]) >= ToTime( 15, 50, 00)
    && ToTime(Time[0]) <= ToTime( 16, 10, 00) 
    && (Positions[1].Quantity == Quantity))
    If not, does the following logic run as you expect?

    Code:
    if (BarsInProgress == 1)
    if ( ToTime(Time[0]) >= ToTime( 15, 50, 00)
    && ToTime(Time[0]) <= ToTime( 16, 10, 00) 
    &&[B] (Positions[1].MarketPosition == MarketPosition.Short))[/B]
    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Thanks, I tried both suggestions and I still have the same problem.
      I also changed Data series of secondary instrument to 1 minute, to make sure I don't have any issues with Ticks, and still not consistently closing at 3:51:00.
      You can see from the attached picture that it closes sometimes at 3:56:00, or 3:55:00.

      Please note that primary instrument is also 1 minute, and I am not missing any historical data for these inconsistencies.
      Attached Files

      Comment


        #4
        Hello markkm,

        You should add prints to the script to determine why these exits are not occuring at the desired time.

        I’ve provided a link to a youtube video which covers an example of using prints to understand behavior:
        Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.3:11 Creating a New NinjaScript Strategy13:52 Analyz...


        I’ve provided a link covering debugging which you may find helpful.
        Debugging: http://ninjatrader.com/support/forum...979#post510979

        If you try the above and still unable to figure out the issue, please leave the print statements in the script and you can upload a copy and I'll take a look and see if anything jumps out.

        To export a NinjaScript from NinjaTrader 8 do the following:
        From the Control Center window select Tools -> Export -> NinjaScript...
        Click Add>Select the indicator>OK>Export.
        Then attach that file you saved; under My Docs>NT8>Bin>Custom>Select the downloaded .zip file.

        Please let us know if you need further assistance.
        Last edited by NinjaTrader_ChelseaB; 08-15-2018, 08:05 AM.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by markkm View Post
          Hello,
          I would like to close the secondary instrument before the end of session, I wrote this code, and it seems like it doesn't close the open positions within the time I want.
          I attached a picture.

          if (BarsInProgress == 1);
          if ( ToTime(Time[0]) >= ToTime( 15, 50, 00)
          && ToTime(Time[0]) <= ToTime( 16, 10, 00)
          && (Positions[1].Quantity == Quantity))

          {
          ExitShort(Positions[1].Quantity, "END","S1");
          }

          I am running the analyzer on 1 minute for primary. The secondary instrument is on 300Tick.
          Am I missing something?

          Thank you,
          Try this:

          Code:
          if(BarsInProgress==1 
          && Positions[1].MarketPosition == MarketPosition.Short 
          && ToTime(Time[0])>=ToTime(15,50,00) 
          && ToTime(Time[0])<=ToTime(16,10,00))
          
          		{
          				ExitShort(Convert.ToInt32(Positions[1].Quantity), "END","S1");
          
          		}
          Also make sure the Trading Hour template you're using is the correct template. Maybe it's skipping over the above times. Make sure your entry's name is S1.

          Or try this:

          Code:
          [CODE]
          
          if(BarsInProgress==1 
          && Positions[1].MarketPosition == MarketPosition.Short 
          && ToTime(Time[0])>=ToTime(15,50,00))
          
          		{
          				ExitShort(Convert.ToInt32(Positions[1].Quantity), "END","S1");
          
          		}
          [/CODE]

          You're trying to exit after 15,50,00... no need for the other argument. Also my gut feeling is saying that b/c you're saying BarsInProgress ==1 and then using ToTime..it should be ToTimes i think... see this thread:

          Support for the development of custom automated trading strategies using NinjaScript.
          Last edited by staycool3_a; 08-14-2018, 10:46 PM.

          Comment


            #6
            Thanks!
            I tried all the above, and it didn't work.
            I put together a simple strategy that shows the same discrepancies.
            I also attached a screen shot of my settings in the analyzer.
            Attached Files

            Comment


              #7
              Here's an example, the first line on the attached screen shot, tracing that order got me:

              9/1/2017 3:51:00 PM Strategy 'Test4/-1': Entered internal SubmitOrderManaged() method at 9/1/2017 3:51:00 PM: BarsInProgress=1 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='END' FromEntrySignal='S1'
              9/1/2017 3:51:00 PM Strategy 'Test4/-1: Cancelled pending exit order, since associated position is closed, orderId='NT-00001-13300' account='Backtest' name='Profit target' orderState=Working instrument='ZF 09-18' orderAction=BuyToCover orderType='Limit' limitPrice=117.3125 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-13300' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2017-08-31 17:02:00' gtd='2099-12-01' statementDate='2018-08-15'


              It's clear that order was still open, so why is it not closing it?
              Attached Files

              Comment


                #8
                Just to clarify, here's when it was first opened.

                8/31/2017 5:02:00 PM Strategy 'Test4/-1': Entered internal SubmitOrderManaged() method at 8/31/2017 5:02:00 PM: BarsInProgress=1 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='S1' FromEntrySignal=''
                Strategy 'Test4/-1': Stop/target handling set to 'By strategy position' since currency based stop order was placed.
                8/31/2017 5:05:00 PM Strategy 'Test4/-1': Entered internal SubmitOrderManaged() method at 8/31/2017 5:05:00 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
                9/1/2017 3:51:00 PM Strategy 'Test4/-1': Entered internal SubmitOrderManaged() method at 9/1/2017 3:51:00 PM: BarsInProgress=1 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='END' FromEntrySignal='S1'
                9/1/2017 3:51:00 PM Strategy 'Test4/-1: Cancelled pending exit order, since associated position is closed, orderId='NT-00001-13300' account='Backtest' name='Profit target' orderState=Working instrument='ZF 09-18' orderAction=BuyToCover orderType='Limit' limitPrice=117.3125 stopPrice=0 quantity=1 tif=Gtc oco='NT-00000-13300' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2017-08-31 17:02:00' gtd='2099-12-01' statementDate='2018-08-15'

                Comment


                  #9
                  Hello markkm,

                  I highlighted in the attached screen shot what looks to be entries after 3:51:00, (Its not clear because your screen shot doesn't show full entry times), and since a position can't be closed till its been opened I would expect that the close time would be after 3:51.

                  I suggest investigating your entry times as well as adding prints to understand the behavior as in the previous link I provided.

                  If you would like further assistance debugging this script and would like to work with a professional third party, please let us know and we can provide a list of third parties.

                  Please let us know if you need further assistance.
                  Attached Files
                  Alan P.NinjaTrader Customer Service

                  Comment


                    #10
                    I don't need to work with 3rd parties. I think there are discrepancies within the software, unless I am mistaken, and you can point out my mistake.
                    Again, attached is a second version of the simple strategy that will not open any position after 15:50, and should close the secondary instrument at no later than 15:51. The problem is it doesn't.
                    I also attached a picture of the entry time, and exit time.

                    The easiest example is the first one (highlighted). It didn't close at 15:51. it closed at 15:55.
                    here is the tracing of that position (it canceled it, because it said position closed. That position was still opened)


                    9/1/2017 1:00:00 AM Strategy 'Test4/-1': Entered internal SubmitOrderManaged() method at 9/1/2017 1:00:00 AM: BarsInProgress=1 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='S1' FromEntrySignal=''
                    9/1/2017 3:51:00 PM Strategy 'Test4/-1': Entered internal SubmitOrderManaged() method at 9/1/2017 3:51:00 PM: BarsInProgress=1 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='END' FromEntrySignal='S1'
                    9/1/2017 3:51:00 PM Strategy 'Test4/-1: Cancelled pending exit order, since associated position is closed, orderId='NT-00003-14376' account='Backtest' name='Profit target' orderState=Working instrument='ZF 09-18' orderAction=BuyToCover orderType='Limit' limitPrice=117.2890625 stopPrice=0 quantity=1 tif=Gtc oco='NT-00001-14376' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2017-09-01 01:00:00' gtd='2099-12-01' statementDate='2018-08-15'


                    Please help.
                    Attached Files

                    Comment


                      #11
                      Hello markkm,

                      In order for OnBarUpdate to get called there must be a trade, with no trade you should not expect OBU to be called.

                      Since the trade in question is from Sept 2017 and you are running the test on Sept 2018 5yr note futures, I would confirm that there was a trade which would have triggered OBU. Did a trade occur in the ZF 09-18 contract at 551 on 9-1-17?

                      It looks like at 355pm on 9-1-17 the short position was covered and the profit target was canceled. Is this correct?

                      I look forward to your reply.
                      Alan P.NinjaTrader Customer Service

                      Comment


                        #12
                        I am using ZF 09-18, and my setup is to have the contracts merged. That's why it was covered at 355pm. Otherwise there would be not deal at all on September 2017.
                        Now the question is, why at 355pm and not 351pm?
                        It should've exited at 351pm at Market price, no later than that.

                        Comment


                          #13
                          Hello markkm,

                          I'm able to replicate but when I go to my Historical Data Manager via Control Center>Tools>Historical Data and navigate to ZF and sept 1, I see there is no historical 1 minute data for the times in question.

                          Do you also lack 1 minute data for these times?

                          I look forward to your reply.
                          Attached Files
                          Alan P.NinjaTrader Customer Service

                          Comment


                            #14
                            Thanks for your help. I am missing these data too.
                            Why is that data missing? It's Minutes, and not Ticks.
                            In a live trading, wouldn't it just close at Market price?

                            Comment


                              #15
                              Hello markkm,

                              If there is no data, no bar, then OnBarUpdate will not be triggered.

                              There may have been issues with the data server at that time which prevented those bars from being recorded.

                              Please let us know if you need further assistance.
                              Alan P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Kaledus, Today, 01:29 PM
                              3 responses
                              9 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by frankthearm, Yesterday, 09:08 AM
                              14 responses
                              47 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by gentlebenthebear, Today, 01:30 AM
                              2 responses
                              13 views
                              0 likes
                              Last Post gentlebenthebear  
                              Started by PaulMohn, Today, 12:36 PM
                              2 responses
                              17 views
                              0 likes
                              Last Post PaulMohn  
                              Started by Conceptzx, 10-11-2022, 06:38 AM
                              2 responses
                              56 views
                              0 likes
                              Last Post PhillT
                              by PhillT
                               
                              Working...
                              X