[pshaiBot] Deribot

2 364 Views No Comments 4 months ago
  • Somewhat advanced scalper bot with nice features and settings. And as the name suggests, it works well in LEVERAGE MARKETS.

    At start, bot places 2 orders: Long entry and short entry.

    Once one of these is filled, the bot is taken a position and will either continue to grow that position, or exits it.

    There is also a stop-loss that will trigger at “StopLoss Offset” away from the AVERAGE ENTRY PRICE(*) once the LAST ORDER in the grid is filled.

    If you have any improvements and/or suggestions, please let me know.

    (*) Bot uses HaasScript’s virtual positions to monitor average entry prices. So any manual trades made in the same account the bot is using, will not affect the bot’s actions.

    **NOTE: This bot doesn’t place a grid beforehand, but 2 orders at a time. The idea behind this was to be able to run as many bots as possible, so using only 2 orders at a time per bot was my final decision.**

    • This topic was modified 3 months, 2 weeks ago by pshai. Reason: Clarification
    • This topic was modified 3 months, 2 weeks ago by pshai.
    • This topic was modified 3 months, 2 weeks ago by pshai.
    HaasScript Code
    EnableHighSpeedUpdates()
    HideTradeAmountSettings()
    HideOrderSettings()
    
    local positions_options = {
        'Long only',
        'Short only',
        'Both'
        }
    
    -- INPUTS
        local input_positions = InputOptions('1. Positions', positions_options[1], positions_options, '', 'General')
        local input_gridOrders = Input('1. Grid orders', 3, '', 'Grid')
        local input_gridStep = Input('2. Step size', 25, '', 'Grid')
        local input_stepMult = Input('3. Step Multiplier', 3, '', 'Grid')
        local input_gridSize = Input('4. Grid order size', 10, '', 'Grid')
        local input_sizeMult = Input('5. Size multiplier', 3, '', 'Grid')
        local input_takeStep = Input('6. Take step', 25, '', 'Grid')
        local input_slOffset = Input('1. StopLoss offset', 70, '', 'Safeties')
        local input_slTimer = Input('2. StopLoss timer', 2500, '', 'Safeties')
    
    
    -- VARIABLES
        local cp = CurrentPrice()
        local lpid = Load('lpid', NewGuid()) -- long position id
        local spid = Load('spid', NewGuid()) -- short position id
        local pos_long = PositionContainer(lpid)
        local pos_short = PositionContainer(spid)
        local order_count = Load('oc', 0)
        local cooldownTimer = Load('cdt', 0)
        local leid = Load('leid', '')
        local seid = Load('seid', '')
        local exid = Load('exid', '')
        local basePrice = Load('bp', cp.close)
        local slPrice_long = pos_long.enterPrice - input_slOffset
        local slPrice_short = pos_short.enterPrice + input_slOffset
    
    
    -- TRADE LOGIC
        if exid == '' and pos_long.isLong == false and pos_short.isShort == false then
            if cooldownTimer < Time() and IsAnyOrderOpen() == false then
                basePrice = cp.close
                if input_positions == positions_options[1] or input_positions == positions_options[3] then
                    leid = PlaceGoLongOrder(basePrice - input_gridStep, input_gridSize, {type=MakerOrCancelOrderType, timeout=9999, positionId=lpid, note='L-E 1 ('..lpid..')'})
                end
    
                if input_positions == positions_options[2] or input_positions == positions_options[3] then
                    seid = PlaceGoShortOrder(basePrice + input_gridStep, input_gridSize, {type=MakerOrCancelOrderType, timeout=9999, positionId=spid, note='S-E 1 ('..spid..')'})
                end
                LogWarning('----- Placed first orders')
            end
        else
            if pos_long.isLong then
                if leid != '' then
                    if IsOrderFilled(leid) then
                        LogWarning('LONG order filled, continuing... (order '..(order_count+1)..'/'..input_gridOrders..')')
                        CancelAllOrders()
                        order_count = order_count + 1
                        leid = ''
                        seid = ''
                        exid = ''
                    
                    elseif IsOrderOpen(leid) == false then
                        leid = ''
                    end
                end
    
    
            elseif pos_short.isShort then
                if seid != '' then
                    if IsOrderFilled(seid) then
                        LogWarning('SHORT order filled, continuing... (order '..(order_count+1)..'/'..input_gridOrders..')')
                        CancelAllOrders()
                        order_count = order_count + 1
                        leid = ''
                        seid = ''
                        exid = ''
                    
                    elseif IsOrderOpen(seid) == false then
                        seid = ''
                    end
                end
            end
    
            local amt = input_gridSize * Pow(input_sizeMult, order_count)
            local step = input_gridStep * Pow(input_stepMult, order_count)
    
            if order_count < input_gridOrders then
                if leid == '' and pos_long.isLong then
                    leid = PlaceGoLongOrder(basePrice - step, amt, {type=MakerOrCancelOrderType, timeout=999999, positionId=lpid, note='L-E '..(order_count+1)..' ('..lpid..')'})
                    LogWarning('Placed next LONG order (order '..(order_count+1)..'/'..input_gridOrders..')')
                
                
                elseif seid == '' and pos_short.isShort then
                    seid = PlaceGoShortOrder(basePrice + step, amt, {type=MakerOrCancelOrderType, timeout=999999, positionId=spid, note='S-E '..(order_count+1)..' ('..spid..')'})
                    LogWarning('Placed next SHORT order (order '..(order_count+1)..'/'..input_gridOrders..')')
                end
            end
        end
    
        if exid != '' then
            if IsOrderFilled(exid) then
                LogWarning('----- EXITED! Resetting...')
                CancelAllOrders()
                order_count = 0
                leid = ''
                seid = ''
                exid = ''
                lpid = NewGuid()
                spid = NewGuid()
            
            
            elseif IsOrderOpen(exid) == false then
                exid = ''
            end
        else
            local p, posid
    
            if pos_long.isLong then
                p = pos_long.enterPrice + input_takeStep
                posid = lpid
                exid = PlaceExitPositionOrder({price = p, type=MakerOrCancelOrderType, timeout=999999, positionId=posid, note='L-X ('..lpid..')'})
                LogWarning('Placed LONG EXIT order at '..p)
                 
            elseif pos_short.isShort then
                p = pos_short.enterPrice - input_takeStep
                posid = spid
                exid = PlaceExitPositionOrder({price = p, type=MakerOrCancelOrderType, timeout=999999, positionId=posid, note='S-X ('..spid..')'})
                LogWarning('Placed SHORT EXIT order at '..p)
            end
        end
        
        if order_count >= input_gridOrders and pos_long.isLong and cp.bid <= slPrice_long then
            CancelAllOrders()
            exid = PlaceExitPositionOrder({price = 0, type=MarketOrderType, positionId=lpid, note='L-SL ('..lpid..')'})
            LogWarning('Triggered LONG SL EXIT at '..slPrice_long)
            cooldownTimer = Time() + input_slTimer
    
    
        elseif order_count >= input_gridOrders and pos_short.isShort and cp.ask >= slPrice_short then
            CancelAllOrders()
            exid = PlaceExitPositionOrder({price = 0, type=MarketOrderType, positionId=spid, note='S-SL ('..spid..')'})
            LogWarning('Triggered SHORT SL EXIT at '..slPrice_short)
            cooldownTimer = Time() + input_slTimer
        end
    
    
    
    -- MEMORY MANAGEMENT
        Save('oc', order_count)
        Save('cdt', cooldownTimer)
        Save('lpid', lpid)
        Save('spid', spid)
        Save('leid', leid)
        Save('seid', seid)
        Save('exid', exid)
        Save('bp', basePrice)
Login or Register to Comment

Unlock your crypto trading potential

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

Join for Free