本帖最後由 skyler 於 14-7-25 18:17 編輯
jacklcl 發表於 14-7-25 16:41
S大, 這個Code你能用嗎
我試過還是會有重複下單的問題
這次不是連續Buy及Sell
我貼一下我自己闗於IB的那段程式碼
你參考看看
_SECTION_BEGIN( "IB Controller" );
{
ibc = GetTradingInterface( "IB" );
BuyTrigger = LastValue( Buy );
SellTrigger = LastValue( Sell );
ShortTrigger = LastValue( Short );
CoverTrigger = LastValue( Cover );
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() );
ShortOrderID = StaticVarGetText( "ShortOrderID" + Name() );
CoverOrderID = StaticVarGetText( "CoverOrderID" + Name() );
BuyPending = ibc.IsOrderPending( BuyOrderID );
SellPending = ibc.IsOrderPending( SellOrderID );
ShortPending = ibc.IsOrderPending( ShortOrderID );
CoverPending = ibc.IsOrderPending( CoverOrderID );
Shares = 1;
if ( NewBar )
{
if ( NOT BuyPending )
{
StaticVarSetText( "BuyOrderID" + Name(), "" );
}
if ( NOT SellPending )
{
StaticVarSetText( "SellOrderID" + Name(), "" );
}
if ( NOT ShortPending )
{
StaticVarSetText( "ShortOrderID" + Name(), "" );
}
if ( NOT CoverPending )
{
StaticVarSetText( "CoverOrderID" + 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 == "" AND ibc.GetPositionSize( ContractMonth ) > 0 )
{
SellORderID = ibc.PlaceOrder(ContractMonth, "SELL", Shares, "MKT", 0, 0, "DAY", True);
StaticVarSetText( "SellOrderID" + Name(), SellOrderID, true );
}
else if ( ShortTrigger AND ShortOrderID == "" )
{
ibc.CloseAllOpenPositions( ContractMonth );
ShortOrderID = ibc.PlaceOrder(ContractMonth, "SELL", Shares, "MKT", 0, 0, "DAY", True);
StaticVarSetText( "ShortOrderID" + Name(), ShortOrderID, true );
}
else if ( CoverTrigger AND CoverOrderID == "" AND ibc.GetPositionSize( ContractMonth ) < 0 )
{
CoverOrderID = ibc.PlaceOrder(ContractMonth, "BUY", Shares, "MKT", 0, 0, "DAY", True);
StaticVarSetText( "CoverOrderID" + Name(), CoverOrderID, true );
}
else if ( Reset )
{
StaticVarSetText( "BuyOrderID" + Name(), "" );
if ( BuyPending )
{
ibc.CancelOrder( BuyOrderID );
}
StaticVarSetText( "SellOrderID" + Name(), "" );
if ( SellPending )
{
ibc.CancelOrder( SellOrderID );
}
StaticVarSetText( "ShortOrderID" + Name(), "" );
if ( ShortPending )
{
ibc.CancelOrder( ShortOrderID );
}
StaticVarSetText( "CoverOrderID" + Name(), "" );
if ( CoverPending )
{
ibc.CancelOrder( CoverOrderID );
}
}
} |