微信公众号:AlgoPlus
欢迎关注点赞转发收藏!
您的认可是我们最大的动力!
将AlgoPlus设为星标,及时获取最新研究成果!
提交申请
看穿式认证申请,除了提交个人信息之外,还需要提交以下信息:
1、接入模式。直连还是中继?如果交易相关指令从客户端先被发送到服务端,然后再被转发至期货公司交易前置,这种模式属于中继。没有负责中转的服务端,则属于直连模式。一般情况,自己开发自己使用的基本都属于直连模式。具体属于哪种模式,请以期货公司判定为准。
2、AppID,是你给自己的程序起的名字。格式为:client_不超过10个字符的名字_不超过8个字符的版本号,例如:client_pikachu_1.0。
申请通过后,你会得到一枚授权码,这是期货公司的通行口令。每次接入时都需要同时提供名字和口令,并且与系统留底匹配上才允许进行后续操作,否则后果很严重!什么严重的后果?被拒之门外。
除了授权码,期货公司还会给你用于测试的服务器地址和仿真账号(有期货公司让你自己注册)。新注册账号,一般都需要修改密码之后才能正常交易。如果还不习惯使用API,可以到期货公司官网下载仿真交易客户端修改。我们会给出使用API修改密码的例子。
仿真交易
有些期货公司要求较少,有些期货公司要求则比较多。下面的例子是按照最多的要求情况编写的。安装AlgoPlus后,运行这个范例,将对应结果截图发给期货公司,一定能通过认证。
另外需要提醒大家的是,仿真行情与模拟行情不同,大家可以先通过仿真客户端找到比较活跃的行情进行测试,以免因没有行情导致无法完成仿真交易。
文件目录:
\AlgoPlus\exemplification\8客户端认证\req_authenticate.py
源代码:*
# -*- coding: utf-8 -*-
from AlgoPlus.CTP.TraderApi import TraderApi
from AlgoPlus.CTP.ApiStruct import *
import time
class TraderEngine(TraderApi):
def __init__(self, td_server, broker_id, investor_id, password, app_id, auth_code, md_queue=None
, page_dir='', private_resume_type=2, public_resume_type=2):
self.order_ref = 0
self.Join()
# 撤单
def withdraw(self, exchange_ID, instrument_id, order_ref, order_sysid=''):
pOrderAction = InputOrderActionField(
BrokerID=self.broker_id,
InvestorID=self.investor_id,
UserID=self.investor_id,
ExchangeID=exchange_ID,
ActionFlag="0",
InstrumentID=instrument_id,
FrontID=self.front_id,
SessionID=self.session_id,
OrderSysID=order_sysid,
OrderRef=str(order_ref),
)
l_retVal = self.ReqOrderAction(pOrderAction)
# 报单
def insert_order(self, exchange_ID, instrument_id, order_price, order_vol, order_ref, direction, offset_flag):
pBuyOpen = InputOrderField(
BrokerID=self.broker_id,
InvestorID=self.investor_id,
ExchangeID=exchange_ID,
InstrumentID=instrument_id,
UserID=self.investor_id,
OrderPriceType="2",
Direction=direction,
CombOffsetFlag=offset_flag,
CombHedgeFlag="1",
LimitPrice=order_price,
VolumeTotalOriginal=order_vol,
TimeCondition="3",
VolumeCondition="1",
MinVolume=1,
ContingentCondition="1",
StopPrice=0,
ForceCloseReason="0",
IsAutoSuspend=0,
OrderRef=str(order_ref),
)
l_retVal = self.ReqOrderInsert(pBuyOpen)
# 买开仓
def buy_open(self, exchange_ID, instrument_id, order_price, order_vol, order_ref):
self.insert_order(exchange_ID, instrument_id, order_price, order_vol, order_ref, '0', '0')
# 卖开仓
def sell_open(self, exchange_ID, instrument_id, order_price, order_vol, order_ref):
self.insert_order(exchange_ID, instrument_id, order_price, order_vol, order_ref, '1', '0')
# 买平仓
def buy_close(self, exchange_ID, instrument_id, order_price, order_vol, order_ref):
if exchange_ID == "SHFE" or exchange_ID == "INE":
self.insert_order(exchange_ID, instrument_id, order_price, order_vol, order_ref, '0', '3')
else:
self.insert_order(exchange_ID, instrument_id, order_price, order_vol, order_ref, '0', '1')
# 卖平仓
def sell_close(self, exchange_ID, instrument_id, order_price, order_vol, order_ref):
if exchange_ID == "SHFE" or exchange_ID == "INE":
self.insert_order(exchange_ID, instrument_id, order_price, order_vol, order_ref, '1', '3')
else:
self.insert_order(exchange_ID, instrument_id, order_price, order_vol, order_ref, '1', '1')
def Join(self):
while True:
if self.status >= 0:
# ############################################################################# #
# 确认结算单
req_settlement_infoConfirm = SettlementInfoConfirmField(BrokerID=self.broker_id,
InvestorID=self.investor_id)
self.ReqSettlementInfoConfirm(req_settlement_infoConfirm)
self._write_log(f"=>发出确认结算单请求!")
time.sleep(3)
# ############################################################################# #
# 连续5次买开 - 卖平
ikk = 0
while ikk < 5:
ikk += 1
self.order_ref += 1
self.buy_open(test_exchange_id, test_instrument_id, test_raise_limited, test_vol, self.order_ref)
self._write_log(f"=>{ikk}=>发出涨停买开仓请求!")
time.sleep(1)
# 跌停卖平仓
self.order_ref += 1
self.sell_close(test_exchange_id, test_instrument_id, test_fall_limited, test_vol, self.order_ref)
self._write_log(f"=>发出跌停卖平仓请求!")
# ############################################################################# #
# 连续5次卖开 - 买平
ikk = 0
while ikk < 5:
# 跌停卖开仓
self.order_ref += 1
self.sell_open(test_exchange_id, test_instrument_id, test_fall_limited, test_vol, self.order_ref)
self._write_log(f"=>{ikk}=>发出跌停卖平仓请求!")
time.sleep(1)
# 涨停买平仓
self.order_ref += 1
self.buy_close(test_exchange_id, test_instrument_id, test_raise_limited, test_vol, self.order_ref)
self._write_log(f"=>发出涨停买平仓请求!")
ikk += 1
# ############################################################################# #
# 买开 - 撤单
self.order_ref += 1
self.buy_open(test_exchange_id, test_instrument_id, test_fall_limited, test_vol, self.order_ref)
self._write_log(f"=>发出涨停买开仓请求!")
time.sleep(1)
# 撤单
self.withdraw(test_exchange_id, test_instrument_id, self.order_ref)
self._write_log(f"=>发出撤单请求!")
# ############################################################################# #
# 卖开 - 撤单
self.order_ref += 1
self.sell_open(test_exchange_id, test_instrument_id, test_raise_limited, test_vol, self.order_ref)
self._write_log(f"=>发出跌停卖平仓请求!")
time.sleep(1)
# 撤单
self.withdraw(test_exchange_id, test_instrument_id, self.order_ref)
self._write_log(f"=>发出撤单请求!")
# ############################################################################# #
# 查询订单
qry_order_field = QryOrderField(BrokerID=self.broker_id,
InvestorID=self.investor_id)
self.ReqQryOrder(qry_order_field)
self._write_log(f"=>发出查询订单请求!")
time.sleep(3)
# ############################################################################# #
# 查询资金
qry_trading_account_field = QryTradingAccountField(BrokerID=self.broker_id,
AccountID=self.investor_id,
CurrencyID="CNY",
BizType="1")
self.ReqQryTradingAccount(qry_trading_account_field)
self._write_log(f"=>发出查询资金请求!")
time.sleep(3)
# ############################################################################# #
# 查询成交
qry_trade_field = QryTradeField(BrokerID=self.broker_id,
InvestorID=self.investor_id)
self.ReqQryTrade(qry_trade_field)
self._write_log(f"=>发出查询成交请求!")
time.sleep(3)
# ############################################################################# #
# 查询持仓
qry_investor_position_field = QryInvestorPositionField(BrokerID=self.broker_id,
InvestorID=self.investor_id)
self.ReqQryInvestorPosition(qry_investor_position_field)
self._write_log(f"=>发出查询持仓请求!")
# ############################################################################# #
# 查询资金
qry_trading_account_field = QryTradingAccountField(BrokerID=self.broker_id,
AccountID=self.investor_id,
CurrencyID="CNY",
BizType="1")
self.ReqQryTradingAccount(qry_trading_account_field)
self._write_log(f"=>发出查询资金请求!")
time.sleep(3)
# ############################################################################# #
print("老爷,看穿式监管认证仿真交易已经完成!请截图联系期货公司!")
break
time.sleep(1)
# ############################################################################# #
# 请在这里填写需要测试的合约数据
# 警告:该例子只支持上期所品种平今仓测试
test_exchange_id = 'SHFE' # 交易所
test_instrument_id = 'rb2001' # 合约代码
test_raise_limited = 3708 # 涨停板
test_fall_limited = 3159 # 跌停板
test_vol = 1 # 报单手数
if __name__ == "__main__":
import sys
sys.path.append("..")
from account_info import my_future_account_info_dict
future_account = my_future_account_info_dict['SimNow24']
ctp_trader = TraderEngine(future_account.server_dict['TDServer']
, future_account.broker_id
, future_account.investor_id
, future_account.password
, future_account.app_id
, future_account.auth_code
, None
, future_account.td_page_dir)
说明:
1、account_info请参考《《AlgoPlus使用手册》之全天候模拟测试》。
2、在仿真客户端软件选择活跃的品种设置196-200行参数。