1-4移动均线交叉策略3

2024-06-12 14:48
文章标签 策略 移动 交叉 均线

本文主要是介绍1-4移动均线交叉策略3,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

第一阶段、一个简单策略入门量化投资

1-4移动均线交叉策略3

上一文1-3移动均线交叉策略2中,我们得到的结果是令人失望的。但我们的探索还要继续。
我们知道,使用投资组合的方式进行分散投资是降低风险的好办法。尽管移动均线交叉策略的表现并不理想,我们还是在此策略基础上进行修改,添加采用投资组合进行投资的代码,重新进行回测。
修改后的代码,你只需提前设置你想要购买股票的公司代码列表,例如:

# the list of listed companies that we are concerned about
listed_company_list = ["AAPL","MSFT","GOOG","FB","TWTR","NFLX","AMZN","SNY","NTDOY","IBM","HPQ"]

假设初始资金仍为100万,在使用上面给出的投资组合的情况下,资产的变化情况如下图所示:
这里写图片描述
这时,我们策略的收益率为93.5%
平均年化收益率为9.138%
显然,使用投资组合后,收益进一步减少了,但是我们也清楚其中的积极意义,这样的策略分摊了风险。我们都知道,风险越大,收益越高的现象是普遍存在的,如何权衡呢?
于是我们看到,同样是均线交叉策略,使用和不使用投资组合两种情况,在回测后判断策略优劣时就已经针对风险与收益的权衡出现了问题。
因此,如何更合理的在回测时评价策略的优劣是一个需要探索的有意义工作,未完待续…


