# Pairs Trading Any Position Loop Problem

Asked by Tomasz Mlynowski on 21 Jul 2012
Latest activity Commented on by Jan Simon on 21 Jul 2012

I am modifying perlin's pairs trading code.

I am having a problem with the anyPos part of the for loop. i do not know if anyPos1 and anyPos 2 line I have added is correct. I have % excluded in code below. When I run the code at the moment it is composing a 1's matrix.

I need a -1 and 1 matrix for individual buy (long) 1 and sell (short) -1 positions as defined in the

```directionTrade(k,assetsNumber) =[-1 1];
```
```and
```
```directionTrade(k,assetsNumber)=[1 -1];
```

Because there are two possible options to take to form anyPos I don't know how to combine the final matrix to compose -1 and 1's based on the below code. I have been trying for days and no end result. Please if anyone can provide an answer or guidance I would be very grateful.

```for i=1:length(idx)
if (k==1)||(anyPos(k-1,idx(i))==0)% if any position is already open, dont trade
if (dist(k,idx(i))>0)
assetsNumber=[idx(i) p(idx(i))];
else
assetsNumber=[idx(i) p(idx(i))];
end
```
`          end`
```   anyPos(k,idx(i))=1;
anyPos(k,p(idx(i)))=1;
end```

## 1 Comment

Jan Simon on 21 Jul 2012

I've deleted the duplicate post. Something at your fixing of the typos went wrong.

## Products

No products are associated with this question.