# 案例（三）简单金融数据分析

## 项目三：计算一年内以MACD指标买入卖出信号进行股票交易而产生的收益

# -*- coding: utf-8 -*-
"""
Created on Sun Sept 20 9:04:59 2020

@author: mly
"""
import numpy as np
import datetime

start = datetime.datetime(2018, 6, 1)
end = datetime.datetime.today()
stock_name = '601318.ss'
df = web.DataReader(stock_name, 'yahoo', start, end)

def df_EMA(prices, N):
ema = []
k = len(prices)
if k > 0:
for i in range(k):
if i == 0:
ema.append(prices[i])
else:
ema.append((2 * prices[i] + (N - 1) * ema[i - 1]) / (N + 1))
return (ema)

def df_MACD(df, short=12, long=26, M=9):
if len(fast) > 0:  # & len(slow)>0:
df['Fast'] = np.round(np.array(fast), 2)
df['Slow'] = np.round(np.array(slow), 2)
df['DIF'] = df['Fast'] - df['Slow']
df['DEA'] = np.round(np.array(df_EMA(df['DIF'].values, M)), 2)
df['MACD'] = 2 * (df['DIF'] - df['DEA'])
df['tim'] = df['Close'] / df['Open']
return (df)
else:
print('no data,no MACD')

times0 = 1
marker = 0
df_MACD(df, 12, 26, 9)
for i in df.itertuples(index=True, name='df'):
if getattr(i, 'DIF') > 0 and marker == 0:
times0 = times0 * getattr(i, 'tim')
marker = 1
elif getattr(i, 'DIF') > 0 and marker == 1:
times0 = times0 * getattr(i, 'tim')
marker = 1
elif getattr(i, 'DIF') == 0 and marker == 1:
times0 = times0 * getattr(i, 'tim')
marker = 0
elif getattr(i, 'DIF') < 0 and marker == 1:
times0 = times0 * getattr(i, 'tim')
marker = 0
else:
continue

print(times0)


• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏 1
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

修羽凌云

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-25
07-24 2722

02-27 1285
11-13 2156
04-03 105
12-05 1068
04-12 2322
01-05 72
10-31 1万+
02-25 422