IBcontroller COM/object handle is null

Issue:
I am running AB-64bit. I incorrectly installed the IBcontroller with the default settings and it put it in:
C:\Program Files (x86)\Amibroker

After reading a ton of posts, I read somewhere about the 32 vs 64bit locations, so I deleted everything from the: C:\Program Files (x86)\Amibroker, and deleted all registry items I could find containing "BrokerIB".

I reinstalled pointing the install to the 64bit folder:
C:\Program Files\Amibroker

I still cannot get the following simple sample AT script to communicate with the IBcontroller app:

SetChartOptions( 2, chartWrapTitle );
RequestTimedRefresh( 1 );
BuyOrderTrigger = ParamTrigger( "Place Buy order on TWS", "BUY" );
SellOrderTrigger = ParamTrigger( "Place Sell order on TWS", "SELL" );
CancelOrderTrigger = ParamTrigger( "Cancel order on TWS", "CANCEL" );
TransmitTrigger = ParamTrigger( "Transmit Order", "TRANSMIT" );
ibc = GetTradingInterface( "IB" );
//ibc.SetInfoAccount("F1234A"); 
OrderID = StaticVarGetText( "OrderID" );
ORderStatus = ibc.GetStatus( ORderID, True );

if ( BuyOrderTrigger )
{
    OrderID = ibc.ModifyOrder( OrderID, Name(), "Buy", 100, "MKT", 0, 0, "Day", TransmitTrigger );
    StaticVarSetText( "OrderID", OrderID );
}

if ( SellOrderTrigger )
{
    OrderID = ibc.ModifyOrder( OrderID, Name(), "Sell", 100, "MKT", 0, 0, "Day", TransmitTrigger );
    StaticVarSetText( "OrderID", OrderID );
}
else
    if ( CancelOrderTrigger )
    {
        OrderID = StaticVarGetText( "OrderID" );
        ibc.CancelOrder( ORderID );
    }
    else
        if ( TransmitTrigger )
        {
            if ( ibc.IsOrderPending( ORderID ) )
                ibc.Transmit( ORderID );
        }

OrderID = StaticVarGetText( "OrderID" );

LastTWSMsg = ibc.getLastError( ORderID );
Title = "\n" +
        "           OrderID: " + ORderID + "\n" +
        "Last TWS Error Msg: " + LastTWSMsg + "\n" +
        "Order Status at IB: " + ORderStatus + "\n" +
        " TWS Position Size: " + NumToStr( ibc.GetPositionSize( Name() ), 1.0, False );

No matter what I do I get "COM/object handle is null".

image

I put it on my secondary computer, installing to the correct folder the first time and everything works.

I am thinking it is a registry or setting file somewhere pointed to the wrong folder because after the install into the 64bit folder I went to unlock the app, yet it still had my name and unlock code. So something is remaining from the wrong install.

I have read in other posts that @Tomasz says you can just copy/paste the BrokerIB.exe and BrokerIB.xml to the C:\Program Files\AmiBroker, and then set the properties of BrokerIB.exe to "Run as Admin". But no matter what I do, I still get the "COM/object handle is null" error when trying to Apply this AFL on my main machine.

I even went nuclear and Revo Uninstalled Amibroker and reinstalled, but it still remembered my unlock info, so something.... somewhere.... is holding onto the first install in the 32bit folder (I think!). I've searched the C:, including AppData, but I just cannot find it.

image

The error message you are getting is because IBController is not registered properly as OLE server. Your installation is wrong. You should not make any changes to default and definitely should NOT mark options like "run as administrator" in properties / compatibility tab. This must be turned OFF.

Now you must run FULL setup of AmiBroker again. Don't change any options manually. Just use setup and do nothing more.

On a side note: Everytime when I mention anywhere "run as administrator" it does NOT mean turning any option permanently. "Run as admin" means ONCE only via RIGHT CLICK over icon to run it ONCE. ONCE and ONLY ONCE. Never again. You should NOT change icon properties and you should NOT run permanently as admin. What more must I say to deliver the message?

image

And for gods sake do not use "Revo uninstaller" or other 3rd party nonsense 'cleaning' tools. This is utter rubish that does more harm than good Delete this garbage from your computer.

2 Likes

I uninstalled AB using the Windows uninstaller. I reinstalled AB 64bit. Then reinstalled IBcontroller going into the C:\Program Files\AmiBroker folder correctly the first time. I then right clicked the BrokerIB.exe and chose run as admin, ONLY ONCE. Did not adjust any properies.

I tried to run the sample chart trader code and it is giving a Server Busy _ Switch To _ Retry dialog box:
image

I have to click Switch To... a bunch of times to get unstuck. It ultimately comes out of that and gives the "COM/object handle is null" again.

Your formula is simply incorrect. You must never refer to object that does not exist.
And you are doing this. And that is bad.

Specifically after

ibc = GetTradingInterface( "IB" );

YOU MUST check if ibc is NOT null. So ALL remaining code that ever accesses ibc should be INSIDE "if" clause

