Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

IOrder object problem

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

    IOrder object problem

    Hello there,

    I have a question about a problem i'm having with my automated strategy.
    I use IOrder objects to make sure my stops are moved to BE after the scalper target is hit. This is the way i programmed it now:

    Code:
    #region Variables
            // Wizard generated variables
            // User defined variables (add any user defined variables below)
            
            private IOrder LongEntryOrder = null;
            private IOrder ShortEntryOrder = null;
            
            private IOrder LongScalper = null;
            private IOrder LongTarget1 = null;
            private IOrder LongTarget2 = null;
            
            private IOrder ShortScalper = null;
            private IOrder ShortTarget1 = null;
            private IOrder ShortTarget2 = null;
            
            private IOrder LongStop = null;
            private IOrder ShortStop = null;
    Code:
            protected override void OnExecution (IExecution execution)
            {
                // On entry bar:
                if (LongEntryOrder != null && LongEntryOrder == execution.Order)
                {
                    LongStop = ExitLongStop(0, true, 99000, Low[LowestBar(Low, 96)] - (ATR(7)[0] * 2), "StoppedOutLong", "");
                }
                
                if (ShortEntryOrder != null && ShortEntryOrder == execution.Order)
                {
                    ShortStop = ExitShortStop(0, true, 99000, High[HighestBar(High, 96)] + (ATR(7)[0] * 2), "StoppedOutShort", "");
                }
                
    
    
    
                // On Scalper bar:
                if (LongScalper != null && LongScalper == execution.Order)
                {
                    LongStop = ExitLongStop(0, true, 66000, Position.AvgPrice + (1 * TickSize), "BreakEvenLong", "");
                }
                
                if (ShortScalper != null && ShortScalper == execution.Order)
                {
                    ShortStop = ExitShortStop(0, true, 66000, Position.AvgPrice - (1 * TickSize), "BreakEvenShort", "");
                }
    So the problem is: At this moment when I enter a trade a stop loss is set like the code says in the "//On entry bar" section. But when my scalper target is hit, the strategy places a new order at the BE price.
    But i just want it to move the origional stop loss order to BE and change the quantity.
    What am I doing wrong here? Because i refer to the same IOrder object to change this order in "//On scalper bar" section.

    Many thanks in advance.

    Grtz, Dennis

    #2
    Hello Dennis,

    Thank you for your post.

    I am not certain I fully understand your inquiry. Can you details the unexpected behavior you are seeing and what you expect to see?

    Comment


      #3
      Thanks for the quick reply.

      When I enter a position my stoploss is set 2 ATR below the lowest low of the last 96 bars, as you can see in the code. It also sets a scalper limit order for my first target.

      When the scalper limit order is hit, the strategy places a new stoploss order at BE for the remaining part of my position. So now I have two stoploss orders pending. The one at BE and the origional one.

      In stead I want it to move the origional stoploss to BE and change the quantity according to my position that is left after the scalper is hit.

      Thanks.
      Last edited by Aqua-Life; 01-26-2015, 12:29 PM.

      Comment


        #4
        Originally posted by Aqua-Life View Post
        Thanks for the quick reply.

        When I enter a position my stoploss is set 2 ATR below the lowest low of the last 96 bars, as you can see in the code. It also sets a scalper limit order for my first target.

        When the scalper limit order is hit, the strategy places a new stoploss order at BE for the remaining part of my position. So now I have two stoploss orders pending. The one at BE and the origional one.

        In stead I want it to move the origional stoploss to BE and change the quantity according to my position that is left after the scalper is hit.

        Thanks.
        If you want to move the stop use the same name for the order. Otherwise you are making a new order; in which case just Cancel the old order.
        Last edited by koganam; 01-26-2015, 03:19 PM.

        Comment


          #5
          How do I use the same name then? Because I do refer to the same IOrder object in the origional placement (LongStop = ....) and also when the Scalper order is hit. Because then my code says again LongStop = ....

          I understand this isn't the right way? But what is?
          Last edited by Aqua-Life; 01-26-2015, 01:00 PM.

          Comment


            #6
            Originally posted by Aqua-Life View Post
            How do I use the same name then? Because I do refer to the same IOrder object in the origional placement (LongStop = ....) and also when the Scalper order is hit. Because then my code says again LongStop = ....

            I understand this isn't the right way? But what is?
            When you made the order you named it "StoppedOutShort". If you make a new order and call it "BreakEvenLong", you are not modifying "StoppedOutShort"; you are creating a new order, even if you choose to assign said new order to an existing IOrder object.

            The Name property of the IOrder object is a different entity from the identifier you use for the IOrder. IOW, the IOrder that you gave an identity (we loosely say "named") "LongStop" actually has a property LongStop.Name that is acquired from the name of the signal, not of the IOrder itself. An unfortunate side effect of a clash between descriptive language and OOP principles.

            Comment


              #7
              So if I understand correctly, it has to be like this? :

              Code:
              protected override void OnExecution (IExecution execution)
                      {
                          // On entry bar:
                          if (LongEntryOrder != null && LongEntryOrder == execution.Order)
                          {
                              StoppedOutLong = ExitLongStop(0, true, 99000, Low[LowestBar(Low, 96)] - (ATR(7)[0] * 2), "StoppedOutLong", "");
                          }
                          
                          if (ShortEntryOrder != null && ShortEntryOrder == execution.Order)
                          {
                              StoppedOutShort = ExitShortStop(0, true, 99000, High[HighestBar(High, 96)] + (ATR(7)[0] * 2), "StoppedOutShort", "");
                          }
                          
              
              
              
                          // On Scalper bar:
                          if (LongScalper != null && LongScalper == execution.Order)
                          {
                              StoppedOutLong = ExitLongStop(0, true, 66000, Position.AvgPrice + (1 * TickSize), "BreakEvenLong", "");
                          }
                          
                          if (ShortScalper != null && ShortScalper == execution.Order)
                          {
                              StoppedOutShort = ExitShortStop(0, true, 66000, Position.AvgPrice - (1 * TickSize), "BreakEvenShort", "");
                          }

              Comment


                #8
                Hello Aqua-Life,

                Thank you for your response.

                koganam is referring to the signalName:
                Code:
                ExitShortStop(0, true, 99000, High[HighestBar(High, 96)] + (ATR(7)[0] * 2), [B]"StoppedOutShort"[/B], "");
                May I review the Entries you are using?

                Comment


                  #9
                  Originally posted by Aqua-Life View Post
                  So if I understand correctly, it has to be like this? :

                  Code:
                  protected override void OnExecution (IExecution execution)
                          {
                              // On entry bar:
                              if (LongEntryOrder != null && LongEntryOrder == execution.Order)
                              {
                                  StoppedOutLong = ExitLongStop(0, true, 99000, Low[LowestBar(Low, 96)] - (ATR(7)[0] * 2), "StoppedOutLong", "");
                              }
                              
                              if (ShortEntryOrder != null && ShortEntryOrder == execution.Order)
                              {
                                  StoppedOutShort = ExitShortStop(0, true, 99000, High[HighestBar(High, 96)] + (ATR(7)[0] * 2), "StoppedOutShort", "");
                              }
                              
                  
                  
                  
                              // On Scalper bar:
                              if (LongScalper != null && LongScalper == execution.Order)
                              {
                                  StoppedOutLong = ExitLongStop(0, true, 66000, Position.AvgPrice + (1 * TickSize), "BreakEvenLong", "");
                              }
                              
                              if (ShortScalper != null && ShortScalper == execution.Order)
                              {
                                  StoppedOutShort = ExitShortStop(0, true, 66000, Position.AvgPrice - (1 * TickSize), "BreakEvenShort", "");
                              }
                  Not quite. I guess that my explanation was not very clear: I kept using name in different contexts. To simplify what I meant, here are examples from your first post.

                  You used this to make the initial stop:
                  Code:
                  [COLOR="RoyalBlue"]StoppedOutLong[/COLOR] = ExitLongStop(0, true, 99000, Low[LowestBar(Low, 96)] - (ATR(7)[0] * 2), [B]"StoppedOutLong"[/B], "");
                  The IOder would be "StoppedOutLong", and the IOrder.Name would come from the SignalName, "StoppedOutLong". The text is the same, but their meaning and purpose are not.

                  So, when you move to breakeven, you would use:
                  Code:
                  [COLOR="RoyalBlue"]StoppedOutLong[/COLOR] = ExitLongStop(0, true, 66000, Position.AvgPrice + (1 * TickSize), [B]"StoppedOutLong"[/B], "");
                  Same SignalName, as in the bolded text in both cases.

                  Strange as it may seem, this is actually consistent. One can make an order without assigning it directly to an IOrder, so using the SignalName in all cases to identify the order, as its Name property would be the universal way to do it. Track what I mean by using the colors of the text.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by TradeForge, 04-19-2024, 02:09 AM
                  2 responses
                  28 views
                  0 likes
                  Last Post TradeForge  
                  Started by aprilfool, 12-03-2022, 03:01 PM
                  3 responses
                  327 views
                  0 likes
                  Last Post NinjaTrader_Adrian  
                  Started by giulyko00, Today, 12:03 PM
                  1 response
                  5 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by f.saeidi, Today, 12:14 PM
                  1 response
                  4 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by AnnBarnes, Today, 12:17 PM
                  1 response
                  2 views
                  0 likes
                  Last Post NinjaTrader_Zachary  
                  Working...
                  X