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
Setting a Trailing Stop?
Collapse
X
-
Hello timmbbo,
Thanks for your question.
I believe the answer is related to the timing of the asynchronous events which I mention in your other thread here: http://ninjatrader.com/support/forum...ad.php?t=95788
Leave a comment:
-
Paul, I have a follow up on this. My code is:
EnterLong(1);
Print(Position.MarketPosition);
Print(MarketPosition.Long);
if (Position.MarketPosition == MarketPosition.Long) {
....
The output is:
Flat
Long
So, my If clause never hits, even though I'm successfully in a Long position. I can see the long position executed on the chart, so I know it's there. Suggestions? Thanks
Leave a comment:
-
Hello timmbbo,
Thanks for your reply.
To have a code section only run once per bar, you can use the system bool FirstTickOfBar
if (FirstTickOfBar)
{
// code processed here only once per bar
}
//code here is executed on every tick
Reference: http://ninjatrader.com/support/helpG...ttickofbar.htm
For further study on the subject: http://ninjatrader.com/support/forum...ad.php?t=19387
Leave a comment:
-
Paul, thanks - is helpful. Setting CalculateOnBarClose to false, as you recommend, then has other consequences for my code. I do have a section of code that I want executed upon bar close. So, it then appears I need one section of code executed upon bar close, and another section of code executed upon tick.
How do I accommodate executing code both upon Bar Close and Upon Tick? Thanks
Leave a comment:
-
Hello timmbbo,
Thanks for your post.
Correct, in the managed approach you cannot use the SetTrailStop and SetStopLoss methods on the same order.
What you could do in this case is to exclusively use SetStopLoss(), initially setting it to 5 ticks and then later by comparing the current price to entry price adjusting the value of the SetStopLoss through your code. Note that you will need to use CalculateOnBarClose = false so that you are working with the current price
Once in a position, you can obtain the entry price from Position.AvgPrice.
For example if you were long:
if (Position.MarketPosition == MarketPosition.Long)
{
if (Close[0] >= Position.AvgPrice + 2 * TickSize)
{
SetStopLoss(CalculationMode.Price, Close[0] - 3 * TickSize);// set 3 ticks behind current price
}
}
Note I have shown a very simple starting example, for it to work effectively you would need to use bools so that the trail stop does not go backwards when price reverses and so that the setstop is continually adjusted up as price continues up in the matter you desire.
Leave a comment:
-
Setting a Trailing Stop?
1)
Hi there, how do I set an initial stop, and a trailing stop? Say I want to enter an order with a 5 tick stop loss. Then, for every two ticks in profit, I want to set a trailing stop 3 ticks behind my current position. I would think the code would go something like?
SetStopLoss(CalculationMode.Ticks, 5);
SetTrailStop("", CalculationMode.Ticks, 3, false);
However, I can use both the SetStopLoss, and the SetTrailStop together. How to do? ThanksTags: None
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by alanlopen, 03-18-2023, 03:05 AM
|
4 responses
28 views
0 likes
|
Last Post
![]()
by alanlopen
Today, 07:18 PM
|
||
Started by paypachaysa, 03-18-2023, 09:22 AM
|
4 responses
35 views
0 likes
|
Last Post
![]()
by paypachaysa
Today, 07:17 PM
|
||
Started by alanlopen, Yesterday, 03:40 AM
|
4 responses
17 views
0 likes
|
Last Post
![]()
by alanlopen
Today, 07:06 PM
|
||
Started by alanlopen, Today, 12:36 AM
|
4 responses
20 views
0 likes
|
Last Post
![]()
by alanlopen
Today, 07:04 PM
|
||
Started by alanlopen, 03-18-2023, 02:58 AM
|
3 responses
16 views
0 likes
|
Last Post
![]()
by alanlopen
Today, 06:59 PM
|
Leave a comment: