custom ProfitTrailer for 'Trend Absorber' template

stable
By Kobalt in Safeties Published September 2022 👁 1,690 views 💬 0 comments

Description

custom ProfitTrailing to log, log verbose, plot start, if logging only when position is not closed. dependency in: https://www.haasscripts.com/t/ve-unmanaged-trading-basic-trend-absorber-current-vs-entry-budget-checkstrailing-exits/
HaasScript
DefineCommand("PTQpamt", "ProfitTrailer Quiet, Plotting, ROI, pamt, NO Inputs, 0, 0")
 
local trailStartPrc = DefineParameter(NumberType, "trailStartPrc", "Profit level (as percentage) where the trailing starts.", true, 12, "Input")
local trailDistPrc = DefineParameter(NumberType, "trailDistPrc", "Trailing distance (as percentage) from the highest recorded profit.", true, 3, "Input")
local trailMode = DefineParameter(StringType, "trailMode", "Trailing mode. Use one of the following: 'default', 'grow', or 'shrink'.", false, "default", "Input")
local maxRebounds = DefineParameter(NumberType, "maxRebounds", "Maximum rebounds allowed. If not set, then rebound is not used. Default is 0 (disabled).", false, 0, "Input")
local positionId = DefineParameter(StringType, "positionId", "Unique position ID.", false, "", "Load" )
local ptiqname = DefineParameter(StringType, "name", "Name to distinguish it from others if used multiples in one script.", false, "", "Text" )
local isLogging = DefineParameter(BooleanType, "logging", "Turn logging on or off.", false, true, "" )
local isVerbose = DefineParameter(BooleanType, "verbose", "Log ROI.", false, false, "" )
local isPlotting = DefineParameter(BooleanType, "plotting", "Turn plotting on or off.", false, true, "" )
--local isClosed = DefineParameter(BooleanType, "isClosed", "Signal exit succes.", false, false, "" )

-- Input fields 
--InputGroupHeader(''..ptiqname..'Profit Trailer Settings')
--local trailStartPrc = Input("   Trailing Start %", 0.72, "Profit level (as percentage) where the trailing starts.", ptiqname.."PT")
--local trailDistPrc = Input("  Trailing Distance %", 0.18, "Trailing distance (as percentage) from the highest recorded profit.", ptiqname.."PT")
--local trailMode = Input(" Trailing Mode", 'default', "Trailing mode. Use one of the following: 'default', 'grow', or 'shrink'.", ptiqname.."PT")
--local maxRebounds = Input(" Max. Rebounds", 0, "Maximum rebounds allowed. If not set, then rebound is not used. Default is 0 (disabled).", ptiqname.."PT")
--local isLogging = Input("Logging", true, "", ptiqname.."PT")
--local isVerbose = Input("Verbose Logging", false, "", ptiqname.."PT")

local position = PositionContainer(positionId)
if positionId == "" then positionId = position[1] end --override empty position id
 
local cp = CurrentPrice()
local market = position[2]
local isLong = position[3]
local direction = GetPositionDirection(positionId)
local roi = GetPositionROI(positionId)
local ep = GetPositionEnterPrice(positionId)
local pamt = GetPositionAmount(positionId)
local isClosed = IsPositionClosed(positionId)
local isTrailing = Load('isTrailing'..positionId, false)
local highestRoi = Load('highestRoi'..positionId, roi)
local rebounds = Load('rebounds'..positionId, 0)
local reboundLevel = Load('reboundlevel'..positionId, 0)

--[[if pamt == 0 then 
local isClosed = Load('isClosed', true)
if pamt > 0 then 
local isClosed = Load('isClosed', false)
end]]

maxRebounds = Abs(maxRebounds)
 
local result = false
if not isClosed and direction != NoPosition then
 
    local roi = 0
    if direction == PositionLong then
        roi = (cp.close - ep) / cp.close * 100
    else
        roi = (ep - cp.close) / ep * 100
    end
    if isVerbose and pamt > 0 then
        Log(market..': Current profit: '..roi..' %')
        Log(market..': Position: '..IfElse(isLong, 'Long', 'Short'))
    end
 
    -- Check if we need to start trailing
    if not isTrailing then
        if trailStartPrc < roi then
            isTrailing = true
 
            if isLogging and pamt > 0 then
                LogWarning(market..': Started trailing.')
            end
        end
 
    end
 
  -- plot the start-line
    if true then
        local start_line = 0
 
        if direction == PositionLong then
            start_line = AddPerc(ep, trailStartPrc)
        else
            start_line = SubPerc(ep, trailStartPrc)
        end
        if isPlotting then
        Plot(0, 'PT-Start', start_line, {c = Gray(77), id = positionId})
        end
    end
    -- Update trailing
    if isTrailing and pamt > 0 then
        highestRoi = Max(roi, highestRoi)
        local trailStop = 0
 
        -- Check if we need to stop trailing
        if roi < trailStartPrc then
            isTrailing = false
            rebounds = 0
            highestRoi = 0
            reboundLevel = 0
 
            if isLogging and pamt > 0 then
                LogWarning(market..': Profits have dropped below start value. Trailing is stopped, better luck next time.')
            end
        end
 
        -- Default mode
        if trailMode == "default" then
            trailStop = highestRoi - trailDistPrc
 
        -- Grow mode: trail distance grows as profit grows
        elseif trailMode == "grow" then
            local multiplier = trailDistPrc / trailStartPrc
            local newDist = multiplier * highestRoi
            trailStop = highestRoi - newDist
 
        -- Shrink mode: trail distance shrinks as profit grows
        elseif trailMode == "shrink" then
            local multiplier = trailDistPrc * trailStartPrc
            local newDist = multiplier / highestRoi
            trailStop = highestRoi - newDist
        end
 
        -- Update rebound count if used
        if maxRebounds > 0 and reboundLevel > 0 and roi > reboundLevel then
            rebounds = rebounds + 1
            if isLogging then
                LogWarning(market..': Rebound of '..rebounds..'/'..maxRebounds..'.')
            end
            reboundLevel = 0
        end
 
        -- Check if we need or can exit...
        if trailStop > roi then
 
            -- Prepare for a rebound if used
            if maxRebounds > 0 and rebounds < maxRebounds and roi >= 0 and reboundLevel <= 0 then
                reboundLevel = roi
                highestRoi = roi
                if isLogging and pamt > 0 then
                    LogWarning(market..': Rebound target of '..reboundLevel..' % is set.')
                end
            end
 
            -- Signal for exit if we have enough profits
            if roi > trailStartPrc then
                if rebounds >= maxRebounds then
                    result = true
                    if isLogging and not isClosed then
                        LogWarning(market..': Signaling exit.')
                    end
                end
                if isClosed then Log('Exit success!', Purple)
                end
            else
                -- plot the start-line
    local isPlotting = true
    local exitTriggerPlotColor
    if true then
        local start_line = 0
        if direction == PositionLong then
            start_line = AddPerc(ep, trailStartPrc)
        else
            start_line = SubPerc(ep, trailStartPrc)
        end
                if direction == PositionLong then
            exitTriggerPlotColor = Red
        else
            exitTriggerPlotColor = Green 
        end
 if isPlotting and pamt > 0 then
        Plot(0, 'PT-Start', start_line, {c = exitTriggerPlotColor(88), id = positionId})
    end
 end
                if isLogging and pamt > 0 then
                    LogWarning(market..': Not enough profits to exit position, still watching it.')
                end
            end
        end
 
    end
 
end
 
-- Store values for next update
Save('isTrailing'..positionId, isTrailing)
Save('highestRoi'..positionId, highestRoi)
Save('rebounds'..positionId, rebounds)
Save('reboundlevel'..positionId, reboundLevel)
Save('isClosed', positionId)

 
-- Define output signal
DefineOutput(BooleanType, result, "True if safety has triggered, otherwise false", "SafetyContainer, And, Or, IfElse, IfElseIf")

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!