Hello everyone,
I want to develop a portfolio strategy (mean reversion), using a backtest, which uses buy entries with limit orders on the day following the bar which fulfills the conditions for a possible buy on the next day.
Also I want to use the same backtest to show me when to enter orders according to the strategy (i.e. to be able to implement the strategy). After the backtest I can see the completed trades and the open trades. I have been able to display information about open trades for which the sell condition is fulfilled, so that I know for which stocks I have to enter the respective sell orders for the next day.
Where I have found no solution so far is how to access from the bar which fulfills the condition for a possible buy on the next day. I need to access this to be able to conclude whether buy orders have to be placed for the next day.
I looked at all Amibroker functions (escpecially for custom backtesting), but either I don't see a solution which is actually documented, or it is maybe not obviously documented. I also did a search in the forum and the internet without success. The Signal objects which I can loop through are not sufficient, because they are related to the buy bars themselves, so are one bar too late for getting the infos I need.
I hope my description is understandable. If not, please tell me. Has anybody an idea how I can proceed? By the way, I am new to Amibroker, but not to programming.
Thanks in advance, tomcat
There are multiple ways to solve your problem. One is to use an Exploration which outputs rows for any symbols that have fulfilled the "Setup" condition. However, this will show you ALL symbols that met the condition, not just the number of Symbols that you will actually place orders for. For example, if your max open trades is set to 5 and you currently have 3 open, then you should (conservatively) only place orders for the two top-ranked symbols.
To resolve that issue, you can run a backtest with the "Add artificial future bar" option turned on in the Analysis Settings. Now your backtest will show trades for the day following the last day of data in your database. You'll just need to make sure that on the final (artificial) day of the backtest, you enter a "trade" even if the limit price is not breached. Those "trades" shown for tomorrow are actually your entry orders.
Thank you Matt @mradtke, using "Add artificial future bar" seems to be the solution for me.
But I have trouble getting any result using it. I changed my buying conditions to find out which gives me a buy for the artificial future bar. But when using this condition, the symbol does not show in the results. I suspect that I do not correctly specify the last bar. I use (excerpt)
Buy = <something>;
BuyWithoutPriceCondition = Buy;
Buy = Buy AND Low < BuyLimitPrice;
Buy[BarCount-1] = BuyWithoutPriceCondition[BarCount-1]; // Set Buy for last bar
BuyPrice = <something>;
BuyPrice[BarCount-1] = Close[BarCount-1]; // Set buy price for last bar
Do you have an idea? Or is there some other setting I have to do before "Add artificial future bar" works?
Thanks,
tomcat
That code looks like it should work, but it will likely give incorrect results unless you have a very small trading universe. More on that below. You have not provided enough detail to really confirm anything, so the best I can tell you is to use the Scan and Exploration features (neither of which use the artificial future bar, unfortunately) to verify your assumptions. Then add _TRACE statements that can output interesting data from the AFB during a backtest.
Why your results will likely be incorrect: Assuming that you have a watchlist that is large enough that in live trading you cannot always place orders for every stock meeting the Setup conditions, then to properly implement a backtest using a limit entry you will need a CBT. I have posted about this several times, but you can find a good discussion here: PositionScore / Ranking for trades taken “next day at limit” - #6 by mradtke . I suggest reading through the entire post until it "clicks". TL;DR: You are "cheating" by checking the limit price during Phase 1 of the backtest.