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

Exit Long/Enter Short or Exit Short/Enter Long on Same bar using "OnBarClose"

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

    Exit Long/Enter Short or Exit Short/Enter Long on Same bar using "OnBarClose"

    Imagine.....using any line you can think of......and the close above/below (say Moving average, Pivot line, Indicator zero line, ANY LINE, etc).

    I would like to ExitLong then EnterShort OR ExitShort then EnterLong......basically reverse the trade direction (trade the opposite signal) on the same bar with that close above or below a line. There is no Ninjatrader code for "ReverseDirection" in a Strategy.

    While it seems I can get the action to appear to work....the SetStopLoss and SetProfitTarget does not change to the new details.....and this is resulting in an exit of both trades all on the same bar as it appears the previous Stop or Target is being used to exit the new direction order.

    Is there any way to enter and exit AND set the new Stop and Target all on the cross/close using OnBarClose?

    If not, can you suggest the best way to do it?

    I could not find a previous post that best discussed this issue.

    #2
    Hello BigT4X,

    Thanks for your post.

    You could consider calling an Entry order method in the opposite direction to reverse positions from long to short or short to long. For example, if you are in a long position, you could call EnterShort() to exit the long position and enter a short position. Or, if you are in a short position, you could call EnterLong() to exit the short position and enter a long position.

    From the Managed Approach Order Methods help guide page: "Entry() methods will reverse the position automatically. For example if you are in a 1 contract long position and now call EnterShort() -> you will see 2 executions, one to close the prior long position and the other to get you into the desired 1 contract short position."

    The SampleMACrossOver strategy that comes default with NinjaTrader is a great example of reversing orders. Please open a New > NinjaScript Editor window, open the Strategies folder, and double-click on the SampleMACrossOver file to view the script's code.

    See this help guide page for more information about Managed Approach Order Methods: https://ninjatrader.com/support/help...d_approach.htm

    Let me know if I may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thanks Brandon for the Suggestion....that is easy to do.

      Will it then take the new Stop and Target levels? I mean I will try it to see....but is there anything I should consider for that portion of the code?

      Comment


        #4
        Hello BigT4X,

        Thanks for your note.

        I see from your initial post that you are using Set methods for your stop loss and profit target.

        Set methods will be applied to both long and short orders. You could consider providing a SignalName to your Entry order methods and using that signal name for the FromEntrySignal argument when calling your Set methods.

        See this reference sample that demonstrates using SignalName and FromEntrySignal to tie orders together: https://ninjatrader.com/support/help...a_position.htm

        Also, see this help guide documentation for more information: https://ninjatrader.com/support/help...m#EntryMethods

        Let me know if I may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          If I tie the orders together with a SignalName than the first suggestion (all in one order) would not be used and instead I would use ExitLong (with the signal name) and EnterShort (with its own Signal name).....correct?

          The Stops/Profit targets still not working with all in one order suggested first because the Stops/Targets being carried over from the previous Long to the new Short...............the goal here is to exit Long and enter Short at same price (except for any fast market movement) and then set new Stops and Targets on the new order.

          Comment


            #6
            Hello BigT4X,

            Thanks for your note.

            If you are using Set methods and call Entry methods in the opposite direction, the current position will be closed and a new order wil be placed in the opposite direction along with a stop and target.

            For example, say you are in a long position with a stop and target placed and you call EnterShort() to place an order in the opposite direction. The long position would be closed along with the stop and target and a short position would be placed with a stop and target for that position.

            See this demonstration video showing that when a crossover occurs we call EnterShort() and the long position is closed and a short position is opened:
            https://brandonh-ninjatrader.tinytak...NV8xOTgyNTY5MQ

            I also attached the script used to test this. The test script is a modified version of SampleMACrossOver that includes calling Set methods to place a stop and target.

            In regard to the question about SignalName and FromEntrySignal, you would provide a SignalName for your Entry order such as 'LongEntry'. Then, you would use 'LongEntry' for the FromEntrySignal argument when calling your Set method.

            For example, the syntax would be:

            SetProfitTarget(string fromEntrySignal, CalculationMode mode, double value)
            SetStopLoss(string fromEntrySignal, CalculationMode mode, double value, bool isSimulatedStop)

            EnterLong(int quantity, string signalName)
            EnterShort(i
            nt quantity, string signalName)

            And, the code might look something like this:

            SetProfitTarget("LongEntry", CalculationMode.Ticks, 10)
            SetStopLoss("LongEntry", CalculationMode.Ticks, 10, false)
            SetProfitTarget("ShortEntry", CalculationMode.Ticks, 10)
            SetStopLoss("ShortEntry", CalculationMode.Ticks, 10, false)

            EnterLong(1, "LongEntry")
            EnterShort(1, "ShortEntry")

            See this help guide page for more information about Managed Approach Order Entry methods: https://ninjatrader.com/support/help...d_approach.htm

            Let me know if I may assist further.
            Attached Files
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Brandon, I think you did not attach the right script as the one you sent has no Stops, Targets or Set Methods. In case you want to edit this for others.

              BUT I understand your suggestion and it works. I can now move on to Editing stops on various criteria.
              Last edited by BigT4X; 07-07-2022, 10:41 PM.

              Comment


                #8
                For the Benefit of others.....here is my code framework (with some obvious replacements that need to be filled in by you):

                protected override void OnBarUpdate()
                {
                if (CurrentBar < BarsRequiredToTrade)
                return;

                //Initial Stops and Targets
                SetStopLoss("Long", CalculationMode.Price, Your Stop here, true);
                SetProfitTarget("Long", CalculationMode.Price, Your Target here, true);
                SetStopLoss("Short", CalculationMode.Price, Your Stop here, true);
                SetProfitTarget("Short", CalculationMode.Price, Your Target here, true);

                SetStopLoss("LongReverse", CalculationMode.Price, Your Stop here, true);
                SetProfitTarget("LongReverse", CalculationMode.Price, Your Target here, true);
                SetStopLoss("ShortReverse", CalculationMode.Price, Your Stop here, true);
                SetProfitTarget("ShortReverse", CalculationMode.Price, Your Target here, true);

                // Entry Rules - Long and Short signals
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                if (YOUR logic to enter Long)
                {
                EnterLong(1, "Long");
                }

                if (YOUR logic to enter Short)
                {
                EnterShort(1, "Short");
                }
                }

                if (Position.MarketPosition == MarketPosition.Short)
                {
                if (YOUR logic to Reverse trade - to exit Short and go Long)
                {
                EnterLong(1, "LongReverse");
                }
                }
                if (Position.MarketPosition == MarketPosition.Long)
                {
                if (YOUR logic to Reverse trade - to exit Long and go Short)
                {
                EnterShort(1, "ShortReverse");
                }
                }
                }
                Last edited by BigT4X; 07-07-2022, 10:56 PM.

                Comment


                  #9
                  Hello BigT4X,

                  Thanks for your note.

                  "I think you did not attach the right script as the one you sent has no Stops, Targets or Set Methods."

                  Set methods are located in the OnStateChange() section of the example script attached in my previous email. See the attached screenshot.

                  Let me know if I may further assist.



                  Attached Files
                  Brandon H.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by aa731, Today, 02:54 AM
                  0 responses
                  4 views
                  0 likes
                  Last Post aa731
                  by aa731
                   
                  Started by thanajo, 05-04-2021, 02:11 AM
                  3 responses
                  470 views
                  0 likes
                  Last Post tradingnasdaqprueba  
                  Started by Christopher_R, Today, 12:29 AM
                  0 responses
                  10 views
                  0 likes
                  Last Post Christopher_R  
                  Started by sidlercom80, 10-28-2023, 08:49 AM
                  166 responses
                  2,237 views
                  0 likes
                  Last Post sidlercom80  
                  Started by thread, Yesterday, 11:58 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post thread
                  by thread
                   
                  Working...
                  X