def query_by_group( self, start_timestamp: int, finish_timestamp: int, interval: str, standard: bool = False, ): if interval == KLINE_INTERVAL_1MIN: return self.query(start_timestamp, finish_timestamp, interval, standard) intervals = { KLINE_INTERVAL_1DAY: 24 * 60 * 60 * 1000, KLINE_INTERVAL_4HOUR: 4 * 60 * 60 * 1000, KLINE_INTERVAL_1HOUR: 60 * 60 * 1000, KLINE_INTERVAL_15MIN: 15 * 60 * 1000, } if intervals.get(interval) is None: raise RuntimeError("can not use the interval", interval) num = intervals[interval] flag_timestamp = start_timestamp result = [] while flag_timestamp < finish_timestamp: candles = self.query(flag_timestamp, flag_timestamp + num, KLINE_INTERVAL_1MIN, standard) flag_timestamp += num if len(candles) == 0: continue tmp_candle = candles[0].copy() tmp_candle["high"] = candles[0]["high"] tmp_candle["low"] = candles[0]["low"] tmp_candle["close"] = candles[-1]["close"] tmp_candle["vol"] = 0 tmp_candle["timestamp"] = flag_timestamp tmp_candle["date"] = moment.get(flag_timestamp).to("Asia/Shanghai").format("YYYY-MM-DD HH:mm:ss") for candle in candles: if candle["high"] > tmp_candle["high"]: tmp_candle["high"] = candle["high"] if candle["low"] < tmp_candle["low"]: tmp_candle["low"] = candle["low"] tmp_candle["vol"] += candle["vol"] result.append(tmp_candle) return result
def freeze( self, amount: float, position: float, timestamp: int, ) -> None: m = moment.get(timestamp).to("Asia/Shanghai") self.__freeze( -standard_number(amount), -position, m.millisecond_timestamp, m.format("YYYY-MM-DD HH:mm:ss"), ) return self.__insert_asset_item( m.millisecond_timestamp, m.format("YYYY-MM-DD HH:mm:ss"), )
def _get_risk_level(self, timestamp: int, instance_id: int) -> int: conn = Conn(self["db_name"]) table_name = "{trade_type}_instance_{mode}".format( trade_type=self["trade_type"], mode=MODE_BACKTEST if self["mode"] == MODE_BACKTEST else MODE_STRATEGY, ) m = moment.get(timestamp).to(self.get("timezone") or "Asia/Shanghai").floor("day") query_sql = """ SELECT id FROM {} WHERE symbol = ? AND exchange = ? AND strategy = ? AND (status IN (?, ?, ?) OR ( status = ? AND liquidate_finish_timestamp > ? )) ORDER BY open_start_timestamp, id """ params = ( self["symbol"], self["exchange"], self["strategy"], INSTANCE_STATUS_OPENING, INSTANCE_STATUS_LIQUIDATING, INSTANCE_STATUS_ERROR, INSTANCE_STATUS_FINISHED, m.millisecond_timestamp, ) if self["mode"] == MODE_BACKTEST: query_sql = """ SELECT id FROM {} WHERE backtest_id = ? AND symbol = ? AND exchange = ? AND strategy = ? AND open_start_timestamp < ? AND (liquidate_finish_timestamp > ? OR status in (?,?)) ORDER BY open_start_timestamp, id """ params = ( self["backtest_id"], self["symbol"], self["exchange"], self["strategy"], timestamp, timestamp, INSTANCE_STATUS_OPENING, INSTANCE_STATUS_LIQUIDATING, ) instances = conn.query( query_sql.format(table_name), params, ) instance_ids = [i["id"] for i in instances] risk_level = len(instance_ids) if instance_id in instance_ids: risk_level = instance_ids.index(instance_id) return risk_level
def first_invest( self, asset_total: float, position_total: float, position_sub: float, ) -> None: m = moment.get(BIRTHDAY_BTC).to( self.get("timezone") or "Asia/Shanghai") query_sql = "SELECT * FROM {} WHERE exchange = ? AND settle_mode = ? AND settle_currency = ? AND subject = ?" \ " AND timestamp <= ? AND backtest_id = ? LIMIT 1".format(self._account_flow_table_name) query_param = ( self._exchange, self._settle_mode, self._settle_currency, SUBJECT_INVEST, m.millisecond_timestamp, self._backtest_id, ) conn = Conn(self._db_name) one = conn.query_one(query_sql, query_param) if one: return self.__invest( standard_number(asset_total), position_total, m.millisecond_timestamp, m.format("YYYY-MM-DD HH:mm:ss"), ) self.__transfer_in( standard_number(asset_total * position_sub / position_total), position_sub, m.millisecond_timestamp, m.format("YYYY-MM-DD HH:mm:ss"), ) self.__insert_asset_item( m.millisecond_timestamp, m.format("YYYY-MM-DD HH:mm:ss"), )
def unfreeze_and_settle( self, unfreeze_asset: float, unfreeze_position: float, settle_asset: float, timestamp: int, ) -> None: m = moment.get(timestamp).to("Asia/Shanghai") self.__unfreeze( standard_number(unfreeze_asset), unfreeze_position, m.millisecond_timestamp, m.format("YYYY-MM-DD HH:mm:ss"), ) self.__settle( standard_number(settle_asset), 0.0, m.millisecond_timestamp, m.format("YYYY-MM-DD HH:mm:ss"), ) return self.__insert_asset_item( m.millisecond_timestamp, m.format("YYYY-MM-DD HH:mm:ss"), )
def get_wait_open(self, timestamp): moment = m.get(timestamp) the_last_day = moment.to(self["timezone"] or "Asia/Shanghai").floor("day") the_start_day = the_last_day.shift(days=-20) results = self._kline.query( the_start_day.millisecond_timestamp, the_last_day.millisecond_timestamp, KLINE_INTERVAL_1DAY, standard=True, ) self["a"].init_account(10) price = max([result["high"] for result in results]) asset = self._a.get_last_asset(timestamp)["total_account_asset"] amount = int(asset * price * self["param"]["position"] / 100000000 / self["unit_amount"]) return [ FutureOrder( trade_type=self["trade_type"], place_type=ORDER_PLACE_TYPE_T_TAKER, db_name=self["db_name"], mode=self["mode"], symbol=self["symbol"], exchange=self["exchange"], contract_type=self["contract_type"], instance_id=self["instance_id"], sequence=0, backtest_id=self["backtest_id"], price=price, amount=amount, lever=self["lever"], ) ]
def __update_instance(self): conn = Conn(self._db_name) orders = conn.query( "SELECT * FROM {} WHERE instance_id = ? ORDER BY sequence".format( self._table_name), (self["instance_id"], ), ) open_amount, open_fee = 0, 0.0 open_start_timestamp, open_finish_timestamp = 0, 0 open_start_datetime, open_finish_datetime = "", "" open_type, open_place_type = ORDER_TYPE_OPEN_LONG, "" liquidate_amount, liquidate_fee = 0, 0.0 liquidate_start_timestamp, liquidate_finish_timestamp = 0, 0 liquidate_start_datetime, liquidate_finish_datetime = "", "" liquidate_type, liquidate_place_type = ORDER_TYPE_LIQUIDATE_LONG, "" swap_times, swap_fee, swap_asset_pnl = 0, 0.0, 0 swap_contract = { "open_amount": 0, "open_sum": 0, "open_avg_price": 0, "liquidate_sum": 0, "liquidate_amount": 0, "liquidate_avg_price": 0, } for order in orders: place_timestamp = order["place_timestamp"] place_datetime = moment.get(order["place_timestamp"]).to( "Asia/Shanghai").format("YYYY-MM-DD HH:mm:ss") if order["type"] in ( ORDER_TYPE_OPEN_LONG, ORDER_TYPE_OPEN_SHORT, ) and order["place_type"] not in ( ORDER_PLACE_TYPE_L_SWAP, ORDER_PLACE_TYPE_O_SWAP, ): open_amount += order["deal_amount"] open_fee += order["fee"] open_type = order["type"] open_place_type = order["place_type"] if open_start_timestamp == 0: open_start_timestamp = place_timestamp open_start_datetime = place_datetime open_finish_timestamp = place_timestamp open_finish_datetime = place_datetime if order["type"] in ( ORDER_TYPE_LIQUIDATE_LONG, ORDER_TYPE_LIQUIDATE_SHORT, ) and order["place_type"] not in ( ORDER_PLACE_TYPE_L_SWAP, ORDER_PLACE_TYPE_O_SWAP, ): liquidate_amount += order["deal_amount"] liquidate_fee += order["fee"] liquidate_type = order["type"] liquidate_place_type = order["place_type"] if liquidate_start_timestamp == 0: liquidate_start_timestamp = place_timestamp liquidate_start_datetime = place_datetime liquidate_finish_timestamp = place_timestamp liquidate_finish_datetime = place_datetime if order["place_type"] in ( ORDER_PLACE_TYPE_O_SWAP, ORDER_PLACE_TYPE_L_SWAP, ): swap_fee += order["fee"] if order["type"] == ORDER_TYPE_OPEN_LONG: swap_times += 1 swap_contract["open_amount"] += order["deal_amount"] swap_contract["open_sum"] -= order["deal_amount"] * order[ "avg_price"] swap_contract["open_avg_price"] = int( -swap_contract["open_sum"] / swap_contract["open_amount"]) elif order["type"] == ORDER_TYPE_OPEN_SHORT: swap_times += 1 swap_contract["open_amount"] += order["deal_amount"] swap_contract["open_sum"] += order["deal_amount"] * order[ "avg_price"] swap_contract["open_avg_price"] = int( swap_contract["open_sum"] / swap_contract["open_amount"]) elif order["type"] == ORDER_TYPE_LIQUIDATE_LONG: swap_contract["liquidate_amount"] += order["deal_amount"] swap_contract["liquidate_sum"] += order[ "deal_amount"] * order["avg_price"] swap_contract["liquidate_avg_price"] = int( swap_contract["liquidate_sum"] / swap_contract["liquidate_amount"]) elif order["type"] == ORDER_TYPE_LIQUIDATE_SHORT: swap_contract["liquidate_amount"] += order["deal_amount"] swap_contract["liquidate_sum"] -= order[ "deal_amount"] * order["avg_price"] swap_contract["liquidate_avg_price"] = int( -swap_contract["liquidate_sum"] / swap_contract["liquidate_amount"]) else: raise RuntimeError("can deal with the order type. ") if open_amount != liquidate_amount: return # 不需要计算swap的情况。 if swap_contract["open_amount"] != swap_contract["liquidate_amount"]: return if swap_contract["open_amount"]: swap_asset_pnl = ( swap_contract["open_sum"] + swap_contract["liquidate_sum"]) * self["unit_amount"] swap_asset_pnl = real_number(swap_asset_pnl) swap_asset_pnl /= real_number(swap_contract["open_avg_price"]) swap_asset_pnl /= real_number(swap_contract["liquidate_avg_price"]) conn.execute( "UPDATE future_instance_backtest SET open_fee = ?, open_type = ?, open_place_type = ?," " open_start_timestamp = ?, open_start_datetime = ?, open_finish_timestamp = ?, open_finish_datetime = ?, " " liquidate_fee = ?, liquidate_type = ?, liquidate_place_type = ?," " liquidate_start_timestamp = ?, liquidate_start_datetime = ?," " liquidate_finish_timestamp = ?, liquidate_finish_datetime = ?," " swap_times = ?, swap_fee = ?, swap_asset_pnl = ? WHERE id = ?", ( open_fee, open_type, open_place_type, open_start_timestamp, open_start_datetime, open_finish_timestamp, open_finish_datetime, liquidate_fee, liquidate_type, liquidate_place_type, liquidate_start_timestamp, liquidate_start_datetime, liquidate_finish_timestamp, liquidate_finish_datetime, swap_times, swap_fee, swap_asset_pnl, self["instance_id"], ), )
def save(self, check: bool = False, raw_order_data: str = None, raw_market_data: str = None): if check: # 检验参数可用性 validate(instance=self, schema=order_input) conn = Conn(self._db_name) one = conn.query_one( "SELECT id FROM {} WHERE instance_id = ? AND sequence = ?".format( self._table_name), (self["instance_id"], self["sequence"]), ) if one: conn.execute( "UPDATE {} SET place_type = ?, `type` = ?, price = ?, amount = ?," " avg_price = ?, deal_amount = ?, status = ?, lever = ?, fee = ?," " symbol = ?, exchange = ?, unit_amount = ?, place_timestamp = ?, place_datetime = ?," " deal_timestamp = ?, deal_datetime = ?, swap_timestamp = ?, swap_datetime = ?," " cancel_timestamp = ?, cancel_datetime = ?, raw_order_data = ?, raw_market_data = ?" " WHERE instance_id = ? AND sequence = ?".format( self._table_name), ( self["place_type"], self["type"], self["price"], self["amount"], self["avg_price"], self["deal_amount"], self["status"], self["lever"], self["fee"], self["symbol"], self["exchange"], self["unit_amount"], self["place_timestamp"], self["place_datetime"], self["deal_timestamp"], self["deal_datetime"], self["swap_timestamp"], self["swap_datetime"], self["cancel_timestamp"], self["cancel_datetime"], raw_order_data, raw_market_data, self["instance_id"], self["sequence"], ), ) else: conn.insert( "INSERT INTO {} (instance_id, sequence, place_type, `type`, price," " amount, avg_price, deal_amount, status, lever," " fee, symbol, exchange, unit_amount, place_timestamp, place_datetime, deal_timestamp, deal_datetime," " cancel_timestamp, cancel_datetime, raw_order_data, raw_market_data) VALUES" " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" .format(self._table_name, ), ( self["instance_id"], self["sequence"], self["place_type"], self["type"], self["price"], self["amount"], self["avg_price"], self["deal_amount"], self["status"], self["lever"], self["fee"], self["symbol"], self["exchange"], self["unit_amount"], self["place_timestamp"], self["place_datetime"], self["deal_timestamp"], self["deal_datetime"], self["cancel_timestamp"], self["cancel_datetime"], raw_order_data, raw_market_data, ), ) orders = conn.query( "SELECT * FROM {} WHERE instance_id = ? ORDER BY sequence".format( self._table_name), (self["instance_id"], ), ) open_amount, open_fee = 0, 0.0 open_start_timestamp, open_finish_timestamp = 0, 0 open_start_datetime, open_finish_datetime = "", "" open_type, open_place_type = ORDER_TYPE_OPEN_LONG, "" liquidate_amount, liquidate_fee = 0, 0.0 liquidate_start_timestamp, liquidate_finish_timestamp = 0, 0 liquidate_start_datetime, liquidate_finish_datetime = "", "" liquidate_type, liquidate_place_type = ORDER_TYPE_LIQUIDATE_LONG, "" for order in orders: place_timestamp = order["place_timestamp"] place_datetime = moment.get(order["place_timestamp"]).to( self.get("timezone") or "Asia/Shanghai").format("YYYY-MM-DD HH:mm:ss") if order["type"] in (ORDER_TYPE_OPEN_LONG, ORDER_TYPE_OPEN_SHORT): open_amount += order["deal_amount"] open_fee += order["fee"] open_type = order["type"] open_place_type = order["place_type"] if order["sequence"] == 0: open_start_timestamp = place_timestamp open_start_datetime = place_datetime open_finish_timestamp = place_timestamp open_finish_datetime = place_datetime if order["type"] in (ORDER_TYPE_LIQUIDATE_LONG, ORDER_TYPE_LIQUIDATE_SHORT): liquidate_amount += order["deal_amount"] liquidate_fee += order["fee"] liquidate_type = order["type"] liquidate_place_type = order["place_type"] if liquidate_start_timestamp == 0: liquidate_start_timestamp = place_timestamp liquidate_start_datetime = place_datetime liquidate_finish_timestamp = place_timestamp liquidate_finish_datetime = place_datetime if open_amount != liquidate_amount: return conn.execute( "UPDATE {trade_type}_instance_{mode} SET open_fee = ?, open_type = ?, open_place_type = ?," " open_start_timestamp = ?, open_start_datetime = ?, open_finish_timestamp = ?, open_finish_datetime = ?, " " liquidate_fee = ?, liquidate_type = ?, liquidate_place_type = ?," " liquidate_start_timestamp = ?, liquidate_start_datetime = ?," " liquidate_finish_timestamp = ?, liquidate_finish_datetime = ? WHERE id = ?" .format( trade_type=self._trade_type, mode=MODE_STRATEGY if self._mode != MODE_BACKTEST else MODE_BACKTEST, ), ( open_fee, open_type, open_place_type, open_start_timestamp, open_start_datetime, open_finish_timestamp, open_finish_datetime, liquidate_fee, liquidate_type, liquidate_place_type, liquidate_start_timestamp, liquidate_start_datetime, liquidate_finish_timestamp, liquidate_finish_datetime, self["instance_id"], ), )
def test_get_datetime_fmt(self): m = moment.get("2019-09-10 00:00:00", "YYYY-MM-DD HH:mm:ss") print(m.format("YYYY-MM-DD HH:mm:ss"))
def test_get_datetime(self): m = moment.get(datetime(2019, 9, 10), "Asia/Shanghai") print(m.format("YYYY-MM-DD HH:mm:ss"))
def test_get_none(self): m = moment.get().to("Asia/Shanghai") print(m.format("YYYY-MM-DD HH:mm:ss.SSSSSS ZZ"))
def test_from_timestamp(self): m = moment.get(1568585483123) print(m.format("YYYY-MM-DD HH:mm:ss.SSSSSS ZZ"))
def _cp_instance_and_gen_order( self, sequence: int, # 所属顺序 timestamp: int, # 策略产生的时间戳 due_timestamp: int, # 对用contract的到期时间 price: int, # 标准化价格 amount: int, # 开仓/平仓数量 order_type: int, # 交易类型 place_type: str, # 下单手法 ): instance = self.copy() param: Param = Param( self["param"].copy(), trade_type=self["trade_type"], db_name=self["db_name"], mode=self["mode"], ) indices: Indices = Indices( self["indices"].copy(), trade_type=self["trade_type"], db_name=self["db_name"], mode=self["mode"], ) instance["param"] = param instance["indices"] = indices if self["trade_type"] == TRADE_TYPE_FUTURE: due_datetime = moment.get(due_timestamp).to( self["timezone"] or "Asia/Shanghai", ).format("YYYY-MM-DD HH:mm:ss") instance["order"] = FutureOrder( { "place_type": place_type, "type": order_type, "symbol": self["symbol"], "exchange": self["exchange"], "contract_type": get_contract_type(timestamp, due_timestamp), "instance_id": self["id"], "sequence": sequence, "price": int(price), "amount": int(amount), "lever": self["lever"], "due_timestamp": due_timestamp, "due_datetime": due_datetime, "unit_amount": self["unit_amount"], }, trade_type=self["trade_type"], db_name=self["db_name"], mode=self["mode"], settle_mode=self["settle_mode"] ) return instance instance["order"] = CommonOrder( { "place_type": place_type, "type": order_type, "symbol": self["symbol"], "exchange": self["exchange"], "instance_id": self["id"], "sequence": sequence, "price": int(price), "amount": int(amount), "lever": self["lever"], "unit_amount": self.get("unit_amount") or 1, }, trade_type=self["trade_type"], db_name=self["db_name"], mode=self["mode"], settle_mode=self["settle_mode"] ) return instance