1.複数株価の一括取得

In [84]:
import pandas as pd
import datetime as dt
import pandas_datareader.data as web

# 期間設定
start = dt.date(2017,12,30)
end = dt.date(2018, 3, 7)

# ダウンロードリスト
tickerList = ['AMZN','ANET',"CGNX","NVDA","FB","NFLX","GOOGL","SPY","SPXL","TMF","BND"]

#ダウンロード
stocks = web.DataReader(tickerList,'yahoo',start,end)
In [85]:
# dfデータフレームに格納。念のためindexでソート。
df = stocks.Close.sort_index(ascending=True).copy()
In [86]:
#  念のため欠損ちを除去&一覧表示で確認
df = df.dropna()
df
Out[86]:
AMZN ANET BND CGNX FB GOOGL NFLX NVDA SPXL SPY TMF
Date
2017-12-29 1169.469971 235.580002 81.570000 61.160000 176.460007 1053.400024 191.960007 193.500000 44.330002 266.859985 22.110001
2018-01-02 1189.010010 231.029999 81.339996 61.380001 181.419998 1073.209961 201.070007 199.350006 45.189999 268.769989 21.379999
2018-01-03 1204.199951 235.600006 81.410004 63.709999 184.669998 1091.520020 205.050003 212.470001 45.990002 270.470001 21.639999
2018-01-04 1209.589966 232.690002 81.370003 65.589996 184.330002 1095.760010 205.630005 213.589996 46.570000 271.609985 21.670000
2018-01-05 1229.140015 236.770004 81.260002 66.379997 186.850006 1110.290039 209.990005 215.399994 47.450001 273.420013 21.500000
2018-01-08 1246.869995 251.059998 81.269997 67.779999 188.279999 1114.209961 212.050003 222.000000 47.740002 273.920013 21.440001
2018-01-09 1252.699951 249.979996 81.029999 67.739998 187.869995 1112.790039 209.309998 221.940002 48.070000 274.540009 20.549999
2018-01-10 1254.329956 252.279999 81.040001 67.019997 187.839996 1110.140015 212.520004 223.679993 47.790001 274.119995 20.510000
2018-01-11 1276.680054 260.790009 81.040001 67.040001 187.770004 1112.050049 217.240005 224.080002 48.869999 276.119995 20.760000
2018-01-12 1305.199951 259.029999 81.029999 67.910004 179.369995 1130.650024 221.229996 222.979996 49.820000 277.920013 20.850000
2018-01-16 1304.859985 252.770004 81.089996 67.419998 178.389999 1130.699951 221.529999 220.110001 49.290001 276.970001 21.070000
2018-01-17 1295.000000 262.809998 81.000000 68.389999 177.600006 1139.099976 217.500000 224.720001 50.660000 279.609985 21.049999
2018-01-18 1293.319946 265.190002 80.839996 68.269997 179.800003 1135.969971 220.330002 224.440002 50.459999 279.140015 20.459999
2018-01-19 1294.579956 270.839996 80.669998 69.059998 181.289993 1143.500000 220.460007 230.110001 51.090000 280.410004 20.120001
2018-01-22 1327.310059 275.679993 80.610001 69.000000 185.369995 1164.160034 227.580002 233.690002 52.349998 282.690002 20.120001
2018-01-23 1362.540039 273.140015 80.820000 69.620003 189.350006 1176.170044 250.289993 238.910004 52.700001 283.290009 20.410000
2018-01-24 1357.510010 273.140015 80.730003 67.339996 186.550003 1171.290039 261.299988 235.800003 52.619999 283.179993 20.100000
2018-01-25 1377.949951 276.760010 80.949997 67.440002 187.479996 1182.140015 269.700012 236.350006 52.689999 283.299988 20.600000
2018-01-26 1402.050049 283.500000 80.800003 66.800003 190.000000 1187.560059 274.600006 243.330002 54.480000 286.579987 20.309999
2018-01-29 1417.680054 277.989990 80.690002 65.320000 185.979996 1186.479980 284.589996 246.850006 53.410000 284.679993 19.940001
2018-01-30 1437.819946 275.230011 80.540001 63.490002 187.119995 1177.369995 278.799988 242.720001 51.709999 281.760010 19.570000
2018-01-31 1450.890015 275.820007 80.559998 62.369999 186.889999 1182.219971 270.299988 245.800003 51.840000 281.899994 19.950001
2018-02-01 1390.000000 275.000000 80.160004 63.099998 193.089996 1181.589966 265.070007 240.500000 51.630001 281.579987 19.040001
2018-02-02 1429.949951 273.480011 79.919998 61.169998 190.279999 1119.199951 267.429993 233.520004 48.270000 275.450012 18.510000
2018-02-05 1390.000000 265.369995 80.269997 56.599998 181.259995 1062.390015 254.259995 213.699997 42.340000 263.929993 19.059999
2018-02-06 1442.839966 272.380005 80.010002 57.939999 185.309998 1084.430054 265.720001 225.580002 44.689999 269.130005 18.680000
2018-02-07 1416.780029 281.619995 79.830002 56.980000 180.179993 1055.410034 264.559998 228.800003 43.919998 267.670013 18.139999
2018-02-08 1350.500000 264.519989 79.769997 53.650002 171.580002 1007.710022 250.100006 217.520004 39.040001 257.630005 18.100000
2018-02-09 1339.599976 272.549988 79.570000 55.020000 176.110001 1046.270020 249.470001 232.080002 40.830002 261.500000 17.719999
2018-02-12 1386.229980 284.540009 79.589996 54.889999 176.410004 1054.560059 257.950012 228.029999 42.430000 265.339996 17.990000
2018-02-13 1414.510010 292.579987 79.660004 55.169998 173.149994 1054.140015 258.269989 232.630005 42.810001 266.000000 18.170000
2018-02-14 1451.050049 302.119995 79.400002 58.049999 179.520004 1072.699951 266.000000 241.419998 44.520000 269.589996 17.610001
2018-02-15 1461.760010 307.959991 79.480003 58.799999 179.960007 1091.359985 280.269989 246.500000 46.189999 273.029999 17.790001
2018-02-16 1448.689941 249.490005 79.650002 55.770000 177.360001 1095.500000 278.519989 243.839996 46.200001 273.109985 18.049999
2018-02-20 1468.349976 249.649994 79.570000 56.439999 176.009995 1103.589966 278.549988 249.080002 45.369999 271.399994 17.799999
2018-02-21 1482.920044 238.029999 79.269997 56.970001 177.910004 1113.750000 281.040009 241.509995 44.619999 270.049988 17.180000
2018-02-22 1485.339966 244.660004 79.370003 54.520000 178.990005 1109.900024 278.140015 242.149994 44.840000 270.399994 17.309999
2018-02-23 1500.000000 245.919998 79.580002 54.200001 183.289993 1128.089966 285.929993 245.929993 46.980000 274.709991 17.770000
2018-02-26 1521.949951 259.850006 79.620003 53.880001 184.929993 1143.699951 294.160004 246.580002 48.619999 277.899994 17.790001
2018-02-27 1511.979980 259.140015 79.449997 53.549999 181.460007 1117.510010 290.609985 246.059998 46.779999 274.429993 17.719999
2018-02-28 1512.449951 269.739990 79.550003 53.709999 178.320007 1103.920044 291.380005 242.000000 45.230000 271.649994 18.040001
2018-03-01 1493.449951 263.799988 79.519997 53.000000 175.940002 1071.410034 290.390015 232.210007 43.380001 267.700012 18.420000
2018-03-02 1500.250000 271.500000 79.370003 53.020000 176.619995 1084.140015 301.049988 236.539993 44.060001 269.079987 17.930000
2018-03-05 1523.609985 281.859985 79.320000 52.520000 180.399994 1094.760010 315.000000 235.649994 45.529999 272.190002 17.840000
2018-03-06 1537.640015 288.600006 79.349998 53.099998 179.779999 1100.900024 325.220001 242.160004 45.889999 272.880005 17.860001

2.データフレームの作成

2.1.列追加

In [87]:
# 自身の保有銘柄の評価金額を計算する列を追加
df["myAMZN"]=0
df["myANET"]=0
df["myCGNX"]=0
df["myNVDA"]=0

# キャッシュを記録する列を追加
df["cash"] = 0

# ドル転分キャッシュを記録する列を追加
df["addCash"] = 0

2.2.ドル転キャッシュの履歴を記録

In [88]:
# 1/4に8880.4ドルをドル転。1/4以降はその影響を受ける。

df["addCash"].loc["2018-01-04":] = 8880.41
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [89]:
# 2/15にドル転を追加。それ以降はその影響を受ける。

df["addCash"].loc["2018-02-15":] = 8880.41 + 405
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [90]:
# 記録した結果の確認

df["addCash"]
Out[90]:
Date
2017-12-29       0.00
2018-01-02       0.00
2018-01-03       0.00
2018-01-04    8880.41
2018-01-05    8880.41
2018-01-08    8880.41
2018-01-09    8880.41
2018-01-10    8880.41
2018-01-11    8880.41
2018-01-12    8880.41
2018-01-16    8880.41
2018-01-17    8880.41
2018-01-18    8880.41
2018-01-19    8880.41
2018-01-22    8880.41
2018-01-23    8880.41
2018-01-24    8880.41
2018-01-25    8880.41
2018-01-26    8880.41
2018-01-29    8880.41
2018-01-30    8880.41
2018-01-31    8880.41
2018-02-01    8880.41
2018-02-02    8880.41
2018-02-05    8880.41
2018-02-06    8880.41
2018-02-07    8880.41
2018-02-08    8880.41
2018-02-09    8880.41
2018-02-12    8880.41
2018-02-13    8880.41
2018-02-14    8880.41
2018-02-15    9285.41
2018-02-16    9285.41
2018-02-20    9285.41
2018-02-21    9285.41
2018-02-22    9285.41
2018-02-23    9285.41
2018-02-26    9285.41
2018-02-27    9285.41
2018-02-28    9285.41
2018-03-01    9285.41
2018-03-02    9285.41
2018-03-05    9285.41
2018-03-06    9285.41
Name: addCash, dtype: float64

2.3.保有銘柄の評価額を記録

In [91]:
# 年初の保有株数を記録。12/29以降は34株を保有
df["myANET"].loc["2017-12-29":] = df["ANET"] * 34
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [92]:
# 年初の保有株数を記録。12/29以降は30株を保有
df["myAMZN"].loc["2017-12-29":] = df["AMZN"]* 30
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [93]:
# 年初の保有株数を記録。12/29以降は336株を保有
df["myCGNX"] = df["CGNX"].loc["2017-12-29":] * 336
In [94]:
# 年初の保有株数を記録。12/29以降は170株を保有
df["myNVDA"] = df["NVDA"] * 170
In [95]:
# ANETを1/4に追加購入。合計46株になったため更新。
df["myANET"].loc["2018-01-04":] = df["ANET"]* 46
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [96]:
# AMZNを1/4に追加購入。合計35株になったため更新。
df["myAMZN"].loc["2018-01-04":] = df["AMZN"] * 35
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [97]:
# CGNXを2/5に売却。合計166株になったため更新。
df["myCGNX"].loc["2018-02-05":] = df["CGNX"]* 166
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [98]:
df["myCGNX"].loc["2018-02-06":] = df["CGNX"] * 1
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [99]:
df["myANET"].loc["2018-02-06":] = df["ANET"]* 1
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [100]:
df["myNVDA"].loc["2018-02-08":] = df["NVDA"]* 300
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [101]:
df["myNVDA"].loc["2018-02-13":] = df["NVDA"]* 200
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [102]:
df["myCGNX"].loc["2018-02-14":] = df["CGNX"]* 151
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [103]:
df["myANET"].loc["2018-02-14":] = df["ANET"]* 51
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [104]:
df["myCGNX"].loc["2018-02-15":] = df["CGNX"]* 1
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [105]:
df["myANET"].loc["2018-02-15":] = df["ANET"]* 1
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [106]:
df["myAMZN"].loc["2018-02-26":] = df["AMZN"]* 40
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [107]:
df["myANET"].loc["2018-02-26":] = df["ANET"]* 53
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)

2.4.Cashの金額推移の記録

