[Snippet] Risk Management for SMM & Dynamic Slot Size

2 122 Views No Comments 2 weeks ago
  • I posted my risk management method in https://discord.com/channels/269316665483722764/491747780029579276/793910926466220032

    It is very important to have risk management to avoid liquidation and to utilize the maximum leverage level so you can trade even if you have small ammunition.
    Leverage does not matter. What matter is the budget you allocate for position size. Is it too big? is it too small? Can it avoid liquidation on 10-20% price change?
    To understand more about trading with leverage please visit https://medium.com/@cryptocreddy/comprehensive-guide-to-position-size-and-leverage-2e27764ce9e0

    So, here is a snippet that you can add to SMM (or other bot?) where it will automatically/dynamically adjust your max open and position size according your margin balance.
    The default is using budgeting 80% of your margin balance for trading (position + buffer for price change, margin budget) and from that 80% using 10% for position size (position budget). If you are trading several coins then you should split that 80%.
    Example you want to trade BTC. LTC, ETH with ratio 2:1:1 then for BTC margin budget is 40%, LTC 20% and ETH is 20% which make the total 80%. The position budget is the same.

    The code is based on how Binance calculation. I believe Deribit and other exchange have different calculation. For that, you have to explore the parameters yourself 🙂 But the rule stay the same, 80% for trading, 10% of that 80% for position.

    Hopefully you will find it useful and bring you much profit and of course, avoid liquidation!

    • This topic was modified 2 weeks, 2 days ago by smokyho.
    • This topic was modified 2 weeks, 2 days ago by smokyho.
    • This topic was modified 2 weeks, 2 days ago by smokyho.
    • This topic was modified 2 weeks, 2 days ago by pshai.
    • This topic was modified 2 weeks, 1 day ago by Derrick.
    HaasScript Code
    -- Budget
    local maxSizeM = Input('01. Max. Open '..AmountLabel(), 100000, 'Maximum open contracts at any given time also as minimum for Dynamic Max Open. After exceeding this value, the bot will dump a portion of position at a loss.')
    local autoMax = Input('01A. Dynamic Max Open', true, 'Dynamically change max open contracts based on available balanca')
    local leverage = Input('01B. Leverage', 50, 'MUST be filled even if using Cross Margin. Important for trading budget.') 
    local maxBudget = Input('01C. Margin Budget %', 0.8, 'How much from available margin allocated for this bot. 0.5 is 50% of available margin.')
    local maxOpen = Input('01D. Position Budget %', 0.1, 'How much from the Margin Budget is for opening positions. 0.1 is 10% of Margin Budget')
    -- Slot Size
    local slotCount = Input('02. Slot Count', 3, 'How many orders are constantly kept open on both long and short side.')
    local slotSizeM = Input('03. Slot Size', MinimumTradeAmount(), 'Trade amount per slot also as minimum slot size for Dynamic Slot Size.')
    local autoSlot = Input('03A. Dynamic Slot Size', true, 'DSSize - Dynamically change slot size favoring trending side.')
    local slotBudget = Input('03B. Max. Open Devider', 377, 'DSSize feature - Devide max open position with this value to get new Slot Size. Example Max Open is 1000 and devider is 200 then slot size is 5') 
    local cp = CurrentPrice()  
    local profitLabel = ProfitLabel()
    if profitLabel == nil then profitLabel = QuoteCurrency() end
    local availableMargin = WalletAmount(AccountGuid(), profitLabel)
    --max position 
    -- This is for Binance. Other exchange might have different calculation.
            if profitLabel == 'USD' or profitLabel == 'USDT' then
                maxMax = availableMargin * maxBudget * maxOpen / cp.close --Binance USDT Futures
                maxMax = availableMargin * maxBudget * maxOpen * cp.close / 10 --Binance COIN Futures, for BTC it is 100
            local maxCheck = autoMax and (maxMax * leverage)
            local maxValue = autoMax and (maxCheck > maxSizeM) and maxCheck or maxSizeM
            local maxSize = autoMax and maxValue or maxSizeM
            -- check max open  
                if autoMax then
                    if maxCheck > maxSizeM then
                        Log('Dynamic Max Open: '..Round(maxCheck, 5)..' '..AmountLabel())
                        Log('Dynamic Max Open: '..Round(maxCheck, 5)..' but using default value: '..Round(maxSizeM, 5)..' '..AmountLabel())
                    Log('Max Open: '..Round(maxSizeM, 5)..' '..AmountLabel())
    -- auto slot size
            local slotCheck = autoSlot and (maxSize / slotBudget)
            local slotValue = autoSlot and (slotCheck > slotSizeM) and slotCheck or slotSizeM
            local slotSize = autoSlot and slotValue or slotSizeM
            -- check slot size  
                if autoSlot then
                    if slotCheck > slotSizeM then
                        Log('Dynamic Slot Size: '..Round(slotCheck, 5)..' '..AmountLabel())
                        Log('Dynamic Slot Size: '..Round(slotCheck, 5)..' but using default value: '..Round(slotSizeM, 5)..' '..AmountLabel())
                    Log('Slot Size: '..Round(slotSizeM, 5)..' '..AmountLabel())
Login or Register to Comment

Unlock your crypto trading potential

Create a free account and enjoy everything we have to offer.

Join for Free