Joesan及AB与IB互联做外汇的大侠请进!

Discussion in 'AmiBroker' started by robinsan, Oct 17, 2008.

  1. 请问目前有没有谁用AB与IB互联,能够做到全自动交易外汇?至少是能够发送Forex Order到IB是正常的?因为进度目前就卡在这里了。

    这些天我一直在测试用AB发单给IB,但是会出错。百思不得其解,在TWS Group看到,有人说是因为IB对于CASH的Position size不支持,所以不能直接自动发单,只能手动。

    请过来人指点一下。谢谢。
    尝试用AB发送Order到IB,出错信息如下:

    920 213.01316833 [4220]# buy,position = 0, last trade =
    921 213.01316833 [4220] buy ,OID = 2072
    922 213.73153687 [4220] # buy, position = 0, last trade...
     
  2. 我从没有在IB交易外汇,不过据我所知 IB FOREX 是没有position 的,所以会显示 position
    =0

    这是IB 的问题。

    外汇用 metatrader 做比较好。
     
  3. 恩,joesan高见。我目前可以在mt4与ib,互联做全自动交易。包括半自动交易。

    比如:用脚本做普通下单;或者用ea做全自动交易。这是一个不小的工具包。

    ib有个地方,很有趣:比如一手eu,再来一手eu,mt中显示两单,但是ib中显示一单:200000eu.哈哈。
     
  4. 这个代码发送Forex Order到IB是正常的 可以手工确认,刚才测试了一下。但是这个代码会不断的发送订单 需要修改。。。


    Buy =Cover=Cross( CCI(14),-100);

    Sell=Short = Cross( 100,CCI(14));


    /*
    /* autotrade



    // retrieve static variables. Every time chart is refreshed the
    variable is retrieved
    */
    BuyVar = StaticVarGetText("BuyVar");
    "BuyVar: " + BuyVar;
    SellVar = StaticVarGetText("SellVar");
    "SellVar: " + SellVar;
    ShortVar = StaticVarGetText("ShortVar");
    "ShortVar: " + ShortVar;
    CoverVar = StaticVarGetText("CoverVar");
    "CoverVar: " + CoverVar;

    //then for the conditional statments use:

    if( LastValue( Buy ) AND BuyVar == "" )
    {
    ibc = GetTradingInterface("IB");

    // check if we are connected OK
    if( ibc.IsConnected() )
    {
    // check if we do not have already open position on this stock
    // if( ibc.GetPositionSize( Name() ) == 0)
    {
    // transmit order
    ibc.PlaceOrder( Name(), "Buy", 25000, "MKT", 0, 0, "Day",
    True );

    // now reset the static variable
    StaticVarSetText("BuyVar","1");
    StaticVarSetText("SellVar","");
    StaticVarSetText("ShortVar","");
    StaticVarSetText("CoverVar","");


    }
    }
    }

    if( LastValue( Sell ) AND SellVar == "")
    {
    ibc = GetTradingInterface("IB");

    // check if we are connected OK
    if( ibc.IsConnected() )
    {
    // check if we do not have already open position on this stock
    //if( ibc.GetPositionSize( Name() ) == 0 )
    {
    // transmit order
    ibc.PlaceOrder( Name(), "sell", 25000, "MKT", 0, 0, "Day",
    True );

    // now reset the static variable
    StaticVarSetText("BuyVar","");
    StaticVarSetText("SellVar","1");
    StaticVarSetText("ShortVar","");
    StaticVarSetText("CoverVar","");

    }
    }
    }


    if( LastValue( Cover ) AND coverVar == "")
    {
    ibc = GetTradingInterface("IB");

    // check if we are connected OK
    if( ibc.IsConnected() )
    {
    // check if we do not have already open position on this stock
    //if( ibc.GetPositionSize( Name() ) == 0 )
    {
    // transmit order
    ibc.PlaceOrder( Name(), "buy", 25000, "MKT", 0, 0, "Day",
    True );

    // now reset the static variable
    StaticVarSetText("BuyVar","");
    StaticVarSetText("SellVar","");
    StaticVarSetText("ShortVar","");
    StaticVarSetText("CoverVar","1");

    }
    }
    }




    if( LastValue( Short ) AND shortVar == "")
    {
    ibc = GetTradingInterface("IB");

    // check if we are connected OK
    if( ibc.IsConnected() )
    {
    // check if we do not have already open position on this stock
    //if( ibc.GetPositionSize( Name() ) == 0 )
    {
    // transmit order
    ibc.PlaceOrder( Name(), "sell", 25000, "MKT", 0, 0, "Day",
    True );

    // now reset the static variable
    StaticVarSetText("BuyVar","");
    StaticVarSetText("SellVar","");
    StaticVarSetText("ShortVar","1");
    StaticVarSetText("CoverVar","");

    }
    }
    }
     
  5. 多谢各位。请问一下mailema MT与IB互联是如何实现的?要自己写程序吗?谢谢
     
  6. mailema 是用的MT+twslink.如果MT可以成功下单,那么AB应当也可以。因为用的都是C++的IB API接口。
    贴一段代码,可以解决发单给IB的一些问题,留给有需要人士参考。


    As to the file ER2-AT-001.afl you just posted, I noticed at the top you have an oversight in the setup. Here is the correction:



    //OLD CODE



    // next 4 lines are for ER2 futures

    PointValue = 100;

    NumContracts = 1;

    MarginDeposit = 100;

    PositionSize = NumContracts * MarginDeposit;



    //CORRECTED CODE



    // next 4 lines are for ER2 futures

    PointValue = 100;

    NumContracts = 1;

    MarginDeposit = 3375;

    PositionSize = NumContracts * MarginDeposit;





    ////////////// For General Sharing //////////////////

    // Here is an include file that I call "SetOptions.afl" and use in almost all my code. I modify this file frequently

    // depending on the time frame and type of equity I am trading, but the internals stay mostly the same.

    // This one is directed towards trading futures, but I am trying to adapt it to allow stock trading too.

    // I'm not certain the stock portion works correctly just yet.

    //

    // Many of the parameters are stored so I can use them in my code. For example, ie contains my initial equity.

    // I can then use this to show me the required initial equity in the interpretation window or on the chart.

    //

    // Also use caution with the first two lines for TradeDelays and BuyPrices. This one is setup for manual clicking

    // a trade (in my regular code) so I want to see this bar open pricing. Normally you would not want that.

    //

    // Just copy and paste from SECTION to SECTION and save as an AFL to your Include folder

    ////////////////////////////////////////////////////////



    _SECTION_BEGIN("SetOptions");

    /*************************************** Settings for Operation ***************************************/



    //Modified 11/25/2006 to trade STOCKS when no FUTURE Symbol, as defined below, is selected. You can leave the FUTURES box checked in AA->Settings->General.



    SetTradeDelays(0,0,0,0);



    BuyPrice = SellPrice = ShortPrice = CoverPrice = Open; //Ref(OC,when); Don't do this, Amibroker backtest shifts the dates of the signals to the next day to pick up the right prices!



    //Report on above settings:

    "Trade signals come on the current bar.\nEnter and Exit on Open of the next bar";



    ///////////////////////////////////////////////////////////////////// Force my own Futures info

    fm = True; //Default to futures. If you don't find it below, switch to stocks//Set trading mode and commissions

    cm = 3; margin = 100; myCommission = 2.4; //$2.40 per contract each way IB



    if (StrLeft(Name(),2) == "DD") {myPointValue = 25; myTickSize = 1.00; myMarginDeposit = 12188;}

    else if (StrLeft(Name(),2) == "ER") {myPointValue = 100; myTickSize = 0.10; myMarginDeposit = 3375;}

    else if (StrLeft(Name(),2) == "EM") {myPointValue = 100; myTickSize = 0.10; myMarginDeposit = 3375;} //Midcap 400 identical to ER2

    else if (Name() == "RUT-CBOE-IND") {myPointValue = 100; myTickSize = 0.10; myMarginDeposit = 3375;} //RUT index

    else if (StrLeft(Name(),2) == "ES") {myPointValue = 50; myTickSize = 0.25; myMarginDeposit = 3938;}

    else if (Name() == "SPX-CBOE-IND") {myPointValue = 50; myTickSize = 0.25; myMarginDeposit = 3938;}

    else if (StrLeft(Name(),2) == "NQ") {myPointValue = 20; myTickSize = 0.25; myMarginDeposit = 3750;}

    else if (Name() =="NDX-NASDAQ-IND") {myPointValue = 20; myTickSize = 0.25; myMarginDeposit = 3750;} //NDX index

    else if (StrLeft(Name(),2) == "YM") {myPointValue = 5; myTickSize = 1.00; myMarginDeposit = 2438;}

    else if (StrLeft(Name(),2) == "QM") {myPointValue = 500; myTickSize = 0.025; myMarginDeposit = 2025;} //Light Sweet Crude

    else if (StrLeft(Name(),2) == "ZC") {myPointValue = 5000; myTickSize = 0.0025; myMarginDeposit = 1148;} //Corn

    else if (StrLeft(Name(),2) == "ZB") {myPointValue = 1000; myTickSize = 0.03125; myMarginDeposit = 1215;} //30 year Treasury

    else if (StrLeft(Name(),2) == "ZG") {myPointValue = 100; myTickSize = 0.10; myMarginDeposit = 2734;} //Corn

    else if (StrLeft(Name(),2) == "ZN") {myPointValue = 1000; myTickSize = 0.015625; myMarginDeposit = 810;} //10 year Treasury

    else if (StrLeft(Name(),2) == "6E") {myPointValue = 125000; myTickSize = 0.0001; myMarginDeposit = 2835;} //EuroDollar

    else {fm = False; cm = 1; margin = 100; myCommission = 0.2;} //Futures NOT selected. Assume a stock.



    //Margin is set next based on user selection---> margin = 50 * ParamToggle("Margin","50,100",1) + 50; //False = 50 * 0 + 50 = 50 //True = 50 * 1 + 50 = 100



    SetOption("FuturesMode",fm); //Requires True or False //NOTE: This is insufficient. You must also check Futures mode in AA-Settings.

    SetOption("CommissionMode", cm);

    SetOption("CommissionAmount", myCommission);



    if (fm)

    {

    "Trading in FUTURES MODE";

    ieFixed = Param("Initial Equity. 0 = compute",0,0,100000,1000);

    numContracts = Param("# contracts to start.",4,4,24,1);

    numScaleOut = Param("# contracts @ ScaleOut",1,1,12,1);

    percentOut = numScaleOut / numContracts;

    dollarsOut = numScaleOut * myMarginDeposit;

    iePercent = Param("%Equity to trade",15,-100,100,1); //+ trades fixed # of contracts

    }

    else

    {

    "Trading in STOCK MODE";

    myTickSize = C * 0.01; //Set "tickSize" to 1% of the price

    ieFixed = Param("Initial Equity. ",10000,1000,100000,1000); //Cannot use zero for stocks (could, but code is too much trouble)

    myMarginDeposit = ieFixed; //for stocks

    numContracts = 4; //for stocks so we scale out 25% at a time

    numScaleOut = 1;

    percentOut = 0.25;

    iePercent = Param("%Equity to trade",100,-100,100,1); //+ trades fixed # of contracts

    }

    maxPos = 1; // Just one for Futures code //Param("Max Open Positions - allows to trade a basket of futures",1,1,5,1);

    if (maxPos > 1 AND iePercent < 0) iePercent = -round(100 / maxPos); //Force positionSize to work properly for no. of Positions

    //Set IntialEquity based on the above choices

    if (ieFixed > 0)

    {

    ie = ieFixed; //Just use this amount as the Initial Equity

    }

    else //We need to compute the Initial Equity which is based on the # of contracts to start.

    {

    if (iePercent == 0)

    {

    ie = round(myMarginDeposit * numContracts / 1000 + 0.5) * 1000; //Need to insure it rounds up

    //Both ieFixed and iePercent == 0. You will get barely enough money to trade numContracts

    }

    else

    {

    ie = round(myMarginDeposit / (abs(iePercent) / 100) / 1000) * 1000 * numContracts;

    //Initial Equity per contract to nearest $1000

    }

    //Will be HUGE when user picks to trade a very low % of their equity

    }



    //Set PositionSize based on the above choices

    if (iePercent == 0)

    {

    PSize = myMarginDeposit * numContracts;

    PSize = Min(ie,PSize); //Don't trade more than Initial Equity in case it was set manually

    }

    else if (iePercent > 0)

    {

    PSize = ie * (iePercent / 100);

    if (fm) PSize = Max(myMarginDeposit,PSize); //Trade at least 1 contract for Futures. Leave Stock alone.

    }

    else

    {

    PSize = iePercent; //Value is negative, this means we are compounding so just leave it

    if (fm AND abs(PSize/100 * ie) < myMarginDeposit AND ieFixed > 0) PSize = -round(myMarginDeposit / ie * 100 + 0.5);

    //User isn't trading enough PSize to get any position so we are forcing additional PSize to allow the first trade.

    //Testing for ieFixed > 0 because the PSize was already computed and fixed for this condition above

    }



    SetOption("InitialEquity", ie);

    SetOption("AllowSameBarExit",True);

    SetOption("ActivateStopsImmediately",True);

    SetOption("AllowPositionShrinking",True);

    SetOption("InterestRate",0);

    SetOption("MarginRequirement",margin);

    SetOption("MaxOpenPositions", maxPos);

    SetOption("MinShares",1);

    SetOption("NoDefaultColumns",False);

    SetOption("PriceBoundChecking",True);

    SetOption("ReverseSignalForcesExit",True); //Normally True if not trading Long and Short simultaneously

    SetOption("UsePrevBarEquityForPosSizing",False);

    SetOption("DisableRuinStop", False);



    "\nInitial Equity = $" + NumToStr(ie,1.0);

    "Initial PositionSize = " + WriteIf(PSize < 0,NumToStr(PSize,1.0) + "% of Equity","$" + NumToStr(PSize,1.0));



    leverage = 100/margin; //defines leverage for stop loss and gain calcs based on selected margin.

    PositionSize = PSize * leverage; //Modified 1/20/2006 to add margin to fixed dollar positions too.



    //The following 4 lines for ApplyStop are here to prevent AA Settings from over-riding this code.

    ApplyStop(stopTypeLoss,stopModeDisable,100,1,False,0);

    ApplyStop(stopTypeTrailing,stopModeDisable,100,1,False,0);

    ApplyStop(stopTypeNBar,stopModeDisable,100,1,False,0);

    ApplyStop(stopTypeProfit,stopModeDisable,100,1,False,0);





    //Set system values based on the above if they are not available in Symbol Info

    if (fm)

    {

    RoundLotSize = 1;

    if (PointValue <= 1) PointValue = myPointValue;

    if(TickSize == 0) TickSize = myTickSize;

    if (MarginDeposit == 0) MarginDeposit = myMarginDeposit;

    }

    else RoundLotSize = 100;



    //////////////////////// End of Include file

    _SECTION_END();
     
  7. 好像ami官方就提供与ib连接的dll接口的吧。
     
  8. 最新版已到1.80,但要注册用户才可以下。
    October 15, 2008
    Interactive Brokers TWS plugin version 1.8.0
    A new Interactive Brokers TWS data plugin version 1.8.0 is released now.

    This version fixes “Duplicate ticker ID for API historical Data query” error that occurred sometimes with newest TWS version.

    It is available for download from:
    http://www.amibroker.com/bin/ib180/IB.dll

    (to be copied to Plugins subfolder, replacing old version)
     
  9. robinsan

    用IB 自动交易外汇顺利与否,技术上的问题都解决了吗? 滑价大小如何?
     
  10. 目前正在测试当中。技术上的问题还没有解决。
    Joesan,你目前做的是backtest吧。
     
  11. 我目前用AB+IBcontroller+TWS传送Order及接收数据都没有问题了,但还没有到实际交易的地步。Joesan你是用什么方式来实现的?可否大概说下。
     
  12. 我遇到两个情况,AB设置的都是5min,但是bar却显示的是1min.不知道是不是盗版的原因?

    还有如何让AB显示的时间是交易所的时间?
     
  13. 我的方法和你相同。
     


  14. 你把AB 哪里设置成 5MIN 了?

    要显示5min的话,先在 tools-preference-intraday 中勾选填入5 minutes ,然后在图上游击鼠标,在intraday 中选 5min 。 或者如果database 基准周期长度就是五分钟的话,直接点击工具栏上 “i” 符号即可



    如何让AB显示的时间是交易所的时间?

    file-database-intraday setting 中 daily time compression use:中选择 exchange time 即可。
     
  15. 谢谢joesan