def send_order(self, code, amount, time, towards, price, order_model, amount_model): """[summary] Arguments: code {[type]} -- [description] amount {[type]} -- [description] time {[type]} -- [description] towards {[type]} -- [description] price {[type]} -- [description] order_model {[type]} -- [description] amount_model {[type]} -- [description] Returns: [type] -- [description] """ flag = False date = str(time)[0:10] if len(str(time)) == 19 else str(time) time = str(time) if len(str(time)) == 19 else '{} 09:31:00'.format( str(time)[0:10]) amount = amount if amount_model is AMOUNT_MODEL.BY_AMOUNT else int( amount / price) if self.market_type is MARKET_TYPE.STOCK_CN: amount = int(amount / 100) * 100 marketvalue = amount * price if amount_model is AMOUNT_MODEL.BY_AMOUNT else amount amount_model = AMOUNT_MODEL.BY_AMOUNT if int(towards) > 0: # 是买入的情况(包括买入.买开.买平) if self.cash_available >= marketvalue: self.cash_available -= marketvalue flag = True elif int(towards) < 0: if self.allow_sellopen: flag = True if self.sell_available.get(code, 0) >= amount: self.sell_available[code] -= amount flag = True if flag and amount > 0: return QA_Order(user_cookie=self.user_cookie, strategy=self.strategy_name, frequence=self.frequence, account_cookie=self.account_cookie, code=code, market_type=self.market_type, date=date, datetime=time, sending_time=time, callback=self.receive_deal, amount=amount, price=price, order_model=order_model, towards=towards, amount_model=amount_model) # init else: return flag
def send_order(self, code=None, amount=None, time=None, towards=None, price=None, money=None, order_model=None, amount_model=None, *args, **kwargs): """ ATTENTION CHANGELOG 1.0.28 修改了Account的send_order方法, 区分按数量下单和按金额下单两种方式 - AMOUNT_MODEL.BY_PRICE ==> AMOUNT_MODEL.BY_MONEY # 按金额下单 - AMOUNT_MODEL.BY_AMOUNT # 按数量下单 在按金额下单的时候,应给予 money参数 在按数量下单的时候,应给予 amount参数 python code: Account=QA.QA_Account() Order_bymoney=Account.send_order(code='000001', price=11, money=0.3*Account.cash_available, time='2018-05-09', towards=QA.ORDER_DIRECTION.BUY, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_MONEY ) Order_byamount=Account.send_order(code='000001', price=11, amount=100, time='2018-05-09', towards=QA.ORDER_DIRECTION.BUY, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT ) :param code: 证券代码 :param amount: 买卖 数量多数股 :param time: Timestamp 对象 下单时间 :param towards: int , towards>0 买入 towards<0 卖出 :param price: 买入,卖出 标的证券的价格 :param money: 买卖 价格 :param order_model: 类型 QA.ORDER_MODE :param amount_model:类型 QA.AMOUNT_MODEL :return: """ assert code is not None and time is not None and towards is not None and order_model is not None and amount_model is not None # 🛠todo 移到Utils类中, 时间转换 # date 字符串 2011-10-11 长度10 date = str(time)[0:10] if len(str(time)) == 19 else str(time) # time 字符串 20011-10-11 09:02:00 长度 19 time = str(time) if len(str(time)) == 19 else '{} 09:31:00'.format( str(time)[0:10]) # 🛠todo 移到Utils类中, amount_to_money 成交量转金额 # BY_MONEY :: amount --钱 如10000元 因此 by_money里面 需要指定价格,来计算实际的股票数 # by_amount :: amount --股数 如10000股 # amount = amount if amount_model is AMOUNT_MODEL.BY_AMOUNT else int( # money / (price*(1+self.commission_coeff))) amount = amount if amount_model is AMOUNT_MODEL.BY_AMOUNT else int( money / (price * (1 + self.commission_coeff)) / 100) * 100 # 🛠todo 移到Utils类中, money_to_amount 金额转成交量 money = amount * price * \ (1+self.commission_coeff) if amount_model is AMOUNT_MODEL.BY_AMOUNT else money # amount_model = AMOUNT_MODEL.BY_AMOUNT # flag 判断买卖 数量和价格以及买卖方向是否正确 flag = False assert (int(towards) != 0) if int(towards) in [1, 2, 3]: # 是买入的情况(包括买入.买开.买平) if self.cash_available >= money: if self.market_type is MARKET_TYPE.STOCK_CN: # 如果是股票 买入的时候有100股的最小限制 amount = int(amount / 100) * 100 if self.running_environment == RUNNING_ENVIRONMENT.TZERO: if self.buy_available.get(code, 0) >= amount: flag = True self.cash_available -= money self.buy_available[code] -= amount else: flag = False print('T0交易买入超出限额') else: self.cash_available -= money flag = True else: # 如果有负持仓-- 允许卖空的时候 if self.allow_sellopen and towards == 3: # 多平 _hold = self.sell_available.get(code, 0) left_amount = amount + _hold if _hold < 0 else amount _money = float(left_amount * price + amount * price * self.commission_coeff) if self.cash_available >= _money: self.cash_available -= _money flag = True else: print( 'QAACCOUNT: 可用资金不足 cash_available {} code {} time {} amount {} towards {}' .format(self.cash_available, code, time, amount, towards)) elif int(towards) in [-1, -2, -3]: # 是卖出的情况(包括卖出,卖出开仓allow_sellopen如果允许. 卖出平仓) # print(self.sell_available[code]) _hold = self.sell_available.get(code, 0) # _hold 是你的持仓 # 如果你的hold> amount>0 # 持仓数量>卖出数量 if _hold >= amount: self.sell_available[code] -= amount #towards = ORDER_DIRECTION.SELL flag = True # 如果持仓数量<卖出数量 else: # 如果是允许卖空开仓 实际计算时 先减去持仓(正持仓) 再计算 负持仓 就按原先的占用金额计算 if self.allow_sellopen: # left_amount = amount-_hold if _hold > 0 else amount # 如果仓位是反的 # _money = float(left_amount * price + amount * # price*self.commission_coeff) if towards == -2: # 卖开 if self.cash_available >= money: # 卖空的市值小于现金(有担保的卖空), 不允许裸卖空 #self.cash_available -= money flag = True else: print('sellavailable', _hold) print('amount', amount) print('aqureMoney', money) print('cash', self.cash_available) print("卖空资金不足/不允许裸卖空") # else: # print('资金股份不足/不允许卖空开仓') if flag and amount > 0: _order = QA_Order(user_cookie=self.user_cookie, strategy=self.strategy_name, frequence=self.frequence, account_cookie=self.account_cookie, code=code, market_type=self.market_type, date=date, datetime=time, sending_time=time, callback=self.receive_deal, amount=amount, price=price, order_model=order_model, towards=towards, money=money, amount_model=amount_model, commission_coeff=self.commission_coeff, tax_coeff=self.tax_coeff, *args, **kwargs) # init # 历史委托order状态存储, 保存到 QA_Order 对象中的队列中 self.datetime = time self.orders.insert_order(_order) return _order else: print('ERROR : CODE {} TIME {} AMOUNT {} TOWARDS {}'.format( code, time, amount, towards)) return False
def send_order(self, code=None, amount=None, time=None, towards=None, price=None, money=None, order_model=None, amount_model=None): """ ATTENTION CHANGELOG 1.0.28 修改了Account的send_order方法, 区分按数量下单和按金额下单两种方式 - AMOUNT_MODEL.BY_PRICE ==> AMOUNT_MODEL.BY_MONEY # 按金额下单 - AMOUNT_MODEL.BY_AMOUNT # 按数量下单 在按金额下单的时候,应给予 money参数 在按数量下单的时候,应给予 amount参数 python code: Account=QA.QA_Account() Order_bymoney=Account.send_order(code='000001', price=11, money=0.3*Account.cash_available, time='2018-05-09', towards=QA.ORDER_DIRECTION.BUY, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_MONEY ) Order_byamount=Account.send_order(code='000001', price=11, amount=100, time='2018-05-09', towards=QA.ORDER_DIRECTION.BUY, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT ) :param code: :param amount: :param time: :param towards: :param price: :param money: :param order_model: :param amount_model: :return: """ assert code is not None and time is not None and towards is not None and order_model is not None and amount_model is not None flag = False date = str(time)[0:10] if len(str(time)) == 19 else str(time) time = str(time) if len(str(time)) == 19 else '{} 09:31:00'.format( str(time)[0:10]) # BY_MONEY :: amount --钱 如10000元 因此 by_money里面 需要指定价格,来计算实际的股票数 # by_amount :: amount --股数 如10000股 amount = amount if amount_model is AMOUNT_MODEL.BY_AMOUNT else int( money / (price * (1 + self.commission_coeff))) money = amount * price * \ (1+self.commission_coeff) if amount_model is AMOUNT_MODEL.BY_AMOUNT else money # amount_model = AMOUNT_MODEL.BY_AMOUNT if int(towards) > 0: # 是买入的情况(包括买入.买开.买平) if self.cash_available >= money: self.cash_available -= money if self.market_type is MARKET_TYPE.STOCK_CN: # 如果是股票 买入的时候有100股的最小限制 amount = int(amount / 100) * 100 flag = True else: print('可用资金不足') elif int(towards) < 0: if self.sell_available.get(code, 0) >= amount: self.sell_available[code] -= amount flag = True elif self.allow_sellopen: if self.cash_available > money: # 卖空的市值小于现金 flag = True else: print('资金股份不足/不允许卖空开仓') if flag and amount > 0: _order = QA_Order(user_cookie=self.user_cookie, strategy=self.strategy_name, frequence=self.frequence, account_cookie=self.account_cookie, code=code, market_type=self.market_type, date=date, datetime=time, sending_time=time, callback=self.receive_deal, amount=amount, price=price, order_model=order_model, towards=towards, money=money, amount_model=amount_model, commission_coeff=self.commission_coeff, tax_coeff=self.tax_coeff) # init self.orders.insert_order(_order) # order状态存储 return _order else: print('ERROR : amount=0') return False
def send_order(self, code=None, amount=None, time=None, towards=None, price=None, money=None, order_model=None, amount_model=None): """ ATTENTION CHANGELOG 1.0.28 修改了Account的send_order方法, 区分按数量下单和按金额下单两种方式 - AMOUNT_MODEL.BY_PRICE ==> AMOUNT_MODEL.BY_MONEY # 按金额下单 - AMOUNT_MODEL.BY_AMOUNT # 按数量下单 在按金额下单的时候,应给予 money参数 在按数量下单的时候,应给予 amount参数 python code: Account=QA.QA_Account() Order_bymoney=Account.send_order(code='000001', price=11, money=0.3*Account.cash_available, time='2018-05-09', towards=QA.ORDER_DIRECTION.BUY, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_MONEY ) Order_byamount=Account.send_order(code='000001', price=11, amount=100, time='2018-05-09', towards=QA.ORDER_DIRECTION.BUY, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT ) :param code: 证券代码 :param amount: 买卖 数量多数股 :param time: Timestamp 对象 下单时间 :param towards: int , towards>0 买入 towards<0 卖出 :param price: 买入,卖出 标的证券的价格 :param money: 买卖 价格 :param order_model: 类型 QA.ORDER_MODE :param amount_model:类型 QA.AMOUNT_MODEL :return: """ assert code is not None and time is not None and towards is not None and order_model is not None and amount_model is not None # 🛠todo 移到Utils类中, 时间转换 # date 字符串 2011-10-11 长度10 date = str(time)[0:10] if len(str(time)) == 19 else str(time) # time 字符串 20011-10-11 09:02:00 长度 19 time = str(time) if len(str(time)) == 19 else '{} 09:31:00'.format( str(time)[0:10]) # 🛠todo 移到Utils类中, amount_to_money 成交量转金额 # BY_MONEY :: amount --钱 如10000元 因此 by_money里面 需要指定价格,来计算实际的股票数 # by_amount :: amount --股数 如10000股 amount = amount if amount_model is AMOUNT_MODEL.BY_AMOUNT else int( money / (price * (1 + self.commission_coeff))) # 🛠todo 移到Utils类中, money_to_amount 金额转成交量 money = amount * price * \ (1+self.commission_coeff) if amount_model is AMOUNT_MODEL.BY_AMOUNT else money # amount_model = AMOUNT_MODEL.BY_AMOUNT # flag 判断买卖 数量和价格以及买卖方向是否正确 flag = False assert (int(towards) != 0) if int(towards) > 0: # 是买入的情况(包括买入.买开.买平) if self.cash_available >= money: if self.market_type is MARKET_TYPE.STOCK_CN: # 如果是股票 买入的时候有100股的最小限制 amount = int(amount / 100) * 100 if self.running_environment == RUNNING_ENVIRONMENT.TZERO: if self.buy_available.get(code, 0) >= amount: flag = True self.cash_available -= money self.buy_available[code] -= amount else: flag = False print('T0交易买入超出限额') else: self.cash_available -= money flag = True else: print('可用资金不足 {} {} {} {}'.format(code, time, amount, towards)) elif int(towards) < 0: # 是卖出的情况(包括卖出,卖出开仓allow_sellopen如果允许. 卖出平仓) #print(self.sell_available[code]) if self.sell_available.get(code, 0) >= amount: self.sell_available[code] -= amount flag = True elif self.allow_sellopen: if self.cash_available > money: # 卖空的市值小于现金(有担保的卖空), 不允许裸卖空 flag = True else: print("卖空资金不足/不允许裸卖空") else: print('资金股份不足/不允许卖空开仓') if flag and amount > 0: _order = QA_Order(user_cookie=self.user_cookie, strategy=self.strategy_name, frequence=self.frequence, account_cookie=self.account_cookie, code=code, market_type=self.market_type, date=date, datetime=time, sending_time=time, callback=self.receive_deal, amount=amount, price=price, order_model=order_model, towards=towards, money=money, amount_model=amount_model, commission_coeff=self.commission_coeff, tax_coeff=self.tax_coeff) # init # 历史委托order状态存储, 保存到 QA_Order 对象中的队列中 self.datetime = time self.orders.insert_order(_order) return _order else: print('ERROR : amount=0 {} {} {} {}'.format( code, time, amount, towards)) return False
def send_order(self, code=None, amount=None, time=None, towards=None, price=None, money=None, order_model=None, amount_model=None): """[summary] Arguments: code {[type]} -- [description] amount {[type]} -- [description] time {[type]} -- [description] towards {[type]} -- [description] price {[type]} -- [description] order_model {[type]} -- [description] amount_model {[type]} -- [description] Returns: [type] -- [description] ATTENTION CHANGELOG 1.0.28 修改了Account的send_order方法, 区分按数量下单和按金额下单两种方式 - AMOUNT_MODEL.BY_PRICE ==> AMOUNT_MODEL.BY_MONEY # 按金额下单 - AMOUNT_MODEL.BY_AMOUNT # 按数量下单 在按金额下单的时候,应给予 money参数 在按数量下单的时候,应给予 amount参数 ```python Account=QA.QA_Account() Order_bymoney=Account.send_order(code='000001', price=11, money=0.3*Account.cash_available, time='2018-05-09', towards=QA.ORDER_DIRECTION.BUY, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_MONEY ) Order_byamount=Account.send_order(code='000001', price=11, amount=100, time='2018-05-09', towards=QA.ORDER_DIRECTION.BUY, order_model=QA.ORDER_MODEL.MARKET, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT ) ``` """ assert code is not None and time is not None and towards is not None and order_model is not None and amount_model is not None flag = False date = str(time)[0:10] if len(str(time)) == 19 else str(time) time = str(time) if len(str(time)) == 19 else '{} 09:31:00'.format( str(time)[0:10]) # BY_MONEY :: amount --钱 如10000元 因此 by_money里面 需要指定价格,来计算实际的股票数 # by_amount :: amount --股数 如10000股 amount = amount if amount_model is AMOUNT_MODEL.BY_AMOUNT else int( money / (price * (1 + self.commission_coeff))) if self.market_type is MARKET_TYPE.STOCK_CN: amount = int(amount / 100) * 100 money = amount * price * (1 + self.commission_coeff) amount_model = AMOUNT_MODEL.BY_AMOUNT if int(towards) > 0: # 是买入的情况(包括买入.买开.买平) if self.cash_available >= money: self.cash_available -= money flag = True elif int(towards) < 0: if self.allow_sellopen: flag = True if self.sell_available.get(code, 0) >= amount: self.sell_available[code] -= amount flag = True if flag and amount > 0: return QA_Order(user_cookie=self.user_cookie, strategy=self.strategy_name, frequence=self.frequence, account_cookie=self.account_cookie, code=code, market_type=self.market_type, date=date, datetime=time, sending_time=time, callback=self.receive_deal, amount=amount, price=price, order_model=order_model, towards=towards, amount_model=amount_model) # init else: return False