ProfitTrailerQuiet

2 108 Views No Comments 1 month ago
  • A ProfitTrailer implementation by Pshai. Works like a safety and returns true when active. Modded by Firetron to have parameters to toggle the logging and verbose logging.

    HaasScript Code
    DefineCommand("ProfitTrailerQuiet", "A ProfitTrailer implementation by Pshai. Works like a safety and returns true when active.")
    
    local trailStartPrc = DefineParameter(NumberType, "trailStartPrc", "Profit level (as percentage) where the trailing starts.", true, 0, "Input")
    local trailDistPrc = DefineParameter(NumberType, "trailDistPrc", "Trailing distance (as percentage) from the highest recorded profit.", true, 0, "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 isLogging = DefineParameter(StringType, "logging", "Turn logging on or off.", false, false, "" )
    local isVerbose = DefineParameter(StringType, "verbose", "Log extra detail.", false, false, "" )
    
    
    local position = PositionContainer(positionId)
    if positionId == "" then positionId = position[1] end --override empty position id
    
    local market = position[2]
    local isLong = position[3]
    local direction = GetPositionDirection(positionId)
    local roi = GetPositionROI(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)
    
    maxRebounds = Abs(maxRebounds)
    
    local result = false
    
    if direction != NoPosition then
    
        if isVerbose 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 then
                    LogWarning(market..': Started trailing.')
                end
            end
    
        end
    
    
        -- Update trailing
        if isTrailing 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 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 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 then
                            LogWarning(market..': Signaling exit.')
                        end
                    end
                else
                    if isLogging 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)
    
    
    -- Define output signal
    DefineOutput(BooleanType, result, "True if safety has triggered, otherwise false", "SafetyContainer, And, Or, IfElse, IfElseIf")
Login or Register to Comment

Unlock your crypto trading potential

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

Join for Free