Pairs trading - Opened positions
Author: alsilva86
Creation Date: 2/8/2017 12:46 AM
profile picture

alsilva86

#1
Hi all,

I'm developing an algorithm for pairs trading, for while very straightforward, and somehow the algo is not "waiting" a pair close the initial position opened and is opening others in parallel. (See attach, please)

Could you help me with this issue, please? Any doubt let me know, please.

CODE:
Please log in to see this code.


profile picture

Eugene

#2
Hi,

1a. You cannot start the loop only after 30 bars (the MovAvgPer) since it uses at least 252*3 bars to build a SMA (corrected).
1b. Consider loading enough data (at least ~800 bars or more).
2. It's expected that this code runs in Single-Symbol mode. Executing it in Multi-Symbol Backtest mode can lead to incorrect results. Step on either symbol to run it.

Please try this out:
CODE:
Please log in to see this code.
profile picture

Cone

#3
Also, please read this wiki article for trading pairs in real time. Additions to the script are required to ensure synchronization with the symbol pair.
Choosing optimal trading tool: Strategy Monitor or Strategy window
profile picture

alsilva86

#4
Hi,

Thanks a lot all, its working fine now.

Now, I´m having a problem in the performance, in most cases the algo is having a much better result in long trades than in the short ones, with a positive Sharp in the first and much worse negative in the second.

I can´t find anythig else in the code, and I don´t know if its only due to market prices, i.e., due to the strategy itself.

Could you give a clue on this, please?

tks! best.

PS: Bars.Symbol = "PETR3.SA"

CODE:
Please log in to see this code.


Wealth-Lab Score 5,53 57,67 -64,00 -17,56
Sharpe Ratio 0,30 0,54 -0,40 0,03
Profit Factor 1,05 1,86 0,63 0,00
Recovery Factor 0,70 1,97 0,00 0,00
Payoff Ratio 1,00 1,58 0,68 0,00
Profit / Total Bars R$ 2,86 R$ 16,43 -R$ 13,56 -R$ 92,34
profile picture

Eugene

#5
Hi,

While I'm not in the position to offer improvements to your algorithm, what I'm still not sure is why you're using 252*2 or 252*3 bars as the SMA is a stable indicator. This would be enough:

CODE:
Please log in to see this code.


The rest looks OK on the surface.
profile picture

alsilva86

#6
Eugene, thanks for answering. I highlighted the sharpe issue because seems like I am only entering with long with the 1st pair element. And my intenttion is to long and short equally, since the strategy usually involves profit at one side and loss on another, where this loss < profit the sharpe ratio tells me I am only entering on one side of the strategy, the long entries. When actually what I really want to make sure is that both opportunities are up the long entry on 1st pair and the opposite trigger as well(short trade), this would be something that explains - very distant sharpe among long and short, so based on your point of view, is there any symptom you're seeing for this weird behaviour? Only entering long on first pair?
profile picture

Eugene

#7
QUOTE:
I am only entering with long with the 1st pair element.

I just did a quick run of the system in Single Symbol Backtest mode and can see it entering both legs of the pair on the same day. Is this how you execute the code?
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).