andy, my knowledge of this stuff is a little rusty but adding this code on top of your code seems to work. You can also place this code in a seperate file (e.g. andy_cbi.afl) and call it from your main code using for instance:
SetCustomBacktestProc( "C:\\AmibrokerAFL\\replies\\andy_cbi.afl" );
I added the files,
regards, Ed
SetOption("UseCustomBacktestProc", True );
if( Status("action") == actionPortfolio ) {
bo = GetBacktesterObject();
bo.PreProcess(); // Initialize backtester
for(bar=0; bar<BarCount; bar++)
{
// loop through open positions and count them
cntPositions = 0;
for( openpos = bo.GetFirstOpenPos(); openpos; openpos = bo.GetNextOpenPos() )
{
if (openpos.IsLong() OR !openpos.IsLong() )
{
cntPositions = cntPositions + 1;
}
}
// loop through signals
for ( sig = bo.GetFirstSignal(bar); sig; sig=bo.GetNextSignal(bar))
{
// for new entries while still in a position remove funds
if ( sig.IsEntry() AND cntPositions > 0 )
{
sig.PosSize = 0;
}
}
bo.ProcessTradeSignals(bar);
}
bo.PostProcess(); // Finalize backtester
}
----- Original Message -----
From: Andy
To: [email protected]
Sent: Thursday, January 29, 2009 12:40 PM
Subject: [amibroker] Re: Sell and Buy on different days
This is got to be a very simple task but unfortunately AmiBroker told
me that I would have to write Backtester Interface code for this. I'm
sure this has been done a million times. Anyone have sample code?
I'm using EOD data to trade one stock at a time from a basket of
stocks. The problem is that a selling of a stock can occur on the
same day as a buy of *another* stock. Of course the problem is that
the sell trade can occur after the buy trade.
--- In [email protected], "Andy" <se...@...> wrote:
>
> How do I fix the below code so it doesn't buy a different stock on a
> sell day?
>
> --------------------------------------------------------
> // Backtester Options
> SetOption("MaxOpenPositions", 1 );
> SetOption("AllowSameBarExit", False);
>
> // Optimization numbers
> BuyPeriod = Optimize("BuyPeriod",16,10,20,2);
> BuyFactor = Optimize("BuyFactor",1.2,0.5,1.5,.1);
> SellPeriod = Optimize("SellPeriod",20,10,20,2);
> SellFactor = Optimize("SellFactor",0.8,0.5,1.5,.1);
>
> // ATR formulas
> TodaysBuyTarget = High - BuyFactor * ATR(BuyPeriod);
> YesterdaysBuyTarget = Ref(High,-1) - BuyFactor *
Ref(ATR(BuyPeriod),-1);
> YesterdaysSellTarget = Ref(Low,-1) + SellFactor *
Ref(ATR(SellPeriod),-1);
>
> // Buy/Sell signals and prices
> Buy = YesterdaysBuyTarget > Low;
> BuyPrice = IIf(YesterdaysBuyTarget > Open, Open, YesterdaysBuyTarget);
> Sell = YesterdaysSellTarget < High;
> SellPrice = IIf(YesterdaysSellTarget < Open, Open,
YesterdaysSellTarget);
> Buy = ExRem(Buy,Sell);
> Sell = ExRem(Sell,Buy);
>
andy_cbi.afl
Description: Binary data
andy.afl
Description: Binary data
