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

Help to use the stop signal to open the opposite position

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

    Help to use the stop signal to open the opposite position

    Hi guys, please help, I want to put a limit order opposite to the position at which the stop triggered, and essential, limit order must be placed at the original entry point.

    I found on the forum a similar theme, where the author uses - if (stopOrder != null && stopOrder == execution.Order) but nothing happened((

    below threw the code for the test

    Help, guys
    Attached Files

    #2
    Hello Kovalev,

    If you are certain that you want to reverse the position and you do not want to flatten the position, you have two options.

    You can choose to not use an exit order and instead place a limit entry order in the opposite direction. As long as no other orders are working this would not be rejected.

    The other option is to use an exit order, detect when the exit has filled, and submit the new entry after the exit for the previous position has filled.
    This is similar to the code you have with your post in the way orders are submitted in OnExecution after the entry order is filled.

    However, there are syntax errors in the script you have attached.

    On line 87:
    Code:
    if (Position.MarketPosition == MarketPosition.Long)[B];[/B]
    On line 101:
    Code:
    if (Position.MarketPosition == MarketPosition.Long)[B];[/B]
    On line 110:
    Code:
    if (execution.Order.OrderState != OrderState.PartFilled)[B];[/B]
    A semicolon is used to terminate a command. If a semi-colon is added at the end of a branching command (an if statement) there will be no action block for that branching command.
    (Meaning the code after the command will always be executed because if statements should not be followed by a semicolon)

    Below is a publicly available link to a 3rd party education site on branching commands.
    Encode branching logic with if, else-if and else. Evaluate conditions to true or false.


    Also, below is another publicly available link to a 3rd party educational site on if statements. Look for the heading 'The Phantom Semi-Colon'.
    http://www.glennstevenson.com/csharp...2/LessonB.html

    In the script you have attached, when the entry order fills, an exit limit and exit stop limit are placed.
    In this way, when one of these exit orders fills in OnExecution you can trigger the entry of a new order in the opposite direction.
    Last edited by NinjaTrader_ChelseaB; 02-05-2017, 06:58 PM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      ChelseaB, many thanks that responded

      and can be done in the following way - in OnOrderUpdate add

      if ((order.Name == "MyLongStop") && (order.OrderState == OrderState.Filled)){ entryOrder = EnterLongLimit......... etc......???????

      Comment


        #4
        Hello Kovalev,
        Originally posted by Kovalev View Post
        and can be done in the following way - in OnOrderUpdate add

        if ((order.Name == "MyLongStop") && (order.OrderState == OrderState.Filled)){ entryOrder = EnterLongLimit......... etc......???????
        I think you are attempting to show code for what I have described in my previous post.
        "The other option is to use an exit order, detect when the exit has filled, and submit the new entry after the exit for the previous position has filled."

        Yes, this is something you could do. I would recommend you do this in OnExecution().


        With this style, the new entry order would not be placed until the exit order for the previous position has filled.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Good day, Chelsea, please tell me where I'm wrong, the script opens a new position without waiting for the closing of the old... In the conditions I use "entryOrder == null", but something goes wrong.
          Attached Files

          Comment


            #6
            Hello Kovalev,

            Thank you for your response.

            In your code the entryOrder is null after it is filled, this allows the entry to occur on the opposite side if the condition is true. I would recommend utilizing the Position.MarketPosition to prevent additional entry in OnBarUpdate(). For example:
            Code:
                    protected override void OnBarUpdate()
                    {
            			// Submit an entry limit order if we currently don't have an entry order open
            			if (entryOrder == null && Close[0] > Open[0]
                            [B]&& Position.MarketPosition == MarketPosition.Flat)[/B]
            			{
            				/* The entryOrder object will take on a unique ID from our EnterLong()
            				that we can use later for order identification purposes in the OnOrderUpdate() method. */
            				entryOrder = EnterLong(1, "Long");
            			}
            			if (entryOrder == null && Close[0] < Open[0]
                            [B]&& Position.MarketPosition == MarketPosition.Flat)[/B]
            			{
            				/* The entryOrder object will take on a unique ID from our EnterLong()
            				that we can use later for order identification purposes in the OnOrderUpdate() method. */
            				entryOrder = EnterShort(1, "Short");
            			}
                    }

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Perr0Grande, Today, 08:16 PM
            0 responses
            2 views
            0 likes
            Last Post Perr0Grande  
            Started by elderan, Today, 08:03 PM
            0 responses
            5 views
            0 likes
            Last Post elderan
            by elderan
             
            Started by algospoke, Today, 06:40 PM
            0 responses
            10 views
            0 likes
            Last Post algospoke  
            Started by maybeimnotrader, Today, 05:46 PM
            0 responses
            12 views
            0 likes
            Last Post maybeimnotrader  
            Started by quantismo, Today, 05:13 PM
            0 responses
            7 views
            0 likes
            Last Post quantismo  
            Working...
            X