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) |
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:
- Get contract size of that instrument (https://algogene.com/TechDoc#QueryContractSpec)
- Calculate the number of contract, and round to 2 decimals
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)
Original Posted by - b'walle':Shouldn't we include stock price in the calculation as below?volume = pct*availableBalance/(contractSize*last_srock_price)
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.