コード例 #1
0
ファイル: hw3.py プロジェクト: ybhartia/Machin-learning
def marketsim(cash, orders_file, data_item):
    # Read orders
    orders = defaultdict(list)
    symbols = set([])
    for year, month, day, sym, action, num in csv.reader(
            open(orders_file, "rU")):
        orders[date(int(year), int(month), int(day))].append(
            (sym, action, int(num)))
        symbols.add(sym)

    days = orders.keys()
    days.sort()
    day, end = days[0], days[-1]

    # Reading the Data for the list of Symbols.
    timestamps = getNYSEdays(datetime(day.year, day.month, day.day),
                             datetime(end.year, end.month, end.day + 1),
                             timedelta(hours=16))

    dataobj = DataAccess('Yahoo')
    close = dataobj.get_data(timestamps, symbols, data_item)

    values = []
    portfolio = Portfolio(cash)
    for i, t in enumerate(timestamps):
        for sym, action, num in orders[date(t.year, t.month, t.day)]:
            if action == 'Sell': num *= -1
            portfolio.update(sym, num, close[sym][i])

        entry = (t.year, t.month, t.day, portfolio.value(close, i))
        values.append(entry)

    return values
def marketsim(cash, orders_file, data_item):
    # Read orders
    orders = defaultdict(list)
    symbols = set([])
    for year, month, day, sym, action, num in csv.reader(open(orders_file, "rU")):
        orders[date(int(year), int(month), int(day))].append((sym, action, int(num)))
        symbols.add(sym)
    
    days = orders.keys()
    days.sort()
    day, end = days[0], days[-1]
    
    # Reading the Data for the list of Symbols.
    timestamps = getNYSEdays(datetime(day.year,day.month,day.day),
                             datetime(end.year,end.month,end.day+1),
                             timedelta(hours=16))
    
    dataobj = DataAccess('Yahoo')
    close = dataobj.get_data(timestamps, symbols, data_item)
    
    values = []
    portfolio = Portfolio(cash)
    for i, t in enumerate(timestamps):
        for sym, action, num in orders[date(t.year, t.month, t.day)]:
            if action == 'Sell': num *= -1
            portfolio.update(sym, num, close[sym][i])
        
        entry = (t.year, t.month, t.day, portfolio.value(close, i))
        values.append(entry)
    
    return values
コード例 #3
0
ファイル: hw2.py プロジェクト: ybhartia/Machin-learning
def findEvents(symbols_year, startday, endday, event, data_item="close"):
    dataobj = DataAccess('Yahoo')
    symbols = dataobj.get_symbols_from_list("sp500%d" % symbols_year)
    symbols.append('SPY')

    # Reading the Data for the list of Symbols.
    timestamps = getNYSEdays(startday, endday, timedelta(hours=16))

    # Reading the Data
    print "# reading data"
    close = dataobj.get_data(timestamps, symbols, data_item)

    # Generating the Event Matrix
    print "# finding events"
    eventmat = copy.deepcopy(close)
    for sym in symbols:
        for time in timestamps:
            eventmat[sym][time] = NAN

    for symbol in symbols:
        event(eventmat, symbol, close[symbol], timestamps)

    return eventmat
def findEvents(symbols_year, startday, endday, event, data_item="close"):
    dataobj = DataAccess('Yahoo')
    symbols = dataobj.get_symbols_from_list("sp500%d" % symbols_year)
    symbols.append('SPY')
    
    # Reading the Data for the list of Symbols.
    timestamps = getNYSEdays(startday, endday, timedelta(hours=16))
    
    # Reading the Data
    print "# reading data"
    close = dataobj.get_data(timestamps, symbols, data_item)
    
    # Generating the Event Matrix
    print "# finding events"
    eventmat = copy.deepcopy(close)
    for sym in symbols:
        for time in timestamps:
            eventmat[sym][time] = NAN
    
    for symbol in symbols:
        event(eventmat, symbol, close[symbol], timestamps)
    
    return eventmat
コード例 #5
0
ファイル: hw1.py プロジェクト: ybhartia/Machin-learning
            len(self.values))

    def __str__(self):
        return '\n'.join([
            "\n[%s]" % (self.name if self.name is not None else "Equities"),
            "Sharpe Ratio     : %.6f" % self.sharpe_ratio(),
            "Total Return     : %.4f" % self.tot_return(),
            "Average Daily Ret: %.6f" % self.average_return(),
            "STDEV Daily Ret  : %.6f" % self.stdev_return(),
        ])


if __name__ == '__main__':
    # TODO: sharp ratio higher than 4?
    PORTFOLIO = (('AAPL', 0.6), ('GLD', 0.2), ('WMT', 0.1), ('CVX', 0.1))

    YEAR = 2011
    timestamps = getNYSEdays(datetime(YEAR, 1, 1), datetime(YEAR, 12, 31),
                             timedelta(hours=16))

    BENCHMARK = 'SPY'
    symbols = [s for s, _ in PORTFOLIO] + [BENCHMARK]

    close = DataAccess('Yahoo').get_data(timestamps, symbols, "close")

    print Equities(
        [sum([close[s][i] for s in symbols]) for i in range(len(timestamps))],
        "Portfolio")
    print Equities([close[BENCHMARK][i] for i in range(len(timestamps))],
                   "Benchmark")