Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple Positions within a Strategy Question

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

    Multiple Positions within a Strategy Question

    Hi All,

    I have been working on a strategy (Strategy Builder) and last night I discovered that when my strategy places a second or third order, my stops, and targets kind of reset and get calculated as if it was one position on its average. I hope I am explaining this correctly.

    My question is, what is the easiest way to handle this? I would like to have a stop loss, target, and breakeven for each order and not re-calculate that based on the average price overall once I continue to buy additional positions.

    Let me know if I will need to explain further.

    #2
    Hello felixolmo,

    Thanks for your question.

    If you want to have a strategy that has separate stop/target levels for each entered order, each entry should have a unique signal name, and you should have unique exits tied to each signal name.

    An Auto Breakeven behavior requires modifying the stop loss to move it to the break even point. In the Strategy Builder, this is possible using the Conditions and Actions page to create your own stop loss behaviors with Exit methods.

    I have attached some examples showing how to create separate entries to have separate exits, and to also demonstrate moving to breakeven.

    We look forward to assisting.
    Attached Files

    Comment


      #3
      Thank you for the quick response Jim.

      I have used some of those to develop my own strategy. In my case right now and based on these examples also, I am still having a combined position average. So, when I exit a position, it will exit to X amount of ticks, based on the user input, but it will be based on the whole position average not on the original buying price.

      Don't know if I am explaining myself correctly?

      Comment


        #4
        Thanks for your reply and clarifying felixolmo,

        The average entry price of individual orders would not be available with the Strategy Builder.

        You could consider triggering market orders to enter, and then you can save the current market price to a variable, and instead of using Position.AveragePrice, you can use your variable that holds the Close price of when the entry order was submitted. This wouldn't be exact, but would be as close as you can get within the confines of the Strategy Builder.

        With unlocked code, you can track Order objects, and use information from Orders to offset prices for your target and stop.

        An example that uses the AverageFillPrice of an Order for submission of target and stop can be found below.

        Comment


          #5
          Thanks for the information Jim.

          Do you happen to have or guide me to a thread about the unlocked Code to track Order Objects and how to use them? I think if I can track the order, even if I have multiple orders I will be able to achieve this.

          I am currently using Unlocked Code, I consider myself about intermediate code "Expert".

          Comment


            #6
            Hello felixolmo,

            Order objects would be assigned from the order method's return value or from OnORderUpdate when the order object is updating there. When the Order object is not null, it can be considered active/pending. In OnOrderUpdate, we would also check if the order is Rejected or Cancellled, which would mean the order is not longer active, and then we would set the Order object back to null.

            SampleOnOrderUpdate holds on to Order objects in OnExecutionUpdate and used information from the Order object for target and stop submission, and after the target and stop are submitted, the Order object is set to null.

            When using Order objects, they must be transitioned to realtime orders in State.Realtime with GetRealtimeOrder.

            SampleOnOrderUpdate would be a good script to see how Order objects can be used .

            If you just need to get the individual fill prices of each order, you may not have to consider implementing Order object assignment and OnOrderUpdate, and you could just use OnExecutionUpdate and execution.Order.AverageFillPrice or execution.Price and submit the target and stop with Exit orders in OnExecutionUpdate. This may be easier to tackle before getting further into using Order objects.

            (Note that if there is a partial fill for an order that has a quantity greater than 1, there will be multiple executions for that order, and execution.Price will not equal execution.Order.AverageFillPrice.)

            Comment


              #7
              Hi Jim, I might be getting stuck in some of the advancements I am doing. Can you review this code and see if you can help?

              Explanation of what I am trying to do:
              - I analyze some values and create a signal out of those values in OnBarUpdate()
              - I have two options - one is called "1" and the other "1B" - is just two different signals in the same strategy, but once the signal comes on either one then it follows to get an Entry Order and place its own Exit Orders. This can happen with both signals without thinking if we have other positions open.
              - Then, I enter a long position. and set up the targets and stops thru OnOrderUpdate and OnExecutionUpdate
              - Once that is completed. I review the current position to generate a "Breakeven" Order after it goes above a certain value. I do this thru OnBarUpdate utilizing a for to be able to attach the replacement order to the new breakeven order.

              When I build and test thru Strategy Builder with a single signal. Everything runs as expected. I can see multiple orders at the same time and targets, stop losses and breakeven are as expected. However, when I go and add the second signal and create the same code on OnOrderUpdate and OnExecutionUpdate for the second signal, my strategy builder does not run. I am thinking that I do not have the proper logical step to separate or identify which portion of the code will need to be run based on the entry signal? And an additional issue that I might not be seen in the strategy builder but could be a problem in real-time is that I believe that by using the "FOR" I might be trying to create exit orders for positions that have already been closed.

              As a note, to build the close I have I utilized several of the examples that I have seen thru the forum by NinjaTrader experts, like you. But obviously, those are more for single entries not necessarily for multiple positions at the same time.
              - Average Price List - I use this to save the Fill Price and assign names to my orders. That way I can go back and adjust the StopLoss to Breakeven or some additional profit. This is the one I was referring to above that I use in OnBarUpdate.
              - SampleOnOrderUpdate - Which I use to create the initial target, stop loss and break even.

              I have, indeed, look at a lot of other codes, but these two and probably the ones the most helpful for what I am trying to do so far.

              I have attached the cod. In the end, what I am trying to do is have several signals in one single strategy. Basically like 2 or 3 per day. So, I might have an issue separating them logically within the same strategy.

              Again, when I do not update the OnOrderUpdate with 2 or more signals everything runs fine. when I add the setup for the second or more signals then is not running. Plus, I believe that by using the FOR I might be creating additional entries to closed positions. This issue doesn't not flag when I run Strategy Analyzer but I was curious if that could become a problem in real-time. For both testings, everything compiles with no errors.

              Let me know if you can provide some guidance here. and some example codes on how I can achieve this.
              Attached Files

              Comment


                #8
                Hello felixolmo,

                We wont be able to provide in depth testing to check your work. The script also cannot be compiled in its current state. Another suggestion will be to use consistent formatting in your script so it is easier to follow.

                Checking your work to confirm your logic is written appropriately would be done by using debugging prints to confirm appropriate parts of your logic are being reached, and if they are not being reached, to use prints to see how the conditions controlling those actions are being evaluated.

                Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

                Getting deeper into order object handling may be too ambitious for the time being. I suggest continuing with the path forward with just using OnExecutionUpdate to submit target and stop for the separate entries. Once this is familiar, and you want to venture into order object handling, I think that would be a good time to look into that functionality.

                To help you further, I have attached an example script that just uses OnExecutionUpdate and OnBarUpdate to have target and stop at different levels and to also move the stop losses to their breakeven points.
                Attached Files

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                55 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                132 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                73 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                45 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                49 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X