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

相关文章

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

我在移动打工的日志

客户:给我搞一下录音 我:不会。不在服务范围。 客户:是不想吧 我:笑嘻嘻(气笑) 客户:小姑娘明明会,却欺负老人 我:笑嘻嘻 客户:那我交话费 我:手机号 客户:给我搞录音 我:不会。不懂。没搞过。 客户:那我交话费 我:手机号。这是电信的啊!!我这是中国移动!! 客户:我不管,我要充话费,充话费是你们的 我:可是这是移动!!中国移动!! 客户:我这是手机号 我:那又如何,这是移动!你是电信!!

用Unity2D制作一个人物,实现移动、跳起、人物静止和动起来时的动画:中(人物移动、跳起、静止动作)

上回我们学到创建一个地形和一个人物,今天我们实现一下人物实现移动和跳起,依次点击,我们准备创建一个C#文件 创建好我们点击进去,就会跳转到我们的Vision Studio,然后输入这些代码 using UnityEngine;public class Move : MonoBehaviour // 定义一个名为Move的类,继承自MonoBehaviour{private Rigidbo

简单的角色响应鼠标而移动

actor类 //处理移动距离,核心是找到角色坐标在世界坐标的向量的投影(x,y,z),然后在世界坐标中合成,此CC是在地面行走,所以Y轴投影始终置为0; using UnityEngine; using System.Collections; public class actor : MonoBehaviour { public float speed=0.1f; CharacterCo

缓存策略使用总结

缓存是提高系统性能的最简单方法之一。相对而言,数据库(or NoSQL数据库)的速度比较慢,而速度却又是致胜的关键。 如果使用得当,缓存可以减少相应时间、减少数据库负载以及节省成本。本文罗列了几种缓存策略,选择正确的一种会有很大的不同。缓存策略取决于数据和数据访问模式。换句话说,数据是如何写和读的。例如: 系统是写多读少的吗?(例如基于时间的日志)数据是否是只写入一次并被读取多次?(例如用户配

Flink任务重启策略

概述 Flink支持不同的重启策略,以在故障发生时控制作业如何重启集群在启动时会伴随一个默认的重启策略,在没有定义具体重启策略时会使用该默认策略。如果在工作提交时指定了一个重启策略,该策略会覆盖集群的默认策略默认的重启策略可以通过 Flink 的配置文件 flink-conf.yaml 指定。配置参数 restart-strategy 定义了哪个策略被使用。常用的重启策略: 固定间隔 (Fixe

Java后端微服务架构下的API限流策略:Guava RateLimiter

Java后端微服务架构下的API限流策略:Guava RateLimiter 大家好,我是微赚淘客返利系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿! 在微服务架构中,API限流是保护服务不受过度使用和拒绝服务攻击的重要手段。Guava RateLimiter是Google开源的Java库中的一个组件,提供了简单易用的限流功能。 API限流概述 API限流通过控制请求的速率来防止

未雨绸缪:环保专包二级资质续期工程师招聘时间策略

对于环保企业而言,在二级资质续期前启动工程师招聘的时间规划至关重要。考虑到招聘流程的复杂性、企业内部需求的变化以及政策标准的更新,建议环保企业在二级资质续期前至少提前6至12个月启动工程师招聘工作。这个时间规划可以细化为以下几个阶段: 一、前期准备阶段(提前6-12个月) 政策与标准研究: 深入研究国家和地方关于环保二级资质续期的最新政策、法规和标准,了解对工程师的具体要求。评估政策变化可

物联网之流水LED灯、正常流水灯、反复流水灯、移动流水灯

MENU 硬件电路设计软件程序设计正常流水LED灯反复流水LED灯移动流水LED灯 硬件电路设计 材料名称数量直插式LED1kΩ电阻杜邦线(跳线)若干面包板1 每一个LED的正极与开发板一个GPIO引脚相连,并串联一个电阻,负极接GND。 当然也可以选择只使用一个电阻。 软件程序设计 正常流水LED灯 因为要用到多个GPIO引脚,所以最好把所有的GPI

12C 新特性,MOVE DATAFILE 在线移动 包括system, 附带改名 NID ,cdb_data_files视图坏了

ALTER DATABASE MOVE DATAFILE  可以改名 可以move file,全部一个命令。 resue 可以重用,keep好像不生效!!! system照移动不误-------- SQL> select file_name, status, online_status from dba_data_files where tablespace_name='SYSTEM'