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

i need help with executions and position average price.

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

    i need help with executions and position average price.






    people with nt,


    i have been working with the samples for onorder and onexecution update methods that nt makes available.


    i have read the help guides and the notes on the samples and i still would like a brief explanation of the different commands used inside the onorder and onexecution update sections.





    now, i have been able to get strategies with time limits and stop loss orders to work acceptably, so i'm now trying to get strategies that also include secondary entries to work and i have some questions.



    - i'm not sure if the platform is using Position.AveragePrice as i understand it. my stop loss orders look like this:


    pullbackstop = ExitShortStopMarket(1, true, execution.Order.Filled, Position.AveragePrice + ( Convert.ToInt32(Pullbackticks) * TickSize ), "shstlopuen01", "shpuen01");



    what i have in mind is: 1) execute initial entry, 2) set stop loss order for initial entry at average price plus several ticks. 3) if a secondary entry is triggered and executed, 4) set stop loss orders for all contracts (initial and secondary entries) at average price plus ticks.


    i understand that Position.AveragePrice after the first execution for 2 contracts will be different than Position.AveragePrice after a second entry for 1 additional contract has been executed. ¿is this correct? ¿will the nt platform adequately understand the values for Position.AveragePrice to be different after every execution? ¿is it ok to use Position.AveragePrice or is there code that would work better for what i have in mind?




    - and somewhat related to this, i would like to know what kind of code should i use in the onbarupdate section of an strategy so that nt will adjust a stop loss order but only after a particular (secondary) execution has happened.


    if ( Position.MarketPosition == MarketPosition.Short && Closes[1][0] < Position.AveragePrice - ( Convert.ToInt32(Profitticks) * TickSize ) )
    {
    if (shstloorenor != null && shstloorenor.StopPrice > Position.AveragePrice)
    {
    shstloorenor = ExitShortStopMarket(1, true, shstloorenor.Quantity, Position.AveragePrice - ( Convert.ToInt32(Breakeventicks) * TickSize ), "shstlooren01", "shoren01");
    }
    }


    i tried to work with a very basic breakeven stop sample that nt makes available and thought that using only a condition for an order like shstloorenor != null would be enough but the strategy will not work in that case.


    if the secondary entry in my strategy is called shpuenor, ¿what kind of code should use in the onbarupdate to make nt adjust the stop loss order for the initial entry but only after the secondary entry has been executed? ¿can nt detect every execution that happens as some kind of event? ¿will executions be not null (!=) as long as its corresponding entry has not been closed? i'm interested in understanding how will the nt platform process orders and executions so that i can try to create code that will work as intended.




    very well, thanks, regards.

    #2
    Hello rtwave,

    Thanks for your post.

    The Position object represents the Strategy Position. This consists of all entries that the strategy has currently entered. This is updated based on Execution events. (Updated as soon as OnExecutionUpdate is processed.) As you enter more contracts in the strategy, Position.Quantity will increase.

    what i have in mind is: 1) execute initial entry, 2) set stop loss order for initial entry at average price plus several ticks. 3) if a secondary entry is triggered and executed, 4) set stop loss orders for all contracts (initial and secondary entries) at average price plus ticks.
    My insight on this is:
    1. Submit your first entry order from OnBarUpdate (based on incoming market data/signals)
    2. Check for the execution of your entry order in OnExecutionUpdate to place initial stop and target
    3. Submit the second entry order from OnBarUpdate (based on incoming market data/signals)
    4. Check for the execution of the second entry in OnExecutionUpdate. Once found:
      1. Submit new target and stop for the new entry with the stop loss at Position.AveragePrice (with offset you like)
      2. Move the stop the stop loss of the initial entry to Position.AveragePrice (with offset you like)
    The above would not move the stop loss in OnBarUpdate because you would be using the execution of the second entry to trigger the move to breakeven.

    You should be managing a total of 6 Order objects. 2 entry Order objects, 2 Order objects for the stops, and 2 for the targets. Order object handling is demonstrated in the SampleOnOrderUpdate strategy.

    Orders objects should be assigned in OnOrderUpdate, and then set to null when the order is no longer active and when you no longer need it. For example, you would check for order cancels or rejects in OnOrderUpdate to reset that Order object to null. If you are using the Order object later, for example, using an entry Order object in OnExecutionUpdate to submit target/stop for that entry, you can set the entry Order object to null in OnExecutionUpdate since you will not need it after that.

    SampleOnOrderUpdate is linked below for the thread's reference.

    https://ninjatrader.com/support/help...and_onexec.htm

    You would not need to worry about executions being null and the points above is what I would recommend focusing on to attain your goal.

    JimNinjaTrader Customer Service

    Comment


      #3




      NinjaTrader_Jim, people with nt,




      there's one other capability that i could use help with.


      i have now been trying to incorporate breakeven stops to strategies i have developed which include two separate entries.


      and the thing is that i would like to have two different versions for the breakeven stops. if only an ordinary entry has been executed and price has been made to move in my favor, that would be perfect for me and i would just like to use a breakeventicks01 value. however, if a secondary pullback entry was also executed, the position being held is now larger and risk is greater so i would want to use a larger breakeventicks02 value in that case.



      that is why i asked for more information regarding orders and executions.


      this below is the breakeven section of code inside one of the onorderupdate samples that nt makes available.




      if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AveragePrice + (7 * (TickSize / 2)))
      {

      if (stopOrder != null && stopOrder.StopPrice < Position.AveragePrice)
      {

      stopOrder = ExitLongStopMarket(0, true, stopOrder.Quantity, Position.AveragePrice, "MyStop", "MyEntry");
      }
      }


      ¿how could i create differentiated breakeven stop orders? ¿is there any mechanism by which the nt platform can be aware of whether just one single entry or multiple entries have been executed and are now being held? ¿is there any check that nt can perform for just entry01 having been executed and now being held versus entry01 and entry02 having been executed and now being held?



      very well, thanks, regards.

      Comment


        #4
        Hello rtwave,

        Position.AveragePrice will reflect the average entry price of the entire strategy position. Essentially, the average of all entry order fill prices for the active position.

        If you want to differentiate average entry prices per entry order, this would be done by checking Order.AverageFillPrice once the entry order's OrderState property has reach OrderState.Filled in OnOrderUpdate.

        If you are modelling logic like SampleOnOrderUpdate (where the entry order object is set to null after stop and target are submitted in OnExecutionUpdate,) you could consider the following:
        1. In OnExecutionUpdate when entryOrder1 fills, assign entryOrder1.AverageFillPrice to a private double variable in your strategy, let's just call it "EntryPriceForEntry1"
        2. In OnExecutionUpdate, when the stop and target Order objects for entryOrder1 are set to null (when either the stop or target is filled) reset your private "EntryPriceForEntry1" to a value like 0 so we know we are not holing onto old values.
        3. When you want to trigger the breakeven just for the position opened by entryOrder1, use "EntryPriceForEntry1" instead of Position.AveragePrice, because this will be the average of that specific entry and not the full position.
        ^ This assumes that entryOrder1 reflects the first of multiple entries, and there are separate stops and targets submitted for additional entries.

        Let us know if you have any questions.
        JimNinjaTrader Customer Service

        Comment


          #5


          NinjaTrader_Jim, people with nt,



          i have been thinking about differentiated break even stop orders depending on the number of entries - (partial) positions a strategy is currently holding.


          ¿how can i get the nt platform to check how many contracts it is currently holding?


          in the case of some pullback strategies i have been working on, this would not be the most elegant solution, but i think the number of contracts would work to create differentiated break even stop orders. if number of contracts <= 2 and ticks in favor > breakeventicks01 then move stop loss order to break even. if number of contracts >= 3 and ticks in favor > breakeventicks02 then move stop loss order to break even.



          if it was possible to check whether only entry01 is active and being held or both entry01 and entry02 are active and being held that would be preferable, but if the nt platform can't check these conditions, i would just have to use number of contracts for this objective.


          very well, regards.

          Comment


            #6
            Hello rtwave,

            ¿how can i get the nt platform to check how many contracts it is currently holding?
            Position.Quantity will reflect the number of contracts held by the strategy.

            If you want to break that down to individual entry's you could track order objects and check the Order.Filled property of the order object. (This can be saved to a private variable if the entry order is completely filled and you want to null the order object.)

            if it was possible to check whether only entry01 is active and being held or both entry01 and entry02 are active and being held that would be preferable, but if the nt platform can't check these conditions, i would just have to use number of contracts for this objective.
            When we use Order objects, we would set them to null when the order is filled, cancelled, or rejected. If the order is not null, it would be active.

            To specifically see if the position of Entry1 is open or Entry2 is open, you would need to use logic to identify that. For example, in OnExecutionUpdate, we would submit target and stop orders when the associated entry fills. If the quantity of the first target/stop matches the quantity of the first entry order, this would show that the first entry is in position.
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cmv727, Today, 05:38 AM
            0 responses
            10 views
            0 likes
            Last Post cmv727
            by cmv727
             
            Started by AlessioCianini12, Today, 01:38 AM
            0 responses
            33 views
            0 likes
            Last Post AlessioCianini12  
            Started by nandhumca, Yesterday, 11:54 PM
            0 responses
            29 views
            0 likes
            Last Post nandhumca  
            Started by nandhumca, Yesterday, 11:51 PM
            0 responses
            28 views
            0 likes
            Last Post nandhumca  
            Started by ezrollin, Yesterday, 11:48 PM
            2 responses
            31 views
            0 likes
            Last Post ezrollin  
            Working...
            X