hi, 謝大大,
還有一個問題, 我的buy/sell/short/cover都已經設定好, 也加了buy=exrem(buy,sell).....等語句, 於回測中也是沒問題, 但奇怪是, 一接上IB controller, 自動實時下單, 他要是就一大堆buy, 要是就一大堆sell, 我paper trade account的錢像流水一樣....
我下單的部份就是根據大大之前給的再組合你後來教的cleartrigger.....
{
ibc = GetTradingInterface("IB");
BuyTrigger = LastValue(Buy);
SellTrigger = LastValue(Short);
Clear = 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);
// BuyOrderID= ibc.ModifyOrder( BuyOrderID, ContractMonth, "Buy", Shares, "MKT", 0, 0, "Day", True);
StaticVarSetText("BuyOrderID"+Name(),BuyOrderID);
}
else if( SellTrigger AND SellOrderID == "" )
{
ibc.CloseAllOpenPositions(ContractMonth);
SellOrderID = ibc.PlaceOrder(ContractMonth, "SELL", Shares, "MKT", 0, 0, "DAY", True);
// SellORderID = ibc.ModifyOrder( SellORderID , ContractMonth, "Sell", Shares, "MKT", 0, 0, "Day", True);
StaticVarSetText("SellOrderID"+Name(),SellOrderID);
}
//sell or cover
else if( ClearTrigger AND ClearID == "")
{
ibc.CloseAllOpenPositions(ContractMonth);
StaticVarSetText("ClearID"+Name(), ClearID);
}
//sell or cover
else if( Reset )
{
StaticVarSetText("BuyOrderID"+Name(),"");
if( BuyPending ) ibc.CancelOrder( BuyOrderID );
StaticVarSetText("SellOrderID"+Name(),"");
if( SellPending ) ibc.CancelOrder( SellOrderID );
// ibc.CloseAllOpenPositions();
}
|