marketsim.py - \"MC2-P1 Market simulator import pandas as pd import numpy as np import datetime as dt from util import get_data def

marketsim.py - "MC2-P1 Market simulator import pandas as pd...

This preview shows page 1 - 2 out of 3 pages.

"""MC2-P1: Market simulator.""" import pandas as pd import numpy as np import datetime as dt from util import get_data def compute_portvals(orders_file="./orders/orders.csv", start_val=1000000): BUY_STRING = 'BUY' CASH_STRING = 'Cash' CASH_MULTIPLIER = 1.0 DATE_STRING = 'Date' DATE_FORMAT = '%Y-%m-%d' EXEMPTED_DATE = '2011-06-15' ORDER_STRING = 'Order' SHARES_STRING = 'Shares' SYMBOL_STRING = 'Symbol' orders_df = pd.read_csv( orders_file, index_col=DATE_STRING, parse_dates=True, na_values=['nan'], names=[DATE_STRING, SYMBOL_STRING, ORDER_STRING, SHARES_STRING], skiprows=[0]) orders_df.index = pd.to_datetime(orders_df.index, format=DATE_FORMAT) orders_df.index.to_series().apply(lambda x: x.date()) orders_df.sort_index(inplace=True) # Fetch symbols, start and end dates from orders_df symbols = list(set(orders_df[SYMBOL_STRING])) start_date = min(orders_df.index) end_date = max(orders_df.index) date_range = pd.date_range(start_date, end_date) # Fetch prices for all relevant shares prices = get_data(symbols=symbols, dates=date_range, addSPY=False) prices[CASH_STRING] = pd.Series(CASH_MULTIPLIER, index=prices.index) prices = prices[pd.notnull(prices[symbols[0]])] # Trades will contain the number of trades performed on any given day trades_df = prices.copy() trades_df[trades_df != 0] = 0 for index, row in orders_df.iterrows():
Image of page 1
Image of page 2

You've reached the end of your free preview.

Want to read all 3 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes