Advice on How to Process Historical Intraday Trade List with only Signed Trade Quantities


I read Tomasz’s code sample in “How to Generate Backtest Statistics from a List of Historical Trades Stored in a File”. I want to modify it to process InteractiveBrokers’ trade fill file, which has fill time stamps and signed trade quantities – the sign being designed in the “Action” column by “BOT” or “SLD”. There appears to be a number of changes I need to make and I need some help to figure out the best way to achieve this:

  1. Process fills that all happen in the same intraday bar. Do I need to aggregate these fills in my code or is there already an built-in facility in Amibroker to do this?
  2. The scale-in, scale-out sample code in “Pyramiding” seems to suggest that one has to be able to figure out if a trade is “Buy”, “Short”, “Sell”, or “Cover”. Given the trade list only has signed trade quantity, do I need to keep a running position which processing the file to figure out, for example, whether a positive quantity fill is a “Buy” (scale-in) or “Cover” (scale-out), by either doing it in a pre-processing stage (e.g. using Python) or in AFL?
  3. Related to 2 above, there may be “stop and reverse” trades in which a long position is changed to a short position in a single fill – allowed in IB TWS but not necessarily in other platforms such as Tradestation. How do I handle these trades? Do I need to manually break the single fill into 2 trades, one closing the long position and the other opening the short position?
  4. As the symbology used by IB is not necessarily the same as that of the backtest database, what is the best way to do the ticker mapping, assuming I use an “exception”-based dictionary, e.g. if the IB symbol is in the dictionary, use the dictionary entry to translate to the target database symbol; Otherwise, use the IB symbol. I know how to do this cleanly in Python, but I hope to minimize non-AFL code to benefit all in the Amibroker community when I upload my solution into this Forum.

Thanks in advance for your thoughts.

With regards to ticker mapping you can use Alias field (one instrument then has Symbol and Alias
and can be identified by either).

Alias can be found from symbol using single line of code:

alias = GetFnDataForeign("Alias", symbol ); // v5.50 required

Ok, Tomasz. Do you have any thoughts on the other 3 items on the list? Thanks again for spending time on my question.

Tomasz, fxshrat, helixtrader, milosz and other active expert posters on the forum:

While I listed specific issues in designing a solution in my original post, I don’t mean to ask you to give me specific programming help. All I need is a pointer or two, like “Hey Newbie, have you read this section of the Manual” or “This code posted from 10 years ago probably can be used a template for your solution”.

Thanks for your kind attentions.

WIth regards to point 1 - it is better (simpler) to have max one entry signal per bar, so it is better to aggregate. If you don’t to do that, you would need to do all the work in low-level CBT and call EnterTrade/ExitTrade for each and every entry/exit. That is a lot of work.

With regards to point 2 & 3 - yes you should now which side you are trading, so you would need to process trade fill file so you know what side you are currently in.

Thank you, Tomasz for your inputs. I will proceed to code and post my working code here when I am done.