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!
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
NinjaTrader
Set Stop Loss with limit price
Collapse
X
-
Josh
Should I place this second stop OnBarUpdate or OnOrderUpdate???
How to test it with simulator?? It is very diffucoult to manage gaps in simulator???
Czarek
Originally posted by NinjaTrader_Josh View PostYou can check price like how you normally would do it. Close[0]. You can check your IOrder object for the stop and the limit prices of your orders.
Ex:
Code:if (myStopOrder != null && Close[0] > myStopOrder.StopPrice)
Comment
-
Czarek,
1. Your code will not submit new prices simply because your condition only evaluates to true once. You place your order and it is kept alive. You never submit any amending orders after that because your entryOrderLong1 is not null anymore. If you call EnterLongLimit() again it will submit whatever price adjustment you want.
2. Same reason as #1. Your condition of entryOrderLong1 being null will only evaluate to true once. In this case it evaluates EnterLongLimit() and submits it on bar 1. On bar 2, entryOrderLong1 != null so you do not resubmit EnterLongLimit(). Because you do not resubmit the order is cancelled on bar 2. Somewhere else in your logic you have reset the entryOrderLong1 back to null so now on bar 3 you submit a new order.
3. This scenario you remove the requirement of entryOrderLong1 == null and this allows you to resubmit the order on every single bar. Becase you are resubmitting on every single bar you are not auto cancelling and so the order is kept alive.
You can place your second stop anywhere in your code that you want it to evaluate. You most likely want it in OnBarUpdate() since that is where you will receive updates to your price. There is no "gapping" in the simulator. It is just simulated data that populates NinjaTrader based on the start sim price you set in Instrument Manager.Josh P.NinjaTrader Customer Service
Comment
-
Josh.
I will leave sample 3. It works as my desire.
The new problem appeared when I want to move stop loss to breakeven.
As I mentioned before my strategy based on your sample http://www.ninjatrader-support2.com/...ead.php?t=3222
Strategy has 4 entries per direction. All stop loss and target limit shoud be executed together.
Stop loss is based on first entry price.
That mean if :
first entry price is 1000 stop loss 50 so stop loss price is 950, and no matter the price of second, third and fourth entriers stop loss for them is also 950.
Then if the price will go in good direction I move stop loss to the first entry breakeven (first entry price). This is no problem if I move all 4 entries one time. If at first 4 orders are placed than moved to breakeven without problems.
The problem appeared if for example first entry is executed and than close [0] price is greater than my moving breakeven level. In this case second entry order stop loss is placed with first entry price -950 and and even do not amended on bar close.To resolve this problem would place second order at onec with breakeven price.
I made variable to check where the first entry stop loss is (in original position or breakeven) before I place second, third and fourth order.
I attache code below.
Unfortunately this code does not work, stop loss are placed always in original (950).
I can not find the reason why my code does not work and what should I change??
Can you help me to find the place I made mistake in logic????
Code:if (entryOrderLong2 != null && entryOrderLong2.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 (lookBreakeven == false ) { // Stop-Loss order second stopOrderLong2 = ExitLongStopLimit(0, true, execution.Order.Filled, firstEntryLong - averageTicks*stop* TickSize, firstEntryLong - averageTicks*stop* TickSize, "MyStopLong2", "MyEntryLong2"); // Target-Limit the second targetOrderLong2 = ExitLongLimit(0, true, execution.Order.Filled, firstEntryLong + averageTicks*limit* TickSize, "MyTargetLong2", "MyEntryLong2"); } // Resets the entryOrder object to null after the order has been filled or partially filled if (execution.Order.OrderState != OrderState.PartFilled) { entryOrderLong2 = null; } if (lookBreakeven == true ) { // Stop-Loss order second stopOrderLong2 = ExitLongStopLimit(0, true, execution.Order.Filled, firstEntryLong + averageTicks*breakeventicks* TickSize, firstEntryLong+ averageTicks*breakeventicks* TickSize, "MyStopLong2", "MyEntryLong2"); // Target-Limit the second targetOrderLong2 = ExitLongLimit(0, true, execution.Order.Filled, firstEntryLong + averageTicks*limit* TickSize, "MyTargetLong2", "MyEntryLong2"); } // Resets the entryOrder object to null after the order has been filled or partially filled if (execution.Order.OrderState != OrderState.PartFilled) { entryOrderLong2 = null; } } }
Czarek.
Comment
-
Not sure I follow. Use Print() to figure out which of your stop loss orders is actually being triggered. Then you will need to print out the values of each of your variables to determine what price it is submitting at. If your lookBreakeven bool is working properly you should be submitting the second stop loss. Remember though if you already submitted your second entry order before your lookBreakeven bool has been set to true the second entry order could very well be filled before lookBreakeven is switched true and that is why you get your 950 stop loss despite the higher breakeven price. If that happens you don't reenter into OnOrderUpdate() for entryOrderLong2 again so of course no ammending orders will be sent.Josh P.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Noerclou, Today, 04:55 AM
|
0 responses
2 views
0 likes
|
Last Post
by Noerclou
Today, 04:55 AM
|
||
Started by llanqui, Yesterday, 09:59 AM
|
2 responses
17 views
0 likes
|
Last Post
by llanqui
Today, 04:00 AM
|
||
Started by ThoriSten, Today, 03:56 AM
|
0 responses
6 views
0 likes
|
Last Post
by ThoriSten
Today, 03:56 AM
|
||
Started by PhillT, 04-19-2024, 02:16 PM
|
3 responses
23 views
0 likes
|
Last Post
by mangel2000
Today, 03:04 AM
|
||
Started by TraderBCL, Today, 02:37 AM
|
0 responses
4 views
0 likes
|
Last Post
by TraderBCL
Today, 02:37 AM
|
Comment