MBT 开发介绍(自动化交易)---原创
使用版本是使用MBT Navigator SDK - ver 11.1.0.2编程语言是 VC++
占个位置后面不断更新
----------------------------------------------------------------
首先解释一下什么叫SDK,SDK的全称叫Software Development Kit,中文是软件开发工具,简单理解就是 你可以通过MBT公司提供的SDK,不用通过登陆MBT给你的软件,而是用你自己写的程序就可以操作你的帐户,比如买入卖出,挂各种订单,撤销订单,等等。
他的运用只要是用于程序化交易,比如我设定 10天均线从下向上交叉26天均线交叉时买入,反之卖出,当然人不可能24小时看盘,所以当条件满足时,可以让电脑自动带你下单,入场离场等等。 呵呵~~
MBT 的COM 对象有MbtCom ,MbtQuotes ,MbtHist 和MbtOrders .
MbtCom :主要是负责平台的用户名,密码的登陆,一些平台的基本信息,包括平台的警报,帐户字串等等基本信息,位于MBTSDK的顶层,是一个单体类。
MBTQuote :提供了连接报价服务器的接口,并且可以查询到需要查询品种的当前BID,ASK价格,当然LEVEL 2报价接口也封装在这个类里面。
MbtHist :提供查询历史数据的接口
MbtOrders :这个比较复杂,有你的帐户的各项详细信息,你现在手上持有的单子(头寸),挂单,交易历史的信息,并且可以对这些单子进行操作的接口。
简单实例:
1。COM初始化
try // One great big try-catch block!
{
if( FAILED(hResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)) )
{
throw( (UINT)IDS_COINITFAILED );
}
else
{
bOLEinited = true;
printf( "CoInitializeEx succeeded!\n" );
}
AfxOleInit();
if( FAILED(m_pComMgr.CreateInstance (( __uuidof(MbtComMgr) )) ))
{
throw( (UINT)IDS_COMMGRFAILED );
}
else
{
printf( "MbtComMgr instantiation successful!\n" );
}
2。平台登陆
m_pComMgr->EnableSplash(VARIANT_FALSE);
// Disable all dialogs. IMPORTANT: If you do NOT supply a valid login, then
// this line will make the app fail with no error message. It's best to
// keep this line commented out.
// m_pComMgr->SilentMode=VARIANT_TRUE;
// Please enter your assigned SDK client ID into the Host ID below. You can
// also enter your Username / Password below to circumvent the Login dialog box.
long lHostId = 9;
_bstr_t bstrUserName = "";
_bstr_t bstrPassword = "";
_bstr_t bstrHostVersion = "";
if( m_pComMgr->DoLogin(lHostId, bstrUserName, bstrPassword, bstrHostVersion) )
{
printf("DoLogin() succeeded!\n");
}
else
{
throw( (UINT)IDS_DOLOGINFAILED );
}
3。获得MBT登陆帐户信息
printf("OrderClient -------------------------\n");
printf(" Remote address: %s\n", (char*)m_pOrderClient->RemoteAddress);
printf(" Remote port : %d\n", m_pOrderClient->RemotePort);
printf(" UserName : %s\n", (char*)m_pOrderClient->UserName);
m_pAccounts = m_pOrderClient->Accounts;
int iCount = m_pAccounts->Count;
printf("\n" );
printf("We have %d account(s) ----------------\n", iCount);
for( int i = 0; i < iCount; i++ )
{
m_pAccount = m_pAccounts->Item;
m_pAccount->Load();
printf(" %s - %s\n", (char*)m_pAccount->Account, (char*)m_pAccount->Customer);
}
m_pDefaultAccount = m_pAccounts->DefaultAccount;
printf("\n");
if( m_pDefaultAccount )
{
printf("DefaultAccount ----------------------\n");
printf(" Account : %s - %s\n",
(char*)m_pDefaultAccount->Account,
(char*)m_pDefaultAccount->Customer);
printf(" Current BP : %12.2f\n", m_pDefaultAccount->CurrentBP);
printf(" CurrentEquity : %12.2f\n", m_pDefaultAccount->CurrentEquity);
printf(" MorningBP : %12.2f\n", m_pDefaultAccount->MorningBP );
printf(" MorningCash : %12.2f\n", m_pDefaultAccount->MorningCash );
printf(" MorningEquity : %12.2f\n", m_pDefaultAccount->MorningEquity );
printf(" MornOvernightBP : %12.2f\n", m_pDefaultAccount->MornOvernightBP );
}
else
throw( (UINT)IDS_NODEFAULTACCOUNT );
m_pQuotes = m_pComMgr->Quotes;
printf("\n");
printf("Quotes ------------------------------\n");
printf(" Remote Address: %s\n", (char*)m_pQuotes->RemoteAddress );
printf(" Remote Port : %d\n", (char*)m_pQuotes->RemotePort );
printf(" Username : %s\n\n", (char*)m_pQuotes->UserName );
4。查询EUR/USD报价
CConsoleQuotes qn(m_pQuotes);
double dPrice;
printf("Advising symbol...\n");
if( qn.DoAdvise( _T("EUR/USD") ) )
{
static int iMaxCBs = 9; // callbacks limit
int iMaxTime = 9000; // milliseconds limit
printf("\n");
printf("Process callbacks until %dms timeout or %d callbacks:\n\n", iMaxTime, iMaxCBs);
// Hang out pump message loop subject to a timeout and a number of callbacks
// to get back a quote
DWORD dwStart = GetTickCount();
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
if( GetTickCount() > dwStart + iMaxTime ) // set a timeout
{
printf("Timeout hit\n\n");
break;
}
if( qn.GotQuote() )// if we have a callback...
{
// ...process it, reset our flag, and check max callback counter.
const CurrentQuote& qt = qn.GetLastQuote();
printf("%d.%sbid %gask %glast %gvol %dtick %s\n",
iMaxCBs, qt.sTime, qt.dBid, qt.dAsk, qt.dLast, qt.lVol, qt.eTick);
qn.SetQuoteWaiting();
dPrice = qt.dLast - 0.5;
iMaxCBs--;
if( 1 > iMaxCBs )
{
printf("Max callbacks received\n\n");
break;
}
}
}
}
else
throw( (UINT)IDS_DOADVISEFAILED );
5。下单买入EUR/USD
long lBuySell = VALUE_BUY;
long lVol = 10000;
CString sSym = "EUR/USD";
// Arbitrary price. Should be filled in with the
//last price for all Market and Stop orders
long lTIF = VALUE_GTC;
long lOrdType = VALUE_LIMIT;
long lVolType = VALUE_NORMAL;
CString sRoute = "MBTX";
BSTR bstrRetMsg = NULL; // NULL works, but still need to free below ::SysAllocString(L"");
CString sOrder;
sOrder.Format("Buy %d %s at $%.2f on %s\n", lVol, sSym, dPrice, sRoute);
printf(sOrder);
COleDateTime dDte;
VARIANT_BOOL bSubmit = m_pOrderClient->Submit(
lBuySell,
lVol,
_bstr_t(sSym),
dPrice,
0,
lTIF,
0,
lOrdType,
lVolType,
0,
m_pDefaultAccount,
_bstr_t(sRoute),
"", 0, 0, dDte, dDte, 0, 0, 0, 0, 0,
&bstrRetMsg
);
if( bSubmit )
{
printf("Order submission successful! bstrRetMsg = [%S]\n", bstrRetMsg); // wide string!
}
else
{
printf("Order submission failed! bstrRetMsg = [%S]\n", bstrRetMsg); // wide string!
}
::SysFreeString(bstrRetMsg);
}
catch( UINT sResID ) {
CString sMsg;
sMsg.LoadString(sResID);
printf("%s\n", sMsg);
}
6。卸载COM
m_pQuotes = NULL;
m_pDefaultAccount = NULL;
m_pAccount = NULL;
m_pAccounts = NULL;
m_pOrderClient = NULL;
m_pComMgr = NULL;
if( bOLEinited )
{
printf("Calling CoUninitialize()");
CoUninitialize();
}
printf("\nHit ENTER key to EXIT...");
--------------------------------------------------------------------
[ 本帖最后由 cww2 于 2009-3-14 21:33 编辑 ] 虽然有些不懂,收藏了 哦的娘啊,哈哈,找了这么长时间,终于有人肯写出来了啊!!!!!!!!!!
非常感谢
等我有了什么突破,来和你讨论啊,
殷切期待中 请问:
MBT等的仓位以加权平均价来计算
因此, 在MBT NAVIGATION里, 没有长单和短单区分
有没有可能把这个功能开发一下啊?
即可以每单为一个买卖计算
这样可以保留长单的情况下, 做短单 回cphelp:
用SDK可以做到同一品种分开单子,但是前提是方向要一样的~~~不能同时有同一品种的多头和空头~~
好东东
好东东,怎么用啊?[ 本帖最后由 gbb_pin119 于 2009-3-16 14:06 编辑 ] 是好东西。不错,我实验下。 我也来支持一下楼主! 好东东啊,LZ阿能搞一个定时,定方向的下单?
比如夜里11,12点,1点计息活跃时间,定时下一单,如果K线是阳,就做多,如果K线是阴,就做空,打一枪就跑,止盈止损啥的设一设,呵呵 刚发现此贴,楼主也是编程的高人啊
页:
[1]
2