Pair trading setup with aux fields

Hi!

I am testing pair trading strategies, and I thought I had a great idea. I produced a csv of a pair with the ratio of the close as the C, and the close price of stock A as aux1, and stock B as aux 1. Then I imported the instrument with the CSV-wizard. My idea was then to trade the ratio, but set the long and short price to aux1 and aux2, to represent both trades in the stocks. However, that would require AB to be able to be both long and short in this "fictional" instrument that is the ratio, and that does not seems possible.

Does anyone know if there is any way to have AB to be long and short in the same time in the same instrument?

Or does anyone have a workaround?

Cheers!

Is there a reason you're not just trading Stock A and Stock B? You could calculate the price ratio on the fly instead of precalculating and importing the ratio.

1 Like

Yes.. I have 4 other fields as well with calculations made in Python that I think will take to long to calculate on the fly.

Also, I need to keep track of all possible combinations of stocks, and in this way, the pair-file itself contains all data I need.

Don't try to adjust the software to your thinking. This rarely ever works because assumptions as to something "should" be are "by design" wrong. Do otherwise - adjust your thinking to the software. Learn how software operates and adjust your thinking to the way it actually operates.
AmiBroker can do lots of calculations very fast.
And you should do as @mradtke told you.

3 Likes

Make a copy of the "fictional instrument," and now you have two same instruments to go long and short separately. Combine the equity curves.

SetCustomBacktestProc("");
if( Status("action") == actionPortfolio )
{
bo = GetBacktesterObject();
bo.Backtest();
AddToComposite( bo.EquityArray,
"~INTRUMENT1", "X",
atcFlagDeleteValues | atcFlagEnableInPortfolio );
}

and same for ~INTRUMENT2.

EQ1 = Foreign("~INTRUMENT1","Close");
EQ2 = Foreign("~INTRUMENT2","Close");

EQ12 = 0.5*(EQ1 + EQ2);

Plot(EQ1,"EQ1",colorred,styleline);
Plot(EQ2,"EQ2",colorgreen,styleline);
Plot(EQ12,"EQ12",colorBlack,styleline);

AddToComposite(EQ12, "~EQ12", "C");

Buy =1;
Sell=0;
buyprice=Close;

It may need some more work. I don't know if this will work for you. I use this code to combine 10 separate systems.

Thank you. Just making a duplicate file, and perhaps postfix them "_long" and "_short", would open up the possibility for my initial idea, looking at the instrument name to decide to long or short. That would minimize the risk of using wrong entry/exit prices, but as Tomasz points out, bending the software this way usually not the best approach.

Thank you for you input!

Even if you want to calculate the ratios and other values externally and import them, you can still trade the actual stocks. One way to do this is to access the ratios using Foreign() or SetForeign(). Another way is to generate Buy/Sell/Short/Cover signals for the ratios during Phase 1, but actually enter trades for the stocks during Phase 2 (CBT).

Thank you! That is actually a structure that resonates even more with me. Precalculation everything and storing all related data in a syntetic instrument, then in the backtest trade the stock by looking at those number with Foreign(). That could also be a way to solve the next problem: not having multiple positions in one stock that is part of several pairs. When evaluating the stock, the backtest can rank all possible setups and pick the best looking.