In [108]:
df["cash"].loc["2018-02-05":] = 10059.28
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
In [109]:
df["cash"].loc["2018-02-06":] = 31335.64
In [110]:
df["cash"].loc["2018-02-08":] = 900
In [111]:
df["cash"].loc["2018-02-13":] = 20000
In [112]:
df["cash"].loc["2018-02-14":] = 140.57
In [113]:
df["cash"].loc["2018-02-15":] = 21263.91
In [114]:
df["cash"].loc["2018-02-26":] = 90.26
In [115]:
df
Out[115]:
AMZN ANET BND CGNX FB GOOGL NFLX NVDA SPXL SPY TMF myAMZN myANET myCGNX myNVDA cash addCash
Date
2017-12-29 1169.469971 235.580002 81.570000 61.160000 176.460007 1053.400024 191.960007 193.500000 44.330002 266.859985 22.110001 35084.099130 8009.720068 20549.760000 32895.00000 0.00 0.00
2018-01-02 1189.010010 231.029999 81.339996 61.380001 181.419998 1073.209961 201.070007 199.350006 45.189999 268.769989 21.379999 35670.300300 7855.019966 20623.680336 33889.50102 0.00 0.00
2018-01-03 1204.199951 235.600006 81.410004 63.709999 184.669998 1091.520020 205.050003 212.470001 45.990002 270.470001 21.639999 36125.998530 8010.400204 21406.559664 36119.90017 0.00 0.00
2018-01-04 1209.589966 232.690002 81.370003 65.589996 184.330002 1095.760010 205.630005 213.589996 46.570000 271.609985 21.670000 42335.648810 10703.740092 22038.238656 36310.29932 0.00 8880.41
2018-01-05 1229.140015 236.770004 81.260002 66.379997 186.850006 1110.290039 209.990005 215.399994 47.450001 273.420013 21.500000 43019.900525 10891.420184 22303.678992 36617.99898 0.00 8880.41
2018-01-08 1246.869995 251.059998 81.269997 67.779999 188.279999 1114.209961 212.050003 222.000000 47.740002 273.920013 21.440001 43640.449825 11548.759908 22774.079664 37740.00000 0.00 8880.41
2018-01-09 1252.699951 249.979996 81.029999 67.739998 187.869995 1112.790039 209.309998 221.940002 48.070000 274.540009 20.549999 43844.498285 11499.079816 22760.639328 37729.80034 0.00 8880.41
2018-01-10 1254.329956 252.279999 81.040001 67.019997 187.839996 1110.140015 212.520004 223.679993 47.790001 274.119995 20.510000 43901.548460 11604.879954 22518.718992 38025.59881 0.00 8880.41
2018-01-11 1276.680054 260.790009 81.040001 67.040001 187.770004 1112.050049 217.240005 224.080002 48.869999 276.119995 20.760000 44683.801890 11996.340414 22525.440336 38093.60034 0.00 8880.41
2018-01-12 1305.199951 259.029999 81.029999 67.910004 179.369995 1130.650024 221.229996 222.979996 49.820000 277.920013 20.850000 45681.998285 11915.379954 22817.761344 37906.59932 0.00 8880.41
2018-01-16 1304.859985 252.770004 81.089996 67.419998 178.389999 1130.699951 221.529999 220.110001 49.290001 276.970001 21.070000 45670.099475 11627.420184 22653.119328 37418.70017 0.00 8880.41
2018-01-17 1295.000000 262.809998 81.000000 68.389999 177.600006 1139.099976 217.500000 224.720001 50.660000 279.609985 21.049999 45325.000000 12089.259908 22979.039664 38202.40017 0.00 8880.41
2018-01-18 1293.319946 265.190002 80.839996 68.269997 179.800003 1135.969971 220.330002 224.440002 50.459999 279.140015 20.459999 45266.198110 12198.740092 22938.718992 38154.80034 0.00 8880.41
2018-01-19 1294.579956 270.839996 80.669998 69.059998 181.289993 1143.500000 220.460007 230.110001 51.090000 280.410004 20.120001 45310.298460 12458.639816 23204.159328 39118.70017 0.00 8880.41
2018-01-22 1327.310059 275.679993 80.610001 69.000000 185.369995 1164.160034 227.580002 233.690002 52.349998 282.690002 20.120001 46455.852065 12681.279678 23184.000000 39727.30034 0.00 8880.41
2018-01-23 1362.540039 273.140015 80.820000 69.620003 189.350006 1176.170044 250.289993 238.910004 52.700001 283.290009 20.410000 47688.901365 12564.440690 23392.321008 40614.70068 0.00 8880.41
2018-01-24 1357.510010 273.140015 80.730003 67.339996 186.550003 1171.290039 261.299988 235.800003 52.619999 283.179993 20.100000 47512.850350 12564.440690 22626.238656 40086.00051 0.00 8880.41
2018-01-25 1377.949951 276.760010 80.949997 67.440002 187.479996 1182.140015 269.700012 236.350006 52.689999 283.299988 20.600000 48228.248285 12730.960460 22659.840672 40179.50102 0.00 8880.41
2018-01-26 1402.050049 283.500000 80.800003 66.800003 190.000000 1187.560059 274.600006 243.330002 54.480000 286.579987 20.309999 49071.751715 13041.000000 22444.801008 41366.10034 0.00 8880.41
2018-01-29 1417.680054 277.989990 80.690002 65.320000 185.979996 1186.479980 284.589996 246.850006 53.410000 284.679993 19.940001 49618.801890 12787.539540 21947.520000 41964.50102 0.00 8880.41
2018-01-30 1437.819946 275.230011 80.540001 63.490002 187.119995 1177.369995 278.799988 242.720001 51.709999 281.760010 19.570000 50323.698110 12660.580506 21332.640672 41262.40017 0.00 8880.41
2018-01-31 1450.890015 275.820007 80.559998 62.369999 186.889999 1182.219971 270.299988 245.800003 51.840000 281.899994 19.950001 50781.150525 12687.720322 20956.319664 41786.00051 0.00 8880.41
2018-02-01 1390.000000 275.000000 80.160004 63.099998 193.089996 1181.589966 265.070007 240.500000 51.630001 281.579987 19.040001 48650.000000 12650.000000 21201.599328 40885.00000 0.00 8880.41
2018-02-02 1429.949951 273.480011 79.919998 61.169998 190.279999 1119.199951 267.429993 233.520004 48.270000 275.450012 18.510000 50048.248285 12580.080506 20553.119328 39698.40068 0.00 8880.41
2018-02-05 1390.000000 265.369995 80.269997 56.599998 181.259995 1062.390015 254.259995 213.699997 42.340000 263.929993 19.059999 48650.000000 12207.019770 9395.599668 36328.99949 10059.28 8880.41
2018-02-06 1442.839966 272.380005 80.010002 57.939999 185.309998 1084.430054 265.720001 225.580002 44.689999 269.130005 18.680000 50499.398810 272.380005 57.939999 38348.60034 31335.64 8880.41
2018-02-07 1416.780029 281.619995 79.830002 56.980000 180.179993 1055.410034 264.559998 228.800003 43.919998 267.670013 18.139999 49587.301015 281.619995 56.980000 38896.00051 31335.64 8880.41
2018-02-08 1350.500000 264.519989 79.769997 53.650002 171.580002 1007.710022 250.100006 217.520004 39.040001 257.630005 18.100000 47267.500000 264.519989 53.650002 65256.00120 900.00 8880.41
2018-02-09 1339.599976 272.549988 79.570000 55.020000 176.110001 1046.270020 249.470001 232.080002 40.830002 261.500000 17.719999 46885.999160 272.549988 55.020000 69624.00060 900.00 8880.41
2018-02-12 1386.229980 284.540009 79.589996 54.889999 176.410004 1054.560059 257.950012 228.029999 42.430000 265.339996 17.990000 48518.049300 284.540009 54.889999 68408.99970 900.00 8880.41
2018-02-13 1414.510010 292.579987 79.660004 55.169998 173.149994 1054.140015 258.269989 232.630005 42.810001 266.000000 18.170000 49507.850350 292.579987 55.169998 46526.00100 20000.00 8880.41
2018-02-14 1451.050049 302.119995 79.400002 58.049999 179.520004 1072.699951 266.000000 241.419998 44.520000 269.589996 17.610001 50786.751715 15408.119745 8765.549849 48283.99960 140.57 8880.41
2018-02-15 1461.760010 307.959991 79.480003 58.799999 179.960007 1091.359985 280.269989 246.500000 46.189999 273.029999 17.790001 51161.600350 307.959991 58.799999 49300.00000 21263.91 9285.41
2018-02-16 1448.689941 249.490005 79.650002 55.770000 177.360001 1095.500000 278.519989 243.839996 46.200001 273.109985 18.049999 50704.147935 249.490005 55.770000 48767.99920 21263.91 9285.41
2018-02-20 1468.349976 249.649994 79.570000 56.439999 176.009995 1103.589966 278.549988 249.080002 45.369999 271.399994 17.799999 51392.249160 249.649994 56.439999 49816.00040 21263.91 9285.41
2018-02-21 1482.920044 238.029999 79.269997 56.970001 177.910004 1113.750000 281.040009 241.509995 44.619999 270.049988 17.180000 51902.201540 238.029999 56.970001 48301.99900 21263.91 9285.41
2018-02-22 1485.339966 244.660004 79.370003 54.520000 178.990005 1109.900024 278.140015 242.149994 44.840000 270.399994 17.309999 51986.898810 244.660004 54.520000 48429.99880 21263.91 9285.41
2018-02-23 1500.000000 245.919998 79.580002 54.200001 183.289993 1128.089966 285.929993 245.929993 46.980000 274.709991 17.770000 52500.000000 245.919998 54.200001 49185.99860 21263.91 9285.41
2018-02-26 1521.949951 259.850006 79.620003 53.880001 184.929993 1143.699951 294.160004 246.580002 48.619999 277.899994 17.790001 60877.998040 13772.050318 53.880001 49316.00040 90.26 9285.41
2018-02-27 1511.979980 259.140015 79.449997 53.549999 181.460007 1117.510010 290.609985 246.059998 46.779999 274.429993 17.719999 60479.199200 13734.420795 53.549999 49211.99960 90.26 9285.41
2018-02-28 1512.449951 269.739990 79.550003 53.709999 178.320007 1103.920044 291.380005 242.000000 45.230000 271.649994 18.040001 60497.998040 14296.219470 53.709999 48400.00000 90.26 9285.41
2018-03-01 1493.449951 263.799988 79.519997 53.000000 175.940002 1071.410034 290.390015 232.210007 43.380001 267.700012 18.420000 59737.998040 13981.399364 53.000000 46442.00140 90.26 9285.41
2018-03-02 1500.250000 271.500000 79.370003 53.020000 176.619995 1084.140015 301.049988 236.539993 44.060001 269.079987 17.930000 60010.000000 14389.500000 53.020000 47307.99860 90.26 9285.41
2018-03-05 1523.609985 281.859985 79.320000 52.520000 180.399994 1094.760010 315.000000 235.649994 45.529999 272.190002 17.840000 60944.399400 14938.579205 52.520000 47129.99880 90.26 9285.41
2018-03-06 1537.640015 288.600006 79.349998 53.099998 179.779999 1100.900024 325.220001 242.160004 45.889999 272.880005 17.860001 61505.600600 15295.800318 53.099998 48432.00080 90.26 9285.41

2.5.ポートフォリオの計算

In [116]:
#運用利回り計算用のポートフォリオ資産推移
df["myPortfolio"] = df["myAMZN"] + df["myANET"] + df["myCGNX"] + df["myNVDA"] - df["addCash"] + df["cash"]

3.運用利回りの計算とグラフ化

In [118]:
# 前日比の変化率を計算。pct_change()関数で一発変換し、dfpctというデータフレームに格納
dfpct = df.pct_change()
In [119]:
# 運用利回りのグラフを作成

# 描画用のライブラリインポート
import matplotlib.pyplot as plt

# 変化率を累積計算し、myPerformanceというデータフレームに変換。 
myPerformance=(1+dfpct).cumprod()

# 変化率計算で発生した1行目のNaNを1に変換。
myPerformance.ix[0]=1

# myPortfolioとベンチマーク先の銘柄を比較。図のサイズとグリッド線の太さを指定。
myPerformance[["myPortfolio","SPY","SPXL"]].plot(figsize = (12,6),grid = True,linewidth=3)

# 描画
plt.show()
/usr/local/lib/python3.6/site-packages/ipykernel_launcher.py:10: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
  # Remove the CWD from sys.path while we load stuff.
In [122]:
#単純な総資産推移。新しく定義したデータフレームassetに入れる。
asset = pd.DataFrame({
    "AMZN" : df["myAMZN"],
    "NVDA": df["myNVDA"],
    "CGNX": df["myCGNX"],
    "ANET" : df["myANET"],
    "cash":df["cash"]
})
In [120]:
# 総資産の推移を描画。assetをエリアプロット。カラーマップは爽やかなsummer

asset.plot(y=['AMZN', "NVDA", "ANET",'CGNX',"cash"], kind='area',alpha=1, figsize=(12,9), stacked=True,cmap='summer',grid = True )

plt.show()