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

Arrays & stop losses

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

    Arrays & stop losses

    I have my profit target properly set in my trading strategy. There are 2 orders that get entered and when the first one hits it's profit target, then the second one is managed by trailing the stop loss.

    In the example that I have been studying (http://www.ninjatrader.com/support/f...ead.php?t=5790) it shows that Arrays are being used. Should I be using Arrays in order to keep the stop in place until the initial profit target is hit and then actively manage the stop on every bar?

    #2
    Hello jg123,

    You do not need to use arrays.

    The sample here adds items to the array. Removes items from the array. But it never actually uses any of the items in the array.

    Your script though, does not need arrays to hold the stop and target IOrder objects.

    I think this following sample would be a better sample for you.
    http://www.ninjatrader.com/support/f...ead.php?t=3222
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you.

      I have looked at this sample several times.

      The issue that I run into is that I can not move the stop until the first entry has hit its profit target.

      Does this allow for that criteria?

      This sample also has everything in OnBarUpdate rather than OnExecution. So can I use this information to make the changes as soon as the profit target Limit order is executed?
      Last edited by jg123; 04-02-2014, 05:26 PM.

      Comment


        #4
        Here is the best code that I am able to come up with for my stop movement.

        Code:
        		protected override void OnExecution(IExecution execution)
        		{
        			TargetPriceLong = Position.AvgPrice + (longSignalHigh - longSignalLow)*TargetPerc;
        			TargetPriceShort = Position.AvgPrice - (shortSignalHigh - shortSignalLow)*TargetPerc;
        			
        			//Setting Long Profit Target
        			if (execution.Order != null
        				&& execution.Order.OrderState == OrderState.Filled)
        				
        				LongLimit = ExitLongLimit(0, true,DefaultQuantity,TargetPriceLong,"LongTarget","LongEntryA");
        			
        			//Setting Short Profit Target
        			if (execution.Order != null
        				&& execution.Order.OrderState == OrderState.Filled)
        				
        				ShortLimit = ExitShortLimit(0, true, DefaultQuantity,TargetPriceShort,"ShortTarget","ShortEntryA");
        		
        			
        			//set the adjusted stop loss
        			if (LongLimit == OrderState.Filled)
        				ExitLongStop (0,true,DefaultQuantity,AvgPrice-(TargetPriceLong-AvgPrice),"EntryBModStop","LongEntryB");
        
        		}
        This has errors. Here is what I hoped would happen in English and then I will explain the errors. I believe that it is these same errors that ultimately lead me to the place of attempting to use Arrays.

        Two orders placed - first called Entry A and the second is Entry B

        Entry A should hit profit target

        After Entry A hits profit target then the stop for Entry B is moved from its original place to:

        Entry Price - ((Entry A Profit Target) - (Entry Price)) = Entry B Stop Price

        In theory, this would mean that if Entry B is stopped out after Entry A hits its profit target, then the entire trade closes at break even.

        Here are the errors that I am running into:

        AvgPrice does not exist in the current context
        Operator '==' cannot be applied to operands of type 'NinjaTrader.Cbi.IOrder' and 'NinjaTrader.Cbi.OrderState'

        Exact Questions:
        Earlier in the code I used AvgPrice for determining the Profit Target level and have had no problems with it. Why is it not allowing me to use AvgPrice in this calculation and what should I be using in order to tell NinjaTrader where to move the stop to if my calculation involves my entry price?

        What should I be using to tell NinjaTrader to only move the stop if the Entry A profit target has been filled?

        thank you for your help

        Comment


          #5
          Hi jg123,

          Yes, modifying an Exit order for entry B when the entry A's target has filled in OnExecution is fine to do.

          There are two issues with the AvgPrice. One is that OnExecution runs before OnPositionUpdate. This means that the position has not been updated when OnExecution is run and Position.AvgPrice may not be accurate.

          The order of the methods triggered when an order fills is OnOrderUpdate > OnExecution > OnPositionUpdate.

          Instead of using Position.AvgPrice, use the execution object's AvgFillPrice. Also, you are setting this no matter what order is being executed because there is no if branching command around this to prevent the profit target and stop loss from also setting this TargetPriceLong variable. You may want to be sure that it is entry A or B and not a stop or profit target.

          To check that the execution is entry a:
          if (EntryA != null && EntryA == execution.Order)
          TargetPriceLong = execution.Order.AvgFillPrice + (longSignalHigh - longSignalLow)*TargetPerc;

          OR to check that its either entry A or entry B:
          if ((EntryA != null && EntryA == execution.Order) || (EntryB != null && EntryB == execution.Order))
          TargetPriceLong = execution.Order.AvgFillPrice + (longSignalHigh - longSignalLow)*TargetPerc;

          You could actually move this code to the line just before the entry target is placed, within the same if statement, and it wouldn't need an if statement of its own.


          The other issue with AvgPrice in the last exit.
          ExitLongStop (0,true,DefaultQuantity,AvgPrice-(TargetPriceLong-AvgPrice),"EntryBModStop","LongEntryB");

          You have not created an AvgPrice variable by itself. You are also not trying to get the AvgPrice as a property from a position like Position.AvgPrice or from the execution object like execution.Order.AvgFillPrice. This variable does not exist.


          Regarding the logic of your code, your if conditions do not seem to be checking what you would like. You check for execution.Order != null. Because OnExecution runs when an order fills, the execution.Order object will never be null. However, I think you mean to be checking that a particular order is being executed.

          For example if this is the profit target for entry A, we should be checking that entry A is the current object (this is pretending your entry order is saved to a variable named EntryA):

          if (EntryA != null && EntryA == execution.Order && execution.Order.OrderState == OrderState.Filled)
          {
          EntryALongTarget = ExitLongLimit(0, true,DefaultQuantity,TargetPriceLong,"LongTarget", "LongEntryA");
          }

          Also, one of your if statements is different than the others. This particular one is the one causing the error. You are comparing an order object with a property of an order object. These are not like objects. I think this is where you are trying to detect that the target of entry A has filled.
          if (LongLimit == OrderState.Filled)
          ExitLongStop (0,true,DefaultQuantity,AvgPrice-(TargetPriceLong-AvgPrice),"EntryBModStop","LongEntryB");

          Should be something like:
          if (EntryALongTarget != null && EntryALongTarget == execution.Order && execution.Order.OrderState == OrderState.Filled)
          {
          EntryBLongStop = ExitLongStop (0, true, DefaultQuantity,execution.Order.AvgFillPrice-(TargetPriceLong-execution.AvgFillPrice), "EntryBStop", "LongEntryB");
          }

          And if this is being used to modify the stop for entry B, you'll want to make sure that you are setting an initial stop for entry B.
          if (EntryB != null && EntryB == execution.Order && execution.Order.OrderState == OrderState.Filled)
          {
          EntryBLongStop = ExitLongStop (0,true,DefaultQuantity,<your initial stop price here>,"EntryBStop","LongEntryB");
          }

          Below is a link to the help guide on OnExecution().
          http://www.ninjatrader.com/support/h...nexecution.htm

          As well as a link to IOrder objects.
          http://www.ninjatrader.com/support/h...nt7/iorder.htm
          Last edited by NinjaTrader_ChelseaB; 04-03-2014, 09:25 AM.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            This has been an incredibly helpful post. Thank you very much.

            There is plenty that I am working through with it in order to put it all properly in my code.

            With that, I do have one immediate question that will hopefully be a quick fix.

            Working on setting the initial stop loss in light of what you have said here

            here is what you said:

            And if this is being used to modify the stop for entry B, you'll want to make sure that you are setting an initial stop for entry B.
            if (EntryB != null && EntryB == execution.Order && execution.Order.OrderState == OrderState.Filled)
            {
            EntryBLongStop = ExitLongStop (0,true,DefaultQuantity,<your initial stop price here>,"EntryBStop","LongEntryB");
            }
            Here is what I have:

            Code:
            			// Set Initial Stop Long Stop Loss
            			if(LongEntryB != null 
            				&& LongEntryB == execution.Order 
            				&& execution.Order.OrderState == OrderState.Filled)
            			{
            				EntryBLongStop = ExitLongStop(0,true,DefaultQuantity,SMA(sMAPeriod)[0]-(signalHigh - signalLow)*StopPerc,"EntryStopB","LongEntryB");
            			}
            I am getting the following error with that:

            Cannot implicitly convert NinjaTrader.Cbi.IOrder to 'double'
            I have a feeling that this is an incredibly easy fix, but I can't seem to figure it out. Could you help guide me in the right direction?

            thanks!

            Comment


              #7
              Hi jg123,

              This code seems ok. I won't know for sure because I am not seeing how LongEntryB, EntryBLongStop, sMAPeriod, signalHigh, or signalLow are being set.

              May I have the .cs file of your script?

              You can send this to me privately if you like by emailing to support [at] ninjatrader [dot] com with Attn: Chelsea written in the email, along with a link to this forum thread.

              Or if you don't mind sharing, attach the file to your next post.
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by ageeholdings, Today, 07:43 AM
              0 responses
              2 views
              0 likes
              Last Post ageeholdings  
              Started by pibrew, Today, 06:37 AM
              0 responses
              4 views
              0 likes
              Last Post pibrew
              by pibrew
               
              Started by rbeckmann05, Yesterday, 06:48 PM
              1 response
              14 views
              0 likes
              Last Post bltdavid  
              Started by llanqui, Today, 03:53 AM
              0 responses
              6 views
              0 likes
              Last Post llanqui
              by llanqui
               
              Started by burtoninlondon, Today, 12:38 AM
              0 responses
              12 views
              0 likes
              Last Post burtoninlondon  
              Working...
              X