[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Code help please


  • Date: Sun, 27 Dec 2009 16:32:57 -0000
  • From: "mozpet2001" <mozpet2001@xxxxxxxxx>
  • Subject: [amibroker] Code help please

PureBytes Links

Trading Reference Links

The following is a simple code for illustration purposes which is meant to enter on the close being greater than the 20 period moving average and exiting on a trailing stop based on the highs minus 3.0 times ATR. I've plotted the trailing stop as an indicator but when I run a backtest of the code below the exits don't line up with trailing stop indicator. In fact the I'm getting exits one bar after entry. I really don't understand what is going on. Can someone help?

Thanks

Maurice

Code:

SetBarsRequired(10000,10000); /* this ensures that the charts include all bars
AND NOT just those on screen */
SetFormulaName("Weekly SMA System with ATR stop"); /*name it for backtest report
identification */
SetTradeDelays( 1, 1, 1, 1 ); /* delay entry/exit by one bar */
SetOption( "initialequity", 100000 ); /* starting capital */
SetOption( "PriceBoundChecking", 1 ); /* trade only within the chart bar's price
range */
SetOption( "CommissionMode", 2 ); /* set commissions AND costs as $ per trade */
SetOption( "CommissionAmount", 32.95 ); /* commissions AND cost */
SetOption( "UsePrevBarEquityForPosSizing", True ); /*set the use of last bars
equity for trade size*/
SetOption("MaxOpenPositions", 20); // This sets maximum number of open positions
to 15;
SetOption( "AllowPositionShrinking", False);
SetOption("AllowSameBarExit", False);
ATRLevel = 3*ATR(20);


Buy = C>MA(C,30);

PositionSize=-10;// invest 10% of portfolio equity in single trade

Sell = 0;
trailARRAY = Null;
trailstop = 0;

for( i = 1; i < BarCount; i++ )
{

if( trailstop == 0 AND Buy[ i ] )
{
trailstop = H[ i ]- ATRLevel[ i ];
}
else Buy[ i ] = 0; // remove excess buy signals

if( trailstop > 0 AND Low[ i ] < trailstop )
{
Sell[ i ] = 1;
SellPrice[ i ] = trailstop;
trailstop = 0;
}

if( trailstop > 0 )
{
trailstop = Max( H[ i ]- ATRLevel[ i ], trailstop );
trailARRAY[ i ] = trailstop;
}

}

Sell = trailARRAY;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);



------------------------------------

**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to 
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

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:
    amibroker-digest@xxxxxxxxxxxxxxx 
    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/