Drawing or Plotting Trailing Stop loss lines
Author: Tobey
Creation Date: 5/19/2011 2:00 PM
profile picture

Tobey

#1
Hi Cone & Eugene,

I'd like to tighten a trailing stop loss as the price moves up to the profit target.

At the buy price; A trailing stop loss at 3% off the high.
Half way to the target, move it up to 2% off the high.
At the target price move it up to 1% off the high.
On a 5 minute chart set an exit a 156 bars (2 days)

I think the code below is in the right order to do this. If not please tell me I'm clueless and codeless again.

I'd like to graph these trailing stops on a chart in a way that makes them easy to see and easy to tell the difference when trailing stop loss Percent changes.

Cone referred to Bandwagon Trading as an example coding that might work. The results on the chart look good. Or it could be a line. But I haven't been able to code the line that would allow me to put the Trailing Stop loss in the DrawCirlce line.

stop = ?????
DrawCircle(PricePane, 3, bar + 1, stop, Color.DarkRed, LineStyle.Solid, 2, false);

Who knows, maybe I'm not even looking in the right place.

Once again thanks for the help.
Tobey
CODE:
Please log in to see this code.
profile picture

Eugene

#2
QUOTE:
On a 5 minute chart set an exit a 156 bars (2 days)

Exiting after 156 bars is pretty unelegant. For how it's made properly, check out the KB article on Building blocks of Intraday trading strategies > Exiting after N days in an intraday strategy.
QUOTE:
I'd like to graph these trailing stops on a chart in a way that makes them easy to see and easy to tell the difference when trailing stop loss Percent changes.

Save the stop value in a double variable i.e. stop, change it on every bar, and pass this variable when calling DrawCircle. That simple.

Something like this on-the-fly pseudocode:

CODE:
Please log in to see this code.
profile picture

Tobey

#3
Hi Eugene,

Thanks!
I put the code in and I got Circles and Stops dots. But the dots didn't always match the Circles. I think this was because the Stop is based on the highest high and the circle was based on the bar high.

This code seams to fix that issue.


Both codes work perfectly on closed positions like ATML on 5/16/2011 or SOHU on 5/13/2011.

But both of the two coding give a run-time error on open positions like BKE and AAP on 5/19/2011. Even after the error the chats display the circles ok.

The code complies ok. The Error message is

CODE:
Please log in to see this code.


Searches for "Bar number outside of the chart: " don't turn anything.

Thanks for the help!
Tobey
profile picture

Eugene

#4
Hi Tobey,

You're accessing an invald bar i.e. the one with the number that does not yet exist. This is caused by plotting stops on bar+1 and applies to open positions. Changing bar+1 to bar in DrawCircle should make the problem go away.
profile picture

Cone

#5
.. but make sure not to DrawCircle on a bar that doesn't exist in the future. In other words, if you're using bar+1 in DrawCircle, don't DrawCircle when processing the last bar of the chart.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).