# How to use variable quantity with ibc.placeorder function

Hi,

I am using below code for sending orders TWS;

``````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", quantity , "MKT", 0, 0, "Day", True);
}
}
}
``````

this works if quantity is explicitly declared as a number. But I want to use variable quantity, like what is derived from below formula. Could you pls advice where am I going wrong?

``````firstqty=1000/C;
secondqty=IIf(C<sm1*.95,firstqty*4,0);
thirdqty=IIf(C<SM1*.9, (firstqty+secondqty)*4,0);
quantity=firstqty+secondqty+thirdqty;
``````

Jai

Moderator comment: You have ignored forum rules and posted without reading “How to use this site” and withotu using proper CODE TAGS and I had to fix your post. If you do this next time your post will be deleted for not following forum rules.

Try removing fractional shares from quantity before submitting the order using the int(), round(), ceil() or floor() functions.

I tried all these functions but no luck. If I print the value of quantity it displays 66.000. If I make quantity=66.000; it works, but the moment I change it to quantity=10000/C; it gives “COM error: Type mismatch on one or more arguments”.

Sorry I didn’t notice this earlier…

Calculating 10000/C produces an array with a result for each bar. Your other calculations also produce arrays. You need to extract a single value from the array, presumably the last one.

Try using LastValue(quantity) to place the order.

-Alan

It worked, Thanks a lot Alan.