Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Translating a v6.0 strategy to 7

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

    #16
    Thanks, my entry orders enter and update as they should. Now How can I differentiate between needing a buy limit to exit a short position or a sell limit to exit a long position with using a reference to the open position?

    Comment


      #17
      Unfortunately I am not sure what exactly you are asking. If you want to check your current position you can just use Position.MarketPosition and Position.Quantity to see what you have in terms of direction and size. Whether you want to use a limit order or not is up to you depending on what you want to do.
      Josh P.NinjaTrader Customer Service

      Comment


        #18
        If I can use Position.MarketPosition then thats fine. I just assumed (i guess incorrectly) that I wouldnt be able to use these using the unmanaged setting based on comments earlier in the thread. Thanks for clearing that up though! Makes it much easier.

        Comment


          #19
          Sorry Josh, going to have to take that last comment back. That doesnt help.
          I added the following and came up with 2 different problems.

          1. It should put in a take profit limit order AND Stop loss order, but I am only getting the take profit order. It is ignoring the stop loss order.

          2. It continues to add to take profit order every tick, after a few seconds I havea bout 30 orders there.

          Anyone have any comments on these 2 issues?

          Thanks,

          Here is the code I am using for the exit orders.

          Code:
          else
             {
              if (this.Position.MarketPosition == MarketPosition.Long)
              {
                exitBuyStopLossOrder = SubmitOrder(0,OrderAction.Sell,OrderType.Limit,1,this.Position.AvgPrice + takeProfitTicks * TickSize,0,"","Long Profit");
                exitBuyProfitOrder = SubmitOrder(0,OrderAction.Sell,OrderType.Stop,1,this.Position.AvgPrice - stopLossTicks * TickSize,0,"","Long StopLoss");
           
              
              }
              
              
              else
               
               if (this.Position.MarketPosition == MarketPosition.Short)
               
               {
                exitSellStopLossOrder = SubmitOrder(0,OrderAction.BuyToCover,OrderType.Limit,1,this.Position.AvgPrice - takeProfitTicks * TickSize,0,"","Short Profit");
                exitSellProfitOrder = SubmitOrder(0,OrderAction.BuyToCover,OrderType.Stop,1,this.Position.AvgPrice + stopLossTicks * TickSize,0,"","Short StopLoss");
               }
             }

          Comment


            #20
            maninjapan, I'm not sure why you're stop loss is a limit order and your profit target a stop - there's no signal tracking in the unmanaged mode thus it would continue submitting the orders as you're conditions triggers (which it does, you're still in a Market Position) - you should work with IOrders to only submit new orders if there are no working ones already for your IOrder objects.

            Comment


              #21
              Oops, simple misnaming of orders there........
              Code:
              else
               
                   if (this.Position.MarketPosition == MarketPosition.Short)
               
                   {
                    exitSellProfitOrder = SubmitOrder(0,OrderAction.BuyToCover,OrderType.Limit,1,this.Position.AvgPrice - takeProfitTicks * TickSize,0,"","Short Profit");
                    exitSellStopLossOrder = SubmitOrder(0,OrderAction.BuyToCover,OrderType.Stop,1,this.Position.AvgPrice + stopLossTicks * TickSize,0,"","Short StopLoss");
                   }
              I was trying to avoid conditions that involved signal tracking based on eariler advice in the thread.( Using Position.MarketPosition was Josh's suggestion.)
              Which is why I was trying to ask, how can I word the IF statement so that once I get filled Long or Short, the relevant take profit and Stop Loss orders are submitted without referring to the position.

              Comment


                #22
                I see, thanks - the signal tracking comments relate to specific entry and exit signal naming / tagging, you can work with Josh's suggestion to refer to the overall strategy position of your unmanaged NinjaScript strategy.

                If you want to send the exits directly after the fill, you should do this in the OnExecution() then.

                Comment


                  #23
                  Sorry Bertrand that went a little over my head. How can I word the IF statement so that it knows if it needs to send BuyToCover exits or Sell exits?
                  Also I dont see any OnExecution() section in my original code, is this something I need to add myself?

                  Thanks

                  Comment


                    #24
                    OnExecution() is one of the more advanced NinjaScript methods which you would need to add to your existing strategy code, you can check for the fill states of your entry order here and then send the relevant exit brackets as needed -

                    The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()

                    Comment


                      #25
                      Thanks Bertrand, that was a Huge help. I was able to use that as a template and transfer the logic from my strategy across. Still not quite there though. I was trying to use
                      Code:
                      this.Position.AvgPrice
                      in my exits, but now realize I cant.
                      How can I refer to the entry price with Unmanaged = true; ?
                      Here is the exit order as I tried to use it.

                      Code:
                      exitBuyProfitOrder = SubmitOrder(0,OrderAction.Sell,OrderType.Limit,1,this.Position.AvgPrice + takeProfitTicks * TickSize,0,"","Long Profit");

                      Comment


                        #26
                        You should be able to use the Position.AvgPrice going unmanaged, but you can also directly use the reported price your IOrder object -



                        What happens with your Exit call? Any errors? Is it triggered at all by your code?

                        Thanks

                        Comment


                          #27
                          Betrand, It triggers the exit logic. I had
                          Code:
                          this.Print("Sell Order Filled" + Position.AvgPrice);
                          in the wrong place, but after moving it it prints with the avg Fill price when a sell order gets filled.

                          I tested the Take Profit order and Stop loss order seperately. The take profit order enters at the correct place. However, when I test the Stop Loss order by itself, I get an order rejected error and it shuts down.
                          This is the error message that I recieve.
                          Buy stop or buy stop limit orders can't be placed below the market. affected Order: BuyToCover 1 Stop @ 0
                          The Take Profit limit order seems to be returning the Avg Price fine, Just the Stop Loss Order.



                          Here is the code as I have it, just testing the Stop Loss Order. Any glaring errors?
                          Code:
                           protected override void OnExecution(IExecution execution)
                                  {
                             
                              if (entrySellOrder != null && entrySellOrder.Token == execution.Order.Token)
                             {
                              this.Print("Sell Order Filled" + Position.AvgPrice);
                              if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                              {
                               //exitSellProfitOrder = SubmitOrder(0,OrderAction.BuyToCover,OrderType.Limit,1,this.Position.AvgPrice - takeProfitTicks * TickSize,0,"","Short Profit");
                               exitSellStopLossOrder = SubmitOrder(0,OrderAction.BuyToCover,OrderType.Stop,1,this.Position.AvgPrice + stopLossTicks * TickSize,0,"","Short StopLoss");
                               // Resets the entryOrder object to null after the order has been filled or partially filled
                               if (execution.Order.OrderState != OrderState.PartFilled)
                               {
                                entrySellOrder  = null;
                               }
                              }
                             
                             else

                          Comment


                            #28
                            I also tried using a StopLimit order instead of Stop and recieved the following

                            Buy stop or buy stop limit orders can't be placed below the market. affected Order: BuyToCover 1 Stop @ 0 x 1165.3

                            Comment


                              #29
                              This order rejection for Stop Market and Stop Limit orders is expected if you attempt to submit them below the markets (for buys) or above the market (for sells) - you would need to add a check that the order is submitted at a valid price taking into consideration the inside market.

                              Comment


                                #30
                                Yep, figured that out from what the message said.
                                But I tried average price + 50 ticks and still recieved the error message.


                                Buy stop or buy stop limit orders can't be placed below the market. affected Order: BuyToCover 1 Stop @ 0


                                what does the end of that error message mean?
                                affected order: BuyToStop 1 stop @ 0

                                Also, how can I reword the following print statement to show the price for the stop loss order

                                Code:
                                this.Print("Sell Order Filled" + exitSellStopLossOrder );
                                This is the stop loss order Im trying to submit

                                Code:
                                exitSellStopLossOrder = SubmitOrder(0,OrderAction.BuyToCover,OrderType.Stop,1,this.Position.AvgPrice + 50 * TickSize,0,"","Short StopLoss");
                                Last edited by maninjapan; 07-28-2010, 07:38 AM.

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                649 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                370 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                109 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                574 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                576 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X