I want to have a visual indicator show me a trailing stop relative to a Buy Price.  I intend to indicate the buy date and buy price, and plot the constant buy price and an associated trailing stop.
 
This will not perform any function in Backtests, but is just a visual indication of where the current price is relative to the Buy price and the current percentage trailing stop.  The title statement offers some data on the relationship of the current price to both the Buy price and the Trailing stop price.  A parameter setting selects the percent the Trailing stop is below the current price.  The trailing stop never moves down.
 
The code below accomplishes this but with some compromises and limitations and I am wondering if anyone can help me remove these limitations.
 
I was unable to use the ParamDate command and make it work. Instead, I used the Range marker command "BeginValue" to set a buy price, and everything works as intended as long as there is a beginning range marker on the screen.  Turn off the range marker and the code avoids a coding error but the plot changes appearance.  Plus, there is no way to "remember" the buy date, unless you put a vertical trendline segment on the screen at the buy date.  If you have plots of various stocks or funds you have purchased, this creates lots of housekeeping (and changing of range markers).
 
One other compromise is that the Buy Price is set to the Close price; since this is used with EOD data, the actual buy price (of a stock or ETF) could be different than the close price.
 
So I am wondering if anyone can offer specific coding suggestions on how to input a Buy Date as well as a specific, Param box entered Buy Price into the code.  Ideally, it would be nice if a single plot (in a single pane) would respond to differnt symbols in the ticker window.  In other words, when I bring up different purchases, the plot automatically shows the Buy Price and Buy Date for the displayed stock.  But this may be asking too much, as I can not even conceive of how to make the code do this.
 
Anyway, if anyone is interested, I would appreciate any suggestions.
 
Thanks,
 
Ken
 
Code follows (watch line wraps) plus it is attached.
 

// StopPriceCalc1.AFL KSC 08/29/2006 revised 9/11/2006

// This puts in a hozizontal line from the buy price and

// shows a 3% stop (adjustable via Param) line

SetChartOptions(2,chartShowDates);

LRColor = ParamColor("L/R Color", colorYellow);

LRStyle = ParamStyle("L/R Style", styleLine | styleThick, maskAll);

BPrice = BeginValue(C);//Param("BPrice",C,C-Ref(C,-1),C+Ref(C,-1),1);

StopPC = Param("StopPC",0.03,0.01,0.20,0.005);

Plot(C,Name(),1,1);

Cum1 = Cum(1);

BVCum1 = BeginValue(Cum1);

//BVCum1 = ParamDate("BuyDate","6/1/2006");

BVCum1LV = LastValue(BVCum1);

EVCum1 = EndValue(Cum1);

Periods = EVCum1 - BVCum1;

Show = Cum1 >= BVCum1 AND Cum1 <= EVCum1;

VC = BeginValue(BPrice);

VConstant = LastValue(BPrice);

StopPrice = C * (1 - StopPC);

StopPrice2 = BeginValue(StopPrice);

StopPriceCon = LastValue(StopPrice2);

initial = StopPrice2;

// ------------ Loop for Trailing Stop -----------------

// Start loop at start of range marker

stop[ 0 ] = Close[ 0 ];

bi = BarIndex();

istart = IIf(BeginValue(bi)==0,1, BeginValue(bi) - bi[0]);

for( i = istart ; i < BarCount; i++)

{

if( Close[ i ] > stop[ i - 1])

{

temp = StopPrice[i];

if( temp > stop[ i - 1 ] )

stop[ i ] = temp;

else

stop[ i ] = stop[ i - 1 ];

}

else

stop[ i ] = stop[i-1];//initial[ i ];

}

Off = IIf(BeginValue(C)==0,1,0);

Plot(IIf(Show, VConstant, -1e10), "", colorYellow, styleLine);

Plot(IIf(Show, Stop, -1e10), "", colorRed, styleLine);

//Plot(Stop,"Test Stop",colorBlue,styleLine);

// -------------- Calcs for Title -----------------------------

AboveBuy = 100*(SelectedValue(C) - BPrice)/BPrice;

ColorABuy = SelectedValue(IIf(Abovebuy>0,colorGreen,colorRed));

AboveStop = 100*(SelectedValue(C) - Stop)/Stop;

ColorAStop = SelectedValue(IIf(AboveStop>0,colorGreen,colorRed));

Title = EncodeColor(colorBlue) + Name() + " " + FullName() + " " + Date() + "\n" +

"Above Buy = " + EncodeColor(ColorABuy) + WriteVal(AboveBuy) + "% " +

EncodeColor(colorBlue) + "Above Stop = " + EncodeColor(colorAstop) + WriteVal(AboveStop) +"%";

__._,_.___

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html






SPONSORED LINKS
Software support Small business finance Business finance online
Business finance training Business finance course


YAHOO! GROUPS LINKS




__,_._,___

Attachment: StopPriceCalc1.afl
Description: Binary data

Reply via email to