[pshaiCmd] Break Even Safety

stable
By pshai in Safeties Published March 2021 👁 2,265 views 💬 3 comments

Description

Here is a simple Break Even Safety command. The inputs for the commands are: - Minimum required percentage price change for safety to ACTIVATE - Number of rebounds allowed before TRIGGERING the safety - Optional position ID How it works: When your bot enters a position, it gets an average entry price. This price will become the break-even trigger price, once the activation level has been breached. An example; we just entered a long position at 100, our minimum % required is 1%. If the price reaches the 1% mark (or the price of 101), the trigger price (which is our entry price) will be activated as a type of "stop-loss level". Once our entry price is breached after activation, the bot will trigger a signal which you can use to exit the position near break-even.
HaasScript
-- Author: pshai

DefineCommand('BreakEven', 'Sets or triggers a stop-loss at breakeven after X% breached.')

local min_prc = DefineParameter(NumberType, 'min_prc', 'Minimum required percentage price change for safety to activate', true, 1, 'Number, Input')
local max_rebounds = DefineParameter(NumberType, 'max_rebounds', 'Number of rebounds allowed before triggering the safety', false, 0, 'Number, Input')
local pos_id = DefineParameter(StringType, 'pos_id', 'Optional position ID', false, '', 'PositionContainer, Text, Load, SessionGet')

local pos = PositionContainer(pos_id)
local market = pos.market
local result = false
local cp = CurrentPrice(market)
local active = Load('active'..pos.positionId, false)
local prev_id = Load('prev_id'..pos.positionId, pos.positionId)
local rebound_count = Load('rebound_count'..pos.positionId, 0)
local rebound_level = Load('rebound_level'..pos.positionId, 0)

-- Update rebound count if used
if max_rebounds > 0 and rebound_level > 0 
and ((pos.isLong and cp.close > rebound_level)
or (pos.isShort and cp.close < rebound_level)) then
    rebound_count = rebound_count + 1
    LogWarning('[BE] '..market..': Rebound of '..rebound_count..'/'..max_rebounds..'.')
    rebound_level = 0
end

if PercentagePriceChange(min_prc, {positionId = pos_id}) then
    active = true

elseif active then

    if (pos.isLong and cp.bid <= pos.enterPrice)
    or (pos.isShort and cp.ask >= pos.enterPrice) then
        
        -- Prepare for a rebound if used
        if max_rebounds > 0 and rebound_count < max_rebounds and rebound_level <= 0 then
            rebound_level = cp.close
            LogWarning('[BE] '..market..': Rebound target set at '..rebound_level..'.')
        end
        
        if rebound_count >= max_rebounds then
            result = true 
        end
    end
end

Save('active'..pos.positionId, active)
Save('prev_id'..pos.positionId, prev_id)
Save('rebound_count'..pos.positionId, rebound_count)
Save('rebound_level'..pos.positionId, rebound_level)


DefineOutput(BooleanType, result, 'True if safety is triggered', 'DoExitPosition, PlaceExitPositionOrder, PlaceExitLongOrder, PlaceExitShortOrder')

3 Comments

Sign in to leave a comment.

K
Kobalt about 5 years ago

I think this could be a great enhancement to include in Profit Trailer (when it keeps falling below Trailing start you can set these rebounds to exit...
What I'm still curious and am looking into scaling to properly scale up and back down the Start triggers need a growth model to scale from so was thinking
monitor the % distance it is making then output this and trail the trailings if that makes sense positions performance in-decrease triggers a scaled to match response factoring in the recent HL spikes to perhaps counter a negative one against a SL with a 5-9 minute timer so flash wicks are skipped...

But back to it..... the main conclusion : It needs to be adaptive.
​geared towards protecting profits TSSL system so it can grow to manage a big often quick trend

Starting with a 6% trail start and 2-3 percent is not optimal for when you enter it will still get you either little action or still get you out early...on BNB, BUSD these are 1 minute low liquidity events and like Benji our ninja would say Catching da noise here starts at 1% but more often multiples of 1.4, 3, 6, 8 nibbling at the BNB's but that's consolidation noise not a trend..... these go >100%
When the bulls are back....lol

P.s.
TLDR: will experiment with this

K
Kobalt almost 4 years ago

Just noticed this again....

These rebounds... they allow price to drop below activation, trigger Price right?

--this defeats the purpose of safety.
-- but it is great when in profit and your position leveraged a rolling Rebounds with a reducing trailDistance could be just what you need to lock in profits without exiting or wasting it on distance..

activate rebounds on choppy moves rising, falling reversals and after a min BotProfit
--compare rebound results to Activate exit
-- If condition before rebounds is a sharp price move do not use rebounds

P
pshai about 3 years ago

The rebounds can be used when the market is really choppy (or you are trading really tightly) and you dont want to immediately get break-evened. Setting max rebounds to zero will make it trigger right away when conditions are met.