Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Determine positions

$
0
0

I have a date set (df) with 4 columns: date, price_1, price_2, ratio ratio = price_1/price_2 it has something to do with pairs trading. I need to determine when a position is opened, and closed. A position will be opened if the ratio exceeds m + 1*s or m-1*s; this stays opened until it meets the criteria to close which is when this ratio <= m. A position will also be opened if the price exceed m -1*s, and closed as soon as the ratio becomes >= m. Position consists of the opening point and closing point. the first time the ratio exceeds either m+1*s or m-1*s is the position 1 opening point, then it closes in the case of ratio > m+1*s when ratio <= m; and the other way around if the ratio exceeds m-1*s then closing is at ratio >= m so position1 consists 1&2 in which 1 is the opening and closing is point 2 when its it hits the mean (black line). s = (dashed lines). == mean can't be just because of floating points.

 open_upper <- which(retail_stocks$ratio > (m + k*s) )
 open_lower <- which(retail_stocks$ratio < (m - k*s) )
 close_upper <- which(retail_stocks$ratio <= m )
 close_lower <- which(retail_stocks$ratio >= m )

but now I need to find out the position 1, 2, 3, 4 etc. with the opening and closing i. enter image description here


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>