Advertisement
Guest User

Untitled

a guest
May 14th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 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. # Uses the geometric mean instead
  41. gbpjpy <- cbind( gbpjpy,
  42. close = close,
  43. delta = close - open,
  44.  
  45. # Which mean to use?
  46. #
  47. # arithmetic mean treats all numbers equal (so large numbers can dominate)
  48. #
  49. # geometric mean will move between 0 and max(a,b), rising quickly when there
  50. # is a strong difference between a & b, but changing more slowly as a & b
  51. # get nearer in value. This mean is closer to small numbers than the
  52. # arithmetic mean.
  53. #
  54. # harmonic mean weights small numbers as being more important than large numbers,
  55. # so the mean will tend to be closer to the smallest numbers.
  56. #
  57. # In general:
  58. # smallest numbers <= harmonic mean <= geometric mean <= arithmetic mean <= largest numbers
  59. #
  60.  
  61. # Harmonic mean
  62. # tickcountavg = 2 * ta * tb / (ta + tb),
  63.  
  64. # Geometric mean
  65. # tickcountavg = round( sqrt( ta + tb ) ),
  66.  
  67. # Arithmetic mean
  68. tickcountavg = (ta + tb) / 2,
  69.  
  70. # This is a variation on the concept of RSI. 'ta' and 'tb'
  71. # are tick counts for GBP/USD and USD/JPY (respectively).
  72. # The following measure ranges between 0 and 1. 1 indicates
  73. # that both pairs had near equal tick counts, 0 indicates
  74. # one saw a lot of movement, but the other didn't.
  75. # If, however, ta = tb = 0, this measure will be zero as well.
  76. tickcountrsi = 2 * sqrt( ta * tb ) / (ta + tb + 0.000001)
  77. );
  78.  
  79.  
  80. # Cleanup
  81. rm(rows, gbpusd, usdjpy, ta, tb, open, close, query);
  82.  
  83.  
  84. antigrid <- function (data, d, sellPoint, spread) {
  85. funds <- 0;
  86. losses <- 0;
  87. volume <- 0;
  88. i <- 2;
  89. baseGrid <- data[1, 'close'];
  90. lastGrid <- baseGrid;
  91. currentPrice <- 0;
  92. direction <- 0;
  93. numJumps <- 0;
  94.  
  95. while (i < nrow(data)) {
  96. currentPrice <- data[i, 'close'];
  97. direction <- currentPrice - lastGrid;
  98.  
  99. if (abs(direction) >= d) {
  100. numJumps <- sign(direction) * floor(abs(direction / d));
  101. lastGrid <- lastGrid + d * numJumps;
  102.  
  103. # The 3rd term needs a little explanation.
  104. # If sign(volume) == sign(numJumps), then we're going to buy into
  105. # the same position as sign(volume) even further. By subtracting
  106. # this quantity, 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. if ( abs(volume) * currentPrice + losses > sellPoint ) {
  126. losses <- 0;
  127. funds <- funds + abs(volume) * currentPrice;
  128. volume <- 0;
  129. baseGrid <- currentPrice;
  130. lastGrid <- currentPrice;
  131. }
  132. }
  133.  
  134. i <- i + 1;
  135. }
  136.  
  137. return something here
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement