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
How to set a stop that moves up after hitting a certain unrealized tick gain
Collapse
X
-
How to set a stop that moves up after hitting a certain unrealized tick gain
Hello how would I set up a stop that moves when lets say the unrealized tick gain on the trade is 12 it moves or places a new stop at breakeven or slightly above, I would have a hard stop setup when the trade is entered. ThanksTags: None
-
Hello Prestonf,
Thanks for your post.
You can use exit order methods and add custom logic in your strategy to accomplish this goal.
To get the strategy's UnrealizedPnL value in ticks, you could use the Position.GetUnrealizedProfitLoss() with the PerformanceUnit parameter set to PerformanceUnit.Ticks.
See this help guide page about the Position.GetUnrealizedProfitLoss() method and sample code: https://ninjatrader.com/support/help...profitloss.htm
My colleague Chelsea has created educational examples demonstrating breakeven and trailing stop in a NinjaScript strategy here:
https://ninjatrader.com/support/foru...der#post806596
You could view the example scripts and click the 'View code' button in the Strategy Builder to see the generated code for accomplishing this.
Please let us know if we may assist further.Brandon H.NinjaTrader Customer Service
-
Hello Prestonf,
Set 1 resets the TriggerState to 0 when flat to reset for a new entry.
Set 2 is the entry conditions. Any conditions for entry would go here, would submit the entry, and would set the TriggerState to 1 to let Set 3 know the entry has been submitted and is waiting to fill.
Set 3 when the entry is filled and the position has changed, the stop price is set to where the exit stop will be initially submitted, as well as the price where the breakeven will occur. The TriggerState is set to 2 so that we know the stop has initially be set and the trigger for breakeven has been set.
Set 4 is the breakeven condition. If the TriggerState is 2, the stop price has already been set and the trigger price has been set. Once the market price reaches the trigger, this set will update the stop price to the breakeven price.
Set 5 submits the stop to the set stop price after the trigger state is 2, meaning the entry has filled.Chelsea B.NinjaTrader Customer Service
Comment
-
Hello Prestonf,
The concepts would stay the same, the StopPrice and TriggerPrice would be different.
The StopPrice would be n ticks above the Strategy -> Average position price, so the InitialStopDistance would be a positive number instead of a negative number.
The TriggerPrice would be n ticks below the position average price, so the BreakEvenTrigger would be a negative number instead of a positive number.Chelsea B.NinjaTrader Customer Service
Comment
-
Hello Prestonf,
Yes, you would need conditions for the long position placing exit stop orders below the market, and separate conditions for the short position placing exit stop orders above the market.
SetStopLoss() and SetTrailStop() cannot use dynamic values in the Strategy Builder and should not be used at the same time as exit orders. You would need to choose between exit orders or set methods.
Chelsea B.NinjaTrader Customer Service
Comment
-
Hello Prestonf,
Thanks for your notes.
As my colleague Chelsea stated, you would need to use Exit methods or Set methods in your script since you cannot use not both. This means that if you choose to use Exit methods, you would need to use Exit methods, such as ExitLongLimit(), to place profit target orders.
Managed Approach Order Methods: https://ninjatrader.com/support/help...d_approach.htm
That said, I do not see anything specific standing out in the code you shared that would prevent orders from being submitted.
If the expected trade(s) are not appearing, this would indicate that the condition to place the order is not evaluating as true or the order is being ignored for other reasons.
To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.
In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.
Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).
Below is a link to a forum post that demonstrates how to use prints to understand behavior.
https://ninjatrader.com/support/foru...121#post791121
Let us know if we may assist further.Brandon H.NinjaTrader Customer Service
Comment
-
I watched Chelsea's video about prints and it was very helpful regarding prints.
I'm still having trouble as where to put the prints and what I can actually print now.
I can only print the code under else if (State == State.DataLoaded)?
It must be something in the Sets but I don't know where to look, it should be making trades.
I just copied over the conditions that were working for me before and just added the Sets from Chelsea's breakeven example.
Really struggling to figure out what's wrong and how to fix it.
I checked other strategies to see if it was just a setting i had on the chart but they seem to work.
Appreciate your help and patience with me thank you.
Comment
-
Hell Prestonf,
Thanks for your post.
Prints could be added within OnStateChange(), OnBarUpdate(), and other methods to find out how a strategy is behaving and placing orders.
If you are not seeing trades appear then it is likely that the conditions to place the order is not becoming true.
You may create a new Set and move the new Set between Set 1 and 2. Then, you could add prints to the new Set that prints out Stochastics1.D[0], BarCounter1[0], BarCounterStart, BarCounterStop, BarsSinceExecution, and BarAfterExit.
This will print those values to a New > NinjaScript Output window outside of the condition to place the short order so that you may see how those values are evaluating.
For example, if you see that Stochastics1.D[0] is not printing a value greater than or equal to 90, the order would not be placed. If BarCounter1[0] is not greater than or equal to BarCounterStart and less than or equal to BarCounterStop, the condition to place the order is not true so the order would not be placed.
Please let me know if you need further assistance creating a certain prints to debug your script.Brandon H.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Max238, Today, 01:28 AM
|
4 responses
35 views
0 likes
|
Last Post
by Max238
Today, 07:49 AM
|
||
Started by r68cervera, Today, 05:29 AM
|
1 response
7 views
0 likes
|
Last Post Today, 07:47 AM | ||
Started by geddyisodin, Today, 05:20 AM
|
1 response
11 views
0 likes
|
Last Post Today, 07:38 AM | ||
Started by timko, Today, 06:45 AM
|
2 responses
14 views
0 likes
|
Last Post Today, 07:30 AM | ||
Started by habeebft, Today, 07:27 AM
|
0 responses
7 views
0 likes
|
Last Post
by habeebft
Today, 07:27 AM
|
Comment