Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Modifying the price of stop loss and profit target orders
Collapse
X
-
Thank you Cal. Due to the fact that I am quite tired I was applying it to the wrong data series.
Thank you very much for your support, you've been very helpful.
Best regards,
Mitnick
-
Mitnick,
This not a matter of the code being syntax wrong but the conditions you have set.
What instrument are you going to be trading this to?
Your TickSize checks might be too big of a gap with the market data.
Try lowering and changing the conditions around to see if those might reach better results.
When I applied the strategy to the chart with lower settings I noticed that the conditions get called more often cause they are more likely to be true.
Use the Print() statements in your conditions. When they get called they will print a statement to the Output window, Tools > Output Window.
This will allow you to see if your conditions are indeed getting called when expected.
Leave a comment:
-
Cal,
I also tried removing the "if (Position.MarketPosition == MarketPosition.Flat)" line, but without any success.
I tried with the code you suggested and it still executes the fist command (sets the stop at 30 ) and after that....nothing, till the final take profit order.
I also attached the full script, maybe it helps.
ThanksAttached Files
Leave a comment:
-
Mitnick,
Let's take a look this code here -
Code:else if (Position.MarketPosition == MarketPosition.Long) { // Once the price is greater than entry price+50 ticks, set stop loss to breakeven if (Close[0] > Position.AvgPrice + 50 * TickSize) { SetStopLoss(CalculationMode.Price, Position.AvgPrice + 30 * TickSize); } if (Position.MarketPosition == MarketPosition.Flat) if (Close[0] > Position.AvgPrice + 70 * TickSize) { SetStopLoss(CalculationMode.Price, Position.AvgPrice + 50 * TickSize); } }
E.G.
if(Close[0] > Position.AvgPrice + 50 * TickSize)
When this statement becomes true then it will process this request of SetStopLoss.
This would work logically with your code, however you have
if (Position.MarketPosition == MarketPosition.Flat)
Meaning the following line below will be invalid since this is nested inside a condition that checks if your long.
You will need to get rid of this MarketPosition.Flat check and then you would be able to process your code logically.
You can also use this method of the SetStopLoss so that you know which one will get processed instead of doing them both at the same time in the condition check.
Code:else if (Position.MarketPosition == MarketPosition.Long) { // Once the price is greater than entry price+50 ticks, set stop loss to breakeven if (Close[0] > Position.AvgPrice + 70 * TickSize) { SetStopLoss(CalculationMode.Price, Position.AvgPrice + 50 * TickSize); } else if (Close[0] > Position.AvgPrice + 50 * TickSize) { SetStopLoss(CalculationMode.Price, Position.AvgPrice + 30 * TickSize); } }
Leave a comment:
-
Cal,
I posted below the code with the stop being raised for the second time. This one doesn't work. The one initially posted had just one stop and work perfectly. I am sure that it's a programing error, even though it compiles.
Thank you for your support!
protected override void OnBarUpdate()
{
// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, stoplossticks);
}
// If a long position is open, allow for stop loss modification to breakeven
else if (Position.MarketPosition == MarketPosition.Long)
{
// Once the price is greater than entry price+50 ticks, set stop loss to breakeven
if (Close[0] > Position.AvgPrice + 50 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice + 30 * TickSize);
}
if (Position.MarketPosition == MarketPosition.Flat)
if (Close[0] > Position.AvgPrice + 70 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice + 50 * TickSize);
}
}
Leave a comment:
-
Mitnick,
While the code you supplied has the syntax correct, there could be an issue that the condition is not being met for the change of the stoploss to take place.
I would suggest using the Print() function to see if the condition is being met and if the SetStopLoss() is being modified for when you are in a long position.
First of all you would want to use Print() statements to verify values are what you expect - Debugging your NinjaScript code.
For strategies add TraceOrders = true to your Initialize() method and you can then view valuable output related to strategy submitted orders through Tools > Output window - TraceOrders
Leave a comment:
-
Cal,
I struggled all day with the script referred by you, and I couldn't raise the stop for the second time. That's why I tried to get some help from the forum.
PS: I am not a experimented coder
Leave a comment:
-
Mitnick,
If you are doing one trade and not multiple orders then you would only need to use the idea form the sample reference.
Each order can only have one SetStopLoss() and one SetProfitTarget() but you can modify those prices of the Sets() by using the sample from the reference I supplied you.
Let me know if this clarified things for you.
Leave a comment:
-
Can this method be used for just a single trade? Because I saw something similar but it opened 3 positions and for each one it had a different stop and profit order.
Leave a comment:
-
Mitnick,
You need to have multiple Sets(), setup in your initialize section -
E.G.
Code:SetStopLoss(Long1, CalculationMode.Ticks, 15); SetStopLoss(Long2, CalculationMode.Ticks, 20); SetStopLoss(Long3, CalculationMode.Ticks, 25); SetProfitTarget(Long1, CalculationMode.Ticks, 15); SetProfitTarget(Long2, CalculaitonMode.Ticks, 20); SetProfitTarget(Long3, CalculationMode.Ticks, 25);
Take a look at the link from Post #2 about the FromEntrySignal and then take a look at the Entry methods from the link below.
http://www.ninjatrader.com/support/h...?enterlong.htm
Let me know if this helps.
Leave a comment:
-
Hi Cal,
Unfortunately it doesn't, what I am using is the same as that example. And it works just for one rising of the stop loss, I need two more. I already searched the forum for possible answers and couldn't find any, I was hoping from some help from you guys.
Thank you!
Leave a comment:
-
Hi Mitnick,
I would suggest looking at the reference sample below on modifying your stop loss and profit targets
http://www.ninjatrader.com/support/f...ead.php?t=3222
This sample will show you how to change the price of your stop loss and profit targets in the code.
Leave a comment:
-
Hi Cal,
Thank you for your answer, but how can I move my stop loss, because I tried using the same code (posted below) and it didn't work. Let's say I want the stop to be at Position.AvgPrice + 30 when Close[0] > Position.AvgPrice + 50, ( this one I got ) and I want the same thing for two more levels( when price is at Position.AvgPrice + 80 the stop should be at at one Position.AvgPrice + 60 and so on). How can I do it?
{
if (Close[0] > Position.AvgPrice + 50 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice + 30 * TickSize);
}
}
Leave a comment:
-
Hi Mitnick_d,
Thank you for posting.
You can use this overload of the SetProfitTarget -
Code:SetProfitTarget(string fromEntrySignal, CalculationMode mode, double value)
You can also see the help guide on the properties of these methods form the link below -
http://www.ninjatrader.com/support/h...ofittarget.htm
Let me know if I can be of further assistance.
Leave a comment:
-
Modifying the price of stop loss and profit target orders
Hi guys,
Please give me a hand with my problem. I would like to set 3 different profit targets for the trades and when each target is reached the stop loss should rise to the level of the previous target (or any other value that was previously set ).
For 1 profit target and 1 stop loss moved to the specified level, I used the code that I partially posted below.
Please tell me how can I do this with 2 more targets?
Thank you advance!
Code:protected override void Initialize() { SetStopLoss(CalculationMode.Ticks, stoplossticks); SetProfitTarget(CalculationMode.Ticks, profittargetticks); CalculateOnBarClose = true; } protected override void OnBarUpdate() { if (Position.MarketPosition == MarketPosition.Flat) { SetStopLoss(CalculationMode.Ticks, stoplossticks); } else if (Position.MarketPosition == MarketPosition.Long) { if (Close[0] > Position.AvgPrice + 50 * TickSize) { SetStopLoss(CalculationMode.Price, Position.AvgPrice + 30 * TickSize); } }
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by samish18, 10-27-2024, 05:48 PM
|
9 responses
148 views
0 likes
|
Last Post
![]() |
||
Started by iceman2018, Today, 01:44 PM
|
0 responses
1 view
0 likes
|
Last Post
![]()
by iceman2018
Today, 01:44 PM
|
||
Started by moneymaster, Yesterday, 12:35 AM
|
3 responses
20 views
0 likes
|
Last Post
|
||
Started by iantriestrading, Today, 11:55 AM
|
6 responses
13 views
0 likes
|
Last Post
![]() |
||
Started by rexsole, 04-21-2025, 01:46 PM
|
4 responses
48 views
0 likes
|
Last Post
![]() |
Leave a comment: