# 练习02.MACD

Author: 3028165668, Date: 2021-06-09 10:02:31
Tags:

```
'''

'''

# 程序主函数
def onTick():
account = exchange.GetAccount()
bar = exchange.GetRecords(PERIOD_D1)  	# 获取K线数组
if len(bar) < 100:		# 如果K线数组长度太小，就直接返回跳过
return
macd = TA.MACD(bar, 5, 50, 15)  		# 计算MACD值
dif = macd[0][-2]  					# 获取DIF的值
dea = macd[1][-2]  					# 获取DEA的值
dim = macd[2][-2]                   # 获取距离
last_close = bar[-1]['Close']		# 获取最新价格（卖价）
flag = 0                            # 用来标记是否进行了交易
if dim < -100 and dif < dea and account["Stocks"] > 0.00000001 and dif > 0 and dea > 0:
Log('卖出0.3仓位')
exchange.Sell(last_close - 1, account["Stocks"] / last_close * 0.5) 	    # 卖出0.3仓位
flag = 1

elif dim > 100 and dif > dea and  account["Balance"] > 10 and dif < 0 and dea < 0:
Log('买入0.3仓位')
exchange.Buy(last_close, account["Balance"] / last_close * 0.5)  		# 买入0.3仓位
flag = 1

if flag == 1:
Log("Balance : " , account["Balance"], "Coin : " , account["Stocks"])
Sleep(1000 * 60 * 60 * 24 * 3)        # 如果进行了交易，等待3天以后再看

def main():
while True:
onTick()
Sleep(1000 * 60 * 60 * 24)    # 一天查看一次

```

More

louzfei 反向操作就稳定盈利了

Daiyoyo 妙啊>_<