ibc = GetTradingInterface( "IB" );
if( ibc )
{
   // access IBcontroller IF and ONLY IF object exists !
   ibc.PlaceOrder(.....)
}

Also IBController clealy displays error messages on your computer. Switch to "Messages" tab that is HIGHLIGHTED in Blue color. Read the error messages.

1 Like

I added the null check. That got me past the "COM/Oject handle is null" issue but still wouldn't work, so i added a trace, and yep, IBC was/is null. I've included a snapshot of what I am seeing in the IBcontroller messages tab, as well as the "Switch To / Retry" prompt that keeps coming up every time I try to Apply.

image

Here is the revised code:

doTrace = 1;

SetChartOptions( 2, chartWrapTitle );
RequestTimedRefresh( 1 );
BuyOrderTrigger = ParamTrigger( "Place Buy order on TWS", "BUY" );
SellOrderTrigger = ParamTrigger( "Place Sell order on TWS", "SELL" );
CancelOrderTrigger = ParamTrigger( "Cancel order on TWS", "CANCEL" );
TransmitTrigger = ParamTrigger( "Transmit Order", "TRANSMIT" );
ibc = GetTradingInterface( "IB" );

if (ibc)
{
	//ibc.SetInfoAccount("F1234A"); 
	OrderID = StaticVarGetText( "OrderID" );
	ORderStatus = ibc.GetStatus( ORderID, True );

	if ( BuyOrderTrigger )
	{
		OrderID = ibc.ModifyOrder( OrderID, Name(), "Buy", 100, "MKT", 0, 0, "Day", TransmitTrigger );
		StaticVarSetText( "OrderID", OrderID );
		if (doTrace) { _TRACE("TickerSym: " + Name() + "  BuyOrderTrigger OrderID: " + NumToStr(OrderID, 1.2)); }
	}

	if ( SellOrderTrigger )
	{
		OrderID = ibc.ModifyOrder( OrderID, Name(), "Sell", 100, "MKT", 0, 0, "Day", TransmitTrigger );
		StaticVarSetText( "OrderID", OrderID );
		if (doTrace) { _TRACE("TickerSym: " + Name() + "  SellOrderTrigger OrderID: " + NumToStr(OrderID, 1.2)); }
	}
	else
	if ( CancelOrderTrigger )
	{
		OrderID = StaticVarGetText( "OrderID" );
		ibc.CancelOrder( ORderID );
		if (doTrace) { _TRACE("TickerSym: " + Name() + "  CancelOrderTrigger OrderID: " + NumToStr(OrderID, 1.2)); }
	}
	else
	if ( TransmitTrigger )
	{
		if ( ibc.IsOrderPending( ORderID ) )
		{
			ibc.Transmit( ORderID );
			if (doTrace) { _TRACE("TickerSym: " + Name() + "  TransmitTrigger OrderID: " + NumToStr(OrderID, 1.2)); }
		}
			
	}
	OrderID = StaticVarGetText( "OrderID" );
	LastTWSMsg = ibc.getLastError( ORderID );
	Title = "\n" +
			"           OrderID: " + ORderID + "\n" +
			"Last TWS Error Msg: " + LastTWSMsg + "\n" +
			"Order Status at IB: " + ORderStatus + "\n" +
			" TWS Position Size: " + NumToStr( ibc.GetPositionSize( Name() ), 1.0, False );
}
else
{
	if (doTrace) { _TRACE("TickerSym: " + Name() + "  IBC IS Null"); }
}

It really seems as though AB is trying to communicate with the IBcontroller but something is blocking it. I thought it could be Windows Defender, so I turned that off temporarily as well as my antivirus, and still a no go. Same as above.

Obviously from "Messages" tab one can see that for many minutes you are not able to connect. Connection should be immediate because TWS is running LOCALLY and connection to local network on SAME computer (127.0.01) is instant. If it isn't it means problem with your COMPUTER configuration. Something (antivirus, 3r party softwares, firewall or TWS configuration, IP port numbers, etc) is wrong on your end and that prevents connection. When IBController is waiting for connection (that is apparently refused) it will be "busy" and that's why you are getting this message box. Normally it would NEVER appear.

You seem to be using paper trading. But you must be aware that Interactive Brokers recently changed ports for paper trading see: Port for Paper Trading TWS is now 7497

Port numbers MUST match between TWS and IBController. If ports are wrong, you won't be able to connect.

2 Likes

It was not connecting at first because I did not have TWS open. Once I opened TWS it got the "ok", as I circled at the top of the graphic above.

Yes, I saw from the get-go that TWS was using 7497 and made that adjustment in the IB plugin for my database, as well as in the IBcontroller config.

I agree something is blocking it. I've stopped everything that I could think of that could do that. Windows Defender and my anti-virus...

Even did an uninstalled and reinstall of both AB and the IBcontroller... did not change anything.

Unfortunately it appears that I will have to go nuclear and re-install Windows.

uninstalled AVG Antivirus, and per @alligator suggestion, I first started TWS, then AB, then Apply the code and it worked!

I think the culprit was AVG.