[CMD]Autoriskmanagement (converted to command script)

stable
By Strooth in Safeties Published February 2021 👁 1,553 views 💬 0 comments

Description

Took the dynamic max open size and used firetrons template to make it a nice and neat command you can call in any of your scripts. Also uses a custom debug logger Simple test showing how to it works
local maxSize = CC_AutoRiskManagement(walletBal, maxSizeM, autoMax, leverage, maxBudget, maxOpen, contVal, cp.close, true, true)
        maxSize = Parse(maxSize, NumberType)
HaasScript
--  ============================================================================
--    Name AutoRiskManagement
--
--    Description.  AutoRiskManagement as a command so it can be easily used in all your scripts 
--
--
--    Discord: @strooth
--  ============================================================================
 
--  ========================================================
--    Variables
--  ========================================================
 
--  ------------------------------------
--    Definition
--  ------------------------------------
 
local description
local inputSuggestions
local output
local outputSuggestions
 
--  ------------------------------------
--    Parameter
--  ------------------------------------
 
--    Basic
 
local pmaxSizeM
local pautoMax
local pleverage
local pmaxBudget
local pmaxOpen
local pwalletBal
local source
local pcontVal

--    Logging
 
local pIsLogging
local pIsVerbose

 
 
--  ========================================================
--    Command Definition
--  ========================================================
 
description = 'Auto Risk Management will calculate the maximum opening position size adjusted for leverage and wallet balance'
DefineCommand('AutoRiskManagement', description)
 
--  ========================================================
--    Parameter Definition
--  ========================================================
 
--  ------------------------------------
--    Main
--  ------------------------------------

 
description      = 'Current Wallet Balance'
inputSuggestions = 'Input'
pwalletBal = DefineParameter(NumberType, 'Wallet Balance', description, false, 1000, inputSuggestions)

description      = '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.'
inputSuggestions = 'Input'
pmaxSizeM = DefineParameter(NumberType, 'Max Open Size', description, false, 10, inputSuggestions)

description      = 'Dynamically change max open contracts based on available balance'
inputSuggestions = 'Input'
pautoMax = DefineParameter(BooleanType, 'Dynamic Max Open Size', description, false, true, inputSuggestions )

description      = 'MUST be filled even if using Cross Margin. Important for trading budget.'
inputSuggestions = 'Input'
pleverage = DefineParameter(NumberType, 'Leverage', description, false, 75, inputSuggestions) 

description      = 'How much from available margin allocated for this bot. 0.5 is 50% of available margin.'
inputSuggestions = 'Input'
pmaxBudget = DefineParameter(NumberType, 'Margin Budget %', description, false, 0.8, inputSuggestions )

description      = 'How much from the Margin Budget is for opening positions. 0.1 is 10% of Margin Budget'
inputSuggestions = 'Input'
pmaxOpen = DefineParameter(NumberType, 'Position Budget %', description, false, 0.2, inputSuggestions)

description      = 'ONLY if bot trading on INVERSE Futures then enter the Contract Value. Ignore if trade on USDT'
inputSuggestions = 'Input'
pcontVal = DefineParameter(NumberType, 'COIN-M Value', description, false, 10, inputSuggestions)

source = DefineParameter(ListNumberType, 'source', 'The source values to base the price changes on.', false, ClosePrices(), '')

description      = 'Enable Logging'
inputSuggestions = 'Input'
pIsLogging = DefineParameter(BooleanType, 'Logging', description, false, false, inputSuggestions )

description      = 'Enable Verbose Logging'
inputSuggestions = 'Input'
pIsVerbose = DefineParameter(BooleanType, 'Verbose', description, false, false , inputSuggestions )

--  ========================================================
--    Functions
--  ========================================================
 
--  ------------------------------------
--    Function Category
--  ------------------------------------
 
local MaxPosition = function (walletBal, maxSizeM, autoMax, leverage, maxBudget, maxOpen, contVal, Logging, Verbose)

  
  local profitLabel = ProfitLabel()
  if profitLabel == nil then profitLabel = QuoteCurrency() end
  --local availableMargin = WalletAmount(PriceMarket(), profitLabel)
  local maxMax

  -- dynamic slot size and max position 
    --max position
        if profitLabel == 'USD' or profitLabel == 'USDT' then
            maxMax = walletBal * maxBudget * maxOpen / source 
 
        else
            maxMax = walletBal * maxBudget * maxOpen * source / contVal 
        
        end
 
        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  
        CC_DebugLogger(Logging, nil, nil, nil, Verbose)
          if autoMax then
            if maxCheck > maxSizeM then
              CC_DebugLogger(Logging, nil, {Parse('Dynamic Max Open: '..Round(maxCheck, 5)..' '..AmountLabel(), StringType)}, nil, Verbose)
            else        
              CC_DebugLogger(Logging, nil, {Parse('Dynamic Max Open: '..Round(maxCheck, 5)..' but using default value: '..Round(maxSizeM, 5)..' '..AmountLabel(), StringType)}, nil, Verbose)
            end
          else 
            CC_DebugLogger(Logging, nil, {Parse('Max Open: '..Round(maxSizeM, 5)..' '..AmountLabel(), StringType)}, nil, Verbose)
          end 
        
            
  --return {maxCheck, maxValue, maxSize}
  return {
  check  = maxCheck,
  value  = maxValue,
  size   = maxSize
  }
  --return maxValue
 
end
 
--  ========================================================
--    Execution
--  ========================================================
local output = {}
output = MaxPosition(pwalletBal, pmaxSizeM, pautoMax, pleverage, pmaxBudget, pmaxOpen, pcontVal, pIsLogging, pIsVerbose)

--  ========================================================
--    Output Definitions
--  ========================================================
 
description       = ''
outputSuggestions = 'output'
DefineOutput(ListDynamicType, output, description, outputSuggestions)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!