[R-SIG-Finance] Coding for a trailing stop (% or fixed $)

Brian G. Peterson brian at braverock.com
Sun Mar 28 17:44:48 CEST 2010


On 03/28/2010 10:12 AM, David L. Van Brunt, Ph.D. wrote:
> For the life of me, I don't know why this is eluding me...
>
> I'm trying to add a variable into a time series (have quote data added via
> quantmod).  I've added technical indicators, but now want a column that will
> reflect the trailing stop value for that day, given a starting date and
> trade value, and a given trailing stop.
>
> Is there a more sensible way than using a FOR loop to march through the
> rows, lagging to compare prior highs/lows to current stop value?  I don't
> want the canned Parabolic SAR, but a custom user-specifiable trailing stop
> (for example, 5% below the high since position entry for long, 5% above low
> since short entry)
>    

If I understand you correctly, what you want is path dependent, based on 
your current position and entry price.

You *may* be able to do it with an ifelse statement, suitably crafted, 
but otherwise, loop away.

A wise mentor of mine once said:
"First make it work, then make it work right, then make it fast; in that 
order, do not deviate."

Still true almost two decades later.

Regards,

     - Brian

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list