Buy and sell at support and resistance code

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #245741 quote
    efahmyefahmy
    Participant
    Average
    // === PARAMETERS ===
    supportLookback = 50
    resistanceLookback = 50
    testTolerance = 2.0        // Looser tolerance for matching levels
    minTests = 1               // Allow support/resistance with only 1 test
    atrPeriod = 14
    minATRMultiplier = 1.0     // Less strict ATR confirmation
    
    // === VARIABLES ===
    strongSupport = 0
    strongResistance = 0
    supportTests = 0
    resistanceTests = 0
    
    // === ATR Calculation ===
    atr = AverageTrueRange[atrPeriod]
    avgATR = Average[atrPeriod](atr)
    atrConfirmed = atr >= minATRMultiplier * avgATR
    
    // === DETECT STRONG SUPPORT ===
    for i = 1 to supportLookback do
    if close[i] < close[i+1] and close[i] < close[i-1] then
    level = close[i]
    tests = 0
    for j = 1 to supportLookback do
    if abs(close[j] - level) <= testTolerance then
    tests = tests + 1
    endif
    next
    if tests >= minTests then
    strongSupport = level
    supportTests = tests
    break
    endif
    endif
    next
    
    // === DETECT STRONG RESISTANCE ===
    for i = 1 to resistanceLookback do
    if close[i] > close[i+1] and close[i] > close[i-1] then
    level = close[i]
    tests = 0
    for j = 1 to resistanceLookback do
    if abs(close[j] - level) <= testTolerance then
    tests = tests + 1
    endif
    next
    if tests >= minTests then
    strongResistance = level
    resistanceTests = tests
    break
    endif
    endif
    next
    
    // === DEBUG VISUALIZATION ===
    if strongSupport > 0 then
    DRAWSEGMENT(barindex - 10, strongSupport, barindex, strongSupport) COLOURED(0,255,0)
    endif
    
    if strongResistance > 0 then
    DRAWSEGMENT(barindex - 10, strongResistance, barindex, strongResistance) COLOURED(255,0,0)
    endif
    
    // === ENTRY CONDITIONS ===
    buySignal  = close <= strongSupport and atrConfirmed
    sellSignal = close >= strongResistance and atrConfirmed
    
    // === EXECUTE TRADES ===
    IF buySignal THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF
    
    IF sellSignal THEN
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    
    // === OPTIONAL: EXIT LOGIC ===
    // SET STOP pLOSS 5
    // SET TARGET pPROFIT 10

    Could anyone help in checking this code and fix any error

    #245758 quote
    GraHalGraHal
    Participant
    Master

    If you comment out the Drawing instructions (as below) then it works fine.

    Drawing instructions dont work in Auto-Trading.

    Drawing instructions only work on Indicators.

    if strongSupport > 0 then
    //DRAWSEGMENT(barindex 10, strongSupport, barindex, strongSupport) COLOURED(0,255,0)
    endif
    Iván González and robertogozzi thanked this post
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Buy and sell at support and resistance code


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
efahmy @efahmy Participant
Summary

This topic contains 1 reply,
has 2 voices, and was last updated by GraHalGraHal
1 year, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/09/2025
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...