# Tokyo Quantopian User Group Vol.11 ドキュメント翻訳
## 名前 Kei Sanada 役割 ユーザーレビュー
## 状況
・「Tutorial 1 Getting Started Lesson 1-8」 を
GitHubでissue_8から、rstファイルを開いて([1_getting_started_lesson1.rst](https://github.com/tokyoquantopian/quantopian-doc-ja/blob/issue_8/source/tutorial/1_getting_started_lesson1.rst) など)を一通り読みました。
・ここ最近の翻訳の進め方、把握が遅れがち
・read the docsの使い方が良くわかっていません。
## 今日のTODO
1.引き続きissue_14 2_pipeline_lesson1-12(翻訳済、査読中)の[Tutorial 2 Pipeline Lesson1-12](https://github.com/tokyoquantopian/quantopian-doc-ja/blob/issue_14/source/tutorial/2_pipeline_lesson1.rst)を読み進めてQuatopianを思いだす。
2.「Tutorial 1 Getting Started Lesson 1-8」を読んだ初学者のつもりになって、Tutorialのサンプルーコードを改修できるか試してみる。
**ネタ:ROE偏重経営、もろさ露呈 想定外に備えはあるか**
https://r.nikkei.com/article/DGXMZO5742458030032020SHA000?disablepcview=&s=3
**要約:自己資本比率の高い企業の方が長期的には成長するのでは?**
> データを分析すれば明らかだ。1999年当時に自己資本が総資産に対してどれだけあったか(=自己資本比率)で世界の上場企業を5分類し、その後20年の利益の伸びを比較した。
> 
>
>
> 利益の伸びが3.4倍と最も見劣りするのが、自己資本比率が20%未満の企業だ。少しのショックでも経営がぐらつき、競争力を高める機会を逃しやすい。一方、自己資本比率が60%以上~80%未満と比較的厚い企業は利益の伸びが6.5倍と最も大きい。不況の際にも経営は揺るがず、むしろ大規模な投資でシェアを拡大するといった攻めの一手を打てるためだ。
使用するコード、データセット(morningstar)から自己資本比率を算出
```
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.morningstar import Fundamentals
from quantopian.pipeline.domain import US_EQUITIES
from quantopian.research import run_pipeline
# Construct a factor that simply gets the most recent total revenue value.
# total_revenue = Fundamentals.total_revenue.latest
total_equity = Fundamentals.total_equity.latest
total_assets = Fundamentals.total_assets.latest
capital_adequacy_ratio = total_equity / total_assets * 100 #自己資本比率 データの取得元正しいのかな?
# Add the factor to the pipeline.
pipe = Pipeline(
columns={
# 'total_equity': total_equity,
#' total_assets': total_assets,
'capital_adequacy_ratio': capital_adequacy_ratio
},
domain=US_EQUITIES,
)
# Run the pipeline over a year and print the result.
df = run_pipeline(pipe, '2015-05-05', '2016-05-05')
print(df.head())
```
---
> Pipeline Execution Time: 1.24 Seconds
> capital_adequacy_ratio
> 2015-05-05 00:00:00+00:00 Equity(2 [HWM]) 32.425618
> Equity(21 [AAME]) 32.868256
> Equity(24 [AAPL]) 49.390874
> Equity(25 [HWM_PR]) 32.425618
> Equity(31 [ABAX]) 84.526509
取得できていそう。
こちらを元に「1_getting_started_lesson4 戦略定義」のコードなら改修できそう。
```python
# Pipeline インポート
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.psychsignal import stocktwits
from quantopian.pipeline.factors import SimpleMovingAverage
from quantopian.pipeline.filters import QTradableStocksUS
# モーニングスターのデータセット
from quantopian.pipeline.data.morningstar import Fundamentals
from quantopian.pipeline.domain import US_EQUITIES
# Pipeline 定義
def make_pipeline():
base_universe = QTradableStocksUS()
total_equity = Fundamentals.total_equity.latest
total_assets = Fundamentals.total_assets.latest
capital_adequacy_ratio = total_equity / total_assets * 100 #自己資本比率 データの取得元正しいのかな?
# 自己資本比率に基づいて上位下位350銘柄のみを取得するフィルターを作成
top_bottom_scores = (
capital_adequacy_ratio.top(350) | capital_adequacy_ratio.bottom(350)
)
return Pipeline(
columns={
'capital_adequacy_ratio': capital_adequacy_ratio,
},
# 定義したフィルターとトレーディングユニバースのどちらにも入っている銘柄のみにスクリーニングする
screen=(
base_universe
& top_bottom_scores
)
)
# run_pipeline インポート
from quantopian.research import run_pipeline
# 評価する期間を指定
period_start = '2013-01-01'
period_end = '2016-01-01'
# 指定期間で pipeline 実行
pipeline_output = run_pipeline(
make_pipeline(),
start_date=period_start,
end_date=period_end
)
```
<b>Pipeline Execution Time:</b> 14.48 Seconds
```python
# prices 関数をインポート
from quantopian.research import prices
# pipeline が出力した dataframe の index から銘柄リストを取得し、 unique 関数を使って、重複しないリストを取得します。
asset_list = pipeline_output.index.levels[1].unique()
# 銘柄リストに入っている銘柄全てに対して、指定期間の価格を取得します。
asset_prices = prices(
asset_list,
start=period_start,
end=period_end
)
```
```python
# Alphalens インポート
import alphalens as al
# センチメントスコアに基づいて、quantileに指定された分位数にわける
factor_data = al.utils.get_clean_factor_and_forward_returns(
factor=pipeline_output['capital_adequacy_ratio'],
prices=asset_prices,
quantiles=2,
periods=(1,5,10),
)
# 上から5行を表示
factor_data.head(5)
```
Dropped 1.7% entries from factor data: 1.7% in forward returns computation and 0.0% in binning phase (set max_loss=0 to see potentially suppressed Exceptions).
max_loss is 35.0%, not exceeded: OK!
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th></th>
<th>1D</th>
<th>5D</th>
<th>10D</th>
<th>factor</th>
<th>factor_quantile</th>
</tr>
<tr>
<th>date</th>
<th>asset</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="5" valign="top">2013-01-02 00:00:00+00:00</th>
<th>Equity(693 [AZO])</th>
<td>-0.002074</td>
<td>-0.020455</td>
<td>-0.021436</td>
<td>-24.872762</td>
<td>1</td>
</tr>
<tr>
<th>Equity(755 [BC])</th>
<td>0.012223</td>
<td>0.010613</td>
<td>0.005783</td>
<td>7.408589</td>
<td>1</td>
</tr>
<tr>
<th>Equity(1023 [BOH])</th>
<td>0.011838</td>
<td>0.014244</td>
<td>0.018623</td>
<td>7.656026</td>
<td>1</td>
</tr>
<tr>
<th>Equity(1343 [CCK])</th>
<td>0.005535</td>
<td>-0.008698</td>
<td>0.000264</td>
<td>-0.328515</td>
<td>1</td>
</tr>
<tr>
<th>Equity(1402 [CEF])</th>
<td>-0.015986</td>
<td>-0.012268</td>
<td>0.009294</td>
<td>99.881505</td>
<td>2</td>
</tr>
</tbody>
</table>
</div>
```python
# ファクターの四分位別に、平均を算出
mean_return_by_q, std_err_by_q = al.performance.mean_return_by_quantile(factor_data)
# 四分位と保有ごとに、平均を描画
al.plotting.plot_quantile_returns_bar(
mean_return_by_q.apply(
al.utils.rate_of_return,
axis=0,
args=('1D',)
)
);
```

```python
import pandas as pd
# ファクターでウェイト付けしたロングショートのポートフォリオを収益を算出
ls_factor_returns = al.performance.factor_returns(factor_data)
# 5日間保有した場合の累積収益を描画
al.plotting.plot_cumulative_returns(ls_factor_returns['5D'], '5D', freq=pd.tseries.offsets.BDay());
```

```python
```
結果はともかくとして、動かすことはできました。