[pshaiBot] Simple Trend Trader

stable
By pshai in Trading Bots Published April 2020 👁 2,119 views 💬 0 comments

Description

Hello fellas! Here's a simple trend trading bot that utilizes 3 different timeframes to determine when to trade. There are also different indicators for each timeframe: MAs for high TF, RSI for low TF and Stochastics for timing the entries. I hope you find good use for it, or can at least extract some educational information out of it. **NOTE**: The default values are scanned using Haas Labs, start of scan was 6th April 2020, scanned for a week-long backtest. Screenshot of results for backtest (7th April 2020): https://puu.sh/FulHj/ff4282b9a5.png Screenshot of Haas Labs setup: https://puu.sh/FulM2/d934f90938.png
HaasScript
-- INPUTS
    local htf = InputInterval('1.Timeframe', 360, 'Timeframe for trend check', 'TREND')
    local ma_len1 = Input('2. MA1 Length', 18, '', 'TREND')
    local ma_type1 = InputMaTypes('3. MA1 Type', T3Type, '', 'TREND')
    local ma_len2 = Input('4. MA2 Length', 68, '', 'TREND')
    local ma_type2 = InputMaTypes('5. MA2 Type', DemaType, '', 'TREND')
    local ma_swing = Input('6. Swing %', 0, '', 'TREND')

    local ltf = InputInterval('1. Timeframe', 15, 'Timeframe for momentum check', 'MOMENTUM')
    local rsi_len = Input('2. RSI Length', 14, '', 'MOMENTUM')
    --local rsi_ob = Input('3. RSI Overbought', 55, '', 'MOMENTUM')
    --local rsi_os = Input('4. RSI Oversold', 45, '', 'MOMENTUM')

    local etf = InputInterval('1. Timeframe', 5, 'Timeframe for entry check', 'ENTRY TIMING')
    local stoch_fastk = Input('2. STOCH FastK Length', 5, '', 'ENTRY TIMING')
    local stoch_slowk = Input('3. STOCH SlowK Length', 5, '', 'ENTRY TIMING')
    local stoch_slowd = Input('4. STOCH SlowD Length', 7, '', 'ENTRY TIMING')
    local stoch_ob = Input('5. STOCH Overbought', 80, '', 'ENTRY TIMING')
    local stoch_os = Input('6. STOCH Oversold', 36, '', 'ENTRY TIMING')

    local sl_prc = Input('1. Stop-Loss % (ROI)', 290, 'Ignored if set to zero', 'SAFETIES')
    local tp_prc = Input('2. Take-Profit % (ROI)', 360 , 'Ignored if set to zero', 'SAFETIES')

-- DATA
    local prices = {
        h = HighPrices,
        l = LowPrices,
        c = ClosePrices
    }

    -- trend
    local ma1 = MA(prices.c(htf), ma_len1, ma_type1)
    local ma2 = MA(prices.c(htf), ma_len2, ma_type2)
    local trend_up = Delta(ma2, ma1) > ma_swing
    local trend_dn = Delta(ma2, ma1) < -ma_swing

    -- momentum
    local rsi = RSI(prices.c(ltf), rsi_len)
    local mom_up = CrossOverSince(rsi, 50) < 10 and rsi > 50
    local mom_dn = CrossUnderSince(rsi, 50) < 10 and rsi < 50

    -- entry
    local stoch = STOCH(prices.h(etf), prices.l(etf), prices.c(etf), stoch_fastk, stoch_slowk, stoch_slowd)
    local entry_long = stoch.slowK <= stoch_os
    local entry_short = stoch.slowK >= stoch_ob

    -- safeties
    local sl = sl_prc > 0 and StopLossROI(sl_prc)
    local tp = tp_prc > 0 and TakeProfitROI(tp_prc)

-- PLOT
    Plot(0, 'MA1', ma1, Yellow)
    Plot(0, 'MA2', ma2, Purple)

    Plot(1, 'Trend Up', IfElse(trend_up, 1, 0), Green)
    Plot(1, 'Trend Dn', IfElse(trend_dn, -1, 0), Red)

    Plot(2, 'RSI', rsi)
    PlotHorizontalLine(2, '', Gray, 50, Dashed)

    Plot(3, '%K', stoch.slowK, Yellow)
    PlotHorizontalLine(3, '', Red, stoch_ob, Dashed)
    PlotHorizontalLine(3, '', Green, stoch_os, Dashed)
    PlotHorizontalZone(3, '', DarkGray(20), stoch_os, stoch_ob)

-- LOGIC
    local botPos = GetPositionDirection()
    local botRoi = GetPositionROI()
    local postfix = '['..Round(botRoi, 4)..']'

    if botPos == NoPosition then
        if trend_up and mom_up and entry_long then
            DoBuy('Long Entry')
        elseif trend_dn and mom_dn and entry_short then
            DoSell('Short Entry')
        end
    else
        if botPos == PositionLong and (trend_dn or rsi < 50)  then
            DoExitPosition('Long Exit '..postfix)
        elseif botPos == PositionShort and (trend_up or rsi > 50) then
            DoExitPosition('Short Exit '..postfix)
        elseif sl then
            DoExitPosition('StopLoss '..postfix)
        elseif tp then
            DoExitPosition('TakeProfit '..postfix)
        end
    end


0 Comments

Sign in to leave a comment.

No comments yet. Be the first!