Amibroker and Interactive Brokers (TWS) auto trading

Discussion in 'Automated Trading' started by pontricinc, May 14, 2012.

  1. I have a simple trading system. Want to develop a auto trading code from Amibroker to TWS (Traders Workstation). The code I have is not working or placing several buy and sell signal.

    Description of the trading system:
    When 2 period EMA (Exponential Moving Average) crosses above 5 period EMA buy.
    When 2 period EMA crosses below 5 period EMA sell.
    Period: 30 minutes
    Trade US equities (Example: Apple stock).

    What I need:
    The chart should trigger buy and sell and execute the trade in TWS

    AFL Code:
    Buy = Cross( EMA(C, 2), EMA(C,5) );
    Sell = Cross( EMA(C,5), EMA(C, 2) );

    //BuyPrice = Open;
    //SellPrice = Open;
    Short = Sell;
    //ShortPrice = Open;
    Cover = Buy;
    //CoverPrice = Open;

    // trade on next bar open
    SetTradeDelays( 1, 1, 1, 1 );
    BuyPrice = SellPrice = Close;

    // trade size: 25% of current portfolio equity
    //SetPositionSize( 25, spsPercentOfEquity );


    SetTradeDelays(1,1,1,1);


    if ( LastValue( Buy ) )
    {
    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", 1, "MKT", 0, 0, "Day", False );
    }

    }
    }


    if ( LastValue( Sell) )
    {
    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", 1, "MKT", 0, 0, "Day", False );
    }

    }
    }

    Any help would be appreciated.
     
  2. SteveH

    SteveH

    Go over to groups.yahoo.com and search for the Amibroker group. That's the best place to get help.
     
  3. Thank you Steve!
     
  4. I can recommend this API if you're after a quick fix: http://code.google.com/p/ib-csharp/

    Its relatively stable and is maintained.

    You just need to write the connection scripts to port your code over.