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

[amibroker] Re: How can a Low level backtest access AFL function values ??



PureBytes Links

Trading Reference Links

You can use AddToComposite in the main AFL code to create a new
composite symbol of the ATR, then Foreign in the custom backtester to
get it realigned with the backtester bars.

For example, in the main AFL code:

AddToComposite(2*ATR(30), "~2atr30_"+Name(), "C", atcFlagDefaults |
atcFlagEnableInBacktest);

Then in custom backtester:

atr30 = Foreign("~2atr30_"+sig.Symbol, "C");

Your conditional statement would then be:

if (pos.GetPrice(bar,"C") < pos.GetEntryValue() -
atr30[bar-pos.BarsInTrade])

Note though that I don't think this is what you want, as the entry
value is the total dollar value of the position, not the share price.
To compare prices you can use the EntryPrice property.

I'm also not sure why you want to subtract BarsInTrade from the array
index. For one thing, this will give an out-of-bounds error whenever
'bar' is less than BarsInTrade.

Regards,
GP


--- In amibroker@xxxxxxxxxxxxxxx, "joelamibroker" <joelamibroker@xxx>
wrote:
>
> Hello,
> 
> 
> How can you access AFL function values that work on price arrays in a
> low level backtest??
> 
> I am attempting to handle multiple signals using Custom Backtester.
> In version 4.76.0 Tomasz introduced the "Handle" property.
> 
> In this simple example I want to use "Handle" to exit if share has
> dropped 2xATR(30) from BuyPrice.
> (note: 2xATR(30) is calculated on the 'Buy' day)
> 
> The middle line of code is obviously incorrect, but HOW should it be
> written?
> 
> for( pos = bo.GetFirstOpenPos(); pos; pos = bo.GetNextOpenPos() ) 
> {
>   if(pos.GetPrice(bar,"C") < pos.GetEntryValue() -
> 2*ATR(30).[bar-pos.BarsInTrade])
>    	bo.ExitTrade(......);
> }
> 
> 
> Any ideas on how this can be done would be appreciated.
> 
> 
> Complete code is listed below:
> 
> 
> /* Backtest is carried out on multiple symbols*/
> 
> Buy = Cross(C,Ref(HHV(H,20),-1);
> BuyPrice = Open;
> Sell = 0;
> SellPrice = 0;
> 
> if( Status("action") == actionPortfolio )
> {
>    // actual backtest routine(low-level)
>    bo = GetBacktesterObject();
>    bo.PreProcess();
> 	
>    for( i = 1; i < BarCount; i++ )
>    {
>       // first update backtest stats and handle stops
>       bo.UpdateStats( i, 0 );
> 		      
>       for( sig = bo.GetFirstSignal(i);sig; sig = bo.GetNextSignal(i) )
>       {
>       	if (sig.IsLong() AND sig.Price != -1)
> 	{
> 	  bo.RawTextOutput("Bar " + i + " Symbol: " + sig.Symbol);
> 	  bo.EnterTrade(i, sig.Symbol, True, sig.Price,-10);
>   	  for(pos=bo.GetFirstOpenPos();pos;pos = bo.GetNextOpenPos() ) 
> 	  {
> 	     bo.RawTextOutput("Open Pos Found For: " + pos.Symbol);
> 	     /* --- Now Process Exit ---*/
> 	     /* --- This won't work  ---*/
> 	     /* --- Have we closed 2xATR(30) below entry price? */
> 	     if(pos.GetPrice(i,"C") < pos.GetEntryValue() -
> 2*ATR(30).[i-pos.BarsInTrade])
> 	     bo.ExitTrade( i+1, OpenPos.Handle, OpenPos.GetPrice( i+1, "O" ) );
> 	
>      	  }		
> 
> 	}
>         bo.UpdateStats( i, 2 );
>     }
>     bo.PostProcess();
> }
>




Please note that this group is for discussion between users only.

To get 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/