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.
// 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
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.
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.
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 ?
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: