三行情书代码

If you want to consistently earn money with your investments, backtesting is one of the best ways to assess the effectiveness of your trading strategies — of course, assuming that you implement it properly. The idea is that you can experiment with the different parameters for your chosen strategies, and see which combinations give you the best returns for your investment.

如果您想通过投资来持续赚钱,回测是评估交易策略有效性的最佳方法之一-当然,前提是您正确实施了该策略。 这个想法是,您可以为所选策略尝试不同的参数,并查看哪种组合可以为您的投资带来最佳的回报。

However, this can easily start getting tedious as you would have to run hundreds or even thousands of parameter combinations manually.

但是,这很容易开始变得乏味,因为您必须手动运行数百甚至数千个参数组合。

To solve this problem, we can use fastquant to implement a technique called “grid search”, which basically allows you to run a backtest across each of the parameter combinations that you want to run for your strategy.

为了解决这个问题,我们可以使用fastquant实现一种称为“网格搜索”的技术,该技术基本上允许您对要为策略运行的每个参数组合进行回测。

For the rest of the article, I’ll be demonstrating how to apply automated grid search when backtesting your trading strategies.

对于本文的其余部分,我将演示在回测交易策略时如何应用自动网格搜索。

Note: If you’re not yet familiar with how to do basic backtesting with fastquant, you may want to check out my previous article on how to do this in 3 lines of code.

注意:如果您还不熟悉如何使用fastquant进行基本的回测,则可能需要查看我的前 一篇文章 ,其中包含3行代码。

Let’s start by installing fastquant via pip!

让我们开始通过pip安装fastquant!

# Run this on your terminalpip install fastquant# Alternatively, you can run this from jupyter this way!pip install fastquant

For example, let’s use Jollibee Food Corp. (JFC) stock from 2018–01–01 to 2019–12–31 as our sample data.

例如,让我们使用2018年1月1日至2019年12月31日之间的Jollibee Food Corp.(JFC)库存作为样本数据。

Note: get_stock_data supports all of the companies accessible from Yahoo Finance and PSE.

注意: get_stock_data 支持可从 Yahoo Finance 和PSE 访问的所有公司

from fastquant import get_stock_datadf = get_stock_data("JFC", "2018-01-01", "2019-12-31")df.head()#         dt  close# 2018-01-03  255.4# 2018-01-04  255.0# 2018-01-05  255.0# 2018-01-08  256.0# 2018-01-09  255.8

Say you want to backtest a simple moving average crossover (SMAC) strategy on JFC stock with a fast period of 15 days, and slow period of 40 days.

假设您要以15天的快速周期和40天的慢周期对JFC股票进行简单的移动平均交叉(SMAC)策略进行回测。

from fastquant import backtestbacktest("smac", df, fast_period=15, slow_period=40)# Starting Portfolio Value: 100000.00# Final Portfolio Value: 68742.36

You’ll notice that your final portfolio value went down by a lot (~31%), and by looking at the chart below, you’ll see that this is because the strategy was still recommending trades (green and red arrows) when the stock was on an overall down trend.

您会注意到您的最终投资组合价值下降了很多(〜31%),并且通过查看下表,您会发现这是因为该策略仍在建议交易时(绿色和红色箭头)。库存总体呈下降趋势。

You’ll see trades happening (green and red arrows) even when the stock is going down overall
即使股票整体下跌,您也会看到交易发生(绿色和红色箭头)

So now, we’ll want to see if there’s a combination of “slow period” and “fast period” that will lead to an SMAC strategy that knows not to make trades during the overall downtrend.

因此,现在,我们要查看是否存在“慢速期”和“快速期”的组合,这将导致SMAC策略在整个下降趋势中都不进行交易。

One way to do this is to try out a large number of possible fast period and slow period combinations. For this example, we’ll set the slow period to take values within the range 20 to 241 (skipping every 5), while the fast period can take values within the range 1 to 20.

一种方法是尝试大量可能的快速周期和慢速周期组合。 在此示例中,我们将慢速周期设置为采用20到241范围内的值(每5个跳跃一次),而快周期可以采用1到20范围内的值。

This means fast period can take 20 possible values, while slow period can take 45 possible values. Together, that translates to 900 (20 x 45) possible combinations! Imagine doing this one by one right.

这意味着快周期可以采用20个可能的值,而慢周期可以采用45个可能的值。 总之,这意味着900(20 x 45)种可能的组合! 想象一下,一步一步地做到这一点。

Thankfully, we can easily automate this by using fastquant’s built-in grid search capabilities by using iterators (e.g. list , range ) as inputs, rather than single numbers.

值得庆幸的是,我们可以使用fastquant的内置网格搜索功能(通过将迭代器(例如listrange ))作为输入而不是单个数字来轻松实现此自动化。

results = backtest("smac", df, fast_period=range(1, 21), slow_period=range(20, 241, 5))results[["fast_period", "slow_period", "final_value"]].head()# fast_period slow_period final_value#0          3         105   107042.02#1          8         205   103774.66#2         11         170   103774.66#3          8         200   103774.66#4          9          95   103388.12

Note that “backtest” returns a pandas dataframe (“results”) which contains the results for each iteration on each row. Each column of the dataframe corresponds to either a parameter that was used (e.g. fast_period), or a performance metric for the strategy (e.g. final_value).

请注意,“ backtest”返回一个熊猫数据框(“结果”),其中包含每一行每次迭代的结果。 数据帧的每一列都对应于所使用的参数(例如fast_period ),或者对应于该策略的性能指标(例如final_value )。

Conveniently, the rows are sorted in descending order based on each iteration’s portfolio return, and so the “best” parameter combinations are the ones at the top of the dataframe.

方便地,这些行基于每次迭代的投资组合收益按降序排序,因此“最佳”参数组合是数据框顶部的参数组合。

In this case, it seems that the top parameter combination is: fast_period=3 , and slow_period=105 , with a portfolio return of 7,042.02. This indicates that we may want to use this parameter combination when utilizing the SMAC strategy on JFC stock.

在这种情况下,似乎最上面的参数组合是:fast_period = 3和slow_period = 105,投资组合回报为7,042.02。 这表明在对JFC股票使用SMAC策略时,我们可能要使用此参数组合。

Note: It is very possible that this parameter combination is still overfitting to the chosen period. See the caveats and safeguards of backtesting in this previous article.

注意:此参数组合很可能仍会过度适合所选时间段。 请参阅上 一篇文章中 的回测注意事项和保护措施

Now, let’s run our optimal strategy!

现在,让我们运行最佳策略!

from fastquant import backtestbacktest("smac", df, fast_period=3, slow_period=105)# Starting Portfolio Value: 100000.00# Final Portfolio Value: 107042.02

As shown in the plot below, the parameter combination above would have recommended us to sell our stock right before the overall downtrend — see the red downward arrow right before the drop. This explains how it was able to avoid the losses that we saw during our initial implementation of the SMAC strategy at the beginning of this article.

如下图所示,上面的参数组合建议我们在总体下降趋势之前就卖出股票-看到下跌之前的红色向下箭头。 这说明了如何避免本文开头介绍的SMAC战略的最初实施过程中出现的损失。

With the results dataframe, you can even visualize the effectiveness of each parameter combination with a heatmap like below!

使用结果数据框,您甚至可以通过如下所示的热图直观地看到每个参数组合的有效性!

technical tutorial to see how to plot this heat map技术教程 ,了解如何绘制此热图

Congratulations! By now, you should be familiar with how to automatically optimize your trading strategy with fastquant’s built in grid search capabilities. Next time you catch yourself plugging in parameter combinations manually to backtest, do consider using this automated approach!

恭喜你! 到目前为止,您应该已经熟悉如何使用fastquant的内置网格搜索功能自动优化交易策略。 下一次你发现自己堵漏参数组合手动backtest ,就可以考虑使用这种自动化的方法!

It’s also important to note that this is a capability that was adapted from the backtrader package, which fastquant is built on top of. The difference is that fastquant allows you to do this in much fewer lines of code (as few as 3), so that you can spend more time testing your hypotheses, rather than setting up code.

还必须注意,这是从backtrader包改编而成的功能 ,而fastquant是在backtrader包的基础上构建的。 不同之处在于, fastquant允许您用更少的代码行(最多3行)来执行此操作,这样您就可以花更多的时间测试假设,而无需设置代码。

To see a more detailed version of the examples above with full code, including a demo on how the code would have looked without the “built-in grid search”, do check out this technical tutorial by Jerome de Leon.

要查看带有完整代码的上述示例的更详细版本,包括有关没有“内置网格搜索”的情况下代码外观的演示,请查阅Jerome de Leon撰写的本技术教程 。

If you have any questions about this material, please feel free to comment below, or message me on Twitter, or Linkedin.

如果您对此材料有任何疑问,请在下面发表评论,或在Twitter或Linkedin上给我发消息。

Lastly, in case you want to contribute to the package, or learn more about how others are using fastquant, do join our community Slack workspace, where we have bi-weekly meet ups with fellow quants!

最后,如果您想为该软件包做出贡献,或者想了解更多有关其他人如何使用fastquant的信息,请加入我们的社区Slack工作区 ,在这里我们每两周与其他量子成员见面!

翻译自: https://towardsdatascience.com/backtest-with-grid-search-using-only-3-lines-of-code-on-fastquant-551615fdaf69

三行情书代码


http://www.taodudu.cc/news/show-863520.html

相关文章:

  • 词嵌入 网络嵌入_词嵌入简介
  • 如何成为数据科学家_成为数据科学家的5大理由
  • 大脑比机器智能_机器大脑的第一步
  • 嵌入式和非嵌入式_我如何向非技术同事解释词嵌入
  • ai与虚拟现实_将AI推向现实世界
  • bert 无标记文本 调优_使用BERT准确标记主观问答内容
  • 机器学习线性回归学习心得_机器学习中的线性回归
  • 安全警报 该站点安全证书_深度学习如何通过实时犯罪警报确保您的安全
  • 现代分层、聚集聚类算法_分层聚类:聚集性和分裂性-解释
  • 特斯拉自动驾驶使用的技术_使用自回归预测特斯拉股价
  • 熊猫分发_实用熊猫指南
  • 救命代码_救命! 如何选择功能?
  • 回归模型评估_评估回归模型的方法
  • gan学到的是什么_GAN推动生物学研究
  • 揭秘机器学习
  • 投影仪投影粉色_DecisionTreeRegressor —停止用于将来的投影!
  • 机器学习中的随机过程_机器学习过程
  • ci/cd heroku_在Heroku上部署Dash或Flask Web应用程序。 简易CI / CD。
  • 图像纹理合成_EnhanceNet:通过自动纹理合成实现单图像超分辨率
  • 变压器耦合和电容耦合_超越变压器和抱抱面的分类
  • 梯度下降法_梯度下降
  • 学习机器学习的项目_辅助项目在机器学习中的重要性
  • 计算机视觉知识基础_我见你:计算机视觉基础知识
  • 配对交易方法_COVID下的自适应配对交易,一种强化学习方法
  • 设计数据密集型应用程序_设计数据密集型应用程序书评
  • pca 主成分分析_超越普通PCA:非线性主成分分析
  • 全局变量和局部变量命名规则_变量范围和LEGB规则
  • dask 使用_在Google Cloud上使用Dask进行可扩展的机器学习
  • 计算机视觉课_计算机视觉教程—第4课
  • 用camelot读取表格_如何使用Camelot从PDF提取表格

