python 股票技术指标
介绍: (Introduction:)
After making a machine learning algorithm that attempts to predict the future price of forex securities, I thought about trying to automate trading, using theories that originate from technical analysis.
在制作了一种能够预测外汇证券的未来价格的机器学习算法之后,我考虑使用技术分析的理论来尝试使交易自动化。
理论: (Theory:)
The main theory that I tried to recreate was the MA cross concept, basically gathering insights when two moving averages intersect. The two moving averages would be of various ranges. The strategy in question works because, it shows how the current trend is relative to past trends. If the current uptrend is higher than the long term average, it is likely that the price will tend to increase.
我尝试重新创建的主要理论是MA交叉概念,基本上是在两个移动均线相交时收集见解。 两个移动平均线将处于不同范围内。 该策略之所以有效,是因为它表明了当前趋势与过去趋势之间的关系。 如果当前的上升趋势高于长期平均水平,则价格可能会趋于上涨。
代码直通: (Code Run-through:)
Step 1| Dependencies:
步骤1 | 依存关系:
import requests
import numpy as np
from matplotlib import pyplot as plt
import datetimeAPI_KEY = 'XXXXX'
from_symbol = 'EUR'
to_symbol = 'USD'
These dependencies are necessary to make calculations and plot results. Fill in the API_KEY variable with your own api key from alpha vantage.
这些依赖性是进行计算和绘制结果所必需的。 用您自己的来自alpha vantage的api密钥填充API_KEY变量。
Step 2 | Creating the SMA:
步骤2 | 创建SMA:
def SMA(prices,value):
means = []
count = 0
while value+count <= len(prices):
pre_val = prices[count:value+count]
count +=1
means.append(np.mean(pre_val))
return means
The SMA function is in fact very simple, with just the closing price and the value of the SMA lines as parameters. It runs a loop through the prices, gets the current range and then finds the average. It returns a list of the averages which when plotted, is the SMA.
SMA函数实际上非常简单,仅以收盘价和SMA线的值作为参数。 它遍历价格循环,获取当前范围,然后找到平均值。 它返回均线列表,绘制时为SMA。
Step 3 | Calculating Intersections:
步骤3 | 计算交叉点:
def intersection(lst_1,lst_2):
intersections = []
insights = []
if len(lst_1) > len(lst_2):
settle = len(lst_2)
else:
settle = len(lst_1)
for i in range(settle-1):
if (lst_1[i+1] < lst_2[i+1]) != (lst_1[i] < lst_2[i]):
if ((lst_1[i+1] < lst_2[i+1]),(lst_1[i] < lst_2[i])) == (True,False):
insights.append('buy')
else:
insights.append('sell')
intersections.append(i)
return intersections,insights
The intersection takes in two simple moving averages as parameters. It took me a while to get the intersection to work as intended. Here is a list of things I tried:
相交采用两个简单的移动平均值作为参数。 我花了一些时间才能使交叉路口按预期工作。 这是我尝试过的事情的清单:
- Find terms that appeared in both lists and then find their index 查找出现在两个列表中的术语,然后找到其索引
This approach didn’t work because the SMA plotted is a “connect-the-dots” line of the values calculated, and therefore the point of intersection may not be a datapoint in the list of SMA values.
该方法行不通,因为绘制的SMA是计算出的值的“点连接”线,因此相交点可能不是SMA值列表中的数据点。
- Use simultaneous equations to calculate values that satisfy the equation 使用联立方程来计算满足方程的值
This approach didn’t work because the SMA cannot be defined by simple functions. The more complex a function, the more variables are necessary to define it, making it ineffective to find intersections via simultaneous equations.
这种方法行不通,因为无法通过简单的函数定义SMA。 函数越复杂,定义它所需要的变量就越多,这使得通过联立方程查找交点无效。
The approach that finally worked was if the order in which the SMA’s were stacked changed. If it did, it means that an intersection must have occurred before this change. This works well in may cases, but has a slight delay.
最终可行的方法是改变SMA堆叠的顺序。 如果是这样,则意味着必须在此更改之前发生交叉。 这在某些情况下效果很好,但会稍有延迟。
步骤4 | 模拟交易: (Step 4| Simulating Trades:)
def trading_simulator(trade_time,trade_value,close_price,sma_one,sma_two):
sma_1 = SMA(close_price,sma_one)
sma_2 = SMA(close_price,sma_two)
intersections,insights = intersection(sma_1,sma_2)
profit = 0
logs = []
try:
for i in range(len(insights)):
index = intersections[i]
if insights[i] == buy:
if index+trade_time < len(fx_data):
if fx_data[index][-1] < fx_data[index+trade_time][-1]:
profit += trade_value * 0.8
logs.append(trade_value*0.8)
elif fx_data[index][-1] > fx_data[index+trade_time][-1]:
profit -= trade_value
logs.append(-trade_value)
elif insights[i] == sell:
if index+trade_time <= len(fx_data):
if fx_data[index][-1] > fx_data[index+trade_time][-1]:
profit += trade_value * 0.8
logs.append(trade_value*0.8)
elif fx_data[index][-1] < fx_data[index+trade_time][-1]:
profit -= trade_value
logs.append(-trade_value)
profit = profit
except:
print(len(fx_data),index+trade_time)
return profit,logs
The trading simulator is very simple and is essentially accessing the data and comparing it to the insights the program makes. It returns a log of all transactions for troubleshooting and profit tracking, and a sum of the profits.
交易模拟器非常简单,本质上是访问数据并将其与程序的见解进行比较。 它返回所有用于故障排除和利润跟踪的交易的日志,以及利润的总和。
本实验: (The Experiment:)
With so many parameters, I wanted to see how to optimize the parameters to get the most amount of profits. I brute forced this with this code:
有这么多参数,我想看看如何优化参数以获得最大的利润。 我用以下代码强制执行此操作:
profits = []
log_of_logs = []
for minutes in range(1,10):
for sma1 in range(1,100):
for sma2 in range(1,100):
print(sma1,sma2,minutes)
clear_output()
profit,logs = trading_simulator(minutes,10,close_price,sma1,sma2)
log_of_logs.append(logs)
profits.append(profit)
The independent variables were the two SMAs of a range from 1–100 and how long the trade was open for.
自变量是两个SMA,范围从1到100,以及交易开放的时间。
结果: (Results:)
The results to the experiment was very interesting, here is a graph when plotting the profits:
实验结果非常有趣,下面是绘制利润图:
Can you reach any conclusions by looking at this graph?
通过查看该图,您能否得出任何结论?
Hint: By looking at the graph and the program, you can tell that each spike in the graph was the change in how long the trade was open for.
提示:通过查看图表和程序,您可以看出图表中的每个峰值都是开仓时间的变化。
结论: (Conclusions:)
The data shows that no matter the SMA value, longer trade times tend to have more stable results. Additionally, the narrowing down between each spike shows that the higher the two SMA values, the more stable the values are right?
数据显示,无论SMA值如何,交易时间越长,结果越稳定。 另外,每个尖峰之间的范围缩小表明两个SMA值越高,值越稳定?
Wrong! In fact all the conclusions in the previous paragraph are all inaccurate. Let me prove this step by step: Firstly, the discrepancies for the trade times stem from the fact that within a given amount of time, more 1 minute trades can be made than 9 minute trades. Secondly, there are more intersections for smaller values as compared to larger values for SMAs because it is exceeded averages of a closer time period happen more often as compared to averages of a longer time period.
错误! 实际上,上一段中的所有结论都是不准确的。 让我逐步证明这一点:首先,交易时间的差异源于这样的事实,即在给定的时间内,可以进行9分钟的交易,而不是9分钟的交易。 其次,与较大的SMAs值相比,较小的值有更多的交集,因为与较长的时间段的平均值相比,超出了更短时间段的平均值。
So are there any conclusions? Yes, but another measurement must be conceded to see past the fog that plague previous conclusions. This measurement is accuracy. Because accuracy is a percentage, no matter how many intersections and how man trades are made, one can effectively compare how suitable the parameters are, as compared to others.
有什么结论吗? 是的,但是必须承认另一种测量方法才能看清困扰先前结论的迷雾。 这种测量是准确的。 因为准确度是一个百分比,所以无论有多少个交叉点和如何进行人工交易,一个人都可以与其他参数相比有效地比较参数的适用性。
Just one line of code is necessary:
只需要一行代码:
accuracy = logs.count(trade_value*0.8)/len(logs)*100
The results were that for each set of SMA values there were many varying possibilities that gave 90%+ accuracies, for example, SMA values of 60 and 4, and a trade time of 3 minutes.
结果是,对于每组SMA值,存在多种多样的可能性,可提供90%以上的准确度,例如SMA值分别为60和4,交易时间为3分钟。
I hope you learnt something from my article. Thank you for reading this.
希望您从我的文章中学到一些东西。 谢谢您阅读此篇。
翻译自: https://medium.com/analytics-vidhya/using-technical-indicators-in-python-bfb1aaf6b6ce
python 股票技术指标