Coding Position Size as a % of IB Account Balance

Questions about MultiCharts and user contributed studies.
TO_Trader
Posts: 35
Joined: 31 Oct 2011
Has thanked: 9 times
Been thanked: 3 times

Coding Position Size as a % of IB Account Balance

Postby TO_Trader » 15 Nov 2012

Hello,

Currently I am using the following to calculate the number of shares to trade in a strategy:
NumContracts = Floor (CurrentAccountSize / Close)
where CurrentAccountSize = AccountStartSize + NetProfit + PositionProfit
Essentially, I am saying use all available capital to trade.
I would now like to code a version that uses an allocated percentage of the IB account size as follows:

Code: Select all

Input:
PctAllocation(5)
Vars:
NumContracts(0)
NumContracts = Floor ((Net Liquidation Value of the IB account * (PctAllocation/100)) / Close)

How would I properly code this?

Many thanks.

SP
Posts: 441
Joined: 06 Feb 2006
Has thanked: 36 times
Been thanked: 280 times

Re: Coding Position Size as a % of IB Account Balance

Postby SP » 16 Nov 2012

You can get the Net Liquidation Value in signals with :

Code: Select all

inputs:
mAccountID   ("DU12345"),
PctAllocation(5);
Vars:
NetLiquidVal(0),
NumContracts(0);

NetLiquidVal = GetRTAccountEquity(mAccountID);
NumContracts = Floor ((NetLiquidVal * (PctAllocation/100)) / Close);

if LastBarOnChart_s then
print ("NetLiquidVal: ",NetLiquidVal, " ,NumContracts: ",NumContracts );
These users thanked the author SP for the post (total 4):
TO_TraderRBNXyzzyTJ

TO_Trader
Posts: 35
Joined: 31 Oct 2011
Has thanked: 9 times
Been thanked: 3 times

Re: Coding Position Size as a % of IB Account Balance

Postby TO_Trader » 16 Nov 2012

Thank you, SP, for this.

Do I have to specify the account number? I would prefer to keep it generic so that I can use the code on various different accounts. I only ever have one instance of TWS open, so, I would like to use the account number associate with the one instance of TWS that is open.

Thanks.

Xyzzy
Posts: 152
Joined: 19 Mar 2011
Has thanked: 43 times
Been thanked: 74 times

Re: Coding Position Size as a % of IB Account Balance

Postby Xyzzy » 17 Jun 2013

Another thanks for this post -- I'm doing something similar, and this was a big help.

Just for the record, you can have the code automatically detect the account number by using the GetAccountID() function. Just make the following change:

Code: Select all

NetLiquidVal = GetRTAccountEquity(GetAccountID());


Then, you can eliminate the "mAccountID" parameter for the AccountID.
These users thanked the author Xyzzy for the post:
TJ


Return to “MultiCharts”