def get_trade(self, action: TradeActionUnion) -> Trade: action_type, trade_amount = action trade_type = TradeType(int(action_type * len(TradeType))) current_price = self._exchange.current_price( symbol=self.instrument_symbol) base_precision = self._exchange.base_precision instrument_precision = self._exchange.instrument_precision amount = self._exchange.instrument_balance(self.instrument_symbol) price = current_price if trade_type is TradeType.MARKET_BUY or trade_type is TradeType.LIMIT_BUY: price_adjustment = 1 + (self.max_allowed_slippage_percent / 100) price = max( round(current_price * price_adjustment, base_precision), base_precision) amount = round( self._exchange.balance * 0.99 * trade_amount / price, instrument_precision) elif trade_type is TradeType.MARKET_SELL or trade_type is TradeType.LIMIT_SELL: price_adjustment = 1 - (self.max_allowed_slippage_percent / 100) price = round(current_price * price_adjustment, base_precision) amount_held = self._exchange.portfolio.get(self.instrument_symbol, 0) amount = round(amount_held * trade_amount, instrument_precision) return Trade(self.instrument_symbol, trade_type, amount, price)
def get_trade(self, current_step: int, action: TradeActionUnion) -> Trade: """The trade type is determined by `action % len(TradeType)`, and the trade amount is determined by the multiplicity of the action. For example, 1 = LIMIT_BUY|0.25, 2 = MARKET_BUY|0.25, 6 = LIMIT_BUY|0.5, 7 = MARKET_BUY|0.5, etc. """ n_splits = self.n_actions / len(TradeType) trade_type = TradeType(action % len(TradeType)) trade_amount = int(action / len(TradeType)) * float(1 / n_splits) + ( 1 / n_splits) current_price = self._exchange.current_price(symbol=self._instrument) base_precision = self._exchange.base_precision instrument_precision = self._exchange.instrument_precision amount = self._exchange.instrument_balance(self._instrument) price = current_price if trade_type is TradeType.MARKET_BUY or trade_type is TradeType.LIMIT_BUY: price_adjustment = 1 + (self.max_allowed_slippage_percent / 100) price = max( round(current_price * price_adjustment, base_precision), base_precision) amount = round( self._exchange.balance * 0.99 * trade_amount / price, instrument_precision) elif trade_type is TradeType.MARKET_SELL or trade_type is TradeType.LIMIT_SELL: price_adjustment = 1 - (self.max_allowed_slippage_percent / 100) price = round(current_price * price_adjustment, base_precision) amount_held = self._exchange.portfolio.get(self._instrument, 0) amount = round(amount_held * trade_amount, instrument_precision) return Trade(current_step, self._instrument, trade_type, amount, price)
def get_trade(self, current_step: int, action: TradeActionUnion) -> Trade: """The trade type is determined by `action % len(TradeType)`, and the trade amount is determined by the multiplicity of the action. For example, 1 = LIMIT_BUY|0.25, 2 = MARKET_BUY|0.25, 6 = LIMIT_BUY|0.5, 7 = MARKET_BUY|0.5, etc. """ # n_splits = self.n_actions / len(TradeType) # trade_type = TradeType(action % len(TradeType)) # trade_amount = int(action / len(TradeType)) * float(1 / n_splits) + (1 / n_splits) if action == 3: amount = abs(self._exchange.total_position) if self._exchange.total_position > 0: trade_type = TradeType(2) else: trade_type = TradeType(1) else: trade_type = TradeType(action) amount = self.max_allowed_amount current_price = self._exchange.current_price(symbol=self._instrument) # base_precision = self._exchange.base_precision # instrument_precision = self._exchange.instrument_precision # amount = self._exchange.instrument_balance(self._instrument) price = current_price # # if trade_type is TradeType.MARKET_BUY or trade_type is TradeType.LIMIT_BUY: # if trade_type is TradeType.MARKET_LONG: # price_adjustment = 1 + (self.max_allowed_slippage_percent / 100) # price = max(round(current_price * price_adjustment, base_precision), base_precision) # # amount = round(self._exchange.balance * 0.99 * trade_amount / price, instrument_precision) # # amount = round((self._exchange.balance * self._exchange.leverage * price) / 10000) + 5 # # amount = self.max_allowed_amount # # print('Position Size => ', amount, ' USDT') # # elif trade_type is TradeType.MARKET_SELL or trade_type is TradeType.LIMIT_SELL: # elif trade_type is TradeType.MARKET_SHORT: # price_adjustment = 1 - (self.max_allowed_slippage_percent / 100) # price = round(current_price * price_adjustment, base_precision) # amount_held = self._exchange.portfolio.get(self._instrument, 0) # # amount = round(amount_held * trade_amount, instrument_precision) # # amount = round((self._exchange.balance * self._exchange.leverage * price) / 10000) + 5 # # amount = self.max_allowed_amount # # print('Position Size => ', amount, ' USDT') return Trade(current_step, self._instrument, trade_type, amount, price)
def get_trade(self, current_step: int, action: TradeActionUnion) -> Trade: """The trade type is determined by `action % len(TradeType)`, and the trade amount is determined by the multiplicity of the action. 获取Trade对象,交易类型,由 action % len(TradeType)决定, 交易数量, 根据action 动作对应级别获取 For example, 1 = LIMIT_BUY|0.25, 2 = MARKET_BUY|0.25, 6 = LIMIT_BUY|0.5, 7 = MARKET_BUY|0.5, etc. """ # 每个交易动作,对应的操作仓位份数。 20/ 5 = 4 n_splits = self.n_actions / len(TradeType) # 交易类型 0 ~4 % 5 trade_type = TradeType(action % len(TradeType)) # 交易仓位, action/len(TradeType) +1 => 交易数量份数( 1~5) => 乘以每一份 trade_amount_percent = int(action / len(TradeType)) * float( 1 / n_splits) + (1 / n_splits) # 当前交易合约价格 current_price = self._exchange.current_price(symbol=self._instrument) # 基准合约价格精度 base_precision = self._exchange.base_precision # 交易合约价格精度 instrument_precision = self._exchange.instrument_precision # 当前合约持仓数量 amount = self._exchange.instrument_balance(self._instrument) # 当前价格 price = current_price # 市价/限价买入 if trade_type is TradeType.MARKET_BUY or trade_type is TradeType.LIMIT_BUY: # 滑点调整=》价格 price_adjustment = 1 + (self.max_allowed_slippage_percent / 100) # 精度修正=》价格 price = max( round(current_price * price_adjustment, base_precision), base_precision) # 账号基准净值 * 仓位比例 / 价格 =》 修正 =》 买入交易合约数量 amount = round( self._exchange.balance * 0.99 * trade_amount_percent / price, instrument_precision) # 市价卖出/限价卖出 elif trade_type is TradeType.MARKET_SELL or trade_type is TradeType.LIMIT_SELL: # 滑点调整=》价格 price_adjustment = 1 - (self.max_allowed_slippage_percent / 100) # 精度修正=》价格 price = round(current_price * price_adjustment, base_precision) # 交易合约当前持仓数量 amount_held = self._exchange.portfolio.get(self._instrument, 0) # 持仓数量 * 仓位比例 => 修正 =》 卖出交易合约数量 amount = round(amount_held * trade_amount_percent, instrument_precision) # 交易合约,交易类型,交易数量,交易价格 =》 Trade 对象 return Trade(current_step, self._instrument, trade_type, amount, price)
def get_trade(self, current_step: int, action: TradeActionUnion) -> Trade: if abs(float(action)) > 0.8: amount = abs(self._exchange.total_position) if self._exchange.total_position > 0: trade_type = TradeType(2) else: trade_type = TradeType(1) else: amount = self.max_allowed_amount if np.sign(action) > 0: trade_type = TradeType(1) elif np.sign(action) < 0: trade_type = TradeType(2) else: trade_type = TradeType(0) current_price = self._exchange.current_price(symbol=self._instrument) # base_precision = self._exchange.base_precision price = current_price # amount = self._exchange.instrument_balance(self._instrument) # if trade_type is TradeType.MARKET_LONG: # price_adjustment = 1 + (self.max_allowed_slippage_percent / 100) # price = max(round(current_price * price_adjustment, base_precision), base_precision) # # amount = round(self._exchange.balance * 0.99 * # # trade_amount / price, instrument_precision) # # amount = round(trade_amount * self.max_allowed_amount, instrument_precision) # # amount = round(trade_amount * (self._exchange.balance * self._exchange.leverage * price) * self.max_allowed_amount_percent / 100, instrument_precision) # # elif trade_type is TradeType.MARKET_SHORT: # price_adjustment = 1 - (self.max_allowed_slippage_percent / 100) # price = round(current_price * price_adjustment, base_precision) # # amount_held = self._exchange.portfolio.get(self._instrument, 0) # # amount = round(amount_held * trade_amount, instrument_precision) # # amount = round(trade_amount * self.max_allowed_amount, instrument_precision) # # amount = round(trade_amount * (self._exchange.balance * self._exchange.leverage * price) * self.max_allowed_amount_percent / 100, instrument_precision) return Trade(current_step, self._instrument, trade_type, amount, price)
def get_trade(self, current_step: int, action: TradeActionUnion) -> Trade: """ get a new Trade object. 根据action参数,获取一个新的Trade对象 :param action: 交易动作类型,(tuple) :return: """ # 动作类型(int), 交易仓位(0~1) action_type, trade_amount_percent = action # 交易类型 trade_type = TradeType(int(action_type * len(TradeType))) # 获取合约的当前价格 current_price = self._exchange.current_price(symbol=self._instrument) # 获取基准合约价格精度(例如USDT) base_precision = self._exchange.base_precision # 获取交易合约价格精度(例如BTC) instrument_precision = self._exchange.instrument_precision # 当前持有的 # 获取交易合约当前持仓数量 amount = self._exchange.instrument_balance(self._instrument) price = current_price # 市价买入/限价买入 if trade_type is TradeType.MARKET_BUY or trade_type is TradeType.LIMIT_BUY: # 滑点调整=》价格 price_adjustment = 1 + (self.max_allowed_slippage_percent / 100) # 精度修正=》价格 price = max( round(current_price * price_adjustment, base_precision), base_precision) # 账号基准净值 * 仓位比例 / 价格 =》 修正 =》 买入交易合约数量 amount = round( self._exchange.balance * 0.99 * trade_amount_percent / price, instrument_precision) # 市价卖出/限价卖出 elif trade_type is TradeType.MARKET_SELL or trade_type is TradeType.LIMIT_SELL: # 滑点调整=》价格 price_adjustment = 1 - (self.max_allowed_slippage_percent / 100) # 精度修正=》价格 price = round(current_price * price_adjustment, base_precision) # 交易合约当前持仓数量 amount_held = self._exchange.portfolio.get(self._instrument, 0) # 持仓数量 * 仓位比例 => 修正 =》 卖出交易合约数量 amount = round(amount_held * trade_amount_percent, instrument_precision) # 交易合约,交易类型,交易数量,交易价格 =》 Trade 对象 return Trade(current_step, self._instrument, trade_type, amount, price)
def get_trade(self, action: TradeActionUnion) -> Trade: """The trade type is determined by `action % len(TradeType)`, and the trade amount is determined by the multiplicity of the action. For example: 0 = HOLD 1 = LIMIT_BUY|0.25 2 = MARKET_BUY|0.25 5 = HOLD 6 = LIMIT_BUY|0.5 7 = MARKET_BUY|0.5 etc. """ product_idx = int(action / self._actions_per_instrument) product = self._products[product_idx] n_splits = int(self._actions_per_instrument / len(TradeType)) trade_type = TradeType(action % len(TradeType)) trade_amount = int(action / len(TradeType)) * \ float(1 / n_splits) + (1 / n_splits) trade_amount = trade_amount - product_idx current_price = self._exchange.current_price(symbol=product) base_precision = self._exchange.base_precision instrument_precision = self._exchange.instrument_precision amount = self._exchange.instrument_balance(product) price = current_price if trade_type is TradeType.MARKET_BUY or trade_type is TradeType.LIMIT_BUY: price_adjustment = 1 + (self._max_allowed_slippage_percent / 100) price = max( round(current_price * price_adjustment, base_precision), base_precision) amount = round( self._exchange.balance * 0.99 * trade_amount / price, instrument_precision) elif trade_type is TradeType.MARKET_SELL or trade_type is TradeType.LIMIT_SELL: price_adjustment = 1 - (self._max_allowed_slippage_percent / 100) price = round(current_price * price_adjustment, base_precision) amount_held = self._exchange.portfolio.get(product, 0) amount = round(amount_held * trade_amount, instrument_precision) return Trade(product, trade_type, amount, price)
def get_trade(self, action: TradeActionUnion) -> Trade: """The trade type is determined by `action % len(TradeType)`, and the trade amount is determined by the multiplicity of the action. For example, 1 = LIMIT_BUY|0.25, 2 = MARKET_BUY|0.25, 6 = LIMIT_BUY|0.5, 7 = MARKET_BUY|0.5, etc. """ #print('action is' + str(action)) n_splits = self.n_actions / len(TradeType) #4 = 20 / 5 trade_type = TradeType(action % len(TradeType)) #action 除以 TradeType 的 余数? action = 10 trade_amount = int(action / len(TradeType)) * float(1 / n_splits) + ( 1 / n_splits) #int(10 / 5) * (1/4) + (1/4) = 0.75 #这个应该是指买入的percent? current_price = self._exchange.current_price( symbol=self.instrument_symbol) base_precision = self._exchange.base_precision instrument_precision = self._exchange.instrument_precision amount = self._exchange.instrument_balance(self.instrument_symbol) #现在持有的量 price = current_price if trade_type is TradeType.MARKET_BUY or trade_type is TradeType.LIMIT_BUY: price_adjustment = 1 + (self.max_allowed_slippage_percent / 100) price = max( round(current_price * price_adjustment, base_precision), base_precision) amount = round( self._exchange.balance * 0.99 * trade_amount / price, instrument_precision) #99%的balance * 0.75 / 价格, 意思就是交易量是75%的现金 elif trade_type is TradeType.MARKET_SELL or trade_type is TradeType.LIMIT_SELL: price_adjustment = 1 - (self.max_allowed_slippage_percent / 100) price = round(current_price * price_adjustment, base_precision) amount_held = self._exchange.portfolio.get(self.instrument_symbol, 0) amount = round(amount_held * trade_amount, instrument_precision) return Trade(self.instrument_symbol, trade_type, amount, price)
def get_trade(self, action: TradeActionUnion) -> Trade: trade_type = TradeType(action % len(TradeType)) trade_amount = float(1 / (action % self.n_bins + 1)) current_price = self._exchange.current_price(symbol=self.asset_symbol) base_precision = self._exchange.base_precision asset_precision = self._exchange.asset_precision amount = 0 price = current_price if trade_type is TradeType.MARKET_BUY or trade_type is TradeType.LIMIT_BUY: price_adjustment = 1 + (self.max_allowed_slippage_percent / 100) price = round(current_price * price_adjustment, base_precision) amount = round(self._exchange.balance * trade_amount / price, asset_precision) elif trade_type is TradeType.MARKET_SELL or trade_type is TradeType.LIMIT_SELL: price_adjustment = 1 - (self.max_allowed_slippage_percent / 100) price = round(current_price * price_adjustment, base_precision) amount_held = self._exchange.portfolio.get(self.asset_symbol, 0) amount = round(amount_held * trade_amount, asset_precision) return Trade(self.asset_symbol, trade_type, amount, price)