完整代码

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetimeimport stockdata_preProcess as preProcess##### the first step: get the listed companies's stock data that we concerned about# the time interval of the data we want to get(from start to end)
start = datetime.datetime(2010, 1, 1)
end = datetime.date.today()# the list of listed companies that we are concerned about
listed_company_list = ["AAPL","MSFT","GOOG","FB","TWTR","NFLX","AMZN","SNY","NTDOY","IBM","HPQ"]
# *trouble*: I can't get "YHOO" 's data, and I have't find# download data (we do not need to repeat this work)
#preProcess.downloadAndSaveData(listed_company_list, start, end)# use moving average crossover strategy to build the trading signal dataframe
#   stocks: the data, e.g: [("AAPL", apple_adjust_data),("MSFT", microsoft_adjust_data),("GOOG", google_adjust_data)]
#   fast: the span of short-term moving average
#   slow: the span of long-term moving average
def ma_crossover_orders(stocks, fast, slow):fast_str = str(fast) + 'd'slow_str = str(slow) + 'd'ma_diff_str = fast_str + '-' + slow_strtrades = pd.DataFrame({"Price": [], "Regime": [], "Signal": []})for s in stocks:s[1][fast_str] = np.round(s[1]["Close"].rolling(window = fast, center = False).mean(), 2)s[1][slow_str] = np.round(s[1]["Close"].rolling(window = slow, center = False).mean(), 2)s[1][ma_diff_str] = s[1][fast_str] - s[1][slow_str]s[1]["Regime"] = np.where(s[1][ma_diff_str] > 0, 1, 0)s[1]["Regime"] = np.where(s[1][ma_diff_str] < 0, -1, s[1]["Regime"])regime_orig = s[1].ix[-1, "Regime"]s[1].ix[-1, "Regime"] = 0s[1]["Signal"] = np.sign(s[1]["Regime"] - s[1]["Regime"].shift(1))s[1].ix[-1, "Regime"] = regime_origsignals = pd.concat([pd.DataFrame({"Price": s[1].loc[s[1]["Signal"] == 1, "Close"],"Regime": s[1].loc[s[1]["Signal"] == 1, "Regime"],"Signal": "Buy"}),pd.DataFrame({"Price": s[1].loc[s[1]["Signal"] == -1, "Close"],"Regime": s[1].loc[s[1]["Signal"] == -1, "Regime"],"Signal": "Sell"}),])signals.index = pd.MultiIndex.from_product([signals.index, [s[0]]], names = ["Date", "Symbol"])trades = trades.append(signals)trades.sort_index(inplace = True)trades.index = pd.MultiIndex.from_tuples(trades.index, names = ["Date", "Symbol"])return trades# do the backtest
#   signals: the dataframe recording the trading signal
#   cash: the initial cash flow
#   port_value: the largest proportion of single transaction to total assets
#   batch: the smallest unit of the number of shares traded
def backtest(signals, cash, port_value = .1, batch = 100):SYMBOL = 1portfolio = dict()    # denote: all the stock asset allocationport_prices = dict()  # denote: all the stock price correspond to the asset allocationresults = pd.DataFrame({"Start Cash": [],"End Cash": [],"Portfolio Value": [],"Type": [],"Shares": [],"Share Price": [],"Trade Value": [],"Profit per Share": [],"Total Profit": []})for index, row in signals.iterrows():# index[SYMBOL] denote the listed company's name, e.g APPLshares = portfolio.setdefault(index[SYMBOL], 0)trade_val = 0batches = 0# step 1 : sell current stock(if we hold current stock)# if shares>0, means we already hold the stock of the company# so it is a sell signal here, we sell all the shares held nowcash_change = row["Price"] * sharesportfolio[index[SYMBOL]] = 0old_price = port_prices.setdefault(index[SYMBOL], row["Price"]) # get the price when we buy the stock before# step 2 : compute portfolio's value( the value after sell current stock)portfolio_val = 0for key, val in portfolio.items():portfolio_val += val * port_prices[key]# step 3 : buy current stock( if it is a buy singnal here )if row["Signal"] == "Buy" and row["Regime"] == 1:batches = np.floor((portfolio_val + cash) * port_value) // np.ceil(batch * row["Price"])trade_val = batches * batch * row["Price"]cash_change -= trade_valportfolio[index[SYMBOL]] = batches * batchport_prices[index[SYMBOL]] = row["Price"]old_price = row["Price"]elif row["Signal"] == "Sell" and row["Regime"] == -1:passpprofit = row["Price"] - old_priceresults = results.append(pd.DataFrame({"Start Cash": cash,"End Cash": cash + cash_change,"Portfolio Value": cash + cash_change + portfolio_val + trade_val,"Type": row["Signal"],"Shares": batch * batches,"Share Price": row["Price"],"Trade Value": abs(cash_change),"Profit per Share": pprofit,"Total Profit": batches * batch * pprofit}, index = [index]))cash += cash_changeresults.sort_index(inplace = True)results.index = pd.MultiIndex.from_tuples(results.index, names = ["Date", "Symbol"])return results##### get the data we save in .csv file and then return the repaired data to the user
DataSetList = preProcess.repairAndGetData(listed_company_list)##### use moving average crossover strategy to build the trading signal dataframe
# build the data format requied by the function ma_crossover_orders
# ( combine the stock name and the corresponding data )
stock_NameDataTuple_List = []
for i in range(len(listed_company_list)):cur_stock = DataSetList[i];cur_company = listed_company_list[i]stock_NameDataTuple_List.append((cur_company,cur_stock))signals = ma_crossover_orders(stock_NameDataTuple_List, fast = 20, slow = 50)
# these codes are the same as below: (use Ctrl+/ to batch annotation code)
# apple = DataSetList[0]
# microsoft = DataSetList[1]
# google = DataSetList[2]
# facebook = DataSetList[3]
# twitter = DataSetList[4]
# netflix = DataSetList[5]
# amazon = DataSetList[6]
# sony = DataSetList[7]
# nintendo = DataSetList[8]
# ibm = DataSetList[9]
# hp = DataSetList[10]
# signals = ma_crossover_orders([("AAPL", preProcess.ohlc_adjust(apple)),
#                               ("MSFT",  preProcess.ohlc_adjust(microsoft)),
#                               ("GOOG",  preProcess.ohlc_adjust(google)),
#                               ("FB",    preProcess.ohlc_adjust(facebook)),
#                               ("TWTR",  preProcess.ohlc_adjust(twitter)),
#                               ("NFLX",  preProcess.ohlc_adjust(netflix)),
#                               ("AMZN",  preProcess.ohlc_adjust(amazon)),
#                               ("SNY",   preProcess.ohlc_adjust(sony)),
#                               ("NTDOY", preProcess.ohlc_adjust(nintendo)),
#                               ("IBM",   preProcess.ohlc_adjust(ibm)),
#                               ("HPQ",   preProcess.ohlc_adjust(hp))],
#                             fast = 20, slow = 50)
print(signals)##### do the back test
bk = backtest(signals, 1000000)
print(bk)##### show the changes in portfolio value
#bk["Portfolio Value"].groupby(level = 0).apply(lambda x: x[-1]).plot()
portfolio_ValueList = bk["Portfolio Value"].groupby(level = 0).apply(lambda x: x[-1])
portfolio_ValueList.plot()
#print(portfolio_ValueList)##### compute annualized rate of return
initial_value = portfolio_ValueList[0]
deadline_value = portfolio_ValueList[-1]initial_date = portfolio_ValueList.index[0]
deadline_date = portfolio_ValueList.index[-1]
holding_interval = (deadline_date - initial_date).days / 365AnnualReturnRate = ( pow(deadline_value/initial_value,1/holding_interval) - 1 )*100
print('平均年化收益率: ',AnnualReturnRate,"%")
print((deadline_value-initial_value)/initial_value)plt.show()

这篇关于1-4移动均线交叉策略3的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1054519

相关文章

SpringBoot中4种数据水平分片策略

《SpringBoot中4种数据水平分片策略》数据水平分片作为一种水平扩展策略,通过将数据分散到多个物理节点上,有效解决了存储容量和性能瓶颈问题,下面小编就来和大家分享4种数据分片策略吧... 目录一、前言二、哈希分片2.1 原理2.2 SpringBoot实现2.3 优缺点分析2.4 适用场景三、范围分片

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir

利用Python实现时间序列动量策略

《利用Python实现时间序列动量策略》时间序列动量策略作为量化交易领域中最为持久且被深入研究的策略类型之一,其核心理念相对简明:对于显示上升趋势的资产建立多头头寸,对于呈现下降趋势的资产建立空头头寸... 目录引言传统策略面临的风险管理挑战波动率调整机制:实现风险标准化策略实施的技术细节波动率调整的战略价

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

双系统电脑中把Ubuntu装进外接移动固态硬盘的全过程

《双系统电脑中把Ubuntu装进外接移动固态硬盘的全过程》:本文主要介绍如何在Windows11系统中使用VMware17创建虚拟机,并在虚拟机中安装Ubuntu22.04桌面版或Ubunt... 目录一、首先win11中安装vmware17二、磁盘分区三、保存四、使用虚拟机进行系统安装五、遇见的错误和解决

使用FileChannel实现文件的复制和移动方式

《使用FileChannel实现文件的复制和移动方式》:本文主要介绍使用FileChannel实现文件的复制和移动方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录使用 FileChannel 实现文件复制代码解释使用 FileChannel 实现文件移动代码解释

Redis中6种缓存更新策略详解

《Redis中6种缓存更新策略详解》Redis作为一款高性能的内存数据库,已经成为缓存层的首选解决方案,然而,使用缓存时最大的挑战在于保证缓存数据与底层数据源的一致性,本文将介绍Redis中6种缓存更... 目录引言策略一:Cache-Aside(旁路缓存)策略工作原理代码示例优缺点分析适用场景策略二:Re

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

redis过期key的删除策略介绍

《redis过期key的删除策略介绍》:本文主要介绍redis过期key的删除策略,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录第一种策略:被动删除第二种策略:定期删除第三种策略:强制删除关于big key的清理UNLINK命令FLUSHALL/FLUSHDB命