SlideShare a Scribd company logo
1 of 11
Download to read offline
이동평균선
황영재
이동평균법
• 단기추세가 장기추세를 상향 돌파하면 매
수, 하향하면 매도하는 방법
• 골든크로스 : 단기추세가 장기추세를 상향돌파
• 데드크로스 : 단기추세가 장기추세를 하향돌파
이동평균을 구하는 방법
• 단순 이동 평균(Simple Moving Average)
• 가중 이동 평균(Weighted Moving Average)
• 지수 이동 평균(Exponential Moving Average)
참조 : http://pepic.tistory.com/255
이동평균 비교
• 단순 이동 평균(Simple Moving Average)
– 개념이나 구하는 방법은 간단하지만, 현재값의 반영이 느림
• 가중 이동 평균(Weighted Moving Average)
– 현재의 값에 비중을 더 두어 평균을 구함
• 지수 이동 평균(Exponential Moving Average)
– 오래된 값도 버리지 않고, 현재의 이동평균에 반영
단순 이동평균 with python
• 단순 이동 평균(Simple Moving Average)
def sma(dataset, window):
weight = np.repeat(1,window) #가중치
a = (np.convolve(dataset,weight,'valid') / weight.sum()).tolist() # 합성곱을 이용하여 평균구함
[a.insert(0,np.nan) for i in range(window-1)] # 빠진 갯수만큼 nan 채우기
return a
dataset = np.arange(0, 11, 1.0)
print sma(dataset,3)
pprint.pprint(sma(np.random.rand(10),5))
[nan, nan, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
[nan,
nan,
nan,
nan,
0.6846600727316047,
0.7979787565611873,
0.8567790817007008,
0.7467720330518219,
0.7187438722607699,
0.7300641782033382]
가중 이동평균 with python
• 가중 이동 평균(Weighted Moving Average)
def wma(dataset, window):
weight = np.linspace(1, 0, window+1)[:-1] #가중치
a = (np.convolve(dataset,weight,'valid') / weight.sum()).tolist() # 합성곱을 이용하여 평균구함
[a.insert(0,np.nan) for i in range(window-1)] # 빠진 갯수만큼 nan 채우기
return a
dataset = np.arange(0, 11, 1.0)
pprint.pprint([round(val, 4) for val in wma(dataset, 3)])
window = 3
print np.linspace(1, 0, window+1)[:-1]
[nan,
nan,
1.3333,
2.3333,
3.3333,
4.3333,
5.3333,
6.3333,
7.3333,
8.3333,
9.3333]
[ 1. 0.66666667 0.33333333]
지수 이동평균 with python
• 지수 이동 평균(Exponential Moving Average)
def ema(dataset, window):
weight = float(2) / (window + 1) # 지수이동평균식 가중치
result = []
for index, value in enumerate(dataset):
if not result: # dataset[0]은 계산하지 않음
result.append(value)
else:
# dataset[1] 이상부터 지수 평균계산 시작, 계산은 dataset[1]부터 시작하지만, result[window - 1] 부터 window가
적용된 값
result.append((value * weight) + (result[-1] * (1-weight))) #지수이동평균식을 Python Code로
return result
dataset = np.arange(0, 11, 1.0)
pprint.pprint(ema(dataset, 3))
[0.0,
0.5,
1.25,
2.125,
3.0625,
4.03125,
5.015625,
6.0078125,
7.00390625,
8.001953125,
9.0009765625]
http://dev.3cm.co.kr/edu/movingaverage
np.repeat
반복되는 array 생성
np.repeat(0,5)
Code Return
array([0, 0, 0, 0, 0])
np.repeat(1,5) array([1, 1, 1, 1, 1])
np.repeat([10,20],3) array([10, 10, 10, 20, 20, 20])
np.repeat([[10,20],[30,40]],2) array([10, 10, 20, 20, 30, 30, 40, 40]) #shape : (8, )
np.repeat([[10,20],[30,40]],2,0) array([[10, 20], [10, 20], [30, 40], [30, 40]]) #shape : (4, 2)
np.repeat([[10,20],[30,40]],2,1) array([[10, 10, 20, 20], [30, 30, 40, 40]]) #shape : (2, 4)
# np.shape 입력핚 array가 몇행, 몇열로 구성되었는지 확인
ori_array = [[10, 20], [30, 40]]
rep_none_array = np.repeat(ori_array, 2)
rep_0_array = np.repeat(ori_array, 2,0)
rep_1_array = np.repeat(ori_array, 2,1)
print np.shape(ori_array)
print np.shape(rep_none_array)
print np.shape(rep_0_array)
print np.shape(rep_1_array)
(2, 2)
(8,)
(4, 2)
(2, 4)
np.convolve
합성곱
※ 합성곱(合成-, convolution, 콘벌루션)은 하나의 함수와 또 다른 함수를 반전 이동핚 값을 곱핚 다음, 구간에 대해 적분하
여 새로운 함수를 구하는 수학 연산자이다.
np.convolve([10],range(1,6+1)).tolist()
Code Return
[10, 20, 30, 40, 50, 60]
np.convolve([10, 100],range(1,6+1)).tolist() [10, 120, 230, 340, 450, 560, 600]
np.convolve([10, 100, 10000],range(1,6+1)).tolist() [10, 120, 10230, 20340, 30450, 40560, 50600, 60000]
𝑥1 𝑦1
𝑥1 𝑦2
...
𝑥1 𝑦 𝑛
[1] * [n]
𝑥1 𝑦1
𝑥1 𝑦2 + 𝑥2 𝑦1
𝑥1 𝑦3 + 𝑥2 𝑦2
...
𝑥1 𝑦 𝑛 + 𝑥2 𝑦 𝑛−1
𝑥2 𝑦 𝑛
[2] * [n]
𝑥1 𝑦1
𝑥1 𝑦2 + 𝑥2 𝑦1
𝑥1 𝑦3 + 𝑥2 𝑦2 + 𝑥3 𝑦1
𝑥1 𝑦4 + 𝑥2 𝑦3 + 𝑥3 𝑦2
...
𝑥1 𝑦 𝑛 + 𝑥2 𝑦 𝑛−1 + 𝑥3 𝑦 𝑛−2
𝑥2 𝑦 𝑛 + 𝑥3 𝑦 𝑛−1
𝑥3 𝑦 𝑛
[3] * [n]
full same valid
http://dev.3cm.co.kr/edu/macd
https://github.com/hwangyoungjae/cchart/blob/master/web/views_edu.py

More Related Content

Similar to 20170410 황영재 moving_average

자료구조5보고서
자료구조5보고서자료구조5보고서
자료구조5보고서
KimChangHoen
 
Project#5 최단거리 찾기 D0 Hwp
Project#5 최단거리 찾기 D0 HwpProject#5 최단거리 찾기 D0 Hwp
Project#5 최단거리 찾기 D0 Hwp
Kimjeongmoo
 

Similar to 20170410 황영재 moving_average (20)

Adversarial Attack in Neural Machine Translation
Adversarial Attack in Neural Machine TranslationAdversarial Attack in Neural Machine Translation
Adversarial Attack in Neural Machine Translation
 
Rdatamining
Rdatamining Rdatamining
Rdatamining
 
R_datamining
R_dataminingR_datamining
R_datamining
 
ESM Mid term Review
ESM Mid term ReviewESM Mid term Review
ESM Mid term Review
 
확통 회귀분석
확통 회귀분석확통 회귀분석
확통 회귀분석
 
[홍대 머신러닝 스터디 - 핸즈온 머신러닝] 2장. 머신러닝 프로젝트 처음부터 끝까지
[홍대 머신러닝 스터디 - 핸즈온 머신러닝] 2장. 머신러닝 프로젝트 처음부터 끝까지[홍대 머신러닝 스터디 - 핸즈온 머신러닝] 2장. 머신러닝 프로젝트 처음부터 끝까지
[홍대 머신러닝 스터디 - 핸즈온 머신러닝] 2장. 머신러닝 프로젝트 처음부터 끝까지
 
Guided policy search
Guided policy searchGuided policy search
Guided policy search
 
2.linear regression and logistic regression
2.linear regression and logistic regression2.linear regression and logistic regression
2.linear regression and logistic regression
 
R 시작해보기
R 시작해보기R 시작해보기
R 시작해보기
 
3.neural networks
3.neural networks3.neural networks
3.neural networks
 
자료구조5보고서
자료구조5보고서자료구조5보고서
자료구조5보고서
 
내가 이해하는 SVM(왜, 어떻게를 중심으로)
내가 이해하는 SVM(왜, 어떻게를 중심으로)내가 이해하는 SVM(왜, 어떻게를 중심으로)
내가 이해하는 SVM(왜, 어떻게를 중심으로)
 
DL from scratch(6)
DL from scratch(6)DL from scratch(6)
DL from scratch(6)
 
텐서플로우로 배우는 딥러닝
텐서플로우로 배우는 딥러닝텐서플로우로 배우는 딥러닝
텐서플로우로 배우는 딥러닝
 
Project#5 최단거리 찾기 D0 Hwp
Project#5 최단거리 찾기 D0 HwpProject#5 최단거리 찾기 D0 Hwp
Project#5 최단거리 찾기 D0 Hwp
 
강화학습 기초부터 DQN까지 (Reinforcement Learning from Basics to DQN)
강화학습 기초부터 DQN까지 (Reinforcement Learning from Basics to DQN)강화학습 기초부터 DQN까지 (Reinforcement Learning from Basics to DQN)
강화학습 기초부터 DQN까지 (Reinforcement Learning from Basics to DQN)
 
Deep Learning from scratch 4장 : neural network learning
Deep Learning from scratch 4장 : neural network learningDeep Learning from scratch 4장 : neural network learning
Deep Learning from scratch 4장 : neural network learning
 
Learning method
Learning methodLearning method
Learning method
 
Control as Inference.pptx
Control as Inference.pptxControl as Inference.pptx
Control as Inference.pptx
 
세그먼트 트리 느리게 업데이트하기 - Sogang ICPC Team, 2020 Winter
세그먼트 트리 느리게 업데이트하기 - Sogang ICPC Team, 2020 Winter세그먼트 트리 느리게 업데이트하기 - Sogang ICPC Team, 2020 Winter
세그먼트 트리 느리게 업데이트하기 - Sogang ICPC Team, 2020 Winter
 

20170410 황영재 moving_average

  • 2. 이동평균법 • 단기추세가 장기추세를 상향 돌파하면 매 수, 하향하면 매도하는 방법 • 골든크로스 : 단기추세가 장기추세를 상향돌파 • 데드크로스 : 단기추세가 장기추세를 하향돌파
  • 3. 이동평균을 구하는 방법 • 단순 이동 평균(Simple Moving Average) • 가중 이동 평균(Weighted Moving Average) • 지수 이동 평균(Exponential Moving Average) 참조 : http://pepic.tistory.com/255
  • 4. 이동평균 비교 • 단순 이동 평균(Simple Moving Average) – 개념이나 구하는 방법은 간단하지만, 현재값의 반영이 느림 • 가중 이동 평균(Weighted Moving Average) – 현재의 값에 비중을 더 두어 평균을 구함 • 지수 이동 평균(Exponential Moving Average) – 오래된 값도 버리지 않고, 현재의 이동평균에 반영
  • 5. 단순 이동평균 with python • 단순 이동 평균(Simple Moving Average) def sma(dataset, window): weight = np.repeat(1,window) #가중치 a = (np.convolve(dataset,weight,'valid') / weight.sum()).tolist() # 합성곱을 이용하여 평균구함 [a.insert(0,np.nan) for i in range(window-1)] # 빠진 갯수만큼 nan 채우기 return a dataset = np.arange(0, 11, 1.0) print sma(dataset,3) pprint.pprint(sma(np.random.rand(10),5)) [nan, nan, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] [nan, nan, nan, nan, 0.6846600727316047, 0.7979787565611873, 0.8567790817007008, 0.7467720330518219, 0.7187438722607699, 0.7300641782033382]
  • 6. 가중 이동평균 with python • 가중 이동 평균(Weighted Moving Average) def wma(dataset, window): weight = np.linspace(1, 0, window+1)[:-1] #가중치 a = (np.convolve(dataset,weight,'valid') / weight.sum()).tolist() # 합성곱을 이용하여 평균구함 [a.insert(0,np.nan) for i in range(window-1)] # 빠진 갯수만큼 nan 채우기 return a dataset = np.arange(0, 11, 1.0) pprint.pprint([round(val, 4) for val in wma(dataset, 3)]) window = 3 print np.linspace(1, 0, window+1)[:-1] [nan, nan, 1.3333, 2.3333, 3.3333, 4.3333, 5.3333, 6.3333, 7.3333, 8.3333, 9.3333] [ 1. 0.66666667 0.33333333]
  • 7. 지수 이동평균 with python • 지수 이동 평균(Exponential Moving Average) def ema(dataset, window): weight = float(2) / (window + 1) # 지수이동평균식 가중치 result = [] for index, value in enumerate(dataset): if not result: # dataset[0]은 계산하지 않음 result.append(value) else: # dataset[1] 이상부터 지수 평균계산 시작, 계산은 dataset[1]부터 시작하지만, result[window - 1] 부터 window가 적용된 값 result.append((value * weight) + (result[-1] * (1-weight))) #지수이동평균식을 Python Code로 return result dataset = np.arange(0, 11, 1.0) pprint.pprint(ema(dataset, 3)) [0.0, 0.5, 1.25, 2.125, 3.0625, 4.03125, 5.015625, 6.0078125, 7.00390625, 8.001953125, 9.0009765625]
  • 9. np.repeat 반복되는 array 생성 np.repeat(0,5) Code Return array([0, 0, 0, 0, 0]) np.repeat(1,5) array([1, 1, 1, 1, 1]) np.repeat([10,20],3) array([10, 10, 10, 20, 20, 20]) np.repeat([[10,20],[30,40]],2) array([10, 10, 20, 20, 30, 30, 40, 40]) #shape : (8, ) np.repeat([[10,20],[30,40]],2,0) array([[10, 20], [10, 20], [30, 40], [30, 40]]) #shape : (4, 2) np.repeat([[10,20],[30,40]],2,1) array([[10, 10, 20, 20], [30, 30, 40, 40]]) #shape : (2, 4) # np.shape 입력핚 array가 몇행, 몇열로 구성되었는지 확인 ori_array = [[10, 20], [30, 40]] rep_none_array = np.repeat(ori_array, 2) rep_0_array = np.repeat(ori_array, 2,0) rep_1_array = np.repeat(ori_array, 2,1) print np.shape(ori_array) print np.shape(rep_none_array) print np.shape(rep_0_array) print np.shape(rep_1_array) (2, 2) (8,) (4, 2) (2, 4)
  • 10. np.convolve 합성곱 ※ 합성곱(合成-, convolution, 콘벌루션)은 하나의 함수와 또 다른 함수를 반전 이동핚 값을 곱핚 다음, 구간에 대해 적분하 여 새로운 함수를 구하는 수학 연산자이다. np.convolve([10],range(1,6+1)).tolist() Code Return [10, 20, 30, 40, 50, 60] np.convolve([10, 100],range(1,6+1)).tolist() [10, 120, 230, 340, 450, 560, 600] np.convolve([10, 100, 10000],range(1,6+1)).tolist() [10, 120, 10230, 20340, 30450, 40560, 50600, 60000] 𝑥1 𝑦1 𝑥1 𝑦2 ... 𝑥1 𝑦 𝑛 [1] * [n] 𝑥1 𝑦1 𝑥1 𝑦2 + 𝑥2 𝑦1 𝑥1 𝑦3 + 𝑥2 𝑦2 ... 𝑥1 𝑦 𝑛 + 𝑥2 𝑦 𝑛−1 𝑥2 𝑦 𝑛 [2] * [n] 𝑥1 𝑦1 𝑥1 𝑦2 + 𝑥2 𝑦1 𝑥1 𝑦3 + 𝑥2 𝑦2 + 𝑥3 𝑦1 𝑥1 𝑦4 + 𝑥2 𝑦3 + 𝑥3 𝑦2 ... 𝑥1 𝑦 𝑛 + 𝑥2 𝑦 𝑛−1 + 𝑥3 𝑦 𝑛−2 𝑥2 𝑦 𝑛 + 𝑥3 𝑦 𝑛−1 𝑥3 𝑦 𝑛 [3] * [n] full same valid