Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This is the part that gets the data. Be sure to tweak database parameters appropriately.
- # Oh, and RMySQL doesn't work on Dostoevsky. You'll either have to get R setup on another
- # machine or fix it (or wait for me to fix it)
- #
- # Also, part way down the antigrid stuff starts. You'll wanna put them in separate scripts, methinks.
- library('RMySQL');
- db <- dbConnect (MySQL(), user="brainiac", password="go#sand", host="127.0.0.1", db="svmtrader");#, 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('2008-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';
- 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);
- #
- #
- #
- # Anti-grid below:
- #
- #
- #
- antigrid <- function (data, d, sellPoint, spread) {
- funds <- c(0);
- losses <- 0;
- volume <- 0;
- i <- 2;
- baseGrid <- data[1, 'close'];
- lastGrid <- baseGrid;
- currentPrice <- 0;
- direction <- 0;
- numJumps <- 0;
- for (i in 2:nrow(data)) {
- funds <- c(funds, funds[i-1]);
- currentPrice <- data[i, 'close'];
- direction <- currentPrice - lastGrid;
- if (abs(direction) >= d) {
- numJumps <- sign(direction) * floor(abs(direction / d));
- # This 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
- # sign(volume) * numJumps * currentPrice
- # ... 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;
- cost <- ifelse ( volume == 0 || sign(volume) == sign(numJumps),
- currentPrice,
- currentPrice - 2 * lastGrid );
- cost <- abs(numJumps) * cost + spread;
- funds[i] <- funds[i] - cost;
- losses <- losses + cost;
- lastGrid <- lastGrid + d * numJumps;
- 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
- positionValue <- ifelse ( volume > 0, currentPrice, -baseGrid );
- positionValue <- volume * positionValue;
- # print ( positionValue - losses );
- if ( positionValue - losses > sellPoint ) {
- losses <- 0;
- funds[i] <- funds[i] + positionValue;
- print(funds[i]);
- volume <- 0;
- baseGrid <- currentPrice;
- lastGrid <- currentPrice;
- }
- }
- }
- positionValue <- ifelse ( volume > 0,
- volume * currentPrice,
- -volume * baseGrid);
- c(funds, funds[i-1] + positionValue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement