Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Translating a v6.0 strategy to 7
Collapse
X
-
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?
-
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
-
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
-
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
-
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
-
Oops, simple misnaming of orders there........
I was trying to avoid conditions that involved signal tracking based on eariler advice in the thread.( Using Position.MarketPosition was Josh's suggestion.)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"); }
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
-
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
-
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
-
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
-
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 usein my exits, but now realize I cant.Code:this.Position.AvgPrice
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
-
Betrand, It triggers the exit logic. I hadin the wrong place, but after moving it it prints with the avg Fill price when a sell order gets filled.Code:this.Print("Sell Order Filled" + Position.AvgPrice);
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.
The Take Profit limit order seems to be returning the Avg Price fine, Just the Stop Loss Order.Buy stop or buy stop limit orders can't be placed below the market. affected Order: BuyToCover 1 Stop @ 0
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
-
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
-
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
This is the stop loss order Im trying to submitCode:this.Print("Sell Order Filled" + exitSellStopLossOrder );
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
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
370 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
109 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
574 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
576 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|
Comment