PyalgoTrade 交易(五)

news/2024/7/17 17:44:54

我们继续采取简单的策略,这次模拟实际交易。这个想法很简单:

  • 如果调整后的收盘价高于SMA(15),我们将进入多头仓位(我们下单买入市价)。
  • 如果调整后的收盘价低于SMA(15),我们退出多头头寸(我们出售)
from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed from pyalgotrade.technical import ma class MyStrategy(strategy.BacktestingStrategy): def __init__(self, feed, instrument, smaPeriod): super(MyStrategy, self).__init__(feed, 1000) self.__position = None self.__instrument = instrument # We'll use adjusted close values instead of regular close values. self.setUseAdjustedValues(True) self.__sma = ma.SMA(feed[instrument].getPriceDataSeries(), smaPeriod) def onEnterOk(self, position): execInfo = position.getEntryOrder().getExecutionInfo() self.info("BUY at $%.2f" % (execInfo.getPrice())) def onEnterCanceled(self, position): self.__position = None def onExitOk(self, position): execInfo = position.getExitOrder().getExecutionInfo() self.info("SELL at $%.2f" % (execInfo.getPrice())) self.__position = None def onExitCanceled(self, position): # If the exit was canceled, re-submit it. self.__position.exitMarket() def onBars(self, bars): # Wait for enough bars to be available to calculate a SMA. if self.__sma[-1] is None: return bar = bars[self.__instrument] # If a position was not opened, check if we should enter a long position. if self.__position is None: if bar.getPrice() > self.__sma[-1]: # Enter a buy market order for 10 shares. The order is good till canceled. self.__position = self.enterLong(self.__instrument, 10, True) # Check if we have to exit the position. elif bar.getPrice() < self.__sma[-1] and not self.__position.exitActive(): self.__position.exitMarket() def run_strategy(smaPeriod): # Load the yahoo feed from the CSV file feed = yahoofeed.Feed() feed.addBarsFromCSV("orcl", "orcl-2000.csv") # Evaluate the strategy with the feed. myStrategy = MyStrategy(feed, "orcl", smaPeriod) myStrategy.run() print "Final portfolio value: $%.2f" % myStrategy.getBroker().getEquity() run_strategy(15)

运行策略后看到如下结果

2000-01-26 00:00:00 strategy [INFO] BUY at $27.26
2000-01-28 00:00:00 strategy [INFO] SELL at $24.74
2000-02-03 00:00:00 strategy [INFO] BUY at $26.60
2000-02-22 00:00:00 strategy [INFO] SELL at $28.40
2000-02-23 00:00:00 strategy [INFO] BUY at $28.91
2000-03-31 00:00:00 strategy [INFO] SELL at $38.51
2000-04-07 00:00:00 strategy [INFO] BUY at $40.19
2000-04-12 00:00:00 strategy [INFO] SELL at $37.44
2000-04-19 00:00:00 strategy [INFO] BUY at $37.76
2000-04-20 00:00:00 strategy [INFO] SELL at $35.45
2000-04-28 00:00:00 strategy [INFO] BUY at $37.70
2000-05-05 00:00:00 strategy [INFO] SELL at $35.54
2000-05-08 00:00:00 strategy [INFO] BUY at $36.17
2000-05-09 00:00:00 strategy [INFO] SELL at $35.39
2000-05-16 00:00:00 strategy [INFO] BUY at $37.28
2000-05-19 00:00:00 strategy [INFO] SELL at $34.58
2000-05-31 00:00:00 strategy [INFO] BUY at $35.18
2000-06-23 00:00:00 strategy [INFO] SELL at $38.81
2000-06-27 00:00:00 strategy [INFO] BUY at $39.56
2000-06-28 00:00:00 strategy [INFO] SELL at $39.42
2000-06-29 00:00:00 strategy [INFO] BUY at $39.41
2000-06-30 00:00:00 strategy [INFO] SELL at $38.60
2000-07-03 00:00:00 strategy [INFO] BUY at $38.96
2000-07-05 00:00:00 strategy [INFO] SELL at $36.89
2000-07-21 00:00:00 strategy [INFO] BUY at $37.19
2000-07-24 00:00:00 strategy [INFO] SELL at $37.04
2000-07-26 00:00:00 strategy [INFO] BUY at $35.93
2000-07-28 00:00:00 strategy [INFO] SELL at $36.08
2000-08-01 00:00:00 strategy [INFO] BUY at $36.11
2000-08-02 00:00:00 strategy [INFO] SELL at $35.06
2000-08-04 00:00:00 strategy [INFO] BUY at $37.61
2000-09-11 00:00:00 strategy [INFO] SELL at $41.34
2000-09-29 00:00:00 strategy [INFO] BUY at $39.07
2000-10-02 00:00:00 strategy [INFO] SELL at $38.30
2000-10-20 00:00:00 strategy [INFO] BUY at $34.71
2000-10-31 00:00:00 strategy [INFO] SELL at $31.34
2000-11-20 00:00:00 strategy [INFO] BUY at $23.35
2000-11-21 00:00:00 strategy [INFO] SELL at $23.83
2000-12-01 00:00:00 strategy [INFO] BUY at $25.33
2000-12-21 00:00:00 strategy [INFO] SELL at $26.72
2000-12-22 00:00:00 strategy [INFO] BUY at $29.17
Final portfolio value: $979.44

