hi Marlin, BuyAdjusted I define myself, see example. Probably this can be done with arrays only but I have done it in the past like this so I use this example. I defined Sell with a number 5 but 1 can be used as well.
See example and chart. This code enters on the limit. As a limit I take the low
of the day on which I get the signal. I enter the next bar. If the price goes
below the low then the trade is entered. If the price does not go below the low
the a "void" trade is created (in the chart I show an open circle). The trade
is entered at the open but the exit is on the same price also on the open. This
way the money will be reserved for this trade and will simulate that your money
sits in the market waiting fro this trade and is not used for another trade,
rgds, Ed
procedure sellAtLimit_proc(Buy,BuyPrice,buyLimit,sellLimit) {
global Sell;
global SellPrice;
global BuyAdjusted;
global BuyPriceAdjusted;
// initialise arrays
SellPrice = 0;
Sell = 0;
BuyAdjusted = 0;
BuyPriceAdjusted = 0;
for (i = 1; i < BarCount; i++) {
// case where it is likely to enter a long position
if (Buy[ i ] == 1 AND Low[ i ] < buyLimit[ i ]) {
// buy at limit
BuyAdjusted[ i ] = 1;
if (Open[ i ] < buyLimit[ i ]) {
BuyPriceAdjusted[ i ] = Open[ i ];
} else {
BuyPriceAdjusted[ i ] = buyLimit[ i ];
}
// find a sell position + sellprice
for (j = i; j < BarCount; j++) {
if (O[ j ] > sellLimit[ j ]) {
Sell[ j ] = 1;
SellPrice[ j ] = O[ j ];
i = j;
break;
} else if (O[ j ] < sellLimit[ j ] AND H[ j ] > sellLimit[ j ]) {
Sell[ j ] = 1;
SellPrice[ j ] = sellLimit[ j ];
i = j;
break;
} else if (j == BarCount - 1) {
i = BarCount;
}
}
} else if (Buy[ i ] == 1 AND Low[ i ] >= buyLimit[ i ]) {
// enter and exit at the same price and time ("VOID" trade)
BuyAdjusted[ i ] = 1;
BuyPriceAdjusted[ i ] = Open[ i ];
Sell[ i ] = 1;
SellPrice[ i ] = Open[ i ];
}
}
} // end procedure
SetBarsRequired(10000,10000);
SetOption("MaxOpenPositions", 100 );
PositionSize = -5;
SetTradeDelays(0,0,0,0);
PositionScore = Random();
pds = 10;
mav = MA(C,pds);
Buy = StochK(pds) < 15;;
Buy = Ref(Buy,-1);
BuyPrice = O;
buyLimit = Ref(L,-1);
sellLimit = Ref(mav,-1);
sellAtLimit_proc(Buy,BuyPrice,buyLimit,sellLimit);
Buy = BuyAdjusted;
BuyPrice = BuyPriceAdjusted;
SetChartOptions(0, chartShowDates);
GraphXSpace = 5;
Plot(C,"C",1,64);
Plot(sellLimit,"SellLimit",colorGold,styleThick);
PlotShapes(IIf((Buy AND !Sell),shapeUpTriangle,0),colorWhite, layer = 0,
yposition = BuyPrice, offset = 0 );
PlotShapes(IIf((Sell AND !Buy),shapeDownTriangle,0),colorYellow, layer = 0,
yposition = SellPrice, offset = 0 );
PlotShapes(IIf((Buy AND Sell),shapeHollowCircle,0),colorAqua, layer = 0,
yposition = BuyPrice, offset = 0 );
----- Original Message -----
From: redliontrader
To: [email protected]
Sent: Thursday, July 26, 2007 10:10 PM
Subject: [amibroker] Re: Custom Backtest Software
Ed,
Cute trick.. I understand in principle, but I need a little
clarification on your code. If you could answer these questions.
Are the BuyAdjusted and BuyPriceAdjusted vectors special variables
that get used by the backtester?
Why did you set Sell to 5 instead of 1?
My exit is when today's close > 10 day moving average. I can't see
how placing that in the (>>> exit code here) would work since it will
only execute during a buy? help?
You have already cleared up some mystery for me.
Thanks ed for your help
redlion (marlin)
--- In [email protected], "Edward Pottasch" <[EMAIL PROTECTED]>
wrote:
>
> hi,
>
> you will not need the CBI for this. The trick here is to select
your signals as you normally do. However, if the limit is not hit you
want to exit at the same bar at the same price. The backtester will
now only charge commission. The backtester now reserves this amount
of money for this trade however it will not buy anything.
>
> As an example I show a snapshot of how you can do this, Ed
>
>
>
> // case where it is likely to enter a long position
> } else if (Buy[ i ] == 1 AND Low[ i ] < buyLimit[ i ]) {
>
> >>>>> exit code here
>
> // case where limit is not reached, creat a "VOID" trade
> } else if (Buy[ i ] == 1 AND Low[ i ] >= buyLimit[ i ]) {
>
> // enter and exit at the same price and time ("VOID" trade)
> BuyAdjusted[ i ] = 1;
> BuyPriceAdjusted[ i ] = Open[ i ];
>
> Sell[ i ] = 5;
> SellPrice[ i ] = Open[ i ];
>
> }
>
>
> ----- Original Message -----
> From: redliontrader
> To: [email protected]
> Sent: Wednesday, July 25, 2007 12:58 AM
> Subject: [amibroker] Custom Backtest Software
>
>
> Attempting to work my way up the learning curve. I am trying to
> implement a system that scans through a list of stock, finds the
10
> best trades for the day and then enters limit orders at 2% below
the
> current day's close for those 10 trades.
>
> I want to be able to backtest my system. I don't think the
standard
> portfolio testing will work because my system uses limit orders
to buy.
> So they may fill or may not fill. I can't quite get my head
around
> the backtesting object to get started.
>
> Any help would be appreciated.
>
> RLT
>
<<exp.png>>
