[ghibli snippet] control bot’s position from the exchange v1.0

0 258 Views No Comments 3 months ago
  • — Have you ever wanted to open a new position and let the bot manage it (or close bot’s position manually),
    — but did not want to login to Haas interface and simply wanted to do it from the exchange app or website?
    — This snippet allows the user to change the bot’s position directly by making orders on the exchange itself

    — TO BE UPDATED LATER: Currently only opens new positions, if the bot has no open position. Does not close a bot’s position. Can’t flip from Long to Short or vice versa.
    — Important: this snippet is designed to be used with EnableHighFrequency() mode, but will also work without it, with a delay of 4 update cycles
    — The 4 update cycles delay is implemented intentionally because otherwise this script will just mess with your bot’s regular trades (as UserPosition() and GetAllOpenPositions() sometimes desync for 1-3 update cycles).
    — The snippet copies the PRICE of the position entry and also the AMOUNT of the position from the exchange.

    — IMPORTANT: Note that ANY position change on this pair on this account that you manually make on the exchange will be taken into account by the bot,
    — even if the Trading Amount is not the same in the bot as in that manual position change

    — Note that all the variables used inside this snippet are not valid outside the snippet, since they are local (but of course you can change that)

    — DISCLAIMER: you should read and understand how this snippet works,
    — the author takes no responsibility for any losses that may occur from either
    — the user not implementing the snippet properly nor for the snippet not working as expected.

    HaasScript Code
    EnableHighSpeedUpdates(true)
    
    if true then -- Ghibli Snipper (control bot's position manually from exchange)
        local allpos = GetAllOpenPositions() -- Get bot's (virtual) position
        local cposs = 'None'
        if allpos[1] ~= nil then
            if allpos[1].positionId ~= nil then
                if allpos[1].isLong then
                    cposs = 'Long'
                else
                    cposs = 'Short'
                end
            end
        end
        local userpos = UserPositionContainer() -- Get "Real" position from the exchange
        local cpos = 'None'
        local ghibli = Load('ghibli', 6)
        if userpos.isLong == true then
            cpos = 'Long'
        elseif userpos.isShort == true then
            cpos = 'Short'
        end
        -- If the bot's position and the position from the exchange aren't matching, 
        -- then the bot will copy exchange's position in 4 update cycles
        if cposs ~= cpos then
            ghibli = ghibli - 1
        else
            ghibli = 6
        end
        if ghibli <3 then
            if cposs == 'None' then -- After "NoPosition" becomes available, delete this line
                if cpos == 'Long' then
                    if cposs == 'Short' then
                        Log('Closed a short position on exchange')
                        -- Insert command to close virtual position here (once it's available in HaasScript)
                    end
                    CreatePosition(PositionLong, userpos.enterPrice, MaxExitLongAmount())
                    Log('Opened a long position on exchange')
                    ghibli = 6
                elseif cpos == 'Short' then
                    if cposs == 'Long' then
                        Log('Closed a long position on exchange')
                        -- Insert command to close virtual position here
                    end
                    CreatePosition(PositionShort, userpos.enterPrice, MaxExitShortAmount())
                    Log('Opened a short position on exchange')
                    ghibli = 6
                elseif cpos == 'None' then
                    -- Insert command to close virtual position here
                    Log('Trying to make NoPosition')
                    ghibli = 6
                end
            end
        end -- After "NoPosition" becomes available, delete this line
        Save('ghibli', ghibli)
    end
Login or Register to Comment

Unlock your crypto trading potential

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

Join for Free