{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import datetime as dt\n",
"import pandas as pd\n",
"\n",
"from gs_quant.markets.position_set import Position, PositionSet\n",
"from gs_quant.session import Environment, GsSession"
]
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"client = 'CLIENT ID'\n",
"secret = 'CLIENT SECRET'\n",
"\n",
"GsSession.use(Environment.PROD, client_id=client, client_secret=secret, scopes=('read_product_data',))"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"If you have historical positions that you'd like to bring into Marquee, you should create an individual position\n",
"set per each position date. The below examples show the three valid ways to do this (column names/format included)\n",
"\n",
"### Step 1: Define your file format\n",
"#### Option 1: Create historical position sets by weight\n",
"
\n",
" identifier | weight | date | \n",
" \n",
" AAPL UW | \n",
" 0.4 | \n",
" 2022-06-03 | \n",
"
\n",
" \n",
" MSFT UW | \n",
" 0.6 | \n",
" 2022-06-03 | \n",
"
\n",
" \n",
" AAPL UW | \n",
" 0.4 | \n",
" 2023-01-04 | \n",
"
\n",
" \n",
" MSFT UW | \n",
" 0.2 | \n",
" 2023-01-04 | \n",
"
\n",
" \n",
" IBM UN | \n",
" 0.4 | \n",
" 2023-01-04 | \n",
"
\n",
"
"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"columns = ['identifier', 'weight', 'date']\n",
"equalize = False"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Option 2: Create historical position sets by quantity\n",
"\n",
"\n",
" identifier | quantity | date | \n",
" \n",
" AAPL UW | \n",
" 100 | \n",
" 2022-06-03 | \n",
"
\n",
" \n",
" MSFT UW | \n",
" 100 | \n",
" 2022-06-03 | \n",
"
\n",
" \n",
" AAPL UW | \n",
" 200 | \n",
" 2023-01-04 | \n",
"
\n",
" \n",
" MSFT UW | \n",
" 300 | \n",
" 2023-01-04 | \n",
"
\n",
" \n",
" IBM UN | \n",
" 200 | \n",
" 2023-01-04 | \n",
"
\n",
"
"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"columns = ['identifier', 'quantity', 'date']\n",
"equalize = False"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Option 3: Create equally-weighted historical position sets\n",
"\n",
" identifier | date | \n",
" \n",
" AAPL UW | \n",
" 2022-06-03 | \n",
"
\n",
" \n",
" MSFT UW | \n",
" 2022-06-03 | \n",
"
\n",
" \n",
" AAPL UW | \n",
" 2023-01-04 | \n",
"
\n",
" \n",
" MSFT UW | \n",
" 2023-01-04 | \n",
"
\n",
" \n",
" IBM UN | \n",
" 2023-01-04 | \n",
"
\n",
"
"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"columns = ['identifier', 'date']\n",
"equalize = True"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"### Step 2: Upload your excel file and convert your positions into position sets"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"positions_data = pd.read_excel('positions_data.xlsx', engine='openpyxl')\n",
"all_positions = positions_data[columns]\n",
"\n",
"positions_dict, position_sets = {}, []\n",
"for i, row in all_positions.iterrows():\n",
" date = row.get('date')\n",
" if not date in positions_dict:\n",
" positions_dict[date] = set()\n",
" positions_dict[date].add(Position(identifier=row.get('identifier'),\n",
" weight=row.get('weight', None),\n",
" quantity=row.get('quantity', None)))\n",
"\n",
"for date in positions_dict:\n",
" position_date = dt.datetime.strptime(str(date), '%Y-%m-%d %H:%M:%S').date()\n",
" position_set = PositionSet(positions=positions_dict[date], date=position_date)\n",
" if equalize:\n",
" position_set.equalize_position_weights()\n",
" position_sets.append(position_set)\n",
"\n",
"print(position_sets)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}