#include "../shared/DatabaseWithRetry.h" #include "../shared/TwoDLookup.h" #include "../shared/GlobalConfigFile.h" /* This program tells us how many tweets we expect at any specific time of day. * We use this number in real time to say if we have more or fewer tweets * than expected for right now. We look at the total tweets, not the tweets * per symbol. There aren't enough tweets for most symbols to do analysis * which is this precise. */ int main(int argc, char *argv[]) { addConfigItemsFromCommandLine(argv + 1); configItemsComplete(); TwoDArray outputTable; DatabaseWithRetry st("@st", "st"); // This version dumps all the precision we have to the output file. So // we know how many tweets per minute for every minute of the day. When // we look at volume for individual stocks, we are less precise and we only // look at market hours. Since there is only one of these, not one per // symbol as is the case with volume, this is probably okay. The client // will be flexible and can take more or less precision. std::vector< std::string > sql; sql.push_back("SELECT @today := CURDATE()"); sql.push_back("SELECT @days := COUNT(DISTINCT DATE(start))+0.0 " "FROM stocktwits_minutes " "WHERE start BETWEEN @today - INTERVAL 42 DAY AND @today " "AND DAYOFWEEK(start) BETWEEN 2 AND 6"); sql.push_back("SELECT time_to_sec(start) AS SEC, " "SUM(count)/@days AS TWEET_COUNT FROM stocktwits_minutes " "WHERE start BETWEEN @today - INTERVAL 42 DAY AND @today " "AND DAYOFWEEK(start) BETWEEN 2 AND 6 " "GROUP BY sec ORDER BY sec"); double totalTweets = 0; for (MysqlResultRef result = st.tryAllUntilSuccess(sql.begin(), sql.end())[2]; result->rowIsValid(); result->nextRow()) { const int time = result->getIntegerField(0, -1); assert(time >= 0); const double tweets = result->getDoubleField(1, -1); assert(tweets >= 0); totalTweets += tweets; outputTable.add("tweets", ntoa(time), ntoa(totalTweets)); } outputTable.writeToCSV(getConfigItem("output_table", "stocktwits_smile.csv")); }