How to send order with IBcontoller but with SpsPercentOfEquity NOT Shares?


I want my order via IBcontroller sent but with percent of equity (spsPercentOfEquity).

The code I know from IBController 1.3.8 READ ME is...

Shares = 100; //here to change
ibc.PlaceOrder( Name(), "Buy", Shares, "MKT", 0, 0, "Day", True );
SetPositionSize(100, spsShares );

So far i did this:

Buy price example at 7.

Capital = 1000;
Number_ofPercent = 5;
Percent_Equity = ((Capital*Number_ofPercent)/100)/ValueWhen(Buy,C);

// So.....Percent_Equity = ((1000*5)/100)/7;
// So we will get 7,1 shares.

ibc.PlaceOrder( Name(), "Buy", Percent_Equity, "MKT", 0, 0, "Day", True );
SetPositionSize( Number_ofPercent, spsPercentOfEquity);

My question:

  1. is there a more efficient way?
  2. it will get 7.1 shares, how do we change it to 7 NOT 7.1?


SetPositionSize() and SpsPercentOfEquity are commands for the backtester, and do not affect the autotrading interface.

For rounding the shares, look at floor(), ceil(), and round() functions here: AFL Function Reference

Consider using GetRTD() to get a price quote from IBKR instead of using ValueWhen().

Also, think about how you will handle a share price that exceeds your allowed position size, e.g. AMZN at $3,700, so you don't send an order to IBKR with zero shares and get an error. FYI, IBKR now supports fractional shares, although I've never tried it.

Finally, search the AFL library for "PlaceOrder" at:

And search the old KB for posts by Al Venosa and Herman Van Den Bergen at: AmiBroker Users’ Knowledge Base

I hope this helps.


Hello Peter, thank you for input.

is this waht you mean ?

// Get current real-time data 
Bid=GetRTData("Bid"); // return number not array
Ask=GetRTData("Ask"); //return number not array

because I want to do in market order then = buy at ask, so...

Buy price example at 7.

Capital = 1000;
Number_ofPercent = 5;
Percent_Equity = ((Capital*Number_ofPercent)/100)/Ask;

// So.....Percent_Equity = ((1000*5)/100)/7;
// So we will get 7,1 shares.

ibc.PlaceOrder( Name(), "Buy", Percent_Equity, "MKT", 0, 0, "Day", True );

Fractional shares is interesting,I did plan to try it and it's very new. but i don't know how to set amibroker to allow buying stock 0.1, 0.2, 0.3,.....etc. cz it is SHARES not FUTURES

Hi ibenxs,

Yes, that's what I was saying about using the real time quote.

But for autotrading fractional shares, AB doesn't need to "allow" it, although for backtesting, you would have to set RoundLotSize to be less than 1.

Since I've never tried it, I don't know if ibc will allow it (ibc was written before IBKR allowed fractional shares). Also, I think you have to check a box to allow fractional shares in TWS.

1 Like

about RoundLotSize, I took the source from this System test settings window and I found an important point:

Round lot size

Various instruments are traded with various "trading units" or "blocks". For example you can purchase fractional number of units of mutual fund,

but you can not purchase fractional number of shares.

Sometimes you have to buy in 10s or 100s lots. AmiBroker now allows you to specify the block size on global and per-symbol level.

You can define per-symbol round lot size in the Symbol->Information page. The value of zero means that the symbol has no special round lot size and will use "Default round lot size" (global setting) from the Automatic Analysis settings page.

If default size is set also to zero it means that fractional number of shares/contracts are allowed.

I'm sorry if I'm a little confused, so for stocks can I use a fraction (set RoundLotSize to 0) or can't because this is a stock ?

In my prior post, I said RoundLotSize could be less than one... actually I should have written that it should be zero for fractional shares. My bad.

RoundLotSize is used by the backtester, but you could also use it for your autotrading since this is a field that you can edit in the symbol information panel:

So in your DB you could set it to zero for stocks that you know you want to trade with fractional shares, but set it to one for stocks that you don't want to trade fractionally.

You would have to use RoundLotSize in your position sizing formula, perhaps by making it conditional using the IIf() function in such a way that if RoundLotSize is zero, you don't round the shares, but otherwise you do round the shares.

1 Like

for example I will only buy shares in fractional:

Price 145 for 1 share
I want to buy 0.5 shares >> because fractional

then, we set:

RoundLotSize = 0;
MarginDeposit= 0;
Ticksize = 0;
PointValue = 1;
SetPositionSize( 0.5 , spsShares ); // because want to buy 0.5 shares ???

Please correct me if i am wrong, thank you


i just registered trading fraction and it started from 0.01 of shares. I tried to use the IBcontroller to send the message it read 0. first i sent 0.02 (min shares set to 0.01) then 0.1 it read 0 as in the picture,
and didn't get delivered to TWS.

then i changed it to 1 and it work. can someone help me please ?

@Tomasz can you help me please.

Remember that these items are for backtesting, although if you use the variables in your position sizing formulas, you can use them for autotrading. But SetPositionSize() is only for backtesting.

It seems to me that ibc doesn't support fractional shares if your experiment was done correctly:

That's probably because ibc was written before IBKR implemented fractional share trading, but this is just my guess.

Make sure you read these articles and example code carefully:

Thanks PeterD,

I didn't find any example of decimal/fraction order in the library.
maybe another way of modifying IBcontroller, because it's open source.

very hard for non IT like me.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.