Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

10 x 100 shares (instead of 1 x 1000 shares)

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

    10 x 100 shares (instead of 1 x 1000 shares)

    I’m writing an automated share trading strategy.

    When my entry signal is triggered, rather than for example place a single entry buy order for 1000 shares, I want to place 10 buy orders for 100 shares each (with OCO profit target and stop loss orders).

    Is there a sample code fragment, or shared “open source” NinjaScript file, or related forum thread with a previous discussion of this sort of stuff, that will give me a head start on how best to go about this in the most "elegant" fashion?

    As always, thanks in advance.

    #2
    AnotherTrader,

    All you need to do is call EnterLong() ten times and you will have your 10 scale-in entries. SetStopLoss() and SetProfitTarget() are already OCOed.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks, Josh

      I should have been clearer in my opening post (my fault), but I was hoping there was a more elegant way than this.

      I need (for several reasons) to use the OnOrderUpdate() and OnExecution() methods for handling stops and profit targets in the strategy.

      Therefore (unless I have my thinking muddled ... which is quite possible!), in addition to calling EnterLong() ten times (and EnterShort() ten times for entries in the other direction), and giving each of the 2 x 10 entries a signal name, I effectively also need to replicate the logic of the SampleOnOrderUpdate code 2 x 10 times (i.e. once for each of the 2 x 10 entry signal names, to deal with the stop loss and profit target orders for each entry signal name).

      This just seems like a sledge-hammer to crack a walnut ... and I was hoping that someone else might have thought through all this before, and that I could pick up their lead from somewhere on the forum ...


      If not, I'll just have to roll up my sleeves and do my own digging ...

      Comment


        #4
        AnotherTrader,

        I see. If you want to do OnOrderUpdate()/OnExecution() then you will unfortunately need to do it 20x. You don't necessarily need to duplicate the logic 20x though. You could just use some or statements in your if-conditions and just create the logic once.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          OK, perhaps this is what I haven’t grasped correctly...

          Say I have 2 entry signals. In OnBarUpdate(), I will need something like...
          Code:
          [FONT=Courier New][COLOR=black][COLOR=black][FONT=Courier New]entryOrder1 = EnterLong(0, 100, [/FONT][/COLOR][COLOR=maroon][FONT=Courier New]"Entry Signal 1"[/FONT][/COLOR][COLOR=black][FONT=Courier New]);[/FONT][/COLOR]
          [COLOR=black][FONT=Courier New]entryOrder2 = EnterLong(0, 100, [/FONT][/COLOR][COLOR=maroon][FONT=Courier New]"Entry Signal 2"[/FONT][/COLOR][COLOR=black][FONT=Courier New]);[/FONT][/COLOR]
          [/COLOR][/FONT]
          Doesn’t that mean that within OnOrderUpdate(), I will need something like ...
          Code:
          [FONT=Courier New][COLOR=blue][COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New] (entryOrder1 != [/FONT][/COLOR][COLOR=blue][FONT=Courier New]null[/FONT][/COLOR][COLOR=black][FONT=Courier New] && entryOrder1.Token == order.Token)[/FONT][/COLOR]
          [COLOR=black][FONT=Courier New]etc, etc [/FONT][/COLOR]
          [/COLOR][/FONT]
          and then later I will also need …
          Code:
           
           [COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New] (entryOrder2 != [/FONT][/COLOR][COLOR=blue][FONT=Courier New]null[/FONT][/COLOR][COLOR=black][FONT=Courier New] && entryOrder2.Token == order.Token)[/FONT][/COLOR]
          [COLOR=black][FONT=Courier New]etc, etc [/FONT][/COLOR]
          i.e. everything has to be repeated for each of the separate entry orders, and then cascading down through the stopOrder and targetOrder code, too? And the same within OnExecution()?

          Or is there something elegant that I am missing that would allow me to cut right down on the lines coded?

          Thanks.

          Comment


            #6
            AnotherTrader,

            You don't need to duplicate the code for each and every IOrder. Just use || in your if statement.

            if ((entryOrder1 != null && entryOrder1.Token == order.Token) || (entryOrder2 != null && entryOrder2.Token == order.Token) || (....etc...))

            You would create as many || as needed, but the actual logic part does not need to be 20x-ed.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              OK, thanks. Yes, I can see how that's going to help.

              Comment


                #8
                Update

                Today I ran a version of this automated strategy “live” for the first time.

                As a reminder of what I’m trying to do, when my entry signal is triggered, I want to place say 10 buy orders for 100 shares each (with OCO profit target and stop loss orders), rather than place a single entry buy order for say 1000 shares.

                Today, the entry orders were placed and filled as expected. But when the target orders were submitted, these results were not as I expected.

                To explain what happened, I am going to simplify what I was trying to do, by considering a hypothetical case where my strategy was trying, say, to submit just 2 orders for 100 shares each.

                So, in a simplified form, here's what happened:

                1) Both entry orders for 100 shares each were filled successfully
                2) However, after submitting the first target order for 100 shares, the strategy then attempted to modify the quantity for this same first target order to 200 shares (when it should have just been 100 shares. I don't know why it tried to modify the order quantity to 200 shares!).
                3) When the strategy then submitted the second target order for 100 shares, it must have noticed that the strategy had a long position of 200 shares, and now two target orders for a total of 300 shares (the first for 200 shares, and then the second for a further 100 shares).
                4) On account of the imbalance between the long position (200 shares) and the target orders (300 shares), the strategy cancelled the second target order, cancelled all orders, closed all positions, and terminated itself ...

                So, I need to try to understand why the strategy tried to modify the quantity of the first target order up to 200 shares (rather than leave it at 100, as I had expected and intended)?

                The coding is based on the SampleOnOrderUpdate code fragment from the forums. I am guessing the problem lies in the OnExecution section of my code, where I define the target orders; and probably with how I am defining the order quantity (i.e. using "execution.Order.Filled") within those instructions?
                Please can you have a look and let me know what you think?
                Many thanks!
                = = = = = = = = = = = =
                Here is a simplified version of the key parts of my code (I apologise that I haven't managed to figure out how to format the code properly in a forum post!):



                Initialize
                Code:
                [LEFT]protected override void Initialize()
                {
                CalculateOnBarClose = true; 
                TimeInForce = Cbi.TimeInForce.Day;
                EntriesPerDirection = 1; 
                EntryHandling = EntryHandling.UniqueEntries;
                }[/LEFT]

                OnBarUpdate
                Code:
                [LEFT]protected override void OnBarUpdate()
                {
                // Place entries ... 
                if(myCondition)
                { [/LEFT]
                for(int x = 1; x < 3; x++)
                [LEFT]{[/LEFT]
                if(x==1)
                [LEFT]{
                entryOrder01 = EnterLong(0, 100, "Long Entry #01");
                }[/LEFT]
                if(x==2)
                [LEFT]{
                entryOrder02 = EnterLong(0, 100, "Long Entry #02");
                }[/LEFT]
                }
                }
                }

                OnOrderUpdate
                Code:
                protected override void OnOrderUpdate(IOrder order)
                {
                // Handle entry orders
                if (entryOrder01 != null && entryOrder01.Token == order.Token)
                [LEFT]{ 
                if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                {
                entryOrder01 = null;
                }[/LEFT]
                }
                if (entryOrder02 != null && entryOrder02.Token == order.Token)
                { 
                if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                {
                entryOrder02 = null;
                }
                }
                }
                OnExecution (I am guessing that the problem resides here?)
                Code:
                protected override void OnExecution(IExecution execution)
                {
                // Resets the corresponding entryOrder object to null after the order has been filled or partially filled
                 
                if (entryOrder01 != null && entryOrder01.Token == execution.Order.Token)
                {
                if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                {
                if (execution.Order.OrderState != OrderState.PartFilled)
                {
                entryOrder01  = null;
                }
                if(Positions[0].MarketPosition == MarketPosition.Long)
                { 
                targetOrder01 = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + (profitTarget*0.01) , "Long Entry #01 - Target", "Long Entry #01"); 
                }  
                }
                }
                if (entryOrder02 != null && entryOrder02.Token == execution.Order.Token)
                {
                if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                {
                if (execution.Order.OrderState != OrderState.PartFilled)
                {
                entryOrder02  = null;
                }
                 
                [LEFT]if(Positions[0].MarketPosition == MarketPosition.Long)
                
                { 
                [LEFT]targetOrder02 = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + (profitTargetSSO*0.01) , "Long SSO Entry #02 - Target", "Long SSO Entry #02"); 
                }  
                }
                }[/LEFT]
                [/LEFT]
                
                 
                 
                }
                Thanks in advance for any help!
                Last edited by AnotherTrader; 04-16-2010, 04:17 PM.

                Comment


                  #9
                  So to clarify, you want separate stop/targets per entry. If this is the case, in the strategy selection dialogue window please be sure to select PerEntryExecution as your Stop&Target Submission option.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Many thanks, Josh.

                    Yes, I think it's as simple as that.

                    I had the setting as ByStrategyPosition!

                    Damn!

                    PS. Can I set this programatically, within the script? Or set it as the default somewhere for the "Stop&Target Submission" option?

                    Comment


                      #11
                      StopTargetHandling = StopTargetHandling.PerEntryExecution;

                      Please note that this is not an officially supported property and is subject to change.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        (Rome was not built in a day)

                        Here’s the next hurdle encountered...

                        I submit three unique orders immediately one after the other in the code. Each has an OCO target and stop loss associated with it.
                        All three orders fill OK, but target and stop loss are submitted successfully only for the first two; target and stop loss for order #3 gets lost somewhere ...

                        Why would this be?

                        I have e-mailed the trace log to [email protected] so that you can follow the history of the following three orders to see how #3 gets treated differently from #1 and #2.

                        Order #1: Order='a9a8dcb453a34e60af9179e6e6519e03'
                        Order #2: Order='bdf54428c9a941cda89a4428f0ef7bc1’
                        Order #3: Order='b8679bfc73d040f8a7c8cd46cdc05f35’

                        To make the point that I am doing exactly the same thing for orders #1, #2 and #3 (so why should order #3 be treated differently?), I could post key parts of my code that show this...

                        Let me know if you want me to do this.

                        Comment


                          #13
                          AnotherTrader,

                          It would be necessary to turn on TraceOrders = true for you to see why/if orders were ignored during submission as regular trace logs would not hold information to such reasons. You would see the output in the Output Window.
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            Thanks, Josh

                            Can I write my Output Window to a file?

                            I just tried to cut (RH mouse button) the relevant bits of the trace log output from the Output window, but ended up "clearing" instead! Damn! So I can't get further with this today, unfortunately ...

                            = = = = = = = =
                            OK, I found elsewhere on the forum how to save Output window output ...

                            - please press CTRL A to select all text
                            - press CTRL C to paste
                            - open any editor of your liking
                            - press CTRL V to copy

                            This will have to wait now until next time it happens ...
                            Last edited by AnotherTrader; 04-22-2010, 01:57 PM.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by ETFVoyageur, Today, 02:08 AM
                            0 responses
                            5 views
                            0 likes
                            Last Post ETFVoyageur  
                            Started by kujista, 04-22-2024, 07:46 AM
                            3 responses
                            12 views
                            0 likes
                            Last Post kujista
                            by kujista
                             
                            Started by kujista, 04-23-2024, 06:23 AM
                            7 responses
                            57 views
                            0 likes
                            Last Post kujista
                            by kujista
                             
                            Started by SentientDavid, Today, 01:34 AM
                            0 responses
                            7 views
                            0 likes
                            Last Post SentientDavid  
                            Started by MrForgetful, Today, 01:28 AM
                            0 responses
                            6 views
                            0 likes
                            Last Post MrForgetful  
                            Working...
                            X