使用python实现sma
什么是移动平均线(MAs)? (What are Moving Averages(MAs) ?)
A moving average, also called as rolling average or running average is a used to analyze the time-series data by calculating a series of averages of the different subsets of full dataset.
移动平均值(也称为滚动平均值或移动平均值)用于通过计算完整数据集不同子集的一系列平均值来分析时间序列数据。
Moving averages are the averages of a series of numeric values. They have a predefined length for the number of values to average and this set of values moves forward as more data is added with time. Given a series of numbers and a fixed subset size, the first element of the moving averages is obtained by taking the average of the initial fixed subset of the number series. Then to obtain subsequent moving averages the subset is ‘shift forward’ i.e. exclude the first element of the previous subset and add the element immediately after the previous subset to the new subset keeping the length fixed . Since it involves taking the average of the dataset over time, it is also called a moving mean (MM) or rolling mean.
移动平均值是一系列数值的平均值。 它们具有平均长度的预定义长度,可以平均多个值,并且随着时间的推移添加更多数据,这组值会向前移动。 给定一系列数字和固定的子集大小,移动平均值的第一个元素是通过获取数字系列的初始固定子集的平均值而获得的。 然后,为了获得后续移动平均值,子集将“向前移动”,即排除先前子集的第一个元素,并在先前子集之后立即将元素添加到新子集,并保持长度固定。 由于它涉及对一段时间内的数据集取平均值,因此也称为移动平均值(MM)或滚动平均值。
In the technical analysis of financial data, moving averages(MAs) are among the most widely used trend following indicators that demonstrate the direction of the market’s trend.
在财务数据的技术分析中,移动平均线(MA)是显示市场趋势方向的指标之后使用最广泛的趋势之一。
移动平均线的类型 (Types of Moving Averages)
There are many different types of moving averages depending on how the averages are computed. In any time-series data analysis, the most commonly used types of moving averages are —
取决于如何计算平均值,移动平均值有很多不同的类型。 在任何时间序列数据分析中,最常用的移动平均值类型是-
- Simple Moving Average(SMA) 简单移动平均线(SMA)
- Weighted Moving Average(WMA) 加权移动平均线(WMA)
- Exponential Moving Average (EMA or EWMA) 指数移动平均线(EMA或EWMA)
The only noteworthy difference between the various moving averages is the weight assigned to data points in the moving average period. Simple moving averages apply equal weight to all data points. Exponential and weighted averages apply more weight to recent data points.
各个移动平均值之间唯一值得注意的差异是在移动平均值时段内分配给数据点的权重。 简单的移动平均线将相等的权重应用于所有数据点。 指数和加权平均值对最近的数据点施加更大的权重。
Among these, Simple Moving Averages(SMAs) and Exponential Moving Averages(EMAs) are arguably the most popular technical analysis tool used by the analysts and traders. In this article, we’ll focus primarily on the strategies involving SMAs and EMAs.
其中,简单移动平均线(SMA)和指数移动平均线(EMA)可以说是分析师和交易员使用的最受欢迎的技术分析工具。 在本文中,我们将主要关注涉及SMA和EMA的策略。
简单移动平均线(SMA) (Simple Moving Average (SMA))
Simple Moving Average is one of the core technical indicators used by traders and investors for the technical analysis of a stock, index or securities. Simple moving average is calculated by adding the the closing price of last n number of days and then diving by the number of days(time-period). Before we dive deep, let’s first understand the math behind simple averages.
简单移动平均线是交易者和投资者用于对股票,指数或证券进行技术分析的核心技术指标之一。 简单移动平均线是通过将最近n天的收盘价加起来然后乘以天数(时间段)得出的。 在深入探讨之前,让我们首先了解简单平均值背后的数学原理。
We have studied how to compute average in school and even in our daily life we often come across the notion of it. Let’s say you are watching a game of cricket and a batsman comes for batting. By looking at his previous 5 match scores— 60, 75, 55, 80, 50; you can expect him to score roughly around 60–70 runs in today’s match.
我们研究了如何在学校中计算平均值,甚至在我们的日常生活中,我们经常碰到它的概念。 假设您正在看板球比赛,而一名击球手则是击球。 通过查看他之前的5个比赛得分-60、75、55、80、50; 您可以期望他在今天的比赛中得分大约60-70。
By calculating the average of a batsman from his last 5 matches, you were able to make a crude prediction that he’ll score this much runs today. Although, this is a rough estimation and doesn’t guarantee that he’ll score exactly same runs, but still the chances are high. Likewise, SMA helps in predicting the future trend and determine whether an asset price will continue or reverse a bull or bear trend. The SMA is usually used to identify trend direction, but it can also be used to generate potential trading signals.
通过计算最近5场比赛中的击球手的平均值,您可以做出粗略的预测,即他今天将取得如此高的得分。 虽然,这是一个粗略的估计,并不能保证他会得分完全相同,但是机会仍然很高。 同样,SMA有助于预测未来趋势,并确定资产价格将继续还是逆转牛市或熊市趋势。 SMA通常用于识别趋势方向,但也可以用于生成潜在的交易信号。
Calculating Simple moving averages — The formula for calculating the SMA is straightforward:
计算简单移动平均线 — 用于计算SMA的公式很简单:
The simple moving average = (sum of the an asset price over the past n periods) / (number of periods)
简单移动平均数=(过去n个时期内资产价格之和)/(时期数)
All elements in the SMA have the same weightage. If the moving average period is 5, then each element in the SMA will have a 20% (1/5) weightage in the SMA.
SMA中的所有元素都具有相同的权重。 如果移动平均周期为5,则SMA中的每个元素在SMA中的权重为20%(1/5)。
‘n periods’ can be anything. You can have a 200 day simple moving average, a 100 hour simple moving average, a 5 day simple moving average, a 26 week simple moving average, etc.
“ n个期间”可以是任何东西。 您可以有200天的简单移动平均线,100小时的简单移动平均线,5天的简单移动平均线,26周的简单移动平均线等。
Now that we have accustomed ourselves with the basics, let’s jump to the Python implementation.