COCO研究院

 找回密碼
 註冊
搜索
查看: 26613|回復: 3

[教學] 請問各位如何避免Repeat Orders

[複製鏈接]
發表於 17-4-23 12:22 | 顯示全部樓層 |閱讀模式
小弟上網看了不少資料,但因沒有程式編寫背景,仍然一知半解,勉強寫了以下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);


發表於 17-4-26 23:41 | 顯示全部樓層
用這個 ExRem()
請參考help file
 樓主| 發表於 17-5-5 10:43 | 顯示全部樓層
magier 發表於 17-4-26 23:41
用這個 ExRem()
請參考help file

謝謝解答, 但ExRem()似乎只適用於backtesting, amibroker support那邊亦說要從連結IB的AFL著手, 但無論如何, 已解決, 謝謝!
發表於 17-8-20 13:45 | 顯示全部樓層
感謝版主分享!
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-11-22 13:52

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 |