kilroy 發表於 14-7-1 09:25
跟 paper account 無關
跟 ibc.placeorder... 有關
我剛看了一下自己的code
並回頭看了一下69樓 osdak 大寫的程式
與他發生的問題會一直下 sell 跟 buy
因為我也有發生
我大概知道問題在那了
想跟k 大您討論一下
如何改比較好
以下貼出我的程式碼
我將問題的點 MARK 起來
問題出在 ClearID 沒有set 值
因為平倉是用
ibc.CloseAllOpenPositions( ContractMonth );
來做因此並沒有像建立多單或空單的倉位
是用
BuyOrderID = ibc.PlaceOrder(ContractMonth, "BUY", Shares, "MKT", 0, 0, "DAY", True);
或
SellORderID = ibc.PlaceOrder(ContractMonth, "SELL", Shares, "MKT", 0, 0, "DAY", True);
由於沒有set ClearID 的值
因此
if ( ClearTrigger AND ClearID == "" )
這個判斷式會一直成立因而導致會一直送單
要解決可能要將
sell 跟 cover 各寫成一個ID
然後仿照做多做空一樣寫
不知我的想法對嗎?
_SECTION_BEGIN( "IB Controller" );
{
ibc = GetTradingInterface( "IB" );
BuyTrigger = LastValue( Buy );
SellTrigger = LastValue( Short );
Clear = lastvalue( sell or cover );
ClearTrigger = LastValue( Clear );
Reset = ParamTrigger( "Reset All", "RESET" );
PrevTN = StaticVarGet( "TimeNumber" + Name() );
TN = LastValue( TimeNum() );
NewBar = TN != PrevTN;
StaticVarSet( "TimeNumber" + Name(), TN );
BuyOrderID = StaticVarGetText( "BuyOrderID" + Name() );
SellOrderID = StaticVarGetText( "SellOrderID" + Name() );
ClearID = StaticVarGet( "ClearID" + Name() );
BuyPending = ibc.IsOrderPending( BuyOrderID );
SellPending = ibc.IsOrderPending( SellOrderID );
ClearPending = ibc.IsOrderPending( ClearID );
Shares = 1;
if ( NewBar )
{
if ( NOT BuyPending )
{
StaticVarSetText( "BuyOrderID" + Name(), "" );
}
if ( NOT SellPending )
{
StaticVarSetText( "SellOrderID" + Name(), "" );
}
if ( NOT ClearPending )
{
StaticVarSetText( "ClearID" + Name(), "" );
}
}
if ( BuyTrigger AND BuyOrderID == "" )
{
ibc.CloseAllOpenPositions( ContractMonth );
BuyOrderID = ibc.PlaceOrder(ContractMonth, "BUY", Shares, "MKT", 0, 0, "DAY", True);
StaticVarSetText( "BuyOrderID" + Name(), BuyOrderID, true );
}
else if ( SellTrigger AND SellOrderID == "" )
{
ibc.CloseAllOpenPositions( ContractMonth );
SellORderID = ibc.PlaceOrder(ContractMonth, "SELL", Shares, "MKT", 0, 0, "DAY", True);
StaticVarSetText( "SellOrderID" + Name(), SellOrderID, true );
}
else if ( ClearTrigger AND ClearID == "" )
{
ibc.CloseAllOpenPositions( ContractMonth );
StaticVarSetText( "ClearID" + Name(), ClearID );
}
else if ( Reset )
{
StaticVarSetText( "BuyOrderID" + Name(), "" );
if ( BuyPending )
{
ibc.CancelOrder( BuyOrderID );
}
StaticVarSetText( "SellOrderID" + Name(), "" );
if ( SellPending )
{
ibc.CancelOrder( SellOrderID );
}
}
//LastTWSMsg = ibc.getLastError( 0 );
}
|