Back to Community
Using proprietary data from CSV with 'fetch' function

I have a CSV with date, signal, and symbol information linked below. In backtesting, I am able to get the signal data for that date, but I am not able to then trade the security in the 'ticker' column based on the signal. What am I missing when trying to access both?

CSV File

3 responses

Are you saying that you aren't able to access any of your CSV file or are you saying that you can't use the data within the 'ticker' column? Either way, it would be helpful if you sent us your code. I think I recall having to use the sid instead of the ticker when importing CSV files via fetch.

This is what I'm working with. I am able to print out the value of the indicator column for each day, but I am also trying to trade the symbol from the 'symbol' column based on the value in the indicator column for the same day.

Clone Algorithm
7
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# This algorithm trades based on the personal tweets in President Donald Trump's Twitter feed
# (c) 2017 Chris Thurber, idkcode

import numpy as np
import datetime
# Average over 5 weeks, free parameter.
delta_t = 5

def initialize(context):
    # This is the search query we are using, this is tied to the csv file.
    context.sentiment = 'sentiment_data'
    # User fetcher to get data. I uploaded this csv file manually, feel free to use.
    # Note that this data is already weekly averages.
    fetch_csv('https://gist.githubusercontent.com/cthurber/95ef8adf52a65407d72b2be0859ee621/raw/cf11c0c561651266886961787889feebcc6858f0/all-tweets-signal-cat.csv', 
              date_column='created_at', 
              date_format='%Y-%m-%d', 
              timezone='EST',
              symbol='TM'
             )
    context.order_size = 1000
    # # context.sec_id = 8554
    # context.security = symbol('TM')
       
    schedule_function(rebalance, 
                      date_rule=date_rules.every_day())

    context.past_queries = []

def rebalance(context, data):


    c = context
    # print data.current(symbol,'sentiment_data')
    
    current_security = symbol('TM')

    # # Extract weekly average of search query.
    # indicator = data.current(current_security, c.sentiment)

    indicator = data['TM']['sentiment_data']
    print indicator

    # # Track our fetched values in a context variable to build up history.

    order_size = 100

    if indicator == 0:
        print "Sold",current_security
        order_target(current_security, -order_size)
    elif indicator == 1:
        print "Bought",current_security
        order_target(current_security, order_size)
There was a runtime error.

As an update, I've pulled a basket of securities from the file with a regular expression into a 'symbols' data frame in the latest iteration seen below:

Clone Algorithm
7
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import numpy as np
import datetime

delta_t = 5

def initialize(context):

    context.securities = symbols('ERIC','BA','BSMX','FELE','ALSN','FLIR','MINI','FLWS','HFBL','SGMS','FOXF','EMI','WILN','THFF','NYMT','NYMTO','NYRT','NYT','CRESY','EVLV','LYV','WAFD','RXN','OVBC','ARNA','USB','TVC','TVE','EVBS','SONA','CFNB','BANC','DCI','BBSI','IBM','AMP','BOTJ','JRVR','RJF','HBK','ITW','LINC','F','SNDE','CNTF','PGJ','GTWN','PUB','MANH','CFMS','CBRL','FNV','PZG','BVSN','HSY','CXRX','EZPW','ACM','IVR','LVS','TWTR','FB','COP','PHG','PSX','PSXP','CALM','MANU','IAC','INB','LTXB','TCBI','EXPD','AMCN','NCMI','VISN','CBLI','ICD','HUM','ESBK','TRST','ABMD','CNMD','DEX','EVO','HTLF','STOR','COST','WFM','LOCO','MTBC','MTBCP','AMRS','HMNF','CINF','LBAI','LKFN','GATX','STWD','FORM','FDS','JONE','BBL','BBG','CLIR','TRI','GLPG','AGZD','FCCY','BCBP','CATM','CAJ','BIG','SMBC','MCHX','FENG','EAI','DNOW','QUIK','GOV','SWFT','AMCX','MSG','SNI','MSGN','IR','FRAN','COL','DIS','CACQ','CZR','BWLD','BNJ','BLJ','EVJ','CTO','TMP','HOG','MHE','MMV','JBSS','MJN','TISA','KALU','RBS','ISSC','AP','BDE','CAA','NAME','HVT','TRUE','AXP','CTWS','BZM','TIF','MCD','BUFF','AMRI','RBCAA','FRBK','ORI','BK','NYCB','GRID','SND','MDU','ABCD','ACC','CPT','CORE','BOH','MT','AAPL','SIRI','NATI','ANGI','SSRI','SALM','CDTI','JCTCF','DPZ','RY','BLMT','UBS','COR','BAA','ATLO','GS','FUN','BURL','VTTI','CEO','MSA','Q','TSLA','GM','FCAU','TM')
    context.sentiment = 'sentiment_data'
    # User fetcher to get data
    fetch_csv('https://gist.githubusercontent.com/cthurber/95ef8adf52a65407d72b2be0859ee621/raw/cf11c0c561651266886961787889feebcc6858f0/all-tweets-signal-cat.csv', 
              date_column='created_at', 
              date_format='%Y-%m-%d', 
              timezone='EST'
             )
    context.order_size = 100
       
    schedule_function(rebalance, 
                      date_rule=date_rules.every_day())

def rebalance(context, data):

    c = context

    for security in c.securities:

        indicator = data[security]['sentiment_data']
        
        current_price = data.current(security,'price')
        # 46.2 drawdown
        order_size = int(1000 / current_price) if current_price == current_price else 500

        if indicator == 0:
            order_target(security, -order_size)
        elif indicator == 1:
            order_target(security, order_size)
There was a runtime error.