Advertisement
Guest User

Untitled

a guest
May 15th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. # This is the part that gets the data. Be sure to tweak database parameters appropriately.
  2. # Oh, and RMySQL doesn't work on Dostoevsky. You'll either have to get R setup on another
  3. # machine or fix it (or wait for me to fix it)
  4. #
  5. # Also, part way down the antigrid stuff starts. You'll wanna put them in separate scripts, methinks.
  6.  
  7. library('RMySQL');
  8.  
  9. db <- dbConnect (MySQL(), user="brainiac", password="go#sand", host="127.0.0.1", db="svmtrader");#, host="oogabooga");
  10.  
  11. # This query restricts data to the following:
  12. # :- Gets GBP/USD and USD/JPY (13 & 16 respectively)
  13. # :- Only gets monday - friday
  14. # :- Gets data only in the 'power hour', between 1300-1700 GMT
  15. #
  16. # Data retrieved includes the year, week, and day of week so we can try
  17. # various approaches
  18. query <- "select dayofweek(from_unixtime(unixtime)) as weekday,
  19. week(from_unixtime(unixtime)) as week,
  20. year(from_unixtime(unixtime)) as year,
  21. ticker,
  22. open,
  23. close,
  24. tickcount
  25. from minute_filled
  26. where ticker_fk in (13, 16)
  27. AND unixtime >= unix_timestamp('2008-01-01')
  28. AND dayofweek(from_unixtime(unixtime)) in (2,3,4,5)
  29. AND abs(hour(from_unixtime(unixtime)) - 15) <= 2
  30. ";
  31.  
  32. rows <- cbind(fetch(dbSendQuery (db, query), n=-1));
  33.  
  34. # Form the cross
  35. gbpusd <- rows[,'ticker'] == 'GBP/USD';
  36. usdjpy <- rows[,'ticker'] == 'USD/JPY';
  37.  
  38. gbpjpy <- rows[gbpusd, c('weekday', 'week', 'year', 'ticker')];
  39. ta <- rows[gbpusd, 'tickcount'];
  40. tb <- rows[usdjpy, 'tickcount'];
  41. open <- rows[gbpusd, 'open'] * rows[usdjpy, 'open'];
  42. close <- rows[gbpusd, 'close'] * rows[usdjpy, 'close'];
  43.  
  44. gbpjpy[,'ticker'] <- 'GBP/JPY';
  45.  
  46. gbpjpy <- cbind( gbpjpy,
  47. close = close,
  48. delta = close - open,
  49.  
  50. # Which mean to use?
  51. #
  52. # arithmetic mean treats all numbers equal (so large numbers can dominate)
  53. #
  54. # geometric mean will move between 0 and max(a,b), rising quickly when there
  55. # is a strong difference between a & b, but changing more slowly as a & b
  56. # get nearer in value. This mean is closer to small numbers than the
  57. # arithmetic mean.
  58. #
  59. # harmonic mean weights small numbers as being more important than large numbers,
  60. # so the mean will tend to be closer to the smallest numbers.
  61. #
  62. # In general:
  63. # smallest numbers <= harmonic mean <= geometric mean <= arithmetic mean <= largest numbers
  64. #
  65.  
  66. # Harmonic mean
  67. # tickcountavg = 2 * ta * (tb / (ta + tb)),
  68.  
  69. # Geometric mean
  70. # tickcountavg = round( sqrt( ta + tb ) ),
  71.  
  72. # Arithmetic mean
  73. tickcountavg = (ta + tb) / 2,
  74.  
  75. # This is a variation on the concept of RSI. 'ta' and 'tb'
  76. # are tick counts for GBP/USD and USD/JPY (respectively).
  77. # The following measure ranges between 0 and 1. 1 indicates
  78. # that both pairs had near equal tick counts, 0 indicates
  79. # one saw a lot of movement, but the other didn't.
  80. # If, however, ta = tb = 0, this measure will be zero as well.
  81. tickcountrsi = 2 * sqrt( ta * tb ) / (ta + tb + 0.000001)
  82. );
  83.  
  84.  
  85. # Cleanup
  86. rm(rows, gbpusd, usdjpy, ta, tb, open, close, query);
  87.  
  88. #
  89. #
  90. #
  91. # Anti-grid below:
  92. #
  93. #
  94. #
  95.  
  96. antigrid <- function (data, d, sellPoint, spread) {
  97. funds <- c(0);
  98. losses <- 0;
  99. volume <- 0;
  100. i <- 2;
  101. baseGrid <- data[1, 'close'];
  102. lastGrid <- baseGrid;
  103. currentPrice <- 0;
  104. direction <- 0;
  105. numJumps <- 0;
  106.  
  107. for (i in 2:nrow(data)) {
  108. funds <- c(funds, funds[i-1]);
  109. currentPrice <- data[i, 'close'];
  110. direction <- currentPrice - lastGrid;
  111.  
  112. if (abs(direction) >= d) {
  113. numJumps <- sign(direction) * floor(abs(direction / d));
  114.  
  115. # This needs a little explanation.
  116. # If sign(volume) == sign(numJumps), then we're going to buy into
  117. # the same position as sign(volume) even further. By subtracting
  118. # sign(volume) * numJumps * currentPrice
  119. # ... we're incurring a transaction cost.
  120. #
  121. # On the other hand, if sign(volume) != sign(numJumps), then we'll
  122. # need to back out of our current position by abs(numJumps) grid
  123. # points. Since the signs are different, their product will be
  124. # negative, and subtracting this will make it positive, so we'll be
  125. # adding abs(numJumps) * currentPrice back to funds.
  126. # cost <- spread + sign(volume) * numJumps * currentPrice;
  127.  
  128. cost <- ifelse ( volume == 0 || sign(volume) == sign(numJumps),
  129. currentPrice,
  130. currentPrice - 2 * lastGrid );
  131. cost <- abs(numJumps) * cost + spread;
  132.  
  133. funds[i] <- funds[i] - cost;
  134. losses <- losses + cost;
  135.  
  136. lastGrid <- lastGrid + d * numJumps;
  137. volume <- volume + numJumps;
  138.  
  139. # We only dump our current position if we've dropped down one gridpoint
  140. # and we're over the sellpoint.
  141. # if ( sign(volume) != sign(direction)
  142. # && abs(volume) * currentPrice + losses > sellPoint ) {
  143.  
  144. # We sell as soon as we've made sellPoint pips
  145. positionValue <- ifelse ( volume > 0, currentPrice, -baseGrid );
  146. positionValue <- volume * positionValue;
  147.  
  148. # print ( positionValue - losses );
  149. if ( positionValue - losses > sellPoint ) {
  150. losses <- 0;
  151. funds[i] <- funds[i] + positionValue;
  152. print(funds[i]);
  153. volume <- 0;
  154. baseGrid <- currentPrice;
  155. lastGrid <- currentPrice;
  156. }
  157. }
  158. }
  159.  
  160. positionValue <- ifelse ( volume > 0,
  161. volume * currentPrice,
  162. -volume * baseGrid);
  163.  
  164. c(funds, funds[i-1] + positionValue);
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement