[pshaiCmd] Bots Used Margin

stable
By pshai in Other Published May 2022 👁 1,166 views 💬 2 comments

Description

A simple command to get bots used position margin information.
HaasScript
DefineCommand('BotsUsedMargin', 'Updates and records the highest margin used for all bot open position and their open orders.')

local leverage = DefineParameter(NumberType, 'leverage', '', false, Leverage())

local maxPos = Load('mp', 0)
local maxOrd = Load('mo', 0)

local allPos = GetAllOpenPositions()
--local allOrd = GetAllOpenOrders()
local totalPos, totalOrd = 0, 0

for i=1, #allPos do
    local pos = allPos[i]

    if pos != nil then
        totalPos = totalPos + UsedMargin(pos.market, pos.enterPrice, pos.amount, leverage)
    end
end

--[[
for j=1, #allOrd do
    local order = allOrd[j]
    local market = TradeMarketContainer(order.positionId)
    
    totalOrd = totalOrd + UsedMargin(market, order.price, order.executedAmount, leverage)
end
--]]

if totalPos > maxPos then maxPos = totalPos end
if totalOrd > maxOrd then maxOrd = totalOrd end

Save('mp', maxPos)
Save('mo', maxOrd)

local result = {
    positionsMargin = totalPos,
    ordersMargin = totalOrd,
    maxPositionsMargin = maxPos,
    maxOrdersMargin = maxOrd
    }

DefineOutput(ListDynamicType, result, 'Results')
DefineOutputIndex(1, DynamicType, 'positionsMargin', 'Used margin for all bot open positions')
DefineOutputIndex(2, DynamicType, 'ordersMargin', 'Used margin for all bot open orders')
DefineOutputIndex(3, DynamicType, 'maxPositionsMargin', 'Highest used margin for all bot open positions')
DefineOutputIndex(4, DynamicType, 'maxOrdersMargin', 'Highest used margin for all bot open orders')

2 Comments

Sign in to leave a comment.

R
romdisc about 4 years ago

Wouldn't it be more appropriate to change the type of the DevineOutputIndex to NumberType(s)?

P
pshai almost 4 years ago

I guess it probably would... :thinking: Not entirely sure why I even have DynamicType there!