等级: 新手上路
- 注册:
- 2022-6-15
- 曾用名:
|
from PythonApi import *
import time
import os
import csv
import numpy
import talib as ta
# 在这个方法中编写任何的初始化逻辑。context对象将会在你的算法策略的任何方法之间做传递。--(必须实现)
def init(context):
context.s1 = context.run_info.base_book_id
context.ema_short = 12
context.ema_long = 26
context.dea = 9
# before_trading此函数会在每天基准合约的策略交易开始前被调用,当天只会被调用一次。--(选择实现)
def before_trading(context):
pass
# 你选择的品种的数据更新将会触发此段逻辑,例如日或分钟历史数据切片或者是实时数据切片更新。--(必须实现)
def handle_bar(context):
close = history_bars(context.s1, context.ema_long+1, 'self', 'close',True)
if len(close) < context.ema_long+1 :
return
DIFF = ta.EMA(close,context.ema_short) - ta.EMA(close,context.ema_long)
DEA = ta.EMA(DIFF,context.dea)
MACD = 2*(DIFF-DEA)
if DIFF[-1] > DEA[-1] and DIFF[-2] < DEA[-2]:
buy_open(context.s1,"market",volume = 100,serial_id = 1) and buy_close(context.s1,"market",volume = 100)
if DIFF[-1] < DEA[-1] and DIFF[-2] > DEA[-2]:
sell_close(context.s1,"market", volume = 100,serial_id = 2) and sell_open(context.s1,"market", volume = 100)
# after_trading函数会在每天交易结束后被调用,当天只会被调用一次。 --(选择实现)
def after_trading(context):
pass
|
|