AnalysisDocs - specify symbol to run on

Hello,

How to tell AmiBroker's OLE Automation Object Model to run a specific .apx on a specific symbol? As I understand this is possible only by (1) creating a watch list which has one symbol (so >100 watchlists) and then (2) specifying the watchlist in .apx file with using tags 2 and then use and set 17 to use specific list number 17. No way to put name of a Watch List here, I guess.

Am I correct or is there a simpler way on how to set a on which symbol .apx should be run?

Reason for asking: I am trying to do automated backtesting of many (>100) systems on many symbols(>100): each system for one of the symbols (>100 x >100). So I am trying to use AmiBroker's OLE Automation Object Model because manually doing this is not possible for me.

Thanks.

Why don't you just run INDIVIDUAL BACKTEST?
It runs backtest for each symbol separately using the watch list you specified.

@Tomasz Thanks for your answer.

I am sorry but I do not see how this can help. Can you please explain.

What I need is a reliable way to set current symbol (or filter). I have the following Python code and whatever I try, symbol just isn't changed:

dataBasePath = "C:\\Program Files\\AmiBroker\\Databases\\EODDB"

import win32com.client as wcl

# create an instance of Amibroker
try:
    ab = wcl.Dispatch("Broker.Application")
    print("[INFO] Amibroker instance created successfully.")
except Exception as e:
    print("[ERROR] Exception occurred while creating Amibroker instance: ", e)

# open a database
try:
    retLoadDatabase = ab.LoadDatabase(dataBasePath)
    if retLoadDatabase == True:
        print(f"[INFO] Database loaded successfully for dataBasePath={dataBasePath}")
    else:
        print(f"[ERROR] Error loading database dataBasePath={dataBasePath}.")
except Exception as e:
    print(f"[ERROR] Exception occurred while loading database dataBasePath={dataBasePath}: ", e)

symbol = "QQQ"
if ab.Documents.Count > 0:
    selectedDocument = ab.Documents.Item(0)
    selectedDocument.Name = symbol
    selectedDocument.Activate()

# Just run a simple AFL/APX Buy = C > 0; so that symbol really gets activated
Cgt0ApxFile = "C:\\git\\Trading\\Amibroker\\Cgt0.apx"

try:
    analysis = ab.AnalysisDocs.Open(Cgt0ApxFile)
    print(f"[INFO] Analysis opened successfully for tradingIdeaAFLFormulaFile={Cgt0ApxFile}")
except Exception as e:
    print(f"[ERROR] Exception occurred while opening analysis tradingIdeaAFLFormulaFile={Cgt0ApxFile}: ", e)

retAnalysisRun = analysis.Run(2)

You wrote in your first post:

I am trying to see the entire forest instead of the trees.
I am trying to focus on the final goal, not technical details, to find optimum solution, not just to push ineffective way.

If your GOAL is to run automated backtests of mutliple systems each on ONE symbol then the answer is running INDIVIDUAL BACKTEST as I pointed out earlier:

You do NOT NEED Ole automation at all.

AmiBroker will run your multiple backtest automatically, without need to write ANY code in python and ANY code using OLE at all.

If you need different system for each symbol that is easy too:

if( Name() == "MSFT") 
{
   #include "system_for_msft.afl"
}

if( Name() == "AAPL") 
{
   #include "system_for_aapl.afl"
}

@Tomasz Thank you for your answer.

I am already doing what you are suggesting. Even more...

Let me describe my "workflow":

  1. I take an AFL formula (has only simple entry and exit conditions) generated from "AFL formula generator" outside of Amibroker saved as "SYMBOL_STRATEGY_NAME_IDEA_ID.afl". One of the main "features" of this formula is your if strleft(Name(), 4) == "AAPL" (why strleft will be clear below)
  2. Then I generate backtesting afl file named "SYMBOLN_STRATEGY_NAME_IDEA_ID-BACKTEST_variation.afl" (notice N at the end of symbol) where I add different settings and parameters and stop loss, take profit, time stop and filters (all these combinations need to be tested separately and combined). Before that I clone every symbol many times so I have SYMBOL, SYMBOL1, ..., SYMBOLN copies in the database - I absolutely need this to do portfolio backtesting later since I have many different "systems" for each symbol). "_variation" in the afl filename here represents different combinations of stops, take profits, filters, parameters,... Each of these files obviously has #include "SYMBOL_STRATEGY_NAME_IDEA_ID.afl"
  3. I then "package" these in .APX files and run (backtest) them.
  4. Then, if I like them (to like them I need to analyze them individually and how they work together) I add them to portfolio .afl file

There is no(!) way I can test all this using copy/paste and clicking on the Amibroker GUI. The only(!) way for me is to generate/edit .afl, .apx and .abb files and run them (.afl via .apx using OLE automation) and .abb by executing subprocess.

So, please, please, please, ..., please, tell me how I can reliably force Amibroker via OLE to execute .apx file on a specific symbol which I change from one OLE call to another.

Thank you.

Kind regards,
KKTrader

You did not read the message I wrote. Nothing that you are doing is needed.

You do NOT need to generate formulas dynamically
You do NOT need to do any OLE automation
You do NOT need to package anything in APX

Please re-read my answers carefully. All you need is single static formula file and single click on individual backtest.

Please DO read this:

and this:

@Tomasz Thank you for your answer.

I have read both articles you linked. Word by word, once again. Nothing in there is new for me. I knew all this before(!) I have asked the first question in this thread.

I want to use Amibroker super fast backtester and nothing else from Amibroker via OLE automation using Python. I have very good reasons, however irrational they might seem to you and however cumbersome the approach I have taken looks to you, so I want and will go down this path further.

So, once again, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please,..., please tell me how can I using OLE tell Amibroker to apply an .APX (.afl) file to a specific symbol just before calling Run on AnalysisDoc and in next iteration to some other symbol before applying some other .APX (.afl).

Thank you.

Kind regards,
KKTrader

Tomasz has answered your question and it is very hard to understand why you are not able to do it that way.
You can do individual backtest on a WL, and within each type of system test you can do #include like he has coded.

If you still want to go that way, there is a simple way for IPC like pipes. In each iteration, put your variables to a text file and READ that from the apx and then work on them. so you can read symbol from file in use and then apply Filter of if statement as mentioned by Tomasz.
Then when BT is done, set another pipe(file) to tell your py code its done.

Its is crude or rudimentary but maybe you can write afl to work from variables passed to it.

If you are running multiple BT, then each will have its own set of text files.