如果调整sma的测试周期,讲得到不一样的结果

for i in range(10, 30): run_strategy(i)

我们发现sma(20)的结果最好

Final portfolio value: $1075.38



作者:readilen
链接:http://www.jianshu.com/p/3ac363f931d3
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

转载于:https://www.cnblogs.com/zhanglong8681/p/7569226.html


http://www.niftyadmin.cn/n/1194939.html

相关文章

ASP.net(照片列表详细功能CRUD演示)

ASP .net(照片列表详细功能演示&#xff09; 大家好,今天我们需要讲解的内容就是把上篇文章当中提到的照片列表的很多功能细化去做。 那么之间我们两篇文章的目的就是要让大家深刻体会get&#xff0c;post的使用场景极其作用。像一般处理程序的使用&#xff0c;隐藏域的使用&am…

php修改数据库里面的数据,如何用php修改数据库中的数据

公告&#xff1a; 为响应国家净网行动&#xff0c;部分内容已经删除&#xff0c;感谢读者理解。话题&#xff1a;如何用php修改数据库中的数据回答&#xff1a;详细介绍1 创建userinfo_update.php&#xff0c;用于&#xff0c;先显示&#xff0c;在修改&#xff1a; 先通过GET获…

关于log4j.properties例子:DailyRollingFileAppender

package com.v512.log4j; import org.apache.log4j.Logger; public class HelloLog4J { // 构造记录器,形参是记录器所在的类,表示要在该类做日志 private static Logger logger Logger.getLogger(HelloLog4J.class); /** * param args */ …

marquee 循环显示变量_JavaScript中的for循环

JavaScript 语言中的 for 循环用于多次执行代码块&#xff0c;它是 JavaScript 中最常用的一个循环工具&#xff0c;还可用于数组的遍历循环等。我们为什么要使用 for 循环呢&#xff1f;打个比方&#xff0c;例如我们想要控制台输出1到1000之间的所有数字&#xff0c;如果单写…

linux grep oracle,linux文本处理工具之grep

定义:grep(global search regular expression and print out the line):搜索匹配模式的行,并将其打印出来.pattern:匹配模式,可以文本字符,更多时候是元字符法语:grep [选项] [模式] [文件]常用选项:-i:忽略大小写--color:高亮显示所匹配的字符串-o:只打印显示所匹配的字符串-v…

sqlite字段类型

存储类(Storage Classes) 如前文所述&#xff0c;SQLite在处理数据类型时与其它的数据库不同。区别在于它所支持的类型以及这些类型是如何存储、比较、强化(enforc)和指派(assign)。下面各节介绍SQLite处理数据类型的独特方法和它与域完整性的关系。 对于数据类型&#xff0c;S…

什么是Entitlement

Entitlement&#xff08;权限&#xff09;&#xff0c;可以想象成App里用于描述该App可以调用哪些服务的字符串。苹果的操作系统&#xff08;mac os或者iOS)会通过检查这个串&#xff0c;决定这个应用是否可以调用相关功能。比如iCloud权限&#xff0c;推送服务&#xff0c;健康…

mongo-mapreduce测试(2)——列转行与finalize函数

mongo-mapreduce测试&#xff08;1&#xff09;——count/sum/where条件 mongo-mapreduce测试&#xff08;2&#xff09;——列转行与finalize函数 mongo-mapreduce测试&#xff08;3&#xff09;——group by having mongo-mapreduce测试&#xff08;4&#xff09;——avg mon…