Mariner Backtesting - SMA Historic Example

Social Market Analytics Inc (SMA) provide sentiment data for the US stock markets.

You will find ready to clone scripts for accessing the SMA Sentiment Data in the public scripts area under Sentiment.

SMA Sentiment data is available from 2011-12-01 to the current date.

To read sentiment data that came before your script started you will need to place the code in the on_start or on_strategy_start function.

To get the data you use service.query_data and you must pass it three inputs : - the feed name, either '!sentiment/sma/tw/15min' or '!sentiment/sma/st/15min' - the symbol - the start time for the retrieval

Link to Script

 # Copyright Cloudquant, LLC. All right reserved.
from cloudquant.interfaces import Strategy

class SMA_Sentiment_History(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol=="AAPL"

    def on_start(self, md, order, service, account):

        print("HISTORIC SENTIMENT")

# Give the query call a symbol and start time, it then pulls the date from that time to now (we only run this in on_start)
# Give the query call a symbol and start time, the start time can go back as far as the data goes back.
# The service.query_data function then then pulls the information from that time to now (NOTE : we only run this in on_start)
        startTime=(md.market_open_time - service.time_interval(hours=17.5)) # from 9:30am back to 4pm the previous day = 17.5 hours
#        startTime=(md.market_open_time - service.time_interval(hours=24*90) # 90 days

        print("SMA StockTwits HISTORY")
        sma_st = service.query_data('!sentiment/sma/st/15min', md.symbol,start_timestamp=(md.market_open_time - service.time_interval(hours=17.5)))
#        print sma_st
        for item in sma_st:
            print("sma_st history",str(item))

        print("SMA Twitter HISTORY")
        sma_tw = service.query_data('!sentiment/sma/tw/15min', md.symbol,start_timestamp=(md.market_open_time - service.time_interval(hours=17.5)))
#        print sma_tw
        for item in sma_tw:
            print("sma_tw history",str(item))

Example of a complete SMA Event :

 symbol|muts_timestamp|timestamp|raw-s|raw-s-mean|raw-volatility|raw-score|s|s-mean|s-volatility|s-score|s-volume|sv-mean|sv-volatility|sv-score|s-dispersion|s-buzz|s-delta
AA|1524024600000000|2018-04-18 04:10:00|0.269|0.573|1.043|-0.292|0.148|0.294|0.485|-0.301|36.0|18.444|20.674|0.849|0.778|1.675|0.077
Event values
Field Description
Symbol Symbol
muts_timestamp Muts Timestamp
timestamp Date and time of sentiment estimate (UTC)
Raw-S Raw-S is the summation of sentiment of unique tweets received in a rolling 24-hour window
Raw-S-Mean Raw-S-Mean is the 20-day moving average of Raw-S
Raw-S-Volatility Raw-S-Mean is the 20-day moving standard deviation of Raw-S
Raw-Score This is the statistical Z-Score of Raw-S
S Similar to Raw-S. S is the exponentially time weighted summation of tweets in the 24-hour window.
S-Mean The 20-day moving average of S
S-Volatility The 20-day moving standard deviation of S
S-Score Normalized value of S. This is SMA's S-Score-TM, >1 Positive, >2 High Positive, >3 Extreme Positive (Negatives are same but the inverse, values between -1 and +1 are considered Neutral) - Fidelity have a nice PDF with more info
S-Volume Volume of Unique Tweets used to compute the sentiment estimate.
SV-Mean 20 day moving average of S-Volume
SV-Volatility 20 day moving standard deviation of S-Volume at the time of observation
SV-Score Normalized value of S-Volume. A Z-score of the S-Volume, computed like Raw-Score and S-Score
S-Dispersion Measure of the Tweet source diversity contributing to a sentiment estimate.
S-Buzz A measure of unusual volume activity compared to a universe of stocks.
S-Delta Change in S-Score over a 15-min lookback period.