inline time_duration parse_delimited_time_duration(const std::string& s) { unsigned short min=0, sec =0; int hour =0; bool is_neg = (s.at(0) == '-'); boost::int64_t fs=0; int pos = 0; char_separator<char> sep("-:,."); tokenizer<char_separator<char> > tok(s,sep); for(tokenizer<char_separator<char> >::iterator beg=tok.begin(); beg!=tok.end();++beg){ switch(pos) { case 0: { hour = boost::lexical_cast<int>(*beg); break; } case 1: { min = boost::lexical_cast<unsigned short>(*beg); break; } case 2: { sec = boost::lexical_cast<unsigned short>(*beg); break; }; case 3: { //Works around a bug in MSVC 6 library that does not support //operator>> thus meaning lexical_cast will fail to compile. #if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0 fs = _atoi64(beg->c_str()); // msvc wouldn't compile 'time_duration::num_fractional_digits()' // (required template argument list) as a workaround a temp // time_duration object was used time_duration td(hour,min,sec,fs); int precision = td.num_fractional_digits(); #else fs = boost::lexical_cast<boost::int64_t>(*beg); int precision = time_duration::num_fractional_digits(); #endif int digits = beg->length(); if(digits < precision){ // leading zeros get dropped from the string, // "1:01:01.1" would yield .000001 instead of .100000 // the power() compensates for the missing decimal places fs *= power(10, precision - digits); } break; } }//switch pos++; } if(is_neg) { return -time_duration(hour, min, sec, fs); } else { return time_duration(hour, min, sec, fs); } }
// Jan 31 08:09:01 ... boost::posix_time::ptime to_time( auth_time const& ai ) { // year is current string const& month = ai.mon; string const& day = ai.day; string const& time = ai.time; vector<string> t = rlf_hstring::split( time, ':' ); if( t.size() != 3 ) { // wrong time format throw runtime_error {"wrong time format\n"}; } string const& hour = t[0]; string const& minute = t[1]; string const& second = t[2]; time_duration::hour_type hh = string2type<time_duration::hour_type>( hour ); time_duration::min_type mm = string2type<time_duration::min_type>( minute );; time_duration::sec_type ss = string2type<time_duration::sec_type>( second ); date::month_type m = boost::date_time::month_str_to_ushort<date::month_type>( month );; date::day_type d = string2type<short unsigned int>( day );; date::year_type y = day_clock::universal_day().year(); return boost::posix_time::ptime ( date( y, m, d ), time_duration( hh, mm, ss ) ); }
void RunSequence::HandleQuote2( const ou::tf::Quote& quote ) { m_quote = quote; ptime dt( quote.DateTime() ); std::cout << dt << " quote: B:" << quote.Bid() << "-A:" << quote.Ask() << std::endl; static ptime mark( date( 2012, 7, 22 ), time_duration( 21, 7, 5, 368590 ) ); if ( quote.DateTime() == mark ) { std::cout << dt << " quote " << std::endl; } if ( quote.IsValid() ) { m_quotes.Append( quote ); switch ( m_stateTimeFrame ) { case EPreOpen: if ( m_dtOpeningBell <= dt ) { m_stateTimeFrame = EBellHeard; } break; case EBellHeard: m_stateTimeFrame = EPauseForQuotes; break; case EPauseForQuotes: if ( m_dtStartTrading <= dt ) { m_stateTimeFrame = EAfterBell; } break; case EAfterBell: m_stateTimeFrame = ETrading; break; case ETrading: if ( m_dtCancelTrades <= dt ) { m_pPosition->CancelOrders(); m_stateTimeFrame = ECancelling; } else { Trade(); } break; case ECancelling: if ( m_dtClosePositions <= dt ) { m_pPosition->ClosePosition(); m_stateTimeFrame = EClosing; } break; case EGoingNeutral: assert( false ); break; case EClosing: if ( m_dtClosingBell <= dt ) { m_stateTimeFrame = EAfterHours; } break; case EAfterHours: break; } } }
boost::posix_time::time_duration Service::GetTimeOfDay( const boost::posix_time::time_duration& value ) { return value >= DAY_DURATION ? time_duration(value.hours() % 24, value.minutes(), value.seconds()) : value ; }
inline time_duration parse_undelimited_time_duration(const std::string& s) { int offsets[] = {2,2,2}; int pos = 0, sign = 0; int hours = 0; short min=0, sec=0; // increment one position if the string was "signed" if(s.at(sign) == '-') { ++sign; } // stlport choked when passing s.substr() to tokenizer // using a new string fixed the error std::string remain = s.substr(sign); boost::offset_separator osf(offsets, offsets+3); boost::tokenizer<boost::offset_separator> tok(remain, osf); for(boost::tokenizer<boost::offset_separator>::iterator ti=tok.begin(); ti!=tok.end();++ti){ switch(pos) { case 0: { hours = boost::lexical_cast<int>(*ti); break; } case 1: { min = boost::lexical_cast<short>(*ti); break; } case 2: { sec = boost::lexical_cast<short>(*ti); break; } }; pos++; } if(sign) { return -time_duration(hours, min, sec); } else { return time_duration(hours, min, sec); } }
void aggr_source_discovery::dump_cache(base_stream &out, const cache &c) const { time_t now = time(0); for (cache::const_iterator i = c.begin(); i != c.end(); ++i) { out.xprintf("(%{Addr}, %{Addr}) for %{duration}\n", i->first.second, i->first.first, time_duration((now - i->second) * 1000)); } }
void RunSequence::HandleTrade( const ou::tf::Trade& trade ) { m_trade = trade; ptime dt( trade.DateTime() ); std::cout << dt << " trade " << std::endl; m_trades.Append( trade ); static ptime mark( date( 2012, 7, 22 ), time_duration( 22, 46, 42, 621469 ) ); if ( trade.DateTime() == mark ) { std::cout << dt << " trade " << std::endl; } }
void PerformanceTimer::advance(const std::string& step) { BOOST_ASSERT(running()); // if there is an existing step pending then record its duration recordPendingStep(); // record the start time and add a step startTime_ = now(); steps_.push_back(std::make_pair(step, time_duration())); }
TEST_F(DBTableTest, find_data_timestamp) { std::string fieldName = "fieldName"; createTable(tableName, {"a timestamp"}); insertTable(tableName, "'2015-01-25 12:11:54.88'"); ptime expectedTime(date(2015, 1, 25), time_duration(12, 11, 54, 880000)); dbTable = std::make_shared<DBTable>(tableName); auto row = dbTable->find(""); ASSERT_THAT(row->getValue("a"), IsAnyPosixTime(expectedTime)); }
RunSequence::RunSequence( const boost::gregorian::date& dateStart ) : m_portfolio( "test" ), m_stateTimeFrame( EPreOpen ), m_stateTrading( ENeutral ), m_timeOpeningBell( 19, 0, 0 ), m_timeCancelTrades( 16, 40, 0 ), m_timeClosePositions( 16, 45, 0 ), m_timeClosingBell( 17, 0, 0 ), m_dtOpeningBell( dateStart, m_timeOpeningBell ), m_dtStartTrading( m_dtOpeningBell + time_duration( 0, 16, 0 ) ), m_dtCancelTrades( dateStart + date_duration( 1 ), m_timeCancelTrades ), m_dtClosePositions( dateStart + date_duration( 1 ), m_timeClosePositions ), m_dtClosingBell( dateStart + date_duration( 1 ), m_timeClosingBell ) { }
boost::posix_time::time_duration DBResult::getHour( const std::string& name ) const { try { string text(getText(name)); if(!text.empty()) { return duration_from_string(text); } } catch(...) { } return time_duration(not_a_date_time); }
inline time_duration hours(boost::int64_t s) { return time_duration(aux::microseconds_to_performance_counter( s * 1000000 * 60 * 60)); }
inline time_duration microsec(boost::int64_t s) { return time_duration(aux::microseconds_to_performance_counter(s)); }
inline time_duration hours(boost::int64_t s) { return time_duration(aux::microseconds_to_absolutetime(s * 1000000 * 60 * 60)); }
inline time_duration microsec(boost::int64_t s) { return time_duration(aux::microseconds_to_absolutetime(s)); }
inline time_duration operator*(int lhs, time_duration rhs) { return time_duration(boost::int64_t(lhs * rhs.diff)); }
time_duration operator-(time_duration const& c) { return time_duration(diff - c.diff); }
time_duration operator/(int rhs) const { return time_duration(diff / rhs); }
inline time_duration minutes(boost::int64_t s) { return time_duration(s * 1000000 * 60); }
inline time_duration microsec(boost::int64_t s) { return time_duration(s); }
inline time_duration seconds(boost::int64_t s) { return time_duration(s * 1000000); }
Process::Process(void) : m_bIBConnected( false ), m_bIQFeedConnected( false ), m_bSimConnected( false ), m_contractidUnderlying( 0 ), m_bWatchingOptions( false ), m_bTrading( false ), m_bPositionsOpened( false ), m_dblBaseDelta( 2000.0 ), m_dblBaseDeltaIncrement( 100.0 ), m_TradingState( ETSFirstPass ), #ifdef testing m_sSymbolName( "DELL" ), m_dtMarketOpen( time_duration( 0, 30, 0 ) ), m_dtMarketOpeningOrder( time_duration( 0, 31, 0 ) ), m_dtMarketClosingOrder( time_duration( 23, 56, 0 ) ), m_dtMarketClose( time_duration( 23, 59, 59 ) ), #else m_sSymbolName( "GLD" ), m_dtMarketOpen( time_duration( 10, 30, 0 ) ), m_dtMarketOpeningOrder( time_duration( 10, 30, 20 ) ), m_dtMarketClosingOrder( time_duration( 16, 56, 0 ) ), m_dtMarketClose( time_duration( 17, 0, 0 ) ), #endif m_sPathForSeries( "/strategy/deltaneutral2" ), m_sDesiredSimTradingDay( "2010-Sep-10 20:10:25.562500" ), m_bProcessSimTradingDayGroup( false ), m_tws( new IBTWS( "U215226" ) ), m_iqfeed( new IQFeedProvider() ), m_sim( new SimulationProvider() ), m_eMode( EModeLive ), //m_eMode( EModeSimulation ) m_bExecConnected( false ), m_bDataConnected( false ), m_bData2Connected( false ), m_bConnectDone( false ) { boost::gregorian::date dToday = ou::TimeSource::Instance().Internal().date(); m_dExpiry = ou::tf::options::Next3rdFriday( dToday ); while ( boost::gregorian::date_period( dToday, m_dExpiry ).length().days() < 8 ) { // some say use front month for scalping boost::gregorian::date_duration dd( 1 ); boost::gregorian::date d = m_dExpiry + dd; m_dExpiry = ou::tf::options::Next3rdFriday( d ); } m_contract.currency = "USD"; m_contract.exchange = "SMART"; m_contract.symbol = m_sSymbolName; // will need to use the expiry date to look at existing positions to see if a calendar spread is needed for rolling over m_contract.expiry = boost::gregorian::to_iso_string( m_dExpiry ); m_db.SetOnPopulateDatabaseHandler( MakeDelegate( this, &Process::HandlePopulateDatabase ) ); m_idPortfolio = "dn01-" + m_sSymbolName + "-" + m_contract.expiry; // needs to come before database open // Implement the calendar roll over at some point // this is where we select which provider we will be working with on this run // providers need to be registered in order for portfolio/position loading to function properly // key needs to match to account ProviderManager::Instance().Register( "ib01", static_cast<pProvider_t>( m_tws ) ); ProviderManager::Instance().Register( "iq01", static_cast<pProvider_t>( m_iqfeed ) ); ProviderManager::Instance().Register( "sim01", static_cast<pProvider_t>( m_sim ) ); std::string sDbName; switch ( m_eMode ) { case EModeSimulation: sDbName = ":memory:"; m_pExecutionProvider = m_sim; m_pDataProvider = m_sim; break; case EModeLive: sDbName = "TradeGldOptions.db"; m_pExecutionProvider = m_tws; m_pDataProvider = m_tws; break; } m_db.Open( sDbName ); m_pExecutionProvider->OnConnected.Add( MakeDelegate( this, &Process::HandleOnExecConnected ) ); m_pExecutionProvider->OnDisconnected.Add( MakeDelegate( this, &Process::HandleOnExecDisconnected ) ); m_pDataProvider->OnConnected.Add( MakeDelegate( this, &Process::HandleOnDataConnected ) ); m_pDataProvider->OnDisconnected.Add( MakeDelegate( this, &Process::HandleOnDataDisconnected ) ); m_iqfeed->OnConnected.Add( MakeDelegate( this, &Process::HandleOnData2Connected ) ); m_iqfeed->OnDisconnected.Add( MakeDelegate( this, &Process::HandleOnData2Disconnected ) ); }
inline time_duration hours(boost::int64_t s) { return time_duration(s * 1000000 * 60 * 60); }
//! Convert a tm struct to a ptime ignoring is_dst flag inline ptime ptime_from_tm(const std::tm& timetm) { boost::gregorian::date d = boost::gregorian::date_from_tm(timetm); return ptime(d, time_duration(timetm.tm_hour, timetm.tm_min, timetm.tm_sec)); }
time_duration operator+(time_duration const& c) { return time_duration(diff + c.diff); }
time_duration operator*(const double rhs) const { return time_duration( boost::int64_t (diff * rhs) ); }
inline time_duration operator*(time_duration lhs, int rhs) { return time_duration(boost::int64_t(lhs.diff * rhs)); }
inline time_duration parse_undelimited_time_duration(const std::string& s) { int precision = 0; { // msvc wouldn't compile 'time_duration::num_fractional_digits()' // (required template argument list) as a workaround, a temp // time_duration object was used time_duration tmp(0,0,0,1); precision = tmp.num_fractional_digits(); } // 'precision+1' is so we grab all digits, plus the decimal int offsets[] = {2,2,2, precision+1}; int pos = 0, sign = 0; int hours = 0; short min=0, sec=0; boost::int64_t fs=0; // increment one position if the string was "signed" if(s.at(sign) == '-') { ++sign; } // stlport choked when passing s.substr() to tokenizer // using a new string fixed the error std::string remain = s.substr(sign); /* We do not want the offset_separator to wrap the offsets, we * will never want to process more than: * 2 char, 2 char, 2 char, frac_sec length. * We *do* want the offset_separator to give us a partial for the * last characters if there were not enough provided in the input string. */ bool wrap_off = false; bool ret_part = true; boost::offset_separator osf(offsets, offsets+4, wrap_off, ret_part); typedef boost::tokenizer<boost::offset_separator, std::basic_string<char>::const_iterator, std::basic_string<char> > tokenizer; typedef boost::tokenizer<boost::offset_separator, std::basic_string<char>::const_iterator, std::basic_string<char> >::iterator tokenizer_iterator; tokenizer tok(remain, osf); for(tokenizer_iterator ti=tok.begin(); ti!=tok.end();++ti){ switch(pos) { case 0: { hours = boost::lexical_cast<int>(*ti); break; } case 1: { min = boost::lexical_cast<short>(*ti); break; } case 2: { sec = boost::lexical_cast<short>(*ti); break; } case 3: { std::string char_digits(ti->substr(1)); // digits w/no decimal int digits = static_cast<int>(char_digits.length()); //Works around a bug in MSVC 6 library that does not support //operator>> thus meaning lexical_cast will fail to compile. #if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0 // _atoi64 is an MS specific function if(digits >= precision) { // drop excess digits fs = _atoi64(char_digits.substr(0, precision).c_str()); } else if(digits == 0) { fs = 0; // just in case _atoi64 doesn't like an empty string } else { fs = _atoi64(char_digits.c_str()); } #else if(digits >= precision) { // drop excess digits fs = boost::lexical_cast<boost::int64_t>(char_digits.substr(0, precision)); } else if(digits == 0) { fs = 0; // lexical_cast doesn't like empty strings } else { fs = boost::lexical_cast<boost::int64_t>(char_digits); } #endif if(digits < precision){ // trailing zeros get dropped from the string, // "1:01:01.1" would yield .000001 instead of .100000 // the power() compensates for the missing decimal places fs *= power(10, precision - digits); } break; } default: break; }; pos++; } if(sign) { return -time_duration(hours, min, sec, fs); } else { return time_duration(hours, min, sec, fs); } }
inline time_duration operator-(ptime lhs, ptime rhs) { return time_duration(lhs.time - rhs.time); }
inline time_duration str_from_delimited_time_duration(const std::basic_string<char_type>& s) { unsigned short min=0, sec =0; int hour =0; bool is_neg = (s.at(0) == '-'); boost::int64_t fs=0; int pos = 0; typedef typename std::basic_string<char_type>::traits_type traits_type; typedef boost::char_separator<char_type, traits_type> char_separator_type; typedef boost::tokenizer<char_separator_type, typename std::basic_string<char_type>::const_iterator, std::basic_string<char_type> > tokenizer; typedef typename boost::tokenizer<char_separator_type, typename std::basic_string<char_type>::const_iterator, typename std::basic_string<char_type> >::iterator tokenizer_iterator; char_type sep_chars[5] = {'-',':',',','.'}; char_separator_type sep(sep_chars); tokenizer tok(s,sep); for(tokenizer_iterator beg=tok.begin(); beg!=tok.end();++beg){ switch(pos) { case 0: { hour = boost::lexical_cast<int>(*beg); break; } case 1: { min = boost::lexical_cast<unsigned short>(*beg); break; } case 2: { sec = boost::lexical_cast<unsigned short>(*beg); break; }; case 3: { int digits = static_cast<int>(beg->length()); //Works around a bug in MSVC 6 library that does not support //operator>> thus meaning lexical_cast will fail to compile. #if (defined(BOOST_MSVC) && (_MSC_VER < 1300)) // msvc wouldn't compile 'time_duration::num_fractional_digits()' // (required template argument list) as a workaround a temp // time_duration object was used time_duration td(hour,min,sec,fs); int precision = td.num_fractional_digits(); // _atoi64 is an MS specific function if(digits >= precision) { // drop excess digits fs = _atoi64(beg->substr(0, precision).c_str()); } else { fs = _atoi64(beg->c_str()); } #else int precision = time_duration::num_fractional_digits(); if(digits >= precision) { // drop excess digits fs = boost::lexical_cast<boost::int64_t>(beg->substr(0, precision)); } else { fs = boost::lexical_cast<boost::int64_t>(*beg); } #endif if(digits < precision){ // trailing zeros get dropped from the string, // "1:01:01.1" would yield .000001 instead of .100000 // the power() compensates for the missing decimal places fs *= power(10, precision - digits); } break; } default: break; }//switch pos++; } if(is_neg) { return -time_duration(hour, min, sec, fs); } else { return time_duration(hour, min, sec, fs); } }