Hi GP,
Thanks for the reply.
The Ticksize is set to 0.25 since I am using the ESZ8-GLOBEX-FUT
futures for quotes.
What I am finding is that the max loss stop works over a
very limited range of values of max loss, say 1 – 5 ticks.,
To understand what is going on, I have also taken TJ’s
code
for the loop version of the trailing stop referenced in
the kb article, and adapted it to do a loop version maxloss stop both long and
short. Hopefully I don’t have any errors in that
code. And I have used the loop version to compare with the this other
version which we can call the ApplyStop version. The loop version works
smoothly over the whole range of values of max loss while the ApplyStop version
works only over a small limited range. Here is the code for the loop
version. For my comparison, I changed the code in my earlier post to use identical
buy/sell rules.
My objective is to have a max loss stop function which will provide
a signal as an indicator as opposed to the backtester. If I can confirm
that my loop version is doing the job, I will go with it instead of the
ApplyStop version.
Again, thanks for your help. I would welcome any comments
on the code below.
Grover Yowell
///////////////////////////////////////////Loop Code
begins///////////////////////////////
Buy = Cross( MA( C, 10 ), MA( C, 20 ) );
Short = Cross( MA( C, 20 ), MA( C, 10 ) );
Sell = 0;
Cover = 0;
maxlossticks = Param( "MaxLossTicks", 5, 0, 20, 1 );
maxlosspoints = maxlossticks * TickSize;
MaxlossbuyARRAY = Null;
MaxlossshortARRAY = Null;
priceatbuy = 0;
priceatshort = 0;
for ( i = 1; i < BarCount;
i++ )
{
//if(
traillongstop == 0 AND Buy[ i ] )
if ( priceatbuy == 0 AND Buy[i]
) //buy signal and not intrade
{
//traillongstop
= High[ i ] - trailpoints;
priceatbuy = Close[i]; //take the trade
}
else
Buy[ i ] = 0; // remove excess buy signals
if ( priceatbuy
> 0 ) //we are intrade
long
{
maxlossbuyARRAY[i] = priceatbuy - Maxlosspoints;
}
if ( priceatbuy
> 0 AND
Low[i] < priceatbuy - Maxlosspoints ) //trade closed
out
{
Sell[ i ] = 1;
SellPrice[ i ] = Close[i];
priceatbuy = 0;
}
//short
if ( priceatshort
== 0 AND Short[
i ] )
{
priceatshort = Close[i];
}
else
Short[ i ] = 0; // remove excess short signals
if ( priceatshort
> 0 )
{
maxlossshortARRAY[i] = priceatshort + Maxlosspoints;
}
if ( priceatshort
> 0 AND
High[i] > priceatshort + Maxlosspoints ) //trade closed
out
{
Cover[ i ] = 2;
CoverPrice[ i ] = Close[i];
priceatshort = 0;
}
}
PlotShapes( Buy*shapeUpArrow, colorBrightGreen,
0, Low );
PlotShapes( Sell*shapeHollowDownArrow,
colorRed, 0, High );
PlotShapes( Short*shapeDownArrow,
colorRed, 0, H );
PlotShapes( Cover*shapeHollowUpArrow,
colorBrightGreen, 0, L );
SetBarFillColor( IIf( C > O,
colorBrightGreen, colorRed ) );
Plot( Close, "Price", colorWhite,
styleCandle );
Plot( maxlossbuyARRAY, "maxlossbuyArray", colorBrightGreen
);
Plot( maxlossShortARRAY, "maxlossShortARRAY", colorRed
);
Plot( MA( C, 10 ), "MA
fast", colorBrightGreen );
Plot( MA( C, 20 ), "MA
slow", colorRed );
InTradelong = Flip( Buy, Sell
);
InTradeshort = Flip( Short, Cover
);
Plot( 2, /* defines the height of the ribbon in
percent of pane width
*/"ribbon",
IIf( intradelong, colorBrightGreen,
IIf( intradeshort, colorRed,
colorYellow ) ), /* choose color */
styleOwnScale
| styleArea | styleNoLabel, -0.5, 100 );
Title = _DEFAULT_NAME() + StrFormat( "{{NAME}} -
{{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g ", O, H,
L, C );
/////////////////////////////////////////////////Loop Code
Ends//////////////////////////////////////
From:
amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx] On Behalf Of gp_sydney
Sent: Friday, October 03, 2008 7:43 PM
To: amibroker@xxxxxxxxxxxxxxx
Subject: [amibroker] Re: How to plot a maximum loss stop line
What is your value of "TickSize"? If
it's zero, ApplyStops seems to do
nothing and you don't get any sell signals. If it's a positive value,
then it is in dollars and the resulting value of stopLevelPoints may
be too large for the current share price.
If I try your formula with a tick size of 0.01 (ie. 1 cent), it works
as expected.
Regards,
GP
--- In amibroker@xxxxxxxxxxxxxxx,
"gyowell2000" <gyowell1@xxx> wrote:
>
> I have been attempting to plot a maximum loss stop line following the
> code for plotting a trailing stop provided by TJ in the Knowledge
> base:
>
> http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-
> the-price-chart/
>
> My code follows, unfortunately it doesn't work! I have used ticks
> for max loss allowed instead of percentages. I also wanted a system
> that would accomodate either long or short sales.
>
> ////////////////////////Code begins/////////////////////////////
> StopLevelticks = Param("stoplevel ticks", 3, 0, 20, 1 );
> stoplevelpoints = stoplevelticks*TickSize;
> SetTradeDelays(0,0,0,0);
>
> Buy = Cross( MACD(), Signal() );
> Short = Cross(Signal(), MACD() );
> Sell = 0;
> Cover = 0;
> ApplyStop( stopTypeLoss, stopModePoint, StopLevelpoints, True );
>
> Equity( 1, 0 ); // evaluate stops, all quotes
>
> InTradelong = Flip( Buy, Sell );
> InTradeshort = Flip( Short, Cover );
>
> SetOption("EveryBarNullCheck", True );
> stoplinelong = IIf( InTradelong, ValueWhen( Buy, BuyPrice) -
> stoplevelpoints , Null );
> stoplineshort = IIf( InTradeshort, ValueWhen( Short, ShortPrice) +
> stoplevelpoints , Null );
>
> PlotShapes(Buy*shapeUpArrow,colorBrightGreen,0,Low);
> PlotShapes(Short*shapeDownArrow,colorRed,0,High);
> PlotShapes(Cover*shapeHollowUpArrow,colorBrightGreen,0,Low);
> PlotShapes(Sell*shapeHollowDownArrow,colorRed,0,High);
>
> SetBarFillColor( IIf( C > O, colorBrightGreen, colorRed ) );
>
> Plot( Close,"Price",colorWhite,styleCandle);
> Plot( stoplinelong, "maxloss line long", colorBrightGreen );
> Plot( stoplineshort, "maxloss line short", colorRed );
> //////////////////////////Code Ends/////////////////////////////////
>
> Converting TJ's code from a trailing stop to a maximum loss stop and
> getting the functionality to handle shorts proved to be be more
> difficult than I had anticipated.
>
> I would appreciate any help in clearing up the problems in the code
> above.
>
> Thanks in advance,
>
> Grover Yowell
>
__._,_.___
**** IMPORTANT ****
This group is for the discussion between users only.
This is *NOT* technical support channel.
*********************
TO GET TECHNICAL SUPPORT from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com
*********************
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
For other support material please check also:
http://www.amibroker.com/support.html
*********************************
__,_._,___