PureBytes Links
Trading Reference Links
|
Grover,
I rarely use ApplyStop, primarily because I'm not sure of exactly what
it's doing. I also tried Tomasz's code you mentioned, exactly as
written, and also get different results between the two. On comparing
specific exit points (most of which are the same), I notice some
differences will become the same if I change the AA entry and exit
settings (ie. close, high, low, etc), but others will still be
different. Changing the value of the "exitAtStop" parameter of
ApplyStop makes a difference too. However, I can't get both versions
to give exactly the same results, and when I backtest over even just
one symbol, all settings being identical, the results are
significantly different.
That's why I implement my own stops so that I know exactly what
they're doing.
Regards,
GP
--- In amibroker@xxxxxxxxxxxxxxx, "Grover Yowell" <gyowell1@xxx> wrote:
>
> GP,
>
>
>
> I took your comments to heart and started looking at resolving the
> discrepancies. Great comments on your part, but I was unable to get
> anything resolved after spending several hours on the problem. Too many
> variables to consider with my rather limited programming skills.
>
>
>
> So I decided to try a different approach, abandon the max loss problems,
> and instead confirm that TJ's original code on the trailing stop was
doing
> what was intended. Well, I got some surprising results. Tj's code, at
> least in my testing is not giving consistent results between the loop
> version and the apply stop version, and furthermore there are
discrepancies
> between the two versions posted and the backtest results.
>
>
>
> Again, his code is at this address:
>
> o
>
www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-cha
> rt
>
> I used Tj's code in a 1 minute database using 3 min bars on
10/3/2008 and
> tested it on RIMM. I used TJ's parameter values and set the trails
top to
> 1.3%. The only modifications I made to the code were display
items to
> present the data so that I could see what is going on. Oh, I did
make one
> other change, changed Sell[i] = 1; to Sell[i] =4; in the
TrailStopLoop code
> si that the sell by the trail stop would show properly in the backtest.
>
>
>
> Here is my TrailApplyStop code:
>
> SetChartOptions( 0, chartShowArrows | chartShowDates );
>
> SetOption( "FuturesMode", False );
>
> SetOption( "InitialEquity", 10000 );
>
> SetOption( "MinShares", 1 );
>
> SetOption( "allowpositionshrinking", True );
>
> SetOption( "priceboundchecking", True );
>
> SetOption( "commissionMode", 2 );
>
> SetOption( "commissionAmount", 1 );
>
> RoundLotSize = 100;
>
> StopLevel = Param("trailing stop %", 1.3, 0.1, 10, 0.1 );
>
>
>
> SetTradeDelays(0,0,0,0);
>
> Short = Cover = 0;
>
> Buy = Cross( MACD(), Signal() );
>
> Sell = 0;
>
> ApplyStop( stopTypeTrailing, stopModePercent, StopLevel, True );
>
>
>
> Equity( 1, 0 ); // evaluate stops, all quotes
>
> e = Equity(1,0);
>
> InTradelong = Flip( Buy, Sell );
>
> intradeshort = Flip(Short,Cover);
>
> SetOption("EveryBarNullCheck", True );
>
> stopline = IIf( InTradelong, HighestSince( Buy, High ) * ( 1 - 0.01 *
> StopLevel ), Null );
>
>
>
> PlotShapes(Buy*shapeUpArrow,colorBrightGreen,0,Low);
>
> PlotShapes(Sell*shapeDownArrow,colorRed,0,High);
>
> 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 );
>
>
>
> SetBarFillColor( IIf( C > O, colorBrightGreen, colorRed ) );
>
>
>
> Plot( Close,"Price",colorWhite,styleCandle);
>
> Plot( stopline, "trailing stop line", colorRed );
>
> Title = _DEFAULT_NAME() + StrFormat("{{NAME}} -
> {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g ", O, H, L, C ) ;
>
> ///////////////////////////////////////////////End of TrailApplyStop
Code//
> Begin TrailStopLoop Code///////////////////////////////////////
>
> SetChartOptions( 0, chartShowArrows | chartShowDates );
>
> SetOption( "FuturesMode", False );
>
> SetOption( "InitialEquity", 10000 );
>
> SetOption( "MinShares", 1 );
>
> SetOption( "allowpositionshrinking", True );
>
> SetOption( "priceboundchecking", True );
>
> SetOption( "commissionMode", 2 );
>
> SetOption( "commissionAmount", 1 );
>
> RoundLotSize = 100;
>
> StopLevel = 1 - Param("trailing stop %", 1.3, 0.1, 10, 0.1)/100;
>
> Short = Cover = 0;
>
>
>
> Buy = Cross( MACD(), Signal() );
>
> Sell = 0;
>
> trailARRAY = Null;
>
> trailstop = 0;
>
>
>
> for( i = 1; i < BarCount; i++ )
>
> {
>
>
>
> if( trailstop == 0 AND Buy[ i ] )
>
> {
>
> trailstop = High[ i ] * stoplevel;
>
> }
>
> else Buy[ i ] = 0; // remove excess buy signals
>
>
>
> if( trailstop > 0 AND Low[ i ] < trailstop )
>
> {
>
> Sell[ i ] = 4;
>
> SellPrice[ i ] = trailstop;
>
> trailstop = 0;
>
> }
>
> if( trailstop > 0 )
>
> {
>
> trailstop = Max( High[ i ] * stoplevel, trailstop );
>
> trailARRAY[ i ] = trailstop;
>
> }
>
>
>
>
>
> }
>
> 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 );
>
>
>
>
>
> PlotShapes(Buy*shapeUpArrow,colorBrightGreen,0,Low);
>
> PlotShapes(Sell*shapeDownArrow,colorRed,0,High);
>
> SetBarFillColor( IIf( C > O, colorBrightGreen, colorRed ) );
>
>
>
> Plot( Close,"Price",colorWhite,styleCandle);
>
> Plot( trailARRAY,"trailing stop level", colorRed );
>
> Title = _DEFAULT_NAME() + StrFormat("{{NAME}} -
> {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g ", O, H, L, C );
>
> ///////////////////////////////////////////////////////End of
TrailStopLoop
> Code////////////////////////////////////////////////
>
>
>
> I will summarize the results:
>
> TrailApplyStop
> TrailStopLoop
BackTest
> (both Systems)
>
> Entry Exit
Entry
> Exit Entry
Exit
>
> 7:14 8:29
None
> None 7:14
8:59
>
> 9:38 10:14
9:38
> 10:14 None
None
>
> 11:11 11:20
11:11
> 11:20 11:11
11:23
>
> 11:53 12:11
11:53
> 12:11 11:53
12:11
>
>
>
> Note that the TrailStopLoop code did not show the 7:14AM trade that the
> TrailApplyStop code showed and there is a discrepancy on the exit
time of
> the backtest. Also the backtest did not show the 9:38 AM trade.
>
>
>
> When I look at the Code, it is not at all obvious what the problem
is. It
> would seem that each version should be consistent with one another,
and that
> both should be consistent with the backtest results.
>
>
>
> Any ideas? I am just not proficient enough to analyze what is happening
> here.
>
>
>
> Thanks for your help.
>
>
>
> Grover
>
>
>
> From: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx]
On Behalf
> Of gp_sydney
> Sent: Sunday, October 05, 2008 2:12 AM
> To: amibroker@xxxxxxxxxxxxxxx
> Subject: [amibroker] Re: How to plot a maximum loss stop line
>
>
>
> Grover,
>
> Sorry, I don't get attachments as I only view messages on the web
> where attachments aren't saved.
>
> However, on doing as you suggested with GE, but changing the Buy/Short
> conditions of the ApplyStop version to be the same as the loop version
> (ie. MA crossover rather than MACD signal crossover), I noticed the
> following:
>
> - Your loop stop uses close as the entry price and high or low as the
> stop price. I think ApplyStop uses your trade price settings
> programmed in the AA settings (although I've never really experimented
> with ApplyStop to be sure what it's using). Consequently, if they are
> not set to sell on low and cover on high, there will be some difference.
>
> - Your loop code can display signals for concurrent long and short
> trades, whereas ApplyStop with Equity(1) can't (in reality you
> wouldn't have both, as the second would be a sell/cover of the first
> rather than a new position, but I've never checked what happens if you
> have a short signal while you're already long without a corresponding
> sell). The ApplyStop version doesn't show any short trades because the
> whole period is mostly in long trades and all short signals are within
> open long trades (I only have a limited amount of data for GE though).
> Remember, when you use Equity(1), it leaves your signal arrays showing
> the actual trades that would be taken, removing all signals that
> wouldn't be taken. You can't have an open long and short trade in the
> same stock at the same time.
>
> Hope this helps.
>
> Regards,
> GP
>
> --- In amibroker@xxxxxxxxxxxxxxx <mailto:amibroker%40yahoogroups.com> ,
> "Grover Yowell" <gyowell1@> wrote:
> >
> > GP, I will try to get on the same sheet of music with you.
> >
> > I went to the daily database that ships with AB, and pulled up GE.
> Put in
> > ticksize = .01 in the window brought up by the little "I" icon. Then I
> > changed the parameter values for stop level ticks to range up to 200
> ticks
> > which would be 200 X .01 = $2.00 of price change for GE. Then I
put the
> > ApplyStop version in the upper pane and the loop version in the
> lower pane
> > and set the stoplevel ticks to 200 for each pane. Then I exported
> the image
> > to MaxLoss.png which is attached. Then what I see is no stop levels
> on the
> > Applystop version in the top pane and two stop levels with the two
short
> > signals for the loop version in the lower pane. Checking the price
> shown
> > for the stop levels in the lower pane, they check out exactly at
> $2.00 above
> > the short price.
> >
> >
> >
> > Next I reduced the stoplevel ticks on the upper pane until I had
> stop levels
> > showing. This was at 70 ticks, or $0.70 stop loss. No stop levels were
> > showing until I reached 70 ticks on the upper pane. But on the
> lower pane,
> > the stop level moved smoothly from it's location at 200 ticks down
to 70
> > ticks. I have attached the png file for this image as
> > MaxLossStopTicks70.png.
> >
> >
> >
> > Conclusions:
> >
> >
> >
> > I would think that the two versions should give similar results.
> >
> > Something is different using the ApplyStop version.
> >
> > I don't know for sure that my loop version is correct, but it
works more
> > like I expect it to work.
> >
> >
> >
> > Any ideas?
> >
> >
> >
> > Thanks again for your help!
> >
> >
> >
> > Grover
> >
> >
> >
> > From: amibroker@xxxxxxxxxxxxxxx <mailto:amibroker%40yahoogroups.com>
> [mailto:amibroker@xxxxxxxxxxxxxxx <mailto:amibroker%40yahoogroups.com> ]
> On Behalf
> > Of gp_sydney
> > Sent: Saturday, October 04, 2008 4:24 PM
> > To: amibroker@xxxxxxxxxxxxxxx <mailto:amibroker%40yahoogroups.com>
> > Subject: [amibroker] Re: How to plot a maximum loss stop line
> >
> >
> >
> > Grover,
> >
> > I'm not familiar with those futures. What's the current price?
> >
> > And what do you see when it is not working? Is there no sell signal,
> > no stop line, a stop line with an incorrect value?
> >
> > Perhaps give an example of one bar where it's not working, providing
> > the OHLC values and the value of the stop line at that bar, the entry
> > price, and the value of maxlossticks.
> >
> > Regards,
> > GP
> >
> > --- In amibroker@xxxxxxxxxxxxxxx <mailto:amibroker%40yahoogroups.com>
> <mailto:amibroker%40yahoogroups.com> ,
> > "Grover Yowell" <gyowell1@> wrote:
> > >
> > > 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%40yahoogroups.com>
> <mailto:amibroker%40yahoogroups.com>
> > [mailto:amibroker@xxxxxxxxxxxxxxx <mailto:amibroker%40yahoogroups.com>
> <mailto:amibroker%40yahoogroups.com> ]
> > On Behalf
> > > Of gp_sydney
> > > Sent: Friday, October 03, 2008 7:43 PM
> > > To: amibroker@xxxxxxxxxxxxxxx <mailto:amibroker%40yahoogroups.com>
> <mailto:amibroker%40yahoogroups.com>
> > > 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
<mailto:amibroker%40yahoogroups.com>
> <mailto:amibroker%40yahoogroups.com>
> > <mailto:amibroker%40yahoogroups.com> ,
> > > "gyowell2000" <gyowell1@> 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
*********************************
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/amibroker/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/amibroker/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:amibroker-digest@xxxxxxxxxxxxxxx
mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx
<*> To unsubscribe from this group, send an email to:
amibroker-unsubscribe@xxxxxxxxxxxxxxx
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|