[pshaiCmd] Get Dynamic Trade Amount

stable
By pshai in Safeties Published April 2024 👁 1,093 views 💬 1 comments

Description

Hey fellas! This CC is based on the example I shared here: https://www.haasscripts.com/t/example-position-sizing-based-on-pre-determined-acceptable-loss-stop-loss-size/ Usage:

local acceptable_loss = 100 -- USDT
local entry_price = CurrentPrice().ask -- we buy the ask price
local stop_loss_price = SubPerc(entry_price)

local trade_amount = CC_GetDynamicTradeAmount(acceptable_loss, entry_price, stop_loss_price)
local margin_amount = CC_GetDynamicTradeAmount(acceptable_loss, entry_price, stop_loss_price, 'margin')
local notional_amount = CC_GetDynamicTradeAmount(acceptable_loss, entry_price, stop_loss_price, 'notional')
HaasScript
-- [pshaiCmd] Get Dynamic Trade Amount
-- Author: pshai

DefineCommand('GetDynamicTradeAmount', 'Get dynamic trade amount, margin or notional size based on inputs.')

local acceptable_loss = DefineParameter(
        NumberType,
        'acceptable_loss',
        'The acceptable loss amount.',
        true,
        100,
        'Input'
    )
local entry_price = DefineParameter(
        NumberType,
        'entry_price',
        'The entry price.',
        true,
        CurrentPrice().ask
    )
local stop_loss_price = DefineParameter(
        NumberType,
        'stop_loss_price',
        'The stop-loss price.',
        true,
        SubPerc(entry_price, 2)
    )
local type = DefineParameter(
        StringType,
        'result_type',
        'Result type, options: "default" (coin/contracts), "margin" or "notional".',
        false,
        'default'
    )
local leverage = DefineParameter(
        NumberType,
        'leverage',
        'Leverage multiplier (use 1 for SPOT). Uses Leverage() by default.',
        false,
        Leverage()
    )

local stop_loss_per_unit = Abs(entry_price - stop_loss_price)
local position_size = acceptable_loss / stop_loss_per_unit
local investment_size = UsedMargin(PriceMarket(), entry_price, position_size, Leverage())
local notional_size = investment_size * Leverage()

local result

if type == 'default' then
    result = position_size
elseif type == 'margin' then
    result = investment_size
elseif type == 'notional' then
    result = notional_size
else
    LogError('CC_GetDynamicTradeAmount :: Unknown result_type "'..type..'".')
    result = 0
end

DefineOutput(NumberType, result, 'Result value.')

1 Comment

Sign in to leave a comment.

M
Maghfur about 2 years ago

Try it, ok thank