[pshaiBot] RSI-VWAP Trading Bot for Deribit (port)

0 158 Views No Comments 4 weeks ago
  • Ported trading bot from this post: https://docs.wunderbit.co/free-tradingview-strategies/trading-bot-for-deribit/rsi-vwap-trading-bot-for-deribit

    Plots are not converted, only trading logic and the necessities.

    ~~May the profits be with you~~
    ~pshai

    REQUIRED COMMANDS:
    – CC_HighSpeedPrices : https://www.haasscripts.com/t/pshaicmd-highspeedprices/
    – CC_VWAP : https://www.haasscripts.com/t/pshaicmd-volume-weighted-average-price-vwap/

    • This topic was modified 3 weeks, 4 days ago by pshai.
    • This topic was modified 3 weeks, 4 days ago by pshai.
    HaasScript Code
    -- RSI-VWAP trading bot for Deribit
    -- ported by pshai
    --
    -- original post: https://docs.wunderbit.co/free-tradingview-strategies/trading-bot-for-deribit/rsi-vwap-trading-bot-for-deribit
    
    EnableHighSpeedUpdates(true)
    
    -- not used in script but might be useful
    function valuewhen(trigger, values, offset)
        local prev = Load('vw_prev', 0)
        offset = not offset and 0 or offset
        if trigger then
            local type = GetType(values)
            if type == ArrayDataType or type == TextDataType then
                prev = values[offset+1]
            elseif type == FunctionDataType then
                prev = values()
            else
                prev = values
            end
        end
    
        Save('vw_prev', prev)
        return prev
    end
    
    cp = CurrentPrice()
    
    --/ TREND
    InputGroupHeader('Trend Ribbon')
    ribbon_period = Input("1. Period", 14)
    hsp = CC_HighSpeedPrices()
    close = hsp.close
    
    leadLine1 = EMA(close, ribbon_period)
    leadLine2 = SMA(close, ribbon_period)
    
    -- p1 = plot(leadLine1, color= #53b987, title="EMA", transp = 50, linewidth = 1)
    -- p2 = plot(leadLine2, color= #eb4d5c, title="SMA", transp = 50, linewidth = 1)
    -- fill(p1, p2, transp = 60, color = leadLine1 > leadLine2 ? #53b987 : #eb4d5c)
    
    -- Initial Inputs
    InputGroupHeader("RSI-VWAP")
    Act_RSI_VWAP_long = Input("1. RSI VOLUME WEIGHTED AVERAGE PRICE LONG", true)
    RSI_VWAP_length_long = Input("2. RSI-VWAP LENGTH LONG", 20)
    RSI_VWAP_overSold_long = Input("3. RSI-VWAP OVERSOLD LONG", 24)
    RSI_VWAP_overBought_long = Input("4. RSI-VWAP OVERBOUGHT LONG", 94)
    
    Act_RSI_VWAP_short = Input("5. RSI VOLUME WEIGHTED AVERAGE PRICE SHORT", true)
    RSI_VWAP_length_short = Input("6. RSI-VWAP LENGTH SHORT", 16)
    RSI_VWAP_overSold_short = Input("7. RSI-VWAP OVERSOLD SHORT", 12)
    RSI_VWAP_overBought_short = Input("8. RSI-VWAP OVERBOUGHT SHORT", 66)
    
    -- RSI with VWAP as source
    RSI_VWAP_long = RSI(CC_VWAP({resetInterval=1440}), RSI_VWAP_length_long)
    RSI_VWAP_short = RSI(CC_VWAP({resetInterval=1440}), RSI_VWAP_length_short)
    
    -- Plot Them Separately.
    -- Plotting LONG, Put overlay=false
    -- r=plot(RSI_VWAP_long, color = RSI_VWAP_long > RSI_VWAP_overBought_long ? color.red : RSI_VWAP_lnog < RSI_VWAP_overSold_long ? color.lime : color.blue, title="rsi", linewidth=2, style=plot.style_line)
    -- h1=plot(RSI_VWAP_overBought_long, color = color.gray, style=plot.style_stepline)
    -- h2=plot(RSI_VWAP_overSold_long, color = color.gray, style=plot.style_stepline)
    -- fill(r,h1, color = RSI_VWAP_long > RSI_VWAP_overBought_long ? color.red : na, transp = 60)
    -- fill(r,h2, color = RSI_VWAP_long < RSI_VWAP_overSold_long ? color.lime : na, transp = 60)
    
    -- Plotting SHORT, Put overlay=false
    -- r=plot(RSI_VWAP_short, color = RSI_VWAP_short > RSI_VWAP_overBought_short ? color.red : RSI_VWAP_short < RSI_VWAP_overSold_short ? color.lime : color.blue, title="rsi", linewidth=2, style=plot.style_line)
    -- h1=plot(RSI_VWAP_overBought_short, color = color.gray, style=plot.style_stepline)
    -- h2=plot(RSI_VWAP_overSold_short, color = color.gray, style=plot.style_stepline)
    -- fill(r,h1, color = RSI_VWAP_short > RSI_VWAP_overBought_short ? color.red : na, transp = 60)
    -- fill(r,h2, color = RSI_VWAP_short < RSI_VWAP_overSold_short ? color.lime : na, transp = 60)
    
    
    -- position information
    position = PositionContainer()
    aep = position.enterPrice
    
    
    ------/  STRATEGY Take Profit / Stop Loss --------
    InputGroupHeader("Take-Profit / Stop-Loss")
    ------ LONG ------
    long_tp_inp = Input('1. Long Take Profit %', 20)
    long_sl_inp = Input('2. Long Stop Loss %', 2.5)
    long_trailing = Input('3. Trailing Stop Long', 100)
    
    --long_take_level = aep * (1 + long_tp_inp)
    --long_stop_level = aep * (1 - long_sl_inp)
    
    ------ SHORT ------
    short_tp_inp = Input('4. Short Take Profit %', 8.5)
    short_sl_inp = Input('5. Short Stop Loss %', 4.5)
    short_trailing = Input('6. Trailing Stop short', 100)
    
    --short_take_level = aep * (1 - short_tp_inp)
    --short_stop_level = aep * (1 + short_sl_inp)
    
    --/Strategy_Conditions
    --/ LONG --/
    entry_long =CrossOver(RSI_VWAP_long, RSI_VWAP_overSold_long) and leadLine2 < leadLine1
    entry_price_long=valuewhen(entry_long,close,0)
    exit_long =CrossUnder(RSI_VWAP_long, RSI_VWAP_overBought_long) or TrailingStopLoss(long_trailing) or StopLoss(long_sl_inp) or TakeProfit(long_tp_inp)
    
    --/ SHORT --/
    entry_short =CrossUnder(RSI_VWAP_short, RSI_VWAP_overBought_short) and leadLine2 > leadLine1
    entry_price_short=valuewhen(entry_short,close,0)
    exit_short =CrossOver(RSI_VWAP_short, RSI_VWAP_overSold_short) or TrailingStopLoss(short_trailing) or StopLoss(short_sl_inp) or TakeProfit(short_tp_inp)
    
    
    function placeOrder(isLong, isExit)
        local name = (isLong and 'L' or 'S') .. (isExit and 'X' or '')
        local oid = Load(name..'oid', '')
        local timer = Load(name..'timer', 0)
        local cmd = not isExit and (isLong and PlaceGoLongOrder or PlaceGoShortOrder) or (isLong and PlaceExitLongOrder or PlaceExitShortOrder)
        local amount = not isExit and TradeAmount() or position.amount
        local price = not isExit and (isLong and cp.bid or cp.ask) or (isLong and cp.ask or cp.bid)
    
        if oid != '' then
            local order = OrderContainer(oid)
    
            if oid.isOpen then
                return
            else
                oid = ''
    
                local remaining = order.executedAmount - order.filledAmount
    
                if remaining > 0 and IsTradeAmountEnough(PriceMarket(), price, remaining) then
                    amount = ParseTradeAmount(PriceMarket(), price, remaining)
                    timer = 0
                end
            end
        end
    
        if timer < Time() then
            oid = cmd(price, amount, {note = name})
            timer = Time() + CurrentInterval() * 60
        end
    
        Save(name..'oid', oid)
        Save(name..'timer', timer)
    end
    
    
    -- da logica
    if entry_long and (position.amount == 0 or position.isLong) then
        placeOrder(true)
    elseif entry_short and (position.amount == 0 or position.isShort) then
        placeOrder(false)
    elseif exit_long and (position.amount > 0 and position.isLong) then
        placeOrder(true, true)
    elseif exit_short and (position.amount > 0 and position.isShort) then
        placeOrder(false, true)
    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