Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library('RMySQL');
- db <- dbConnect (MySQL(), user="brainiac", password="go#sand", host="oogabooga");
- # This query restricts data to the following:
- # :- Gets GBP/USD and USD/JPY (13 & 16 respectively)
- # :- Only gets monday - friday
- # :- Gets data only in the 'power hour', between 1300-1700 GMT
- #
- # Data retrieved includes the year, week, and day of week so we can try
- # various approaches
- query <- "select dayofweek(from_unixtime(unixtime)) as weekday,
- week(from_unixtime(unixtime)) as week,
- year(from_unixtime(unixtime)) as year,
- ticker,
- open,
- close,
- tickcount
- from minute_filled
- where ticker_fk in (13, 16)
- AND unixtime >= unix_timestamp('2004-01-01')
- AND dayofweek(from_unixtime(unixtime)) in (2,3,4,5)
- AND abs(hour(from_unixtime(unixtime)) - 15) <= 2
- ";
- rows <- cbind(fetch(dbSendQuery (db, query), n=-1));
- # Form the cross
- gbpusd <- rows[,'ticker'] == 'GBP/USD'];
- usdjpy <- rows[,'ticker'] == 'USD/JPY'];
- gbpjpy <- rows[gbpusd, c('weekday', 'week', 'year', 'ticker'];
- ta <- rows[gbpusd, 'tickcount'];
- tb <- rows[usdjpy, 'tickcount'];
- open <- rows[gbpusd, 'open'] * rows[usdjpy, 'open'];
- close <- rows[gbpusd, 'close'] * rows[usdjpy, 'close'];
- gbpjpy[,'ticker'] <- 'GBP/JPY';
- # Uses the geometric mean instead
- gbpjpy <- cbind( gbpjpy,
- close = close,
- delta = close - open,
- # Which mean to use?
- #
- # arithmetic mean treats all numbers equal (so large numbers can dominate)
- #
- # geometric mean will move between 0 and max(a,b), rising quickly when there
- # is a strong difference between a & b, but changing more slowly as a & b
- # get nearer in value. This mean is closer to small numbers than the
- # arithmetic mean.
- #
- # harmonic mean weights small numbers as being more important than large numbers,
- # so the mean will tend to be closer to the smallest numbers.
- #
- # In general:
- # smallest numbers <= harmonic mean <= geometric mean <= arithmetic mean <= largest numbers
- #
- # Harmonic mean
- # tickcountavg = 2 * ta * tb / (ta + tb),
- # Geometric mean
- # tickcountavg = round( sqrt( ta + tb ) ),
- # Arithmetic mean
- tickcountavg = (ta + tb) / 2,
- # This is a variation on the concept of RSI. 'ta' and 'tb'
- # are tick counts for GBP/USD and USD/JPY (respectively).
- # The following measure ranges between 0 and 1. 1 indicates
- # that both pairs had near equal tick counts, 0 indicates
- # one saw a lot of movement, but the other didn't.
- # If, however, ta = tb = 0, this measure will be zero as well.
- tickcountrsi = 2 * sqrt( ta * tb ) / (ta + tb + 0.000001)
- );
- # Cleanup
- rm(rows, gbpusd, usdjpy, ta, tb, open, close, query);
- antigrid <- function (data, d, sellPoint, spread) {
- funds <- 0;
- losses <- 0;
- volume <- 0;
- i <- 2;
- baseGrid <- data[1, 'close'];
- lastGrid <- baseGrid;
- currentPrice <- 0;
- direction <- 0;
- numJumps <- 0;
- while (i < nrow(data)) {
- currentPrice <- data[i, 'close'];
- direction <- currentPrice - lastGrid;
- if (abs(direction) >= d) {
- numJumps <- sign(direction) * floor(abs(direction / d));
- lastGrid <- lastGrid + d * numJumps;
- # The 3rd term needs a little explanation.
- # If sign(volume) == sign(numJumps), then we're going to buy into
- # the same position as sign(volume) even further. By subtracting
- # this quantity, we're incurring a transaction cost.
- #
- # On the other hand, if sign(volume) != sign(numJumps), then we'll
- # need to back out of our current position by abs(numJumps) grid
- # points. Since the signs are different, their product will be
- # negative, and subtracting this will make it positive, so we'll be
- # adding abs(numJumps) * currentPrice back to funds.
- cost <- spread + sign(volume) * numJumps * currentPrice;
- funds <- funds - cost;
- losses <- losses - cost;
- volume <- volume + numJumps;
- # We only dump our current position if we've dropped down one gridpoint
- # and we're over the sellpoint.
- # if ( sign(volume) != sign(direction)
- # && abs(volume) * currentPrice + losses > sellPoint ) {
- # We sell as soon as we've made sellPoint pips
- if ( abs(volume) * currentPrice + losses > sellPoint ) {
- losses <- 0;
- funds <- funds + abs(volume) * currentPrice;
- volume <- 0;
- baseGrid <- currentPrice;
- lastGrid <- currentPrice;
- }
- }
- i <- i + 1;
- }
- return something here
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement