Advertisement
Guest User

Untitled

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