三行情书代码_用三行代码优化您的交易策略相关推荐

  1. python三行情书代码_“三行情书”——给你三行代码的爱恋~

    /**********************************************************/ do { a++; b++; }while(a &b);//我和你原本 ...

  2. python三行情书_献礼教师节|你有三行情书,我有三行代码

    原标题:献礼教师节|你有三行情书,我有三行代码 感 谢 恩 师 三尺讲台上,您指点迷津 文山书海畔,您赠我舟楫 所有我迷茫的时刻,都有你 所有我疲惫的时刻,都有你 谁说我们程序员不懂浪漫 你有三行情书 ...

  3. 教师节:你有三行情书,我有三行代码!

    那些年 老师们苦口婆心劝你好好学编程 你还记得么? 刚刚步入职场 嗷嗷待哺的青涩小鲜肉程序员 又会对老肉片程序员们怎样表达爱意呢? 跟随小爽一起来看看鸭! 作为一名程序猿 在学习前端开发的路上遇到过的 ...

  4. tushare数据存入mysql代码_下载股票的历史日交易数据并存入数据库——基于tushare...

    tushare是一个非常神奇的Python模块包,基于新浪的API,可提供并不限于股票的历史数据. 数据库选用的是sqlite3,单文件,轻量化,不需要配置. 以下是完整代码,且使用的是多线程的方式. ...

  5. mt4 python神经网络_用Python写MT4自动交易策略来炒外汇

    #property version "1.00"#property strict // 调用ZERO-MQ库: MQL-ZMQ from https://github.com/di ...

  6. python做mt4交易_用Python写MT4自动交易策略来炒外汇

    #property version "1.00"#property strict // 调用ZERO-MQ库: MQL-ZMQ from https://github.com/di ...

  7. cta策略 有哪些_简单介绍什么是CTA交易策略

    CTA策略(Commodity Trading Advisor Strategy)称为商品交易顾问策略,也称作管理期货.商品交易顾问对商品等投资标的走势做出预判,通过期货期权等衍生品在投资中进行做多. ...

  8. Java三行情书_函数式编程思维在三行代码情书中的应用

    函数式编程概述 如今主流的编程语言,函数式编程范式或多或少都融入其中成了"标配",或者说主流语言都在进行函数式方面的扩充,这是一个大趋势.以Java为例,随着 Lambda块 和 ...

  9. python三行情书_程序员的三行情书!

    找个程序员就嫁了吧 程序员,格子衫.不浪漫.直男.人傻钱多,甚至某沦落到女生有种"在花花世界玩够了最后想要嫁给我们程序员的地步......". 我想说,女孩们找个程序员就嫁了吧,为 ...

最新文章

  1. 创建Joomla菜单
  2. mysql远程连接问题
  3. 《智源社区周刊:预训练模型》第3期:智源x清华开源万亿AI模型基石FastMoE、英国机构发起世界最大图灵测试...
  4. Make sure the device specification refers to a valid device
  5. java paint方法哪个周期调用_关于一段java程序的小问题,paint函数是什么时候执行的,没有调用啊...
  6. Visual Studio 2008 Windows Server 2008 预发布-20日在南宁
  7. Dockerfile构建容器镜像 - 运维笔记
  8. 冒泡排序 和 归并排序
  9. Python 错误和异常小结[转]
  10. linux操作系统 抢占式,Linux操作系统内核抢占补丁的基本原理(2)
  11. 自动推荐图表、智能分析,这个分析工具有点酷!
  12. 【快速入眠】高效睡眠 - 把失眠踩在脚下
  13. 最全解析如何正确学习JavaScript指南,必看!
  14. 如何运用大数据舆情监测分析软件监测分析舆情的方法技巧
  15. 怎么用Wireshark抓包QQ的到对方的IP地址
  16. 暑期训练第四次团队赛
  17. 用C++写一个班级通讯录管理软件
  18. python实现小说分割器
  19. tensorflow中gpu和cpu切换
  20. cgb2109-day02

热门文章

  1. 第六次课作业(质量管理、项目人力资源管理)
  2. Svn正确的使用方法
  3. $\mathbb{R}^n$中点集概念梳理
  4. 有关sed命令的用法
  5. 分析一下shell(转)
  6. list python 转tensor_Tensorflow模型量化4 --pb转tflite(uint8量化)小结
  7. apk闪退_你家的电视盒子总是闪退?别砸,问题都在这了!
  8. 八皇后非递归算法c语言,要求;编写实现八皇后问题的递归解法或非递归解法,对于任意给定的一? 爱问知识人...
  9. 量化投资之定投,无脑却收益还不错,记得周三来
  10. TensorFlow2快速模型构建及tensorboard初体验