掘金社区

请问为什么context.now会返回真实时间,不应该会在设定的时间内运行吗Pinned highlighted

wyx189926 发表在策略研究 2020-10-26 18:12:44

策略研究
78
2
0
from gm.api import *

import numpy as np

import talib





def init(context):

    # 股票代码

    context.symbol = 'SZSE.300296'

    # 数据时间频率

    context.frequency = '1d'

    # 数据区间

    context.fields = 'open,high,low,close,volume'

    # 数据量

    context.count = 25

    # 触发值系数

    context.k1 = 0.7

    context.k2 = 0.5



    # 定时调度函数

    schedule(schedule_func=algo, date_rule='1d', time_rule='09:30:00')







def algo(context):

    # 获取当前日期

    today = context.now

    print(today)

    last_day = get_previous_trading_date('SZSE', today)



    # 获取过去数据

    data = history_n(

        symbol=context.symbol,

        frequency=context.frequency,

        count=context.count,

        end_time=last_day,

        fields=context.fields,

        fill_missing='last',

        adjust=ADJUST_PREV,

        df=True

    )

    high = np.asarray((data['high']))

    low = np.asarray((data['low']))

    close = np.asarray((data['close']))



    # 计算必要数据

    hh = np.max(high)

    hc = np.max(close)

    lc = np.min(close)

    ll = np.min(low)

    range = max(hh - lc, hc - ll)



    # 获取当日开盘价和当前价格

    data_now = current(symbols=context.symbol)[0]  # 数据结构如何

    data_now_open = data_now['open']

    data_now_price = data_now['price']



    # 交易逻辑

    range_up_price = data_now_open + context.k1 * range

    range_down_price = data_now_open - context.k2 * range

    if data_now_price > range_up_price:

        order_target_percent(

            symbol=context.symbol,

            percent=0.75,

            position_side=PositionSide_Long,

            order_type=OrderType_Market,

            price=0

        )

    if data_now_price < range_down_price:

        order_target_percent(

            symbol=context.symbol,

            percent=0.75,

            position_side=PositionSide_Short,

            order_type=OrderType_Market,

            price=0

        )





if __name__ == '__main__':

    run(

        strategy_id='093359a6-1764-11eb-9689-b025aa2ea10f',

        filename="dual_thrust.py",

        mode=MODE_BACKTEST,  # 回测模式

        token='4b033218a9b1de09eff0178b0ca321092165e821',

        backtest_start_time='2015-01-01 09:00:00',

        backtest_end_time='2019_12_31 15:00:00',

        backtest_initial_cash=1000000,

        backtest_adjust=ADJUST_PREV

    )

0_1603707151025_2AAM309L`4)))XM5$ZYF2UX.png

评论: 2

Looks like your connection to 掘金量化社区 - 量化交易者的策略交流学习社区 was lost, please wait while we try to reconnect.