|
本帖最後由 kilroy 於 14-6-1 22:53 編輯
greg 發表於 14-5-31 14:07
謝謝K 大耐心的回覆!
1. 對不起, 我說得不夠清楚, 我意思是每月轉換 期指 月份時(新一個月), 在還沒有部位 ...
Hi,
1.
可否以範例的方式寫一下買賣條件以及 IB Controller 送的語法
這樣比較好了解
2.
這個部份小弟寫了一個 bracket order 的 pending 範例
ex.
ContractMonth="YM JUN 14-ECBOT-FUT";
ibc=GetTradingInterface("IB");
Buy=ParamTrigger("BUY","BUY");
Short=ParamTrigger("SELL","SELL");
BuyTrigger = LastValue(buy);
SellTrigger = LastValue(short);
PrevTN = StaticVarGet("TimeNumber"+Name());
TN = LastValue(TimeNum());
NewBar = TN != PrevTN;
StaticVarSet("TimeNumber"+Name(),TN);
BuyOrderID = StaticVarGetText("BuyOrderID"+Name());
SellOrderID = StaticVarGetText("SellOrderID"+Name());
BuyPending = ibc.IsOrderPending(BuyOrderID);
SellPending = ibc.IsOrderPending(SellOrderID);
Shares = 1;
if( NewBar )
{
if( NOT BuyPending ) StaticVarSetText("BuyOrderID"+Name(),"");
if( NOT SellPending ) StaticVarSetText("SellOrderID"+Name(),"");
}
if( BuyTrigger AND BuyOrderID == "" )
{
ibc.CloseAllOpenPositions(ContractMonth);
BuyOrderID = ibc.PlaceOrder(ContractMonth, "BUY", Shares, "MKT", 0, 0, "DAY", True);
ibc.PlaceOrder(ContractMonth, "SELL", Shares, "LMT", BUYPRICE*1.1, 0, "DAY", True);
ibc.PlaceOrder(ContractMonth, "SELL", Shares, "STP", BUYPRICE*0.9,buyprice*0.99, "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);
ibc.PlaceOrder(ContractMonth, "BUY", Shares, "LMT", SELLPRICE*0.9, 0, "DAY", True);
ibc.PlaceOrder(ContractMonth, "BUY", Shares, "STP", SELLPRICE*1.1,SELLPRICE*1.1, "DAY", True);
StaticVarSetText("SellOrderID"+Name(),SellOrderID);
}
---
上面的範例我還沒實際操作過(因為這兩天沒開盤,我沒辦法測試)
不過我有想過會遇到的問題大概就是這 order pending 之後,若都沒止損
隔日或數日之後,這個止損單是否都需要一直 pending
所以還是建議把 trailling stop 的語法寫進策略裡
符合止損條件時,將 order pending 出去
再參考看看了
|
|