Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
How to See the Value of SetStopLoss
Collapse
X
-
Thanks Lost Trader. You have a lot of stuff going on there that I have no idea what it means. Still lots to learn.
Tim
-
isNewStop( ref double oldStop, double newStop, bool up)
FWIW, Tim, this is how I did it....
Code:private bool isNewStop( ref double oldStop, double newStop, bool up) { if ((oldStop==0) || ( (up) ? newStop > oldStop : newStop < oldStop) ) { newStop = Instrument.MasterInstrument.Round2TickSize(newStop); if (_debug) Print(header+Time[0].ToString()+ " previous "+((up) ? "LONG" : "SHORT")+ " Stop = "+oldStop +" new Stop = "+newStop); SetStopLoss((up) ? "Long 1a" : "Short 1a", CalculationMode.Price, newStop, false); // false puts order at broker PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\AutoTrail.wav"); DrawDiamond( (up ? "L":"S")+CurrentBar, true, 0, newStop, Color.Black); oldStop = newStop; // ref so it can be changed and returned return true; } return false; // did not change }
Leave a comment:
-
Yes, definitely more than one way to approach this. Capturing the value of your stop loss when it moves and comparing to a previous value is also another way. Glad to hear you were able to work this out.
I can see what you're saying about my earlier suggestion. If the market starts heading back down after breaching 30, it would then adjust back to the first level and might not work for you.
Leave a comment:
-
I just created my own variable called newStopLoss. Each time SetStopLoss is called, I update newStopLoss to the new value. I then compare newStopLoss to the value that I want to update it to. For a long, if newStopLoss is less than what I want to update it to, I update it, if not, I don't.
else if (High[0] > Position.AvgPrice + target2)
{
if (newStopLoss < Position.AvgPrice + stop3)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice + stop3);
newStopLoss = Position.AvgPrice + stop3;
}
}
Seems to work fine.
Tim
Leave a comment:
-
Ryan, I am setting the stop using else if statements like you suggest in your second approach. Using your code as an example, the problem occurs when High[0] is less than 30 (your target) and greater than 25 (your stop). When this happens, the if statement is false and the else if statement is evaluated. The else if is true because High[0] is greater than 20 (your target) so your stop is set to +9. I would like to add something like && newStopLoss > oldStopLoss. Wait a sec, I may have just answered my own question. I'll be back...
Leave a comment:
-
Hi Tim,
The issue you're seeing is the conditions for the lower stop loss movements continue to evaluate true. There are a few different approaches you can take for this. You could work with bool flags so that the lower movements happen only once. You check the bool flag in your condition and set in the opposite direction, resetting at a later time (when flat).
Another solution is using else if and if statements in the appropriate order. Below is an example of this:
if (High[0] > Position.AvgPrice + 30 * TickSize)
SetStopLoss(CalculationMode.Price, Position.AvgPrice + 25 * TickSize);
else if (High[0] > Position.AvgPrice + 20 * TickSize)
SetStopLoss(CalculationMode.Price, Position.AvgPrice + 9 * TickSize);
Note that the second level movement is evaluated first, and else if is used for the first movement. This means that the first if statement must evaluate false before the first stop loss movement is executed.
Leave a comment:
-
How to See the Value of SetStopLoss
Is there a way to "see" the value of SetStopLoss? I have a strategy that updates the stop when price moves a certain number of points. The problem is sometimes the price moves and the stop is moved to a previous level. I only want the stop to be moved up for a long position. I want to compare the existing stop loss and only update if it is lower than what the new stop will be. How might I do that?
Here is a chunk of my code:
// If Entry + 54, Move stop to Entry + 28
else if (High[0] > Position.AvgPrice + 54)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice + 28);
DrawText("tag1", "longS3", 0, Position.AvgPrice + 28, Color.Red);
}
// If Entry + 18, Move stop to Entry + 3
else if (High[0] > Position.AvgPrice + 18)
{
ExitLong("Long 1a");
SetStopLoss(CalculationMode.Price, Position.AvgPrice + 3);
DrawText("tag2", "longS2", 0, Position.AvgPrice + 3, Color.Red);
Thanks, Tim
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Max238, Today, 01:28 AM
|
1 response
22 views
0 likes
|
Last Post
by CactusMan
Today, 06:18 AM
|
||
Started by giulyko00, Yesterday, 12:03 PM
|
2 responses
10 views
0 likes
|
Last Post
by giulyko00
Today, 06:11 AM
|
||
Started by r68cervera, Today, 05:29 AM
|
0 responses
4 views
0 likes
|
Last Post
by r68cervera
Today, 05:29 AM
|
||
Started by geddyisodin, Today, 05:20 AM
|
0 responses
6 views
0 likes
|
Last Post
by geddyisodin
Today, 05:20 AM
|
||
Started by JonesJoker, 04-22-2024, 12:23 PM
|
6 responses
38 views
0 likes
|
Last Post
by JonesJoker
Today, 05:14 AM
|
Leave a comment: