[HaasBot Essentials] PTQ: ProfitTrailer (Quiet, Inputs, trailStart 2, trailDistance 0.6)

stable
By Kobalt in Safeties Published January 2021 👁 1,876 views 💬 1 comments

Description

Much loved PT by pshai, Logging silenced by Firetron https://www.haasscripts.com/t/profittrailerquiet/ 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. trail biiig profits... used in a MadHatter script. This has Inputs and...more or less... Oh the Indicator containers could also use some Q(uiet..)
HaasScript
DefineCommand("ProfitTrailerQ", "ProfitTrailer (Quiet, Inputs, trailStart 2, trailDistance 0.6)")
 
local trailStartPrc = DefineParameter(NumberType, "trailStartPrc", "Profit level (as percentage) where the trailing starts.", false, 0, "Input")
local trailDistPrc = DefineParameter(NumberType, "trailDistPrc", "Trailing distance (as percentage) from the highest recorded profit.", false, 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 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, false, "" )
local isVerbose = DefineParameter(BooleanType, "verbose", "Log extra detail.", false, false, "" )

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

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")

1 Comment

Sign in to leave a comment.

V
VxLCLi over 5 years ago

Could you please Add picture of your Advance Bot Function