|
小弟上網看了不少資料,但因沒有程式編寫背景,仍然一知半解,勉強寫了以下AFL,以交易YM為例,希望可以避免Repeat Orders,請各位前輩師兄指教,謝謝
x = EMA(Close,10);
y = EMA(Close,20);
//Open Long
BuySignal = Cross(x,y);
//Close Long
SellSignal = Cross(y,x);
//Open Short
ShortSignal = SellSignal;
//Close Short
CoverSignal = BuySignal;
Buy= Ref(BuySignal,-1);
Sell=Ref(SellSignal,-1);
Short=Ref(ShortSignal,-1);
Cover=Ref(CoverSignal,-1);
//Auto Trading with IB for trading YM//
if( LastValue( Buy ) )
{
ibc = GetTradingInterface("IB");
if( ibc.IsConnected() )
{
if( ibc.GetPositionSize("YM JUN 17-ECBOT-FUT-USD") == 0 )
{
// transmit order
ibc.PlaceOrder("YM JUN 17-ECBOT-FUT-USD","Buy",1,"MKT",0,0,"GTD", True );
}
}
}
if( LastValue( Sell ) )
{
ibc = GetTradingInterface("IB");
if( ibc.IsConnected() )
{
ibc.PlaceOrder("YM JUN 17-ECBOT-FUT-USD","Sell",1,"MKT",0,0,"GTD", True );
}
}
if( LastValue( Short ) )
{
ibc = GetTradingInterface("IB");
if( ibc.IsConnected() )
{
if( ibc.GetPositionSize("YM JUN 17-ECBOT-FUT-USD") == 0 )
{
// transmit order
ibc.PlaceOrder("YM JUN 17-ECBOT-FUT-USD","Short",1,"MKT",0,0,"GTD", True );
}
}
}
if( LastValue( Cover ) )
{
ibc = GetTradingInterface("IB");
if( ibc.IsConnected() )
{
ibc.PlaceOrder("YM Jun16'17 @ECBOT","Cover",1,"MKT",0,0,"GTD", True );
}
}
//Preventing Repeat Trade
RequestTimedRefresh( 1 );
ibc = GetTradingInterface("IB");
Transmit = ParamToggle("Transmit","OFF|ON",0);
BuyOrderTrigger = ParamTrigger("Place Buy order","BUY");
SellOrderTrigger = ParamTrigger("Place Sell order","SELL");
Reset = ParamTrigger("Reset Program","RESET PROGRAM");
ClearBuyOrderID = ParamTrigger("Clear Buy","CLEAR");
ClearSellOrderID = ParamTrigger("Clear Sell","CLEAR");
BuyOrderID = StaticVarGetText("BuyOrderID");
SellOrderID = StaticVarGetText("SellOrderID");
BuyPending = ibc.IsOrderPending(BuyOrderID);
SellPending = ibc.IsOrderPending(SellOrderID);
if( BuyOrderTrigger AND BuyOrderID == "" )
{
BuyOrderID= ibc.PlaceOrder( "YM Jun16'17 @ECBOT","Buy",1,"MKT",0,0,"GTD", Transmit);
StaticVarSetText("BuyOrderID",BuyOrderID);
SetChartBkColor( colorBrightGreen );
}
if( SellOrderTrigger AND SellORderID == "")
{
SellORderID = ibc.PlaceOrder( "YM Jun16'17 @ECBOT","Sell",1,"MKT",0,0,"GTD", Transmit);
StaticVarSetText("SellOrderID",SellOrderID);
SetChartBkColor( colorRed );
}
else if( Reset )
{
StaticVarSetText("BuyOrderID","");
if( BuyPending ) ibc.CancelOrder( BuyOrderID );
StaticVarSetText("SellOrderID","");
if( SellPending ) ibc.CancelOrder( SellOrderID );
ibc.CloseAllOpenPositions();
}
else if( ClearBuyOrderID AND BuyOrderID != "" )
{
StaticVarSetText("BuyOrderID","");
if( BuyPending ) ibc.CancelOrder( BuyOrderID );
}
else if( ClearSellOrderID AND SellOrderID != "" )
{
StaticVarSetText("SellOrderID","");
if( SellPending ) ibc.CancelOrder( SellOrderID );
}
LastTWSMsg = ibc.getLastError( 0 );
Title = "\n"+
"Last TWS Error Msg: "+LastTWSMsg+"\n"+
" BuyOrder Status: "+WriteIf( BuyOrderID != "", BuyOrderID+" "+ibc.GetStatus( BuyORderID, True ),"")+"\n"+
" SellOrder Status: "+WriteIf( SellOrderID != "", SellOrderID+" "+ibc.GetStatus( SellORderID, True ),"")+"\n"+
" TWS Position Size: "+NumToStr(ibc.GetPositionSize( Name() ),1.0,False);
|
|