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

Friday Close

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

    Friday Close

    I need your assistance for the Friday Close I am working on. I don't want to keep positions open over the weekend so I have developed the following in my strategy:



    ==========

    //Friday Code
    if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) >= 133000)
    {
    if (Position.MarketPosition == MarketPosition.Long)
    ExitLong(@"LongHACEU");
    else if (Position.MarketPosition == MarketPosition.Short)
    ExitShort(@"ShortHACEU");
    if (State == State.Realtime)
    {
    CloseStrategy(null);
    SetState(State.Terminated);
    Print("Condition not met. Disabling Strategy" + Time[0].ToString());
    return;
    }
    }

    ===========================

    The code is correctly closing positions but opening duplicates leaving me open with buy and sell orders. I suspect the CloseStrategy() call is not working properly. I put the CloseStratgy() as a precautionary measure to close any pending orders that might not have closed out properly but what is happening is, If I have a position open on Friday at 13:30, then the Exit closes out the position but I see an additional buy and sell placed after the successful close.

    See below the logs. Looks like there is no need for CloseStratgy() here. Is it not a supported call?

    Closing Position:
    ============

    6/22/2018 13:29 Order Order='639495729/401308720' Name='Profit target' New state='Cancel submitted' Instrument='EURUSD' Action='Sell' Limit price=1.18102 Stop price=0 Quantity=2,000 Type='Limit' Time in force=GTC Oco='639495726' Filled=0 Fill price=0 Error='No error' Native error=''
    6/22/2018 13:29 Order Order='639510005/401308720' Name='Sell' New state='Submitted' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    6/22/2018 13:29 Default Disabling NinjaScript strategy 'Ninja8HACUD/140038321'
    6/22/2018 13:29 NinjaScript NinjaScript strategy 'Ninja8HACUD/140038321' submitting order
    6/22/2018 13:29 Default Disabling NinjaScript strategy 'Ninja8HACEU/140038320'
    6/22/2018 13:29 NinjaScript NinjaScript strategy 'Ninja8HACEU/140038320' submitting order

    Then NT8 reports an error saying "Order ID not found" and places a new sell order.

    =============
    6/22/2018 13:30 Order Order='639510007/401308720' Name='' New state='Accepted' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    6/22/2018 13:30 Order Order='639510007/401308720' Name='' New state='Submitted' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    6/22/2018 13:30 Position Instrument='EURUSD' Account='401308720' Average price=0 Quantity=0 Market position=Flat Operation=Remove
    6/22/2018 13:30 Order 401308720, Order id 639510005 is not found. affected Order: Sell 2000 Market
    ================

    #2
    Originally posted by ajsdnyyrr1234DDf View Post
    I need your assistance for the Friday Close I am working on. I don't want to keep positions open over the weekend so I have developed the following in my strategy:



    ==========

    //Friday Code
    if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) >= 133000)
    {
    if (Position.MarketPosition == MarketPosition.Long)
    ExitLong(@"LongHACEU");
    else if (Position.MarketPosition == MarketPosition.Short)
    ExitShort(@"ShortHACEU");
    if (State == State.Realtime)
    {
    CloseStrategy(null);
    SetState(State.Terminated);
    Print("Condition not met. Disabling Strategy" + Time[0].ToString());
    return;
    }
    }

    ===========================

    The code is correctly closing positions but opening duplicates leaving me open with buy and sell orders. I suspect the CloseStrategy() call is not working properly. I put the CloseStratgy() as a precautionary measure to close any pending orders that might not have closed out properly but what is happening is, If I have a position open on Friday at 13:30, then the Exit closes out the position but I see an additional buy and sell placed after the successful close.

    See below the logs. Looks like there is no need for CloseStratgy() here. Is it not a supported call?

    Closing Position:
    ============

    6/22/2018 13:29 Order Order='639495729/401308720' Name='Profit target' New state='Cancel submitted' Instrument='EURUSD' Action='Sell' Limit price=1.18102 Stop price=0 Quantity=2,000 Type='Limit' Time in force=GTC Oco='639495726' Filled=0 Fill price=0 Error='No error' Native error=''
    6/22/2018 13:29 Order Order='639510005/401308720' Name='Sell' New state='Submitted' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    6/22/2018 13:29 Default Disabling NinjaScript strategy 'Ninja8HACUD/140038321'
    6/22/2018 13:29 NinjaScript NinjaScript strategy 'Ninja8HACUD/140038321' submitting order
    6/22/2018 13:29 Default Disabling NinjaScript strategy 'Ninja8HACEU/140038320'
    6/22/2018 13:29 NinjaScript NinjaScript strategy 'Ninja8HACEU/140038320' submitting order

    Then NT8 reports an error saying "Order ID not found" and places a new sell order.

    =============
    6/22/2018 13:30 Order Order='639510007/401308720' Name='' New state='Accepted' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    6/22/2018 13:30 Order Order='639510007/401308720' Name='' New state='Submitted' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
    6/22/2018 13:30 Position Instrument='EURUSD' Account='401308720' Average price=0 Quantity=0 Market position=Flat Operation=Remove
    6/22/2018 13:30 Order 401308720, Order id 639510005 is not found. affected Order: Sell 2000 Market
    ================
    That most likely means that your conditions for entering a trade are still satisfied, so the strategy is entering another trade, as specified. You need to create a gate that prevents new entries, and set that gate at the time that you want to prevent new entries. Remember to reset the gate on Monday morning. I would suggest that you use a boolean gate.
    Last edited by koganam; 06-25-2018, 09:15 AM.

    Comment


      #3
      Boolean Condition is a good idea. I will look into it.
      The strategy is not entering a new trade. See below the log. The Strategy is closed and disabled. A Buy order generated from the Exit() call to close the position was also successful.

      So I am wondering it is really the CloseStratety() that is screwing up the order management.

      Order='639510005' said it was Filled. Then suddenly the following error was reported and a new Buy order was placed

      Order id 639510005 is not found. affected Order: Sell 2000 Market
      Error='Unable to cancel order' Native error='Order id 639510005 is not found.'


      ----





      6/22/2018 13:30 Position Instrument='EURUSD' Account='401308720' Average price=1.16615 Quantity=2,000 Market position=Short Operation=Operation_Add

      6/22/2018 13:30 Execution Execution='639510007' Instrument='EURUSD' Account='401308720' Exchange=Default Price=1.16615 Quantity=2,000 Market position=Short Operation=Operation_Add Order='639510007' Time='6/22/2018 13:30'
      6/22/2018 13:30 Order Order='639510007/401308720' Name='' New state='Filled' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=2000 Fill price=1.16615 Error='No error' Native error=''
      6/22/2018 13:30 Order Order='639510007/401308720' Name='' New state='Working' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
      6/22/2018 13:30 Order Order='639510007/401308720' Name='' New state='Accepted' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
      6/22/2018 13:30 Order Order='639510007/401308720' Name='' New state='Submitted' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
      6/22/2018 13:30 Position Instrument='EURUSD' Account='401308720' Average price=0 Quantity=0 Market position=Flat Operation=Remove

      6/22/2018 13:30 Order 401308720, Order id 639510005 is not found. affected Order: Sell 2000 Market

      6/22/2018 13:30 Order Order='639510005/401308720' Name='Sell' New state='Filled' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=2000 Fill price=1.16617 Error='Unable to cancel order' Native error='Order id 639510005 is not found.'

      6/22/2018 13:30 Order Order='639495728/401308720' Name='Stop loss' New state='Cancelled' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=1.16202 Quantity=2,000 Type='Stop Market' Time in force=GTC Oco='639495726' Filled=0 Fill price=0 Error='No error' Native error=''
      6/22/2018 13:30 Order Order='639495728/401308720' Name='Stop loss' New state='Cancel submitted' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=1.16202 Quantity=2,000 Type='Stop Market' Time in force=GTC Oco='639495726' Filled=0 Fill price=0 Error='No error' Native error=''
      6/22/2018 13:30 Order Order='639495729/401308720' Name='Profit target' New state='Cancelled' Instrument='EURUSD' Action='Sell' Limit price=1.18102 Stop price=0 Quantity=2,000 Type='Limit' Time in force=GTC Oco='639495726' Filled=0 Fill price=0 Error='No error' Native error=''
      6/22/2018 13:30 Execution Execution='639510005' Instrument='EURUSD' Account='401308720' Exchange=Default Price=1.16617 Quantity=2,000 Market position=Short Operation=Operation_Add Order='639510005' Time='6/22/2018 13:30'

      6/22/2018 13:30 Order Order='639510005/401308720' Name='Sell' New state='Working' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
      6/22/2018 13:30 Order Order='639510005/401308720' Name='Sell' New state='Filled' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=2000 Fill price=1.16617 Error='No error' Native error=''
      6/22/2018 13:30 Order Order='639510005/401308720' Name='Sell' New state='Accepted' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
      6/22/2018 13:29 Order Order='639495729/401308720' Name='Profit target' New state='Cancel submitted' Instrument='EURUSD' Action='Sell' Limit price=1.18102 Stop price=0 Quantity=2,000 Type='Limit' Time in force=GTC Oco='639495726' Filled=0 Fill price=0 Error='No error' Native error=''
      6/22/2018 13:29 Order Order='639510005/401308720' Name='Sell' New state='Submitted' Instrument='EURUSD' Action='Sell' Limit price=0 Stop price=0 Quantity=2,000 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

      6/22/2018 13:29 Default Disabling NinjaScript strategy 'Ninja8HACEU/140038320'
      6/22/2018 13:29 NinjaScript NinjaScript strategy 'Ninja8HACEU/140038320' submitting order

      Comment


        #4
        Hello ajsdnyyrr1234DDf,

        I have an example of preventing new entries after the exit on close and using the exit on close to close all positions before the end of a session you may find helpful.

        NinjaTrader Community, A common inquiry is that the Exit on close didn't work in a NinjaScript Strategy because there is a position after the exit on close should have occurred. When viewing the log we often find that the Exit on close does indeed exit the position shortly before the end of the session (based on the Exit on
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello ajsdnyyrr1234DDf,

          I have an example of preventing new entries after the exit on close and using the exit on close to close all positions before the end of a session you may find helpful.

          https://ninjatrader.com/support/foru...058#post516058
          Hello:

          Appreciate your response and thanks for the pointer on Exit on Close(). My issue is different. My strategy is not opening a position after I closed. Please review the logs and the code I posted below. The strategy did not trigger a new order, It was the CloseStragy() command looks like triggered a new order.

          I have an Exit() and CloseStragy() commands together in the code. The Exit() seems to be working fine and it closed the position. The logs show the Buy order (Buy 2000) to close the existing position was filled. Now somehow, I think it is the CloseStreagy() command detected that order did not complete successfully. It created a new order for Buy (2000) again.

          I don't see much documentation for the CloseStragy() command. I am thinking of removing it from the strategy. I suspect that is working to create the duplicate order.

          As far as your suggestion goes. I think it is a good one but I don't think I am running into that issue. My strategy would at least need five more bars to trigger a new order after an Exit().

          Comment


            #6
            Hello ajsdnyyrr1234DDf,

            Is your goal to have the strategy exit the position at the close of a trading session?

            I was suggesting you might use this as an example of how to exit using the exit on session close and ensure no new entries are placed.

            If not, can you clarify your goal?

            If the script is placing orders at unexpected times, I would recommend you debug the script by using prints to understand the behavior.

            Below is a public link to a post that demonstrates.


            Is the condition that places the order evaluating as true at the time the order is appearing?

            Please include your output and the prints so that we may assist with analyzing the output.

            If you are asking how to use a time filter to ensure that entries can only be placed during certain hours, please see the example in our help guide I am including below.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I am not using Exit on Close in my strategy. I don't believe Exit on Close is effective or FOREX as the session runs from Sunday to Friday and I do believe it is meaningless to exit a position if the market is still open. And Ninja Trader doesn't allow to create a single trading session for the time period Sunday 14:00 to Friday 14:00. So I don't use the Exit-on-Close feature.

              I am checking the day/week time in the strategy and it is Friday 13:30. I do an exit for all open positions and Disable Strategy.

              Now the problem I ran into is, My Exit() calls are working fine but after successful Exit and Disable Strategy, NinjaTrader placed a duplicate order to again Exit the position.

              I can upload the logs from the system.
              Let me know.

              Thanks,

              Comment


                #8
                Hello ajsdnyyrr1234DDf,

                Unforunately, the Log will not include any information about your conditions or the state of the variables being compared in the condition.

                Please include your output and the prints and I will be happy to assist with analyzing the output.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks. I will send you the outputs and the logs. The reason why I am suspecting the role of CloseStrategy() command in this behavior is, I have started using it only for a week. Without the CloseStrategy() command the Friday Exits were working fine. I cannot say if FOREX.com had any problems with executing orders at that time.

                  Comment


                    #10
                    Originally posted by ajsdnyyrr1234DDf View Post
                    Thanks. I will send you the outputs and the logs. The reason why I am suspecting the role of CloseStrategy() command in this behavior is, I have started using it only for a week. Without the CloseStrategy() command the Friday Exits were working fine. I cannot say if FOREX.com had any problems with executing orders at that time.
                    out of curiosity ...
                    Do you include the exception of sending orders during the course of Friday at your particular time in the logic of your script entries?

                    If you do not restrict the sending of entry orders when you send closing orders, you can create a loop.

                    Example...

                    if ("your entry conditions" && Time [0] .DayOfWeek == DayOfWeek.Friday && ToTime (Time [0]) < 133000 )
                    {
                    your entry orders
                    }

                    it's just an idea, surely the ninjatrader technicians can indicate better than me

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Mizzouman1, Today, 07:35 AM
                    3 responses
                    17 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by RubenCazorla, Today, 09:07 AM
                    2 responses
                    13 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by i019945nj, 12-14-2023, 06:41 AM
                    7 responses
                    82 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by timmbbo, 07-05-2023, 10:21 PM
                    4 responses
                    158 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by tkaboris, Today, 08:01 AM
                    1 response
                    8 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Working...
                    X