BTC 4H strategy

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #243264 quote
    Schizophunk
    Participant
    New

    Hi guys,

    I want to share with you my first trading strategy for BTC 4H.

    Sorry for putting the code in Pine Script but I cant test it in 4h timeframe in PRT.

    I just wanted to share with you in case it is interesting for someone that can translate it to PRT code and test it with more data.

    //Juan Diaz Maldonado
    
    //@version=6
    strategy(title = "Bollinger Bands Width Strategy @ JDM", shorttitle = "BBW Strat @ JDM", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value = 100, initial_capital =1000, currency = currency.USD, slippage = 10, commission_type = strategy.commission.percent, commission_value = 0.1)
    
    // ------------------------- Paramethers -------------------------
    
    // Bollinger bands paramethers
    length = input.int(22, minval=1, title="Length")
    src = input.source(close, title="Source")
    mult = input.float(2, minval=0.001, maxval=50, title="StdDev")
    
    // Bollinger bands Width paramethers
    ncandles = input.int(120, minval=1, title="n candles") // number of candles for normalized BB width
    compthreshold = input.int(20, minval=1, title="Compresion Threshold") // compression band under which we will open trades
    
    // ------------------------- Bollinger bands calculation -------------------------
    
    // Bollinger bands
    basis = ta.sma(src, length)
    dev = mult * ta.stdev(src, length)
    upper = basis + dev
    lower = basis - dev
    
    // Bollinger bands width
    bbWidth = (upper - lower) / basis
    
    // Normalize BB widht 
    bbWidthMax = ta.highest(bbWidth, ncandles)  // maximum width of ncandles
    bbWidthMin = ta.lowest(bbWidth, ncandles)   // minimum width of ncandles
    bbWidthNormalized = ((bbWidth - bbWidthMin) / (bbWidthMax - bbWidthMin)) * (100) // bollinger bands width normalized
    
    // ------------------------- Long entry and exits and Stop Loss if any -------------------------
    
    // Longs condition
    longCondition = src > basis and src < upper and volume > ta.sma(volume, 20) and bbWidthNormalized < compthreshold
    if (longCondition)
        strategy.entry("Long", strategy.long)
    
    // Exit longs condition
    longExitCondition = ta.crossunder(src, lower)
    if (longExitCondition)
        strategy.close("Long")
    
    // ------------------------- Gráficos -------------------------
    // Representación de la amplitud normalizada (oscila entre -20 y 120)
    plot(bbWidthNormalized, title="BB Width Normalized", color=color.new(color.orange, 0), linewidth=1)
    
    // Líneas de referencia fijas en 100 y 0 para %B
    hline(100, title="Upper Band", color=color.new(color.black, 0), linestyle=hline.style_solid)
    hline(compthreshold, title="Threshold", color=color.new(color.blue, 0), linestyle=hline.style_solid)
    hline(0, title="Lower Band", color=color.new(color.black, 0), linestyle=hline.style_solid)

    Regards,

    Captura-de-pantalla-2025-01-31-a-las-15.27.19.png Captura-de-pantalla-2025-01-31-a-las-15.27.19.png
    #243270 quote
    JS
    Participant
    Senior

    Hi,

    Hereby the conversion to PRT…

    DefParam CumulateOrders=False
    DefParam PreloadBars = 1000
    
    // Bollinger Bands settings
    length = 22
    mult = 2.0
    ncandles = 120
    compthreshold = 20
    
    // Bollinger Bands calculation
    basis = Average[length](close)
    deviation = mult * Std[length](close)
    upper = basis + deviation
    lower = basis - deviation
    
    // Bollinger Band Width
    bbWidth = (upper - lower) / basis
    
    // Normalized BB Width
    bbWidthMax = Highest[ncandles](bbWidth)
    bbWidthMin = Lowest[ncandles](bbWidth)
    bbWidthNormalized = ((bbWidth - bbWidthMin) / (bbWidthMax - bbWidthMin)) * 100
    
    // Long entry conditions
    longCondition = close > basis AND close < upper AND volume > Average[20](volume) AND bbWidthNormalized < compthreshold
    IF longCondition THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF
    
    // Long exit conditions
    longExitCondition = close < lower
    IF longExitCondition THEN
    SELL AT MARKET
    ENDIF
    
    // Plot BB Width Normalized
    Graph bbWidthNormalized
    Graph 100
    Graph 0
    Graph compthreshold
    GraphOnPrice Upper
    GraphOnPrice Lower
    GraphOnPrice Basis
    Schizophunk and Iván González thanked this post
    Scherm­afbeelding-2025-01-31-om-14.08.53.png Scherm­afbeelding-2025-01-31-om-14.08.53.png
    #243272 quote
    Schizophunk
    Participant
    New

    Thank you very much JS.

    May you test it for a longer period? Maybe around 30k bars could be good.

    #243273 quote
    JS
    Participant
    Senior

    Of course, I want to test it over a longer period, but we need to stay realistic…
    The spread at IG is insane, just like the margin… What spread should I take into account?
    How much is the spread with the broker you use…?

    Schizophunk thanked this post
    #243274 quote
    Schizophunk
    Participant
    New

    I would say 30-50 pips spread can be a realistic one.

    #243275 quote
    JS
    Participant
    Senior
    #243284 quote
    Schizophunk
    Participant
    New

    Thank you very much! Really helpful. What can we do in ProRealTime to compare with a benchmark? Like buy and hold the own BTC in this case.

    #243286 quote
    JS
    Participant
    Senior

    August 11, 2017: BTC $3420
    January 31, 2025: BTC $104,431
    Profit: $101,011
    In retrospect, it would have been better to buy and hold on August 11, 2017…(if you only look at the profit)

    Schizophunk thanked this post
    #244587 quote
    VinzentVega
    Participant
    Veteran

    For this algo you need a 100k account. The risk of total lost ist great.

    Schizophunk thanked this post
Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.

BTC 4H strategy


General Trading: Market Analysis & Manual Trading

New Reply
Author
Summary

This topic contains 8 replies,
has 3 voices, and was last updated by VinzentVega
11 months, 2 weeks ago.

Topic Details
Forum: General Trading: Market Analysis & Manual Trading
Language: English
Started: 01/31/2025
Status: Active
Attachments: 6 files
Logo Logo
Loading...