中级农民
- 积分
- 231
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-7-12
- 最后登录
- 1970-1-1
|
# Trade Data Exercise
You are going to explore and analyze stock market data and trades done by some trading strategy over a historical two-month period. We've provided three CSV file datasets and some helper code to load the data into pandas dataframes. You are free to refer to any external documentation you need while working on these problems. Let's look at the data:
..
### Market Data
. 1point 3acres
The first data set contains two months of market data for a set of US equity symbols, with data aggregated by minute. It's columns are:.google и
- symbol: string, the equity symbol
- time_ns: int64, the unix epoch time in nanoseconds (a note about how to work with this field is below)
- trade_count: int, the total number of trades in the symbols over the previous minute (before time_ns). 1point3acres.com
- volume: int, the total number of shares traded in that minute
- dollar_volume: float, the dollar volume of trades in that minute (the dollar volume of a trade is shares * price)
- last_trade: float, the price of the last trade that happened before time_ns
For your convenience we have translated the time_ns column into date and time columns which you may find useful later on
import pandas as pd
data_dir = 'data'
market_data = pd.read_csv(data_dir + '/history_1.csv')
def add_date_and_time_columns(df):
datetime = pd.to_datetime(df.time_ns)
local_datetime = pd.Series(datetime.array.tz_localize('UTC').tz_convert('US/Eastern'))
df['time_of_day'] = local_datetime.dt.time
df['date'] = local_datetime.dt.date. Waral dи,
add_date_and_time_columns(market_data)
market_data
### Reference Data. Waral dи,
The next file provides equity-specific data, just the sector in which the equity belongs.
. 1point3acres.com
- symbol: string, the equity symbol
- sector: string, the market sector of the equity, e.g. bank, tech, energy
ref_data = pd.read_csv(data_dir + '/refdata_1.csv')
ref_data
### Strategy Trade Data
.--
The final data set contains a history of trading done by some strategy. Columns are:. Waral dи,
- symbol: string, the equity symbol
- time_ns: int64, the unix epoch time when the trade occured
- shares: int, how many shares the strategy bought or sold
- price: float, the price of the transaction. 1point3acres.com
- buy: boolean, true if the strategy bought, false if sold. 1point3acres.com
strategy_trades = pd.read_csv(data_dir + '/trades_1.csv')
add_date_and_time_columns(strategy_trades)
strategy_trades
## Market Trade Analysis - By Symbol
. ----
Let's look at the trades that happened in the market (not the strategy's trades - you do not need the strategy_trades table for these first questions!).
Build a dataset that captures per symbol:
- total trade count
- total share volume
- total dollar volume
- average daily trade count
- average daily share volume
- average daily dollar volume
- percent of total dollar volume represented by that symbol (sum across all symbols should be 100)
Sort and display this dataset from largest average daily dollar volume to smallest
. From 1point 3acres bbs
## Market Trade Analysis - By Sector
Repeat the above but build a data set with one row per sector instead of symbol
## Strategy Analysis - Look-ahead prices
Now let's bring the strategy_trades dataset into the mix.
We want see how each trade fared (its profit or loss) at some time in the future. Specifically we are interested in these time horizons from the time of the trade. .и
- 5 minutes. 1point 3acres
- 10 minutes-baidu 1point3acres
- 15 minutes
- 30 minutes
- 60 minutes
First, write a function that determines the price some minutes in the future for each row of the strategy_trades dataset. Then use that function to create price_min5, price_min10, ..., price_min60 columns in the strategy_trades table. Use the market_data dataset with its one-minute last_trade prices as the source for the look-ahead prices.
## Strategy Analysis - Profit and Return
For each horizon we want
.
- profit or loss in dollars for the trade (for buys this is: (size of trade)(lookahead_price - trade_price) )
- basis points of return for the trade (for buys this is 10000 * (lookahead_price - trade_price) / trade_price). Waral dи,
Create profit_min5, profit_min10, ... profit_min60, and bpreturn_min5, bpreturn_min10, ..., bpreturn_min16 columns in the strategy_trades_dataset
.
## Strategy Analysis - Daily Profits. Χ
For each sector and date, calculate the total profit at each time horizon.
## Strategy Analysis - Plot Cumulative Profit
In one plot, show the _cumulative_ 10-minute profit for each sector across dates - x-axis is date, y-axis is sum of all profits up to and including that date. 1point3acres
## Strategy Analysis - Open Ended
Is there something you can say or show that highlights different trading outcomes (in terms of basis-point return statistics) in the sectors based on time of day of the trades
|
|