Australian (ASX) Stock Market Forum

A problem when backtesting in Amibroker

Joined
29 December 2015
Posts
1
Reactions
0
hi guys,
I'm kind of new in Amibroker and i have a problem when I try to back-test my strategy
when I write buy= cross(rsi(14), 70) the actual buy price is the open of the candle which is not what I'M looking for because most of the time is below 70.
in reality rsi has crossed the 70 line in a certain price. how could i model this.
cheers
 
hi guys,
I'm kind of new in Amibroker and i have a problem when I try to back-test my strategy
when I write buy= cross(rsi(14), 70) the actual buy price is the open of the candle which is not what I'M looking for because most of the time is below 70.
in reality rsi has crossed the 70 line in a certain price. how could i model this.
cheers

Buyprice = Close;
Buy= Cross(RSI(14), 70);

Will work if you want to enter during the closing auction. Otherwise, what trash said (below).
 
If your trade price is at open but the signal is at close then you have to add bar delay.

Code:
tradedelay = 1; // entry/exit bar delay

if ( tradedelay > 0 ) {
    pricearray = Open;
} else {
    pricearray = Close;
}

SetTradeDelays( tradedelay, tradedelay, tradedelay, tradedelay );
 
BuyPrice = SellPrice = pricearray;
ShortPrice = Coverprice = pricearray;

Buy = ;
Sell = ;
Short = ;
Cover= ;
 
Greetings --

It can be confusing to coordinate and synchronize signals and trades. I recommend this procedure, beginning before writing any code:

Assume the development has been successful and you are about to begin trading. Identify the time you will be gathering data and generating the signal, and the time you expect the trade to take place. Put yourself at the time of signal generation and identify what data is available and how any delays or references relate. Write the code to reflect those conditions.

Please make another posting to this thread explaining the conditions you want to backtest then trade, and I will post a template for the AmiBroker code.

Best regards,
Howard
 
hi guys,
I'm kind of new in Amibroker and i have a problem when I try to back-test my strategy
when I write buy= cross(rsi(14), 70) the actual buy price is the open of the candle which is not what I'M looking for because most of the time is below 70.
in reality rsi has crossed the 70 line in a certain price. how could i model this.
cheers

The attached formula from "June 2003 Stocks & Commodities Magazine" will output the required minimum buy price.

Code:
// RevEngEMARSI

eper = Param("EMA periods", 70, 1, 1000, 1 );
wper = Param("Wilder periods", 14, 1, 100, 1 );
Value = EMA( RSI( wper ), eper );
AUC = Wilders( Max( C - Ref( C, -1 ), 0 ), wper );
ADC = Wilders( Max( Ref( C, -1 ) - C, 0 ), wper );
x = ( wper - 1 )*( ADC * Value/(100-Value)-AUC);
RevEndEMARSI = IIf( x >= 0, C + x, C + x*(100-Value)/Value );
Plot( RevEndEMARSI, "Reverse EMARSI", colorRed );

The high price on the next bar would need to equal or exceed the minimum buy price to enter the trade.
 
Another issue with backtesting is getting a buy signal and a sell signal on the same day with no equity available to buy the new signal. With trade delay 1, Amibroker procedure sells the sell signal on next day open (no problems with that) but buys the buy signal on next day open as well. Not possible in reality because the sell equity has to be realised before the new buy can be made. Amibroker procedure has both happen simultaneously. This is wrong because equity has to be available. :bad:

It seems the only fix is to delay the buy signal to close price. I.e. have the sells at open and the buys at close same day. Is that the rough way around?
 
Another issue with backtesting is getting a buy signal and a sell signal on the same day with no equity available to buy the new signal. With trade delay 1, Amibroker procedure sells the sell signal on next day open (no problems with that) but buys the buy signal on next day open as well. Not possible in reality because the sell equity has to be realised before the new buy can be made. Amibroker procedure has both happen simultaneously. This is wrong because equity has to be available. :bad:

It seems the only fix is to delay the buy signal to close price. I.e. have the sells at open and the buys at close same day. Is that the rough way around?

From my take on a trading system, let's assume long for this exercise, you should never end up with a BUY signal being generated on the same bar as a SELL signal.

If you have an example I would be very interested in looking at the logic.

The above comment is meant as a constructive contribution so please do not take it in any other way.
 
Another issue with backtesting is getting a buy signal and a sell signal on the same day with no equity available to buy the new signal. With trade delay 1, Amibroker procedure sells the sell signal on next day open (no problems with that) but buys the buy signal on next day open as well. Not possible in reality because the sell equity has to be realised before the new buy can be made. Amibroker procedure has both happen simultaneously. This is wrong because equity has to be available. :bad:

It seems the only fix is to delay the buy signal to close price. I.e. have the sells at open and the buys at close same day. Is that the rough way around?

https://www.amibroker.com/guide/h_portfolio.html
 
From my take on a trading system, let's assume long for this exercise, you should never end up with a BUY signal being generated on the same bar as a SELL signal.

If you have an example I would be very interested in looking at the logic.

The above comment is meant as a constructive contribution so please do not take it in any other way.
Not the same stock signal. The detailed log of trades shows that cash is $1467.03 leading up to the day that the sell is made of BLD in line with the exit signal and a buy is made of RCG on the same day. I have my buyprice and sellprice delayed 1 day and at open price. This is not good so I will change the buyprice to close price delayed 1 day so the exiting stock is sold at the morning open.


Buy Sell Overlap.png

Thanks.
 
Consistent logic will not produce both a buy (to go long) and a sell (to exit the long position, not to go short) at the same time for the same tradable issue based on a single set of data. If the afl in question does that, there is a problem with implementation of the desired logic. Post the idea behind the program and the code that is giving conflicting signals and let the thread readers help.

That said, having trade delays other than zero causes difficulties in writing consistent code and executing resulting signals. An alternative it to pick a point in time where all of the information needed to compute the signals is available and the order can be placed with zero trade delay, then code the logic from there. It might be desirable to use one version of the program for development, testing, and validation. Followed by a separate version for trading once the development has provided the confidence that the system is sound.

Best,
Howard
 
Anyway for those who don't know and want to backtest EOD closer to reality---

What real figures we have to work with are Open, High, Low, Close and Volume sourced from a reliable data vendor.
What we can do nearer to realistically is buy/sell the day after the signal is generated since the signal is generated at a Closing price.
What we can do nearer to realistically is buy a small random percentage of the daily volume. 5% or less of daily volume at a guess but it is a restricter. The volume at open has to be enough to actually buy/sell but this opening volume figure is unknown so assume there. Buying a large chunk of daily volume is cheating.

When buying, there has to be enough free cash to buy. If, as in my example below with no free cash available, your system signals the day before a sell for one stock held and a buy for another stock, you can't expect those two events to happen simultaneously. You must sell one before buying the other. The backtester has the option of buying at O,H,L,C, but nearer to reality is Open or Close. There are two options I can see

a) Buy the new stock at Close price and the exiting stock can be sold at Open price
b) Don't buy the new stock until next day Open price (2nd day after signal) when free cash will be available

The backtester should work with free cash but assumes a buy of one stock is made and the sell of another stock can be made at Open price simultaneously. Not true.
 
Oh Jesus...

You can simulate anything!

Simply count number of open long positions in CBT in open positions loop and if they exceed then skip trade by setting position size to zero -> sig.PosSize = 0; or set sig.Price = -1; in signals loop. This is as regards to CBT. Then you won't have new Buy at open and Sell at same Open price (i.e if max positions is 5). Then it will wait for next buy signal at Open.

Without CBT code, marked light blue ones with same open price at same bar.
image.png

With CBT code sample entry at Feb 16th will be skipped
and it will be waited for new entry signal at any day's/bar's open price after that.
image.png
 

Attachments

  • image.png
    image.png
    10.3 KB · Views: 0
  • image.png
    image.png
    6.7 KB · Views: 0
Top