Exemplo n.º 1
0
def 判断是否符合开仓(klines):
    二十日均线 = MA(klines, 20)
    四十日均线 = MA(klines, 40)
    if (二十日均线.iloc[-1].ma > 四十日均线.iloc[-1].ma) and (二十日均线.iloc[-2].ma <
                                                    四十日均线.iloc[-1].ma):
        return 1
    else:
        return 0
Exemplo n.º 2
0
def run_tianqin_code(port):
    try:
        api = TqApi(web_gui="127.0.0.1:" + port)
        klines = api.get_kline_serial("SHFE.au1910", 24 * 60 * 60)
        ma = MA(klines, 30)  # 使用tqsdk自带指标函数计算均线
        while True:
            api.wait_update()
    except Exception as e:
        api.close()
Exemplo n.º 3
0
def run_tianqin_code(port, queue):
    try:
        ins_url = "http://127.0.0.1:5000/t/md/symbols/2019-07-03.json"
        api = TqApi(_ins_url=ins_url, web_gui="127.0.0.1:" + port)
        queue.put("webready")
        klines = api.get_kline_serial("SHFE.au1910", 24 * 60 * 60)
        ma = MA(klines, 30)  # 使用tqsdk自带指标函数计算均线
        while True:
            api.wait_update()
    except Exception as e:
        api.close()
Exemplo n.º 4
0
    def test_wait_update_1(self):
        """
        若未连接天勤时修改了K线字段,则不应发送set_chart_data指令到服务器 (即不能调用api.py中_process_serial_extra_array()); 否则导致与服务器断连
        related issue: #146
        """
        # 预设服务器端响应
        dir_path = os.path.dirname(os.path.realpath(__file__))
        self.mock.run(os.path.join(dir_path, "log_file", "test_func_wait_update_1.script"))
        # 测试
        api = TqApi(_ins_url=self.ins_url, _td_url=self.td_url, _md_url=self.md_url)
        TqApi.RD = random.Random(4)
        klines = api.get_kline_serial("SHFE.cu1911", 10)
        klines["ma"] = MA(klines, 15)  # 测试语句
        deadline = time.time() + 10
        while api.wait_update(deadline=deadline):
            pass

        api.close()
Exemplo n.º 5
0
    def trend_line(self, kline_level="day", atr_n=14, MA_n=40):
        '''
        趋势线策略
        '''
        # profit_space_switch = {
        #     "day":lambda n: ATR(self.api.get_kline_serial(self.symbol, 60*60*24), n),
        #     "hour":lambda n: ATR(self.api.get_kline_serial(self.symbol, 60*60), n)
        # }
        # loss_space_switch = {
        #     "day":lambda n: ATR(self.api.get_kline_serial(self.symbol, 60*60), n),
        #     "hour":lambda n: ATR(self.api.get_kline_serial(self.symbol, 5*60), n)
        # }
        # loss_space = loss_space_switch[self.kline_level](atr_n)
        # profit_space = profit_space_switch[self.kline_level](atr_n)
        trend = 0  # 1 上升趋势,0 震荡, -1 下降趋势
        quote = self.api.get_quote(self.symbol)
        target = TargetPosTask(self.api, self.symbol)

        if kline_level == "day":
            loss_atr_period = 60 * 60
            profit_atr_period = 24 * 60 * 60
            trend_period = 24 * 60 * 60
            trend_kline = self.api.get_kline_serial(self.symbol, trend_period)
        elif kline_level == "week":
            loss_atr_period = 60 * 60
            profit_atr_period = 7 * 24 * 60 * 60
            trend_period = 24 * 60 * 60

        loss_space = ATR(
            self.api.get_kline_serial(self.symbol, loss_atr_period), atr_n)
        profit_space = ATR(
            self.api.get_kline_serial(self.symbol, profit_atr_period), atr_n)
        if trend_kline.iloc[-1].close > MA(
                trend_kline, 40).iloc[-1] + 2 * ATR(trend_kline, atr_n):
            trend = 1
        while True:
            pass
Exemplo n.º 6
0
#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = 'limin'

from tqsdk import TqApi
from tqsdk.ta import MA
'''
画图示例: 在附图中画指标线
注意:1 画图功能仅在天勤终端/天勤Vscode插件中生效,请在这两个平台中运行画图相关的代码
     2 画图示例中用到的数据不含有实际意义,请根据自己的实际策略情况进行修改
'''

api = TqApi()
klines = api.get_kline_serial("SHFE.au1912", 24 * 60 * 60)
ma = MA(klines, 30)  # 使用tqsdk自带指标函数计算均线

# 示例: 在附图中画一根绿色的ma指标线
klines["ma_B2"] = ma.ma
klines["ma_B2.board"] = "B2"  # 设置附图: 可以设置任意字符串,同一字符串表示同一副图
klines["ma_B2.color"] = 0xFF00FF00  # 设置为绿色

# 示例: 在另一个附图画一根比ma小4的宽度为4的紫色指标线
klines["ma_4"] = ma.ma - 4
klines["ma_4.board"] = "MA4"  # 设置为另一个附图
klines["ma_4.color"] = 0xFF9933CC  # 设置为紫色
klines["ma_4.width"] = 4  # 设置宽度为4,默认为1

api.close()  # 需要调用此函数将画图指令发送给天勤并关闭api
Exemplo n.º 7
0
def ma_caculate(klines):
    ma_slow = MA(klines, MA_SLOW).iloc[-1].ma
    ma_fast = MA(klines, MA_FAST).iloc[-1].ma
    return ma_slow, ma_fast
Exemplo n.º 8
0
'''
画图示例: 在同一附图中画K线、线段和方框
注意: 画图示例中用到的数据不含有实际意义,请根据自己的实际策略情况进行修改
'''

api = TqApi(web_gui=True)
klines = api.get_kline_serial("CFFEX.T2103", 10)
klines2 = api.get_kline_serial("CFFEX.T2012", 10)

# 示例1 : 在附图画出 T2012 的K线: 需要将open、high、log、close的数据都设置正确
klines["T2012.open"] = klines2["open"]
klines["T2012.high"] = klines2["high"]
klines["T2012.low"] = klines2["low"]
klines["T2012.close"] = klines2["close"]
klines["T2012.board"] = "B2111111"
ma = MA(klines, 30)
klines["ma_MAIN"] = ma.ma

# 示例2: 在附图中画线段(默认为红色)
api.draw_line(klines,
              -10,
              klines2.iloc[-20].low,
              -3,
              klines2.iloc[-3].high,
              id="my_line",
              board="B2111111",
              line_type="SEG",
              color=0xFFFF00FF,
              width=3)

# 示例3: 在附图K线上画黄色的方框: 需要设置画在附图时, 将board参数选择到对应的图板即可
Exemplo n.º 9
0
api = TqApi(TqSim())
logger = logging.getLogger("TQ")
logger.info("策略开始运行")

data_length = LONG + 2  # k线数据长度
# "duration_seconds=60"为一分钟线, 日线的duration_seconds参数为: 24*60*60
klines = api.get_kline_serial(SYMBOL,
                              duration_seconds=60,
                              data_length=data_length)
target_pos = TargetPosTask(api, SYMBOL)

while True:
    api.wait_update()

    if api.is_changing(klines[-1], "datetime"):  # 产生新k线:重新计算SMA
        df = klines.to_dataframe()
        short_avg = MA(df, SHORT)["ma"]  # 短周期
        long_avg = MA(df, LONG)["ma"]  # 长周期

        # 均线下穿,做空
        if long_avg.iloc[-2] < short_avg.iloc[-2] and long_avg.iloc[
                -1] > short_avg.iloc[-1]:
            target_pos.set_target_volume(-3)
            logger.info("均线下穿,做空")

        # 均线上穿,做多
        if short_avg.iloc[-2] < long_avg.iloc[-2] and short_avg.iloc[
                -1] > long_avg.iloc[-1]:
            target_pos.set_target_volume(3)
            logger.info("均线上穿,做多")