vn.py官网
开源量化社区
Member
avatar
加入于:
帖子: 8
声望: 0
from vnpy.app.cta_strategy import (
CtaTemplate,
StopOrder,
TickData,
BarData,
TradeData,
OrderData,
BarGenerator,
ArrayManager,
)
import joblib
from ml import *

class RandomForest(CtaTemplate):
    order_num = 100
    load_num = 24
    percent = 0.9
    interval = "1h"        # 1m 1h d

    atr_value = 0
    atr_ma = 0
    rsi_value = 0
    rsi_buy = 0
    rsi_sell = 0
    intra_trade_high = 0
    intra_trade_low = 0


    parameters = [
        "order_num",
        "load_num",
        "percent",
        "interval"        # 1m 1h d
    ]
    variables = [
        "atr_value",
        "atr_ma",
        "rsi_value",
        "rsi_buy",
        "rsi_sell",
        "intra_trade_high",
        "intra_trade_low"
    ]

    def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
        """"""
        super().__init__(cta_engine, strategy_name, vt_symbol, setting)
        self.bg = BarGenerator(self.on_bar, 1, self.on_bar, self.interval)
        print(self.interval)
        self.am = ArrayManager()

    def on_init(self):
        """
        Callback when strategy is inited.
        """
        self.write_log("模型初始化"+str(self.load_num))
        Machinelearn(self.load_num, self.percent, self.interval, self.vt_symbol.split(".")[0])
        self.write_log("策略初始化")
        self.load_bar(self.load_num)

    def on_start(self):
        """
        Callback when strategy is started.
        """
        self.write_log("策略启动")

    def on_stop(self):
        """
        Callback when strategy is stopped.
        """
        self.write_log("策略停止")

    def on_tick(self, tick: TickData):
        """
        Callback of new tick data update.
        """
        self.bg.update_tick(tick)

    def on_bar(self, bar: BarData):
        """
        Callback of new bar data update.
        """
        self.cancel_all()

        am = self.am
        am.update_bar(bar)
        if not am.inited:
            return

        """
        'close_price', 'high_price', 'low_price', 'open_price',
       'volume', 'MA10', 'MA20', 'MA30', 'MA60', #'SMA120', 'SMA256',
       'BBANDS_upper', 'BBANDS_middle', 'BBANDS_lower', 'DEMA', 'MA', 'EMA12',
       'EMA26', 'KAMA', 'SMA', 'MIDPOINT', 'SAR', 'T3', 'TEMA', 'MIDPRICE',
       'TRIMA', 'SAREXT', 'WMA', 'ATR', 'NATR', 'TRANGE', 'AD', 'ADOSC', 'OBV',
       'HT_DCPERIOD', 'HT_DCPHASE', 'HT_PHASOR_inphase',
       'HT_PHASOR_quadrature', 'HT_SINE_sine', 'HT_SINE_leadsine',
       'HT_TRENDMODE', 'GPRICE', 'MEDPRICE', 'TYPPRICE', 'WCLPRICE', 'ADX',
       'ADXR', 'APO', 'AROON_aroondown', 'AROON_aroonup', 'AROONOSC', 'BOP',
       'CCI', 'CMO', 'DX', 'MACD_macd', 'MACD_macdsignal', 'MACD_macdhist',
       'MACDEXT_macd', 'MACDEXT_macdsignal', 'MACDEXT_macdhist',
       'MACDFIX_macd', 'MACDFIX_macdsignal', 'MACDFIX_macdhist', 'MFI',
       'MINUS_DI', 'MINUS_DM', 'MOM', 'PLUS_DI', 'PLUS_DM', 'PPO', 'ROC',
       'ROCP', 'ROCR', 'ROCR100', 'RSI', 'STOCH_slowk', 'STOCH_slowd',
       'STOCHF_fastk', 'STOCHF_fastd', 'TRIX', 'ULTOSC', 'WILLR', 'BETA',
       'CORREL', 'LINEARREG', 'LINEARREG_ANGLE', 'LINEARREG_INTERCEPT',
       'LINEARREG_SLOPE', 'STDDEV', 'TSF', 'VAR'
        """

        # SMA
        MA10 = am.sma(10)
        MA20 = am.sma(20)
        MA30 = am.sma(30)
        MA60 = am.sma(60)
        BBANDS_upper, BBANDS_middle, BBANDS_lower = am.bbands()
        DEMA = am.dema(30)
        MA = am.sma(30)
        EMA12 = am.ema(6)
        EMA26 = am.ema(12)
        KAMA = am.kama(30)
        SMA = am.sma(30)
        MIDPOINT = am.midpoint(14)
        SAR = am.sar()
        T3 = am.t3(5)
        TEMA = am.tema(30)
        MIDPRICE = am.midprice(14)
        TRIMA = am.trima(30)
        SAREXT = am.sarext(startvalue=0, offsetonreverse=0, accelerationinitlong=0, accelerationlong=0,
                            accelerationmaxlong=0, accelerationinitshort=0, accelerationshort=0,accelerationmaxshort=0)
        WMA = am.wma(30)
        ATR = am.atr(14)
        NATR = am.natr(14)
        TRANGE = am.trange()
        AD = am.ad()
        ADOSC = am.adosc(fast_period=3, slow_period=10)
        OBV = am.obv()
        HT_DCPERIOD = am.ht_dcperiod()
        HT_DCPHASE = am.ht_dcphase()
        HT_PHASOR_inphase, HT_PHASOR_quadrature = am.ht_phasor()
        HT_SINE_sine,HT_SINE_leadsine = am.ht_sine()
        HT_TRENDMODE = am.ht_trendmode()
        $GPRICE = am.avgprice()
        MEDPRICE = am.medprice()
        TYPPRICE = am.typprice()
        WCLPRICE  = am.wclprice()
        ADX = am.adx(14)
        ADXR = am.adxr(14)
        APO = am.apo(fast_period=12, slow_period=26, matype=0)
        AROON_aroondown,AROON_aroonup = am.aroon(14)
        AROONOSC = am.aroonosc(14)
        BOP = am.bop()
        CCI = am.cci(14)
        CMO = am.cmo(14)
        DX = am.dx(14)
        MACD_macd,MACD_macdsignal,MACD_macdhist = am.macd(fast_period=12, slow_period=26, signal_period=9)
        MACDEXT_macd,MACDEXT_macdsignal,MACDEXT_macdhist = am.macdext(fast_period=12,fast_matype=0,
                slow_period=26, slow_matype=0, signal_period=9, signal_matype=0)
        MACDFIX_macd,MACDFIX_macdsignal,MACDFIX_macdhist = am.macdfix(9)
        MFI = am.mfi(14)
        MINUS_DI = am.minus_di(14)
        MINUS_DM = am.minus_dm(14)
        MOM = am.mom(10)
        PLUS_DI = am.plus_di(14)
        PLUS_DM = am.plus_dm(14)
        PPO = am.ppo(fast_period=12, slow_period=26, matype=0)
        ROC = am.roc(10)
        ROCP = am.rocp(10)
        ROCR = am.rocr(10)
        ROCR100 = am.rocr_100(10)
        RSI = am.rsi(14)
        STOCH_slowk, STOCH_slowd = am.stoch( fastk_period=5, slowk_period=3, slowk_matype=0,
                                                           slowd_period=3, slowd_matype=0)
        STOCHF_fastk,STOCHF_fastd = am.stochf(fastk_period=5, fastd_period=3, fastd_matype=0)
        TRIX = am.trix(30)
        ULTOSC = am.ultosc(time_period1=7, time_period2=14, time_period3=28)
        WILLR = am.willr(14)
        BETA = am.beta(5)
        CORREL = am.correl(30)
        LINEARREG = am.linearreg(14)
        LINEARREG_ANGLE = am.linearreg_angle(14)
        LINEARREG_INTERCEPT = am.linearreg_intercept(14)
        LINEARREG_SLOPE = am.linearreg_slope(14)
        STDDEV = am.stddev(timeperiod=5, nbdev=1)
        TSF = am.tsf(14)
        VAR = am.var(timeperiod=5, nbdev=1)

        out_list = list([[bar.close_price, bar.high_price, bar.low_price, bar.open_price,
              bar.volume, MA10, MA20, MA30, MA60, BBANDS_upper, BBANDS_middle,
              BBANDS_lower, DEMA, MA, EMA12, EMA26, KAMA, SMA, MIDPOINT, SAR,
              T3, TEMA, MIDPRICE, TRIMA, SAREXT, WMA, ATR, NATR, TRANGE, AD,
              ADOSC, OBV, HT_DCPERIOD, HT_DCPHASE, HT_PHASOR_inphase,
              HT_PHASOR_quadrature, HT_SINE_sine, HT_SINE_leadsine, HT_TRENDMODE,
              $GPRICE, MEDPRICE, TYPPRICE, WCLPRICE, ADX, ADXR, APO,
              AROON_aroondown, AROON_aroonup, AROONOSC, BOP,CCI, CMO, DX,
              MACD_macd, MACD_macdsignal, MACD_macdhist,MACDEXT_macd,
              MACDEXT_macdsignal, MACDEXT_macdhist,MACDFIX_macd, MACDFIX_macdsignal,
              MACDFIX_macdhist, MFI,MINUS_DI, MINUS_DM, MOM, PLUS_DI, PLUS_DM,
              PPO, ROC,ROCP, ROCR, ROCR100, RSI, STOCH_slowk, STOCH_slowd,
              STOCHF_fastk, STOCHF_fastd, TRIX, ULTOSC, WILLR, BETA,CORREL,
              LINEARREG, LINEARREG_ANGLE, LINEARREG_INTERCEPT,LINEARREG_SLOPE,
              STDDEV, TSF, VAR]])

        import pandas as pd
        df = pd.DataFrame(out_list)
        m2 = joblib.load("max_min.model")
        y_pred = m2.predict(df)
        # 1为顶点 -1为底点

        if self.pos == 0:
            if y_pred == -1:
                self.buy(bar.close_price + 5, self.order_num)
                print("开多",bar.close_price + 5,self.order_num)
            elif y_pred == 1:
                self.short(bar.close_price - 5, self.order_num)
                print("开空",bar.close_price - 5,self.order_num)

        elif self.pos > 0:
            if y_pred == -1:
                self.buy(bar.close_price + 5, self.order_num)
                print("开多",bar.close_price +5,self.order_num)
            elif y_pred == 1:
                self.sell(bar.close_price -5, abs(self.pos))
                print("平多", bar.close_price-5, abs(self.pos))
                self.short(bar.close_price-5, self.order_num)
                print("开空", bar.close_price-5, self.order_num)


        elif self.pos < 0:
            if y_pred == -1:
                self.cover(bar.close_price+5, abs(self.pos))
                print("平空",bar.close_price+5,abs(self.pos))
                self.buy(bar.close_price+5, self.order_num)
                print("开多",bar.close_price+5,self.order_num)
            elif y_pred == 1:
                self.short(bar.close_price-5, self.order_num)
                print("开空", bar.close_price-5, self.order_num)


        self.put_event()

    def on_order(self, order: OrderData):
        """
        Callback of new order data update.
        """
        pass

    def on_trade(self, trade: TradeData):
        """
        Callback of new trade data update.
        """
        self.put_event()

    def on_stop_order(self, stop_order: StopOrder):
        """
        Callback of stop order update.
        """
        pass
Member
avatar
加入于:
帖子: 3
声望: 0

请问from ml import *的ml是什么?

Member
avatar
加入于:
帖子: 8
声望: 0

lance52587 wrote:

请问from ml import *的ml是什么?
另外一个用于模型生成的库,参考这个帖子“https://www.vnpy.com/forum/topic/6376-ji-qi-xue-xi-mo-xing-xun-lian?page=1#pid22756”

Member
avatar
加入于:
帖子: 24
声望: 0

@lance52587
猜测有可能是machine learning 包吧!!!

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3

沪公网安备 31011502017034号