Sharwan Patel

How to set order volume to balance percentage?

Programming


Hi all, I have a question related to placing orders. 
I checked from Tech Doc that the order volume refers to the number of contract to buy. 
However, I want to buy for 1% of balance. How can I do that? 

 
William Cheung
I don't think it is supported in current API functions. 
You need to calculate by yourself. 

 
admin

Hi Patel, the sendOrder function currently doesn't support order size as a percentage of account balance. So it needs some calculation as follows.

As an example, you want to trade for 1% of current available balance:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
    def on_marketdatafeed(self, md, ab):
        
        pct = 0.01  # trade for 1% percent balance

        availableBalance = ab['availableBalance']  # get your current available balance

        contractSize = self.evt.getContractSpec(md.instrument)["contractSize"] # get contract size of current instrument

        volume = round(pct*availableBalance/contractSize, 2)  # how much contract can buy for 1% balance
        
        order = AlgoAPIUtil.OrderObject(
            instrument = instrument,
            openclose = 'open',
            buysell = 1,
            ordertype = 0,
            volume = volume
        )
        self.evt.sendOrder(order)

 
walle
Original Posted by - b'admin':

Hi Patel, the sendOrder function currently doesn't support order size as a percentage of account balance. So it needs some calculation as follows.

As an example, you want to trade for 1% of current available balance:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
    def on_marketdatafeed(self, md, ab):
        
        pct = 0.01  # trade for 1% percent balance

        availableBalance = ab['availableBalance']  # get your current available balance

        contractSize = self.evt.getContractSpec(md.instrument)["contractSize"] # get contract size of current instrument

        volume = round(pct*availableBalance/contractSize, 2)  # how much contract can buy for 1% balance
        
        order = AlgoAPIUtil.OrderObject(
            instrument = instrument,
            openclose = 'open',
            buysell = 1,
            ordertype = 0,
            volume = volume
        )
        self.evt.sendOrder(order)


Shouldn't we include stock price in the calculation as below?

volume = pct*availableBalance/(contractSize*last_srock_price)
 
admin
Original Posted by - b'walle':

Shouldn't we include stock price in the calculation as below?

volume = pct*availableBalance/(contractSize*last_srock_price)
Yes, you are right.
If the underlying currency of the stock is different from your account's base currency. The formula need further adjust with the exchange rate. 
 
walle
Original Posted by - b'admin': Yes, you are right.
If the underlying currency of the stock is different from your account's base currency. The formula need further adjust with the exchange rate. 
Thanks.