Пример #1
0
    def __init__(
        self,
        _data: DataStruct,
        _stop_type: int,
        _stop_rate: float = 0.05,
        _use_key: str = 'closeprice',
        _idx_key: str = 'time',
        _ret_key: str = 'stopprice',
    ):
        super().__init__()

        assert len(_data) == 1

        self.stop_type = _stop_type
        self.stop_rate = _stop_rate
        self.use_key = _use_key
        self.idx_key = _idx_key
        self.ret_key = _ret_key

        self.best_price = _data.toDict()[self.use_key]

        time = _data.index()[0]

        self.data = DataStruct([self.idx_key, self.ret_key], self.idx_key,
                               [[time, self.get_stop_price()]])
Пример #2
0
    def __init__(
            self,
            _data: DataStruct,
            _stop_type: int,
            _stop_rate: float = 0.05,
            _use_key: str = 'closeprice',
            _idx_key: str = 'time',
            _ret_key: str = 'stopprice',
    ):
        super().__init__()

        assert len(_data) == 1

        self.stop_type = _stop_type
        self.stop_rate = _stop_rate
        self.use_key = _use_key
        self.idx_key = _idx_key
        self.ret_key = _ret_key

        self.best_price = _data.toDict()[self.use_key]

        time = _data.index()[0]

        self.data = DataStruct(
            [self.idx_key, self.ret_key],
            self.idx_key,
            [[time, self.get_stop_price()]]
        )
Пример #3
0
    def _addOne(
            self, _price_data: DataStruct,
            _atr_data: DataStruct = None,
    ):
        assert len(_atr_data) == 1

        price = _price_data.toDict()[self.price_use_key]
        atr = _atr_data.toDict()[self.atr_use_key]

        price_time = _price_data.index()[0]
        atr_time = _atr_data.index()[0]
        assert price_time == atr_time

        self.data.addDict({
            self.idx_key: price_time,
            self.ret_key: self.get_stop_price(price, atr),
        })
Пример #4
0
    def _addOne(
            self, _price_data: DataStruct,
            _volatility_data: DataStruct = None,
    ):
        assert len(_volatility_data) == 1

        price = _price_data.toDict()[self.price_use_key]
        volatility = _volatility_data.toDict()[self.volatility_use_key]

        price_time = _price_data.index()[0]
        volatility_time = _volatility_data.index()[0]
        assert price_time == volatility_time

        self.data.addDict({
            self.idx_key: price_time,
            self.ret_key: self.get_stop_price(price, volatility),
        })
Пример #5
0
    def _addOne(
        self,
        _price_data: DataStruct,
        _atr_data: DataStruct = None,
    ):
        assert len(_atr_data) == 1

        price = _price_data.toDict()[self.price_use_key]
        atr = _atr_data.toDict()[self.atr_use_key]

        price_time = _price_data.index()[0]
        atr_time = _atr_data.index()[0]
        assert price_time == atr_time

        self.data.addDict({
            self.idx_key: price_time,
            self.ret_key: self.get_stop_price(price, atr),
        })
Пример #6
0
 def _isStop(self, _data_struct: DataStruct):
     price = _data_struct.toDict()[self.use_key]
     stop_price = self.data[self.ret_key][-1]
     if self.stop_type == SignalType.LONG:
         if price < stop_price:
             self.is_stop = True
     elif self.stop_type == SignalType.SHORT:
         if price > stop_price:
             self.is_stop = True
     else:
         raise Exception('unknown type')
Пример #7
0
 def _isStop(self, _data_struct: DataStruct):
     price = _data_struct.toDict()[self.use_key]
     stop_price = self.data[self.ret_key][-1]
     if self.stop_type == SignalType.LONG:
         if price < stop_price:
             self.is_stop = True
     elif self.stop_type == SignalType.SHORT:
         if price > stop_price:
             self.is_stop = True
     else:
         raise Exception('unknown type')
Пример #8
0
    def _addOne(self, _data_struct: DataStruct):
        price = _data_struct.toDict()[self.use_key]
        if self.stop_type == SignalType.LONG:
            self.best_price = max(price, self.best_price)
        elif self.stop_type == SignalType.SHORT:
            self.best_price = min(price, self.best_price)
        else:
            raise Exception('unknown type')
        time = _data_struct.index()[0]

        self.data.addDict({
            self.idx_key: time,
            self.ret_key: self.get_stop_price(),
        })
Пример #9
0
    def _addOne(self, _data_struct: DataStruct):
        price = _data_struct.toDict()[self.use_key]
        if self.stop_type == SignalType.LONG:
            self.best_price = max(price, self.best_price)
        elif self.stop_type == SignalType.SHORT:
            self.best_price = min(price, self.best_price)
        else:
            raise Exception('unknown type')
        time = _data_struct.index()[0]

        self.data.addDict({
            self.idx_key: time,
            self.ret_key: self.get_stop_price(),
        })
Пример #10
0
    def addMarketEvent(self, _symbol: str, _data: DataStruct) -> ReturnMarket:
        """
        add new tick data into market register, and add event

        :param _symbol:
        :param _data:
        :return:
        """
        for k in self.symbol_dict[_symbol]:
            # add event for each strategy if necessary
            for strategy in self.register_dict[k].strategy_set:
                self.engine.addEvent(MarketEvent(k, strategy, _symbol, _data))
        logging.debug('Data({}) {}'.format(_symbol, _data.toDict()))
        return ReturnMarket(_symbol, _data)
Пример #11
0
    def addMarketEvent(
            self, _symbol: str, _data: DataStruct
    ) -> ReturnMarket:
        """
        add new tick data into market register, and add event

        :param _symbol:
        :param _data:
        :return:
        """
        for k in self.symbol_dict[_symbol]:
            # add event for each strategy if necessary
            for strategy in self.register_dict[k].strategy_set:
                self.engine.addEvent(MarketEvent(k, strategy, _symbol, _data))
        logging.debug('Data({}) {}'.format(_symbol, _data.toDict()))
        return ReturnMarket(_symbol, _data)
Пример #12
0
    def __init__(
            self,
            _data: DataStruct,
            _stop_type: int,
            _init_stop: float = 0.05,
            _profit_thresh: typing.Sequence[float] = (
                    0.1, 0.2, 0.3, 0.5
            ),
            _stop_thresh: typing.Sequence[float] = (
                    1.0, 0.5, 0.3, 0.15
            ),
            _use_key: str = 'closeprice',
            _idx_key: str = 'time',
            _status_key: str = 'status',
            _ret_key: str = 'stopprice',
    ):
        super().__init__()

        assert len(_data) == 1

        self.stop_type = _stop_type
        self.status = 0

        self.profit_thresh = _profit_thresh
        self.stop_thresh = _stop_thresh
        self.use_key = _use_key
        self.idx_key = _idx_key
        self.status_key = _status_key
        self.ret_key = _ret_key

        self.init_price = _data.toDict()[self.use_key]
        if self.stop_type == SignalType.LONG:
            self.init_stop_price = self.init_price * (1 - _init_stop)
        elif self.stop_type == SignalType.SHORT:
            self.init_stop_price = self.init_price * (1 + _init_stop)
        else:
            raise Exception('unknown type')
        self.best_price = self.init_price

        self.data = DataStruct(
            [self.idx_key, self.status_key, self.ret_key],
            self.idx_key,
            [[_data.index()[0], self.status, self.init_stop_price]]
        )
Пример #13
0
    def __init__(
        self,
        _data: DataStruct,
        _stop_type: int,
        _init_stop: float = 0.05,
        _profit_thresh: typing.Sequence[float] = (0.1, 0.2, 0.3, 0.5),
        _stop_thresh: typing.Sequence[float] = (1.0, 0.5, 0.3, 0.15),
        _use_key: str = 'closeprice',
        _idx_key: str = 'time',
        _status_key: str = 'status',
        _ret_key: str = 'stopprice',
    ):
        super().__init__()

        assert len(_data) == 1

        self.stop_type = _stop_type
        self.status = 0

        self.profit_thresh = _profit_thresh
        self.stop_thresh = _stop_thresh
        self.use_key = _use_key
        self.idx_key = _idx_key
        self.status_key = _status_key
        self.ret_key = _ret_key

        self.init_price = _data.toDict()[self.use_key]
        if self.stop_type == SignalType.LONG:
            self.init_stop_price = self.init_price * (1 - _init_stop)
        elif self.stop_type == SignalType.SHORT:
            self.init_stop_price = self.init_price * (1 + _init_stop)
        else:
            raise Exception('unknown type')
        self.best_price = self.init_price

        self.data = DataStruct(
            [self.idx_key, self.status_key, self.ret_key], self.idx_key,
            [[_data.index()[0], self.status, self.init_stop_price]])
Пример #14
0
 def dealMarket(self, _symbol: str, _data: DataStruct):
     tmp = _data.toDict()
     self.symbol_price_dict[_symbol] = (tmp['askprice0'] +
                                        tmp['bidprice0']) / 2