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

[amibroker] Re: Custom Backtester Slippage Code for ApplyStop - Need Help



PureBytes Links

Trading Reference Links

Hi Ed,

Thanks. That's helpful. I knew I could simulate ApplyStop with a
barcount loop and implement whatever slippage. I was hoping to avoid
that, only because barcounts tend to slow down my backtests significantly.

But on the other hand, custom backtester code can also slow things
down, and to implement slippage one needs a barcount there anyway.

I will test out your slip procedure over the next few days. Looks like
it's the more practical approach. 


--- In amibroker@xxxxxxxxxxxxxxx, "Edward Pottasch" <empottasch@xxx>
wrote:
>
> ozzyman,
> 
> you can do this if you write your own applystop and then you can
omit the CBI code.
> 
> Below an example:
> 
> regards,Ed
> 
> 
> procedure sellCover_proc(Buy,BuyPrice,Sell,SellPrice,nBar,slip) 
> { 
> 
> global BuyAdjusted; 
> global BuyPriceAdjusted; 
> global SellAdjusted; 
> global SellPriceAdjusted; 
> 
> BuyAdjusted = 0; 
> BuyPriceAdjusted = 0; 
> SellAdjusted = 0; 
> SellPriceAdjusted = 0; 
> 
> delay = 1; 
> 
> for( i = 1; i < BarCount; i++ ) 
> { 
>     
>    if ( Buy[ i ]) 
>    { 
> 
>       BuyAdjusted[ i ] = 1; 
>       BuyPriceAdjusted[ i ] = Min(H[ i ],BuyPrice[ i ] + slip[ i ]); 
>        
>       for (j = i + delay; j < BarCount; j++) 
>       {       
>           
>          if (Sell[ j ]) 
>          { 
>           
>             SellAdjusted[ j ] = 1; 
>             SellPriceAdjusted[ j ] = Max(L[ j ],SellPrice[ j ] -
slip[ j ]); 
>             i = j; 
>             break; 
>           
>          } 
>          if (( (j - 1) - i) == nBar) 
>          { 
>           
>             SellAdjusted[ j ] = 1; 
>             SellPriceAdjusted[ j ] = Max(L[ j ],C[ j ] - slip[ j ]); 
>             i = j; 
>             break; 
>              
>          } 
>          else if (j == BarCount - 1) 
>          { 
>                    
>             i = BarCount; 
>                    
>          } 
>       } 
>    } 
>     
> } 
> 
> } 
> 
> 
> Buy = Cross(C,MA(C,50)); Buy = Ref(Buy,-1); 
> BuyPrice = Open; 
> Sell = Cross(MA(C,50),C); Sell = Ref(Sell,-1); 
> SellPrice = Open; 
> 
> // nbar stop 
> nbar = 5; 
> // slippage 
> slip = Random() * ATR(10); 
> 
> sellCover_proc(Buy,BuyPrice,Sell,SellPrice,nBar,slip); 
> 
> Buy = BuyAdjusted; 
> BuyPrice = BuyPriceAdjusted; 
> Sell = SellAdjusted; 
> SellPrice = SellPriceAdjusted; 
> 
> SetChartOptions(0, chartShowDates); 
> Plot(C,"Last=",colorBlack,64); 
> Plot(MA(C,50),"ma",colorWhite,1); 
> 
> PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorGreen,0,L,-15); 
> PlotShapes(IIf(Buy,shapeHollowUpArrow,shapeNone),colorWhite,0,L,-15); 
>
PlotShapes(IIf(Buy,shapeHollowSmallCircle,shapeNone),colorWhite,0,BuyPrice,0);

> 
> PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,H,-15); 
>
PlotShapes(IIf(Sell,shapeHollowDownArrow,shapeNone),colorWhite,0,H,-15); 
>
PlotShapes(IIf(Sell,shapeHollowSmallCircle,shapeNone),colorWhite,0,SellPrice,0);

> 
> 
> 
> 
>   ----- Original Message ----- 
>   From: ozzyapeman 
>   To: amibroker@xxxxxxxxxxxxxxx 
>   Sent: Wednesday, February 04, 2009 5:41 PM
>   Subject: [amibroker] Re: Custom Backtester Slippage Code for
ApplyStop - Need Help
> 
> 
>   I do currently try to integrate slippage amounts into commissions.
>   However, what I really want to do is a build a model that mimics the
>   real world as much as possible.
> 
>   It would be nice in the future if AB had a setoption() that allowed us
>   to integrate slippage into all exit and entries, whether we use
>   ApplyStop or regular sell/cover. Such a setoption could also have the
>   ability to set some 'randomness' in the slippage, allowing it to vary
>   from trade to trade, within a range the use could set.
> 
>   --- In amibroker@xxxxxxxxxxxxxxx, "huanyanlu" <huanyan2000@> wrote:
>   >
>   > Hi
>   > 
>   > Have you considered making slippage a part of the commision and 
>   > adjust it in the backtester commision settings beforehand, thus
avoid 
>   > doing complicated calculations with the signal object of custom 
>   > backtester ?
>   > 
>   > Huanyan
>   > 
>   > 
>   > --- In amibroker@xxxxxxxxxxxxxxx, "ozzyapeman" <zoopfree@> wrote:
>   > >
>   > > Hello, hoping someone can help out with this code. Aron was kind 
>   > enough
>   > > to post a version of some code that is meant to inject some
slippage
>   > > when using ApplyStop(). However, I can't seem to get it to work. 
>   > All I
>   > > want it to do is reduce Long exits by 2 pips (I'm backtesting 
>   > Forex) and
>   > > increase Short exits by 2 pips, when using ApplyStop.
>   > > 
>   > > Here is the code. At present, it only ends up blanking out my 
>   > backtest
>   > > report - no trades taken. Without the code, dozens or hundreds of 
>   > trades
>   > > taken, depending on which system I test. As far as I can tell,
this 
>   > code
>   > > should work, but doesn't. Any input appreciated:
>   > > 
>   > > 
>   > > SetCustomBacktestProc( "" );
>   > > if ( Status( "action" ) == actionPortfolio )
>   > > {
>   > > TickSize = 0.0001; // Forex
>   > > bo = GetBacktesterObject();
>   > > bo.PreProcess();
>   > > slipage = TickSize;
>   > > spread = 2 * TickSize;
>   > > 
>   > > for ( bar = 0; bar < BarCount; bar++ )
>   > > {
>   > > for ( sig = bo.GetFirstSignal(bar); sig; sig =
>   > > bo.GetNextSignal(bar) )
>   > > {
>   > > symbol = sig.symbol;
>   > > hi = Foreign(symbol, "H");
>   > > lo = Foreign(symbol, "L");
>   > > 
>   > > if ( sig.IsExit() )
>   > > {
>   > > if ( sig.isLong)
>   > > {
>   > > realexitprice = sig.price - slipage;
>   > > if( realexitprice >= lo[bar] && realexitprice <=
>   > > hi[bar])
>   > > {
>   > > sig.price = realexitprice;
>   > > bo.ExitTrade(bar,sig.symbol,sig.Price); // 
>   > I'm not
>   > > sure if it is needed
>   > > }
>   > > else
>   > > sig.price = -1;
>   > > }
>   > > else
>   > > {
>   > > ealexitprice = sig.price + slipage;
>   > > if (realexitPrice >= lo[bar]+ spread && 
>   > realexitprice
>   > > <= hi[bar]+spread)
>   > > {
>   > > sig.price = realexitprice;
>   > > bo.ExitTrade(bar,sig.symbol,sig.Price); // I'm 
>   > not
>   > > sure if it is needed
>   > > }
>   > > else
>   > > sig.price = -1;
>   > > }
>   > > 
>   > > }
>   > > }
>   > > 
>   > > bo.ProcessTradeSignals( bar );
>   > > }
>   > > }
>   > >
>   >
>



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

**** 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/