生活随笔
收集整理的這篇文章主要介紹了
量化投资之定投,无脑却收益还不错,记得周三来
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目標:
本系列開始重點研究量化,逐步改善模型,改善策略,然后評估各個策略的優劣。 本文是第一篇,也是最容易最無腦投資的一篇,每周三定投,收益還不錯。
內容:
如果工作太忙沒法投資,閑錢無處放,不妨看看這這種方式。
學金融的都知道最簡單靠譜的投資方式:定投。
是否真有效?假期封在家,做了下實驗。
假設每周,通過支付寶線下買基金定投1000,從15年元旦開始。就投創業板吧,虧錢了就當給科技做貢獻。要知道15年可是股災年,19年也是,經歷了2次股災還能有收益嗎?一起來看看到今天2020-02-07,收益如何?
圖1為指數行情,證明數據沒有錯。
圖2為定投VS年化5%無風險收益的對比圖。可見,定投確實收益超過存定期。
圖3為定投選星期幾呢?國外有個著名的“周四效應”,而在創業板中,最好的卻是周三!國外的月亮比一定在國內元。最差周一、周五。差值能達到20000元。
圖4為定投的純收益,虧損最多的是在19年初那次股災。
圖5為收益率曲線,也可以看出周三的收益率最好。
圖6為收益率,定投最多也能虧40%,讓人有點崩潰吧。
為啥定投很少有人堅持?2016-2020年4年都在虧錢,好多人的心理早已崩潰,心理承受能力早已塌陷。但曙光就在后面。
這種機械式定投顯然不是很好的辦法。但確實也有賺錢機會。
股市有風險,投資需謹慎。
改進措施:
最大回撤=82% - (-39%) = 120%。策略波動太大。 虧錢源自于高價位買太多,低價位買太少,所以機械的每次買一樣的金額,明顯不好,可以考慮增加彈性,高價位買少,低價位買多。這樣最大回撤能小很多。 下篇見。
代碼:
實驗數據此處下載:鏈接:https://pan.baidu.com/s/1yVThgukzc3iXFhjHMLVONA
提取碼:1234
"""
作者:luoji
日期:2022年02月06日
"""import pandas
as pd
import time
import matplotlib
.pyplot
as plt
import matplotlib
as mpl
from matplotlib
.ticker
import FuncFormatter
mpl
.rcParams
['font.sans-serif'] = ['KaiTi']
mpl
.rcParams
['font.serif'] = ['KaiTi']
mpl
.rcParams
['axes.unicode_minus'] = False
dateTOBegin
= '2015-01-01' data
= pd
.read_csv
('C:\\LuojiPythonProject\\AGuquant\\data\\399006.csv', encoding
='UTF-8', index_col
=0)
print(data
.head
(5))data
['trade_date'] = pd
.to_datetime
(data
['trade_date'], format='%Y%m%d')
print(data
['trade_date'])
data
= data
[data
['trade_date'] >= dateTOBegin
] data
= data
.set_index
('trade_date') print(data
.head
(5))
print(data
.index
)
print(data
.columns
) data
= data
.sort_values
(['trade_date'], ascending
=True)
print(data
.head
(5))figure
= plt
.figure
(1)
data
['close'].plot
(title
='創業板指數價格', xlabel
='交易日期', ylabel
='價格', legend
='收盤價', grid
=True)
print(data
.columns
)
data
['trade_date'] = data
.index
listWeekDay
= ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
data
['day_of_week'] = data
['trade_date'].dt
.dayofweek
data
['day_of_week'] = data
['day_of_week'].map(lambda x
: listWeekDay
[x
])
print(data
.head
(5))baseCapital
= 1000 for i
in range(5):principal
= 'principal_on_' + listWeekDay
[i
] data
[principal
] = 0 bugAmount
= 'bug_amount_on_' + listWeekDay
[i
] data
[bugAmount
] = 0 for i
in range(5):day
= listWeekDay
[i
] bugAmount
= 'bug_amount_on_' + listWeekDay
[i
] principal
= 'principal_on_' + listWeekDay
[i
] data
.loc
[data
['day_of_week'] == day
, principal
] = baseCapital data
.loc
[data
['day_of_week'] == day
, bugAmount
] = baseCapital
/ data
[data
['day_of_week'] == day
]['close']
print(data
.head
(10))
print(data
.columns
)for i
in range(5):accumulateAmount
= 'accumulate_amount_' + listWeekDay
[i
] bugAmount
= 'bug_amount_on_' + listWeekDay
[i
] data
[accumulateAmount
] = data
[bugAmount
].cumsum
() marketValue
= 'market_value_on_' + listWeekDay
[i
] data
[marketValue
] = data
['close'] * data
[accumulateAmount
] figure
= plt
.figure
(2)
for i
in range(5):marketValue
= 'market_value_on_' + listWeekDay
[i
] data
[marketValue
].plot
(title
='定投市值', xlabel
='交易日期', ylabel
='市值', legend
=marketValue
, grid
=True)
data
['no_risk_principal'] = 0
data
.loc
[data
['day_of_week'] == 'Monday', 'no_risk_principal'] = baseCapital
data
['no_risk_accumulate_principal'] = data
['no_risk_principal'].cumsum
()
no_risk_rate
= 0.05
no_risk_rate_daily
= no_risk_rate
/ 200
data
['no_risk_profit_everyday'] = data
['no_risk_accumulate_principal'] * no_risk_rate_daily
data
['no_risk_profit'] = data
['no_risk_profit_everyday'].cumsum
()
data
['no_risk_market_value'] = data
['no_risk_accumulate_principal'] + data
['no_risk_profit']
data
['no_risk_market_value'].plot
(xlabel
='交易日期', ylabel
='市值', legend
='5%_no_risk_market_value', grid
=True)
data
['no_risk_accumulate_principal'].plot
(xlabel
='交易日期', ylabel
='市值', legend
='無風險本金', grid
=True)
for i
in range(5):principal
= 'principal_on_' + listWeekDay
[i
] accumulate_principal
= 'accumulate_principal_' + listWeekDay
[i
] data
[accumulate_principal
] = data
[principal
].cumsum
() marketValue
= 'market_value_on_' + listWeekDay
[i
] profit
= 'profit_on_' + listWeekDay
[i
] data
[profit
] = data
[marketValue
] - data
[accumulate_principal
] profit_rate
= 'profit_rate_on_' + listWeekDay
[i
] data
[profit_rate
] = data
[profit
] / data
[accumulate_principal
] figure
= plt
.figure
(3)
for i
in range(5):profit
= 'profit_on_' + listWeekDay
[i
]data
[profit
].plot
(title
='定投收益', xlabel
='交易日期', ylabel
='收益(元)', legend
=profit
, grid
=True)data
['no_risk_profit_rate'] = data
['no_risk_profit'] / data
['no_risk_accumulate_principal']
data
['no_risk_profit'].plot
(title
='定投收益', xlabel
='交易日期', ylabel
='收益(元)', legend
='5% no_risk_profit', grid
=True)figure
= plt
.figure
(4)
for i
in range(5):profit_rate
= 'profit_rate_on_' + listWeekDay
[i
]data
[profit_rate
].plot
(title
='定投收益率', xlabel
='交易日期', ylabel
='收益率', legend
=profit_rate
, grid
=True)data
['no_risk_profit_rate'].plot
(xlabel
='交易日期', ylabel
='收益率', legend
='5% no_risk_profit_rate', grid
=True)def to_percent(temp
, position
): return '%1.0f' % (100 * temp
) + '%'plt
.gca
().yaxis
.set_major_formatter
(FuncFormatter
(to_percent
))
plt
.show
()
產出:
如果資金有計劃近期不會使用,可以考慮研究一下如何投資。
修改一下代碼,如果我們2018-01-01年開始定投,會怎么樣?
收益更好,能達到50%。
總結
以上是生活随笔為你收集整理的量化投资之定投,无脑却收益还不错,记得周三来的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。