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

[amibroker] Re: Simple slippage implemented in CBT generates COM error



PureBytes Links

Trading Reference Links

Thanks Tomasz! That finally works!

This copy-paste artist is happy. ;-)


--- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko" <groups@xxx> wrote:
>
> All wrong. Wrong loop ending condition, using trade (closed trade list) instead of signal list.
> Correct code is below.
> Again: if you don't know what you are coding it is STRONGLY encouraged to use
> COMMISSION table instead. Just set custom commission table to implement slippage.
> It is way easier and more straightforward than any other method. I completelly don't understand
> the insistency of copy-paste artists on making it hard way while way easier method (no coding at all) is available.
> 
> 
> Slippage = 0.0002; 
> 
> if ( Status( "action" ) == actionPortfolio ) 
> { 
>     bo = GetBacktesterObject(); 
> 
>     bo.PreProcess(); // Initialize backtester 
> 
>     for ( bar = 0; bar < BarCount; bar++ ) 
>     { 
>       for( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) ) 
>       { 
>            sig.Price = sig.Price + IIf( sig.IsEntry(), Slippage, -Slippage ); 
>       } 
> 
>       bo.ProcessTradeSignals( bar ); 
>     } 
> 
>     bo.PostProcess(); // Finalize backtester 
> } 
> 
> 
> Best regards,
> Tomasz Janeczko
> amibroker.com
>   ----- Original Message ----- 
>   From: ozzyapeman 
>   To: amibroker@xxxxxxxxxxxxxxx 
>   Sent: Thursday, March 19, 2009 4:54 AM
>   Subject: [amibroker] Re: Simple slippage implemented in CBT generates COM error
> 
> 
>   Graham, thanks for that example.
> 
>   I modified the example to try to solve my slippage problem. From every way I look at it, I now appear to have all the correct controls. Yet it still has no effect! Note that I can't use GetPrice on Closed trades, according to the reference guide.
> 
>   I really hate looking like a coding klutz, but do you, or anyone see what I am still doing wrong?!
> 
> 
>   if ( Status( "action" ) == actionPortfolio ) 
>   { 
>       bo = GetBacktesterObject(); 
> 
>       bo.PreProcess(); // Initialize backtester 
> 
>       for ( bar = 0; bar < BarCount-1; bar++ ) 
>       { 
>           bo.ProcessTradeSignals( bar ); 
> 
>           for ( Trade = bo.GetFirstTrade(); Trade; Trade = bo.GetNextTrade() ) 
>           { 
>               if ( NOT Trade.IsOpen() ) 
>               { 
>                   if ( Trade.IsLong() ) // Exit Long 
>                   { 
>                       ExitTrue        = Trade.ExitPrice - Slippage; 
>                       Trade.ExitPrice = ExitTrue; 
>                   } 
> 
>                   else               // Exit Short 
>                   { 
>                       ExitTrue        = Trade.ExitPrice + Slippage; 
>                       Trade.ExitPrice = ExitTrue; 
> 
>                   } 
>               } 
>           } 
>       } 
> 
>       bo.PostProcess(); // Finalize backtester 
>   } 
> 
> 
> 
> 
> 
>   --- In amibroker@xxxxxxxxxxxxxxx, Graham <kavemanperth@> wrote:
>   >
>   > You have not included all the required control functions and method
>   > for getting the signals, here is example from knowledge base, you can
>   > see what is missing. This example does a different actual change to
>   > the trades, but the whole process is the same.
>   > 
>   > if( Status("action") == actionPortfolio )
>   > {
>   > bo = GetBacktesterObject();
>   > 
>   > bo.PreProcess(); // Initialize backtester
>   > <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>   > 
>   > for(bar=0; bar < BarCount; bar++)
>   > //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>   > { //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>   > bo.ProcessTradeSignals( bar );
>   > //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>   > 
>   > CurEquity = bo.Equity;
>   > 
>   > for( pos = bo.GetFirstOpenPos(); pos; pos = bo.GetNextOpenPos() )
>   > {
>   > posval = pos.GetPositionValue();
>   > 
>   > diff = posval - 0.01 * EachPosPercent * CurEquity;
>   > price = pos.GetPrice( bar, "O" );
>   > 
>   > // rebalance only if difference between desired and
>   > // current position value is greater than 0.5% of equity
>   > // and greater than price of single share
>   > if( diff != 0 AND
>   > abs( diff ) > 0.005 * CurEquity AND
>   > abs( diff ) > price )
>   > {
>   > bo.ScaleTrade( bar, pos.Symbol, diff < 0, price, abs( diff ) );
>   > }
>   > }
>   > } //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>   > bo.PostProcess(); // Finalize backtester
>   > //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>   > }
>   > 
>   > 2009/3/19 ozzyapeman zoopfree@:
>   > > The custom commission table unfortunately doesn't  allow me to do what I
>   > > want, which is to slip exits by an amount that varies according to market
>   > > time. The example I am pasting here is simplified. I'm trying to model based
>   > > on what I am seeing in live trades.
>   > >
>   > > Thanks on ProcessTradeSignals. But I still must be doing something wrong,
>   > > because no effect:
>   > >
>   > >
>   > > if ( Status( "action" ) == actionPortfolio )
>   > > {
>   > >   bo = GetBacktesterObject();
>   > >
>   > >     for
>   > > ( sig = bo.GetFirstSignal(); sig; sig = bo.GetNextSignal() )
>   > >      {
>   > >
>   > >       sig.ProcessTradeSignals();
>   > >
>   > >         if( sig.IsExit() )
>   > >          {
>   > >            if ( sig.IsLong() ) // Exit Long
>   > >             {
>   > >              ExitTrue  = sig.Price - Slippage;
>   > >              sig.Price = ExitTrue;
>   > >              }
>   > >
>   > >
>   > > else               // Exit Short
>   > >              {
>   > >              ExitTrue  = sig.Price + Slippage;
>   > >              sig.Price = ExitTrue;
>   > >              }
>   > >           }
>   > >        }
>   > >   }
>   > >
>   > >
>   > >
>   > >
>   > > --- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko" groups@ wrote:
>   > >>
>   > >> Hello,
>   > >>
>   > >> You need to call ProcessTradeSignals.
>   > >>
>   > >> BTW: it is easier to just define custom commission table (AA->Settings
>   > >> "Commission table: Define...")
>   > >> that includes slippage than wresting with code.
>   > >>
>   > >> Best regards,
>   > >> Tomasz Janeczko
>   > >> amibroker.com
>   > >> ----- Original Message -----
>   > >> From: ozzyapeman
>   > >> To: amibroker@xxxxxxxxxxxxxxx
>   > >> Sent: Thursday, March 19, 2009 2:31 AM
>   > >> Subject: [amibroker] Re: Simple slippage implemented in CBT generates COM
>   > >> error
>   > >>
>   > >>
>   > >> Okay on #1, I realize that the COM error was due to the bit of legacy
>   > >> code: SetCustomBacktestProc( "" );
>   > >>
>   > >> However, the slippage code seems to have no effect whatsoever on the
>   > >> backtest trade report.
>   > >>
>   > >> What might I be doing wrong?
>   > >>
>   > >>
>   > >>
>   > >> --- In amibroker@xxxxxxxxxxxxxxx, "ozzyapeman" zoopfree@ wrote:
>   > >> >
>   > >> > Hello, hoping someone can help on this. I am using ApplyStop, which does
>   > >> > not have a slippage factor. I'm trying to avoid using a BarCount loop to
>   > >> > implement slippage on exits and instead am trying to modify the signal
>   > >> > list of the CBT to implement slippage, before the backtester engine
>   > >> > processes the trades.
>   > >> >
>   > >> > But I am running into two problems, namely:
>   > >> >
>   > >> >
>   > >> > 1. Get error COM method/function 'GetFirstSignal' call failed, on the
>   > >> > for loop line, even though that line and prior ones were copied and
>   > >> > pasted direct from the reference guide.
>   > >> >
>   > >> >
>   > >> > 2. More of a question at this point: What if my calculation of ExitTrue
>   > >> > price is below the Low, or above the High of the bar? Will the
>   > >> > backtester engine simply ignore that signal? Or is there some way I can
>   > >> > filter out that possibility directly in the code below?
>   > >> >
>   > >> >
>   > >> >
>   > >> > In this example, Slippage = 0.0002 elsewhere in my code, backtesting on
>   > >> > Forex:
>   > >> >
>   > >> >
>   > >> > SetCustomBacktestProc( "" );
>   > >> >
>   > >> > if ( Status( "action" ) == actionPortfolio )
>   > >> > {
>   > >> > bo = GetBacktesterObject();
>   > >> >
>   > >> > for( sig = bo.GetFirstSignal(); sig; sig = bo.GetNextSignal() )
>   > >> > {
>   > >> > if( sig.IsExit() )
>   > >> > {
>   > >> > if ( sig.IsLong() ) // Exit Long
>   > >> > {
>   > >> > ExitTrue = sig.Price - Slippage;
>   > >> > sig.Price = ExitTrue;
>   > >> > }
>   > >> >
>   > >> > else // Exit Short
>   > >> > {
>   > >> > ExitTrue = sig.Price + Slippage;
>   > >> > sig.Price = ExitTrue;
>   > >> > }
>   > >> >
>   > >> > }
>   > >> > }
>   > >> > }
>   > >> >
>   > >>
>   > >
>   > >
>   > > 
>   > 
>   > 
>   > 
>   > -- 
>   > Cheers
>   > Graham Kav
>   > AFL Writing Service
>   > http://www.aflwriting.com
>   >
>




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

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