| 1 | package Quant::Framework::Spot; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Moose; | ||
| 8 | use Scalar::Util qw( looks_like_number ); | ||
| 9 | use Cache::RedisDB; | ||
| 10 | use Quant::Framework::Spot::Tick; | ||
| 11 | use Quant::Framework::Spot::DatabaseAPI; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Quant::Framework::Spot - Used to store/retrieve spot prices into/from local Redis storage | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | has for_date => ( | ||
| 20 | is => 'ro', | ||
| 21 | ); | ||
| 22 | |||
| 23 | has underlying_config => ( | ||
| 24 | is => 'ro', | ||
| 25 | required => 1, | ||
| 26 | ); | ||
| 27 | |||
| 28 | has calendar => ( | ||
| 29 | is => 'ro', | ||
| 30 | ); | ||
| 31 | |||
| 32 | has feed_api => ( | ||
| 33 | is => 'ro', | ||
| 34 | ); | ||
| 35 | |||
| 36 | has default_redis_key => ( | ||
| 37 | is => 'ro', | ||
| 38 | default => 'QUOTE', | ||
| 39 | ); | ||
| 40 | |||
| 41 | =head1 SYNOPSIS | ||
| 42 | |||
| 43 | Used to store/retrieve spot prices into/from local Redis storage | ||
| 44 | You will need to provide the module with a code reference which will be used | ||
| 45 | in exception cases where local redis is empty and a request to read spot price | ||
| 46 | is received. In this case, module will use the given code references to fetch | ||
| 47 | spot price from a secondary storage (this can be a database). | ||
| 48 | If you omit for_date parameter, you will fetch latest spot price. | ||
| 49 | |||
| 50 | use Quant::Framework::Spot; | ||
| 51 | |||
| 52 | my $foo = Quant::Framework::Spot->new({ | ||
| 53 | symbol => 'GDAXI', | ||
| 54 | for_date => Date::Utility->new('2016-09-11 12:29:10') | ||
| 55 | }); | ||
| 56 | |||
| 57 | ... | ||
| 58 | |||
| 59 | =head1 SUBROUTINES/METHODS | ||
| 60 | |||
| 61 | =head2 $self->spot_tick | ||
| 62 | |||
| 63 | Get last tick value for symbol from Redis or feed database. It will rebuild value from | ||
| 64 | feed db if it is not present in cache. | ||
| 65 | |||
| 66 | This method will fall back to database if no data is found in Redis cache. In this case, | ||
| 67 | Redis will be populated with the data read from db. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub spot_tick { | ||
| 72 | my $self = shift; | ||
| 73 | |||
| 74 | return $self->tick_at($self->for_date->epoch, {allow_inconsistent => 1}) if $self->for_date; | ||
| 75 | |||
| 76 | my $value = Cache::RedisDB->get($self->default_redis_key, $self->underlying_config->symbol); | ||
| 77 | my $tick; | ||
| 78 | if ($value) { | ||
| 79 | $tick = Quant::Framework::Spot::Tick->new($value); | ||
| 80 | } else { | ||
| 81 | $tick = $self->tick_at(time, {allow_inconsistent => 1}); | ||
| 82 | if ($tick) { | ||
| 83 | $self->set_spot_tick($tick); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | return $tick; | ||
| 88 | } | ||
| 89 | |||
| 90 | =head2 spot_tick_hash | ||
| 91 | |||
| 92 | Returns a hash reference denoting available fields in the spot_tick | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub spot_tick_hash { | ||
| 97 | my $self = shift; | ||
| 98 | |||
| 99 | my $tick = $self->spot_tick; | ||
| 100 | |||
| 101 | return ($tick) ? $tick->as_hash : undef; | ||
| 102 | } | ||
| 103 | |||
| 104 | =head2 tick_at | ||
| 105 | |||
| 106 | This method receives a timestamp and an option for inconsistency. | ||
| 107 | Returns the tick for current symbol at the given time. If inconssitency is enabled | ||
| 108 | and there is no tick at that moment, the latest tick before that time is | ||
| 109 | returned. | ||
| 110 | `allow_inconsistent` parameters determines how the case should be handled when there is no tick at the | ||
| 111 | exact requested timestamp. If value of this parameter is `1`, then the code will return tick at or before the given timestamp. | ||
| 112 | If it is passed as `0`, the code will return any tick at or after given timestamp. | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | sub tick_at { | ||
| 117 | my ($self, $timestamp, $allow_inconsistent_hash) = @_; | ||
| 118 | |||
| 119 | my $inconsistent_price; | ||
| 120 | # get official close for previous trading day | ||
| 121 | if (defined $allow_inconsistent_hash->{allow_inconsistent} | ||
| 122 | and $allow_inconsistent_hash->{allow_inconsistent} == 1) | ||
| 123 | { | ||
| 124 | $inconsistent_price = 1; | ||
| 125 | } | ||
| 126 | |||
| 127 | my $pricing_date = Date::Utility->new($timestamp); | ||
| 128 | my $tick; | ||
| 129 | |||
| 130 | if ($self->underlying_config->use_official_ohlc | ||
| 131 | and not $self->calendar->trades_on($pricing_date)) | ||
| 132 | { | ||
| 133 | my $last_trading_day = $self->calendar->trade_date_before($pricing_date); | ||
| 134 | $tick = $self->closing_tick_on($last_trading_day->date_ddmmmyy); | ||
| 135 | } else { | ||
| 136 | my $request_hash = {}; | ||
| 137 | $request_hash->{end_time} = $timestamp; | ||
| 138 | $request_hash->{allow_inconsistent} = 1 if ($inconsistent_price); | ||
| 139 | |||
| 140 | $tick = $self->feed_api->tick_at($request_hash); | ||
| 141 | } | ||
| 142 | |||
| 143 | return $tick; | ||
| 144 | } | ||
| 145 | |||
| 146 | =head2 closing_tick_on | ||
| 147 | |||
| 148 | Get the market closing tick for a given date. | ||
| 149 | |||
| 150 | Example : $underlying->closing_tick_on("10-Jan-00"); | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub closing_tick_on { | ||
| 155 | my ($self, $end, $closing) = @_; | ||
| 156 | my $date = Date::Utility->new($end); | ||
| 157 | |||
| 158 | if ($closing and time > $closing->epoch) { | ||
| 159 | my $ohlc = $self->feed_api->ohlc_start_end({ | ||
| 160 | start_time => $date, | ||
| 161 | end_time => $date, | ||
| 162 | aggregation_period => 86400, | ||
| 163 | }); | ||
| 164 | |||
| 165 | if ($ohlc and scalar @{$ohlc} > 0) { | ||
| 166 | |||
| 167 | # We need a tick, but we can only get an OHLC | ||
| 168 | # The epochs for these are set to be the START of the period. | ||
| 169 | # So we also need to change it to the closing time. Meh. | ||
| 170 | my $not_tick = $ohlc->[0]; | ||
| 171 | return Quant::Framework::Spot::Tick->new({ | ||
| 172 | symbol => $self->underlying_config->symbol, | ||
| 173 | epoch => $closing->epoch, | ||
| 174 | quote => $not_tick->close, | ||
| 175 | }); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | return; | ||
| 179 | } | ||
| 180 | |||
| 181 | =head2 $self->set_spot_tick($value) | ||
| 182 | |||
| 183 | Save last tick value for symbol in Redis. Returns true if operation was | ||
| 184 | successfull, false overwise. Tick value should be a hash reference like this: | ||
| 185 | |||
| 186 | { | ||
| 187 | epoch => $unix_timestamp, | ||
| 188 | quote => $last_price, | ||
| 189 | } | ||
| 190 | |||
| 191 | You will need to provide the module with a code reference which will be used | ||
| 192 | in exception cases where local redis is empty and a request to read spot price | ||
| 193 | is received. In this case, module will use the given code references to fetch | ||
| 194 | spot price from a secondary storage (this can be a database). | ||
| 195 | |||
| 196 | =cut | ||
| 197 | |||
| 198 | sub set_spot_tick { | ||
| 199 | my ($self, $value) = @_; | ||
| 200 | |||
| 201 | my $tick; | ||
| 202 | if (ref $value eq 'Quant::Framework::Spot::Tick') { | ||
| 203 | $tick = $value; | ||
| 204 | $value = $value->as_hash; | ||
| 205 | } else { | ||
| 206 | $tick = Quant::Framework::Spot::Tick->new($value); | ||
| 207 | } | ||
| 208 | Cache::RedisDB->set_nw($self->default_redis_key, $self->underlying_config->symbol, $value); | ||
| 209 | return $tick; | ||
| 210 | } | ||
| 211 | |||
| 212 | =head2 spot_quote | ||
| 213 | |||
| 214 | What is the current spot price for this underlying? | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | sub spot_quote { | ||
| 219 | my $self = shift; | ||
| 220 | my $last_price; | ||
| 221 | |||
| 222 | my $last_tick = $self->spot_tick; | ||
| 223 | $last_price = $last_tick->quote if $last_tick; | ||
| 224 | |||
| 225 | return $last_price; | ||
| 226 | } | ||
| 227 | |||
| 228 | =head2 spot_time | ||
| 229 | |||
| 230 | The epoch timestamp of the latest recorded tick in the system | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | sub spot_time { | ||
| 235 | my $self = shift; | ||
| 236 | my $last_tick = $self->spot_tick; | ||
| 237 | return $last_tick && $last_tick->epoch; | ||
| 238 | } | ||
| 239 | |||
| 240 | =head2 spot_age | ||
| 241 | |||
| 242 | The age in seconds of the latest tick | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | sub spot_age { | ||
| 247 | my $self = shift; | ||
| 248 | my $tick_time = $self->spot_time; | ||
| 249 | return defined $tick_time && time - $tick_time; | ||
| 250 | } | ||
| 251 | |||
| 252 | 1; # End of Quant::Framework::Spot |