AFL real time trading

Hi everyone,
I've been using Amibroker since 2014 as a backtester for my trading systems.
Now I decided to use it as trading system (by IBController).

Quick search of the forum didnt show me any related topics. So let me ask you here.

As I understand for real time trading it can de done by:

  1. Autorepeat/Scan explore cycle.
  2. As chart indicator.

For both methods it seems that every cycle it will read the afl from clear except for StaticVars.
Is it possible that alf reads functions and some temporary variables only once?
So I decided to use afl as indicator with main index chart having one while cycle at the end of a script and all variables and functions above it.

There is a simple example below. But there is a problem that text do_cycle doesnt appear. Looks like all graphix is drawn only at the exit of script each cycle.
Will it be able to request Foreign function Inside this while loop?

function NS_(x){return numtostr(x,format=1.0);} 
Enter_cycle=Param("Enter_Cycle?", 0, 0, 1, 1,0 );

while(Enter_cycle OR do_cycle)
do_cycle=Param("Do_Cycle?", 0, 0, 1, 1,0 );

GfxDrawText( NS_(Enter_cycle), 10, 20, 200, 100, format = 0 );

if (do_cycle==1)
GfxDrawText( NS_(do_cycle), 10, 50, 200, 100, format = 0 );



When I did backtests I didnt think deep how AB really works except for genaral article that Tomasz refers .
Can you suggest how to do that? What is your solution those who already done it?

Each time a new quote arrives input data change therefore you can’t use old variables. You really want to see the changes, otherwise it is pointless to run the code. Also indicator values change. They don’t start clean as you wrote but they are filled with updated data. If you want to keep values of some variables use static declaration. It is here exactly for that purpose.

And no, don’t use endless loops and busy waiting.

Rendering is separate from AFL execution and it is done after AFL has completed.

1 Like

Thank you Tomasz!
Any solution to keep functions in memory and not re-read them with every chart update?

I don't know what you mean to keep "functions" in memory. They are in memory. Where else do you think they are. Function is an algorithm. Nothing more.

1 Like

I used it for a while and it worked most of the time. It is free and unsupported. Now I use AB for signal generation only. All order execution is via Introduction — ib_insync 0.9.71 documentation

The strength of AFL is fast array processing. Order execution is better handled by Python.


Ok. Sorry. I want to know how AB works deeply so that my afl script work faster.
Since functions are in same afl script in indicator chart are they evoked and compiled every refresh time?

Thank you. Why do you prefer Python and not IBController? Is it more stable?

No, formulas aren’t re-read from disk. They stay in RAM. Parsing has near zero impact on total execution time. That is not what you should be worried about.

As far as Ibcontroller is considered it is equally reliable to python. It relies on same IB API and it is TWS that does the job really, not the thin interfacing layer should it be IBC, python or whatever else.

1 Like

How do you send signal from AB to ib_insync? Do you use embedded python for afl?

Using a batch, I write the exploration to CSV, then call the python script in the batch, to read the CSV and execute the orders. I record my orders in an SQL db. The SQL ability of Python is vastly superior to AFL and the logging is also must better. Trading in real time and the automating of systems requires significant effort to work properly, without excessive hand holding by you. The logging and SQL features of Python provides for this very well.


Trading in real time and the automating of systems requires significant effort to work properly, without excessive hand holding by you.

This is so true. And even then, it is still best to keep a very close eye on everything that is happening.

It is a more complex task that it first appears. There are so many things to the live trading/automated part that need to be considered that a trader does not think of when they first take on coding up a system. It takes a while as well.

I do everything in Ami @zoringer as I find that it works well, is fast, and basically does everything that I want - maybe not the way that I want it done sometimes, but in general it is pretty flexible.

1 Like

I use the batch approach as well, with signal generation done by amibroker periodically and written to a file, which is picked up by helper scripts/programs for further processing. i.e All associated trading nuances is done outside amibroker ( position sizing, placing the trade to various brokers/passing signal to downstream applications etc). I have been trading systems fully automated this way for many years, with absolutely minimal manual intervention. I use twilio's automated calls, which is triggered by scanning log files every minute for errors - an approach you can explore once you are confidant in your programs and do not enjoy watching market all day :slight_smile:

I also find it convenient to keep the AFLs clean with only trade signal generation contained in it


This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.