Australian (ASX) Stock Market Forum

Amibroker question - Buy and SetTradeDelays

Joined
29 January 2010
Posts
83
Reactions
0
hi all,

I am in the process of writing a simple trading system to backtest and looking for some assistance.

1. When I enter my buy condition e.g. Buy = MA(C,15) > MA(C,40) what price does it use? The Close price?

2. What price (i.e. O, C, H, L) does SetTradeDelays(1,1,1,1) use? Or does it? I am assuming it is the Open price (of the following day)?

3. Also, if I am looking to determine my position size based on the entry price, how do I do this? Does the following hold up?

PositionSize = (Risk/TrailStopAmount)*BuyPrice;

Where risk = 2% of my capital, TrailStopAmout = 2 * ATR(10) and BuyPrice = ??? (Open of the following or current day?)

As you can see I am not clear what prices are used in each of these conditions.

Thanks in advance :)
 
Hi Superboot --

Until you get into writing more complex code, this is how AmiBroker works:

The prices used for entry and exit are those specified by the reserved variables BuyPrice, SellPrice, ShortPrice, and CoverPrice.
They can be set either:
1. By using the Trades Tab on the Backtester Settings dialog box. Pull down the BuyPrice menu and select Open if you want the price to be the Opening price. Or select Close if you want to use the Closing price. The other options in the list, High, Low, and Average, are not usable for a system that will be traded.
2. By using a statement in the afl code. To specify the Opening price, put this statement at the top of your code:
BuyPrice = Open;

Statements in the afl code take precedence over settings made on the Settings dialog box. I recommend that you always write code in the afl that explicitly sets the execution price.

The number of bars of delay between the bar on which the signal is generated and the bar on which the trade execution takes place is called the trade delay. It can also be set in either of two places:
1. On that same Trades tab. Enter an integer in the BuyDelay box. 0 means execute on the same bar as the signal. 1 means execute on the bar following the signal.
2. By using a statement in the afl code. The afl statement is:
SetTradeDelays(k,l,m,n);
where k is the delay for the Buy, l is the delay for the Sell, m is the delay for the Short, and n is the delay for the Cover. All are integers.

So ---
If you have a long-only system, and you want to generate signals at the close of the day and execute at the next day's opening price (called NextDayOpen, NDO, or MarketOnOpen, MOO), put these three statements at the top of your afl code:

BuyPrice = Open;
SellPrice = Open;
SetTradeDelays(1,1,1,1);

I hope this helps,
Howard
 
Hi,

I have similar question.

My signals are generated on today’s close and I buy on yesterday’s open.
BuyPrice = Open;
SellPrice = Open;
SetTradeDelays(1,1,1,1);

To buy specific number of shares I use the formula
PositionSize = (0.02/Stop)*BuyPrice;

Stop is my initial stop loss, for example stop = 3 * ATR(20);

And now, to buy appropriate number of shares I have to know my today’s risk (stop), so have I write something like that?

BuyPrice = Open;
SellPrice = Open;
SetTradeDelays(1,1,1,1);

Buy = cross(C, EMA(C, 50);

Stop = ValueWhen(Buy, 3 * ATR(20));
ApplyStop(stopTypeLoss, stopModePoint, stop);
PositionSize = (0.02/Stop)*BuyPrice;

When the ValueWhen is not used (only stop = 3 * ATR(20);) I thin Amibroker, to assess Stop, is taking data from day after the signal is generated (so I can’t use this data to calculate number of shares).

Thanks and sorry my English :)
 
Hi,

I have similar question.

My signals are generated on today’s close and I buy on yesterday’s open.
BuyPrice = Open;
SellPrice = Open;
SetTradeDelays(1,1,1,1);

The words describe a situation that can not be executed.

The words and the code are inconsistent.

Describe again when the signal is generated and when the position is entered.

Thanks,
Howard
 
The words describe a situation that can not be executed.

The words and the code are inconsistent.

Describe again when the signal is generated and when the position is entered.

Thanks,
Howard

I am EOD trader. My signal is generated based on today’s close and position is taken on tomorrow’s open:

BuyPrice = Open;
SellPrice = Open;
SetTradeDelays(1,1,1,1);

And now I want to calculated number of shares.
To assess appropriate number of shares to buy (when the signal is generated) I have to know my stop today, so should I use formula?

Stop = ValueWhen(Buy, 3 * ATR(20));
PositionSize = (0.02/Stop)*BuyPrice;

I think, without ValueWhen function, stop and number of shares, are calculated when position is taken ( next day when system gives me buy signal).

Thanks in advance
Rafal
 
So ---
If you have a long-only system, and you want to generate signals at the close of the day and execute at the next day's opening price
Hello Howard,
Recently purchased your A/B intro book and i am working my way through it,so far so good!
Just with regard to the above quote,how do i code the alternative buy delay of ;
Buy on close of bar after a close above close of signal bar,which could be 2 or 3 bars after the buy signal.
Hope this makes sense.

Sparfarkle
 
Hi Superboot --

Until you get into writing more complex code, this is how AmiBroker works:

The prices used for entry and exit are those specified by the reserved variables BuyPrice, SellPrice, ShortPrice, and CoverPrice.
They can be set either:
1. By using the Trades Tab on the Backtester Settings dialog box. Pull down the BuyPrice menu and select Open if you want the price to be the Opening price. Or select Close if you want to use the Closing price. The other options in the list, High, Low, and Average, are not usable for a system that will be traded.
2. By using a statement in the afl code. To specify the Opening price, put this statement at the top of your code:
BuyPrice = Open;

Statements in the afl code take precedence over settings made on the Settings dialog box. I recommend that you always write code in the afl that explicitly sets the execution price.

The number of bars of delay between the bar on which the signal is generated and the bar on which the trade execution takes place is called the trade delay. It can also be set in either of two places:
1. On that same Trades tab. Enter an integer in the BuyDelay box. 0 means execute on the same bar as the signal. 1 means execute on the bar following the signal.
2. By using a statement in the afl code. The afl statement is:
SetTradeDelays(k,l,m,n);
where k is the delay for the Buy, l is the delay for the Sell, m is the delay for the Short, and n is the delay for the Cover. All are integers.

So ---
If you have a long-only system, and you want to generate signals at the close of the day and execute at the next day's opening price (called NextDayOpen, NDO, or MarketOnOpen, MOO), put these three statements at the top of your afl code:

BuyPrice = Open;
SellPrice = Open;
SetTradeDelays(1,1,1,1);

I hope this helps,
Howard

QUESTION

I was backtesting my strategy and achieving pleasing results before I read Howard Bandy answer to a members question wanting to know the formula to execute a BUY the next day after the signals is generated - meaning the BUY is execute at the next day's opening price.

As I that's how I plan to trade I added this code to my strategy.

Once I added the code below to my strategy my Backtest results went from Net Profit of $26,044 to a Net Loss of -$3,534

I wouldn't have thought buying the next day compared to buying the same day would make such a difference to my Backtest results.

Can anyone shed light on how this could be possible?

The Code I added
/////////////////////////////////////////////////
BuyPrice = Open;
SellPrice = Open;
SetTradeDelays(1,1,1,1);
/////////////////////////////////////////////////

skate.
 
QUESTION

I was backtesting my strategy and achieving pleasing results before I read Howard Bandy answer to a members question wanting to know the formula to execute a BUY the next day after the signals is generated - meaning the BUY is execute at the next day's opening price.

As I that's how I plan to trade I added this code to my strategy.

Once I added the code below to my strategy my Backtest results went from Net Profit of $26,044 to a Net Loss of -$3,534

I wouldn't have thought buying the next day compared to buying the same day would make such a difference to my Backtest results.

Can anyone shed light on how this could be possible?

The Code I added
/////////////////////////////////////////////////
BuyPrice = Open;
SellPrice = Open;
SetTradeDelays(1,1,1,1);
/////////////////////////////////////////////////

skate.

See reply in Amibroker FAQ thread.
 
Top