Help with fixiing code

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #245068 quote
    ShugsterShugster
    Participant
    New

    Can someone help me with the problem showing on the following code? line 6 between period and close I dont have any knowlege regarding coding and have created this code using chatGTP. Many thanks.

     

    // Set the period for calculation
    period = 50

    // Calculate linear regression line
    slope = LinearRegressionSlope[period](close)
    intercept = LinearRegressionIntercept[period](close)
    regression_line = intercept + slope * BarIndex

    // Calculate standard error
    sum_squared_errors = 0
    for i = 0 to period – 1 do
    predicted = intercept + slope * (BarIndex – i)
    sum_squared_errors = sum_squared_errors + (close[i] – predicted)^2
    next
    std_error = sqrt(sum_squared_errors / period)

    // Upper and lower channel lines
    upper_channel = regression_line + std_error
    lower_channel = regression_line – std_error

    // Plot the lines
    RETURN regression_line AS “Regression Line”, upper_channel AS “Upper Channel”, lower_channel AS “Lower Channel”

    #245069 quote
    ShugsterShugster
    Participant
    New

    Thanks, how can I access this?

    #245070 quote
    ShugsterShugster
    Participant
    New

    // Set the period for calculation
    period = 50

    // Calculate linear regression line
    slope = LinearRegressionSlope[period](close)
    intercept = LinearRegressionIntercept[period](close)
    regression_line = intercept + slope * BarIndex

    // Calculate standard error
    sum_squared_errors = 0
    for i = 0 to period – 1 do
    predicted = intercept + slope * (BarIndex – i)
    sum_squared_errors = sum_squared_errors + (close[i] – predicted)^2
    next
    std_error = sqrt(sum_squared_errors / period)

    // Upper and lower channel lines
    upper_channel = regression_line + std_error
    lower_channel = regression_line – std_error

    // Plot the lines
    RETURN regression_line AS “Regression Line”, upper_channel AS “Upper Channel”, lower_channel AS “Lower Channel”

    #245079 quote
    Iván GonzálezIván González
    Moderator
    Legend

    hi! here you have the code. Be careful with “_”.

    // Set the period for calculation
    period = 50
    
    // Calculate linear regression line
    slope = LinearRegressionSlope[period](close)
    Intercept = average[period](close) - Slope * average[period](barindex)
    regressionline = intercept + slope * BarIndex
    
    // Calculate standard error
    sumsquarederrors = 0
    for i = 0 to period - 1 do
    predicted = intercept + slope * (BarIndex - i)
    sumsquarederrors = sumsquarederrors + pow((close[i] - predicted),2)
    next
    stderror = sqrt(sumsquarederrors / period)
    
    // Upper and lower channel lines
    upperchannel = regressionline + stderror
    lowerchannel = regressionline - stderror
    
    // Plot the lines
    RETURN regressionline AS "Regression Line", upperchannel AS "Upper Channel", lowerchannel AS "Lower Channel"
    Shugster and robertogozzi thanked this post
    #245394 quote
    DonjohanDonjohan
    Participant
    New

    I need help with the same thing so Im borrowing this thread. Can someone help me please? 🙂

     

    // ProRealTime backtest för DAX, Nasdaq och OMXS30 (med Shorting, Slippage & Optimeringar)
    DEFPARAM PreloadBars = 200   // Säkerställ att vi har tillräckligt med historiska data
    DEFPARAM CumulateOrders = False
    DEFPARAM Slippage = 0.1      // Simulerar slippage på 0.1% per affär

    // Initialt kapital & riskhantering
    INITIAL_CAPITAL = 100000
    POSITION_SIZE = 1    // Antal kontrakt per affär
    SL_MULTIPLIER = 2    // Stop-loss på 2x ATR
    TSL_MULTIPLIER_TREND = 3  // Trailing Stop för trendföljande strategier
    TSL_MULTIPLIER_SHORT = 2  // Trailing Stop för kortsiktiga strategier
    COURTAGE = 10        // Courtage per affär

    // Tidsfilter (undviker första & sista timmen av handeln)
    HOUR = Hour
    IF HOUR < 10 OR HOUR > 16 THEN
    RETURN
    ENDIF

    // — Beräkning av indikatorer —
    MA20 = Average[20](Close)
    MA70 = Average[70](Close)
    MA150 = Average[150](Close)
    ATR14 = AverageTrueRange[14](Close)
    RSI14 = RSI[14](Close)
    BOLL_TOP = BollingerUp[20](Close,2)
    BOLL_BOTTOM = BollingerDown[20](Close,2)

    // — Variabel för Trailing Stop —
    TrailingStop = 0

    // — Strategi 1: Super Simple Bollinger Bands (SSBB) (Long & Short) —
    IF Close < BOLL_BOTTOM THEN
    BUY POSITION_SIZE CONTRACTS AT MARKET
    TrailingStop = Close – (TSL_MULTIPLIER_TREND * ATR14)
    ENDIF

    IF Close > BOLL_TOP THEN
    SELL AT MARKET
    ENDIF

    IF Close > BOLL_TOP THEN
    SELLSHORT POSITION_SIZE CONTRACTS AT MARKET
    TrailingStop = Close + (TSL_MULTIPLIER_TREND * ATR14)
    ENDIF

    IF Close < BOLL_BOTTOM THEN
    EXITSHORT AT MARKET
    ENDIF

    // — Strategi 2: Stark trend & jämviktspendling (Long & Short) —
    IF Close > MA150 AND Close < MA70 THEN
    BUY POSITION_SIZE CONTRACTS AT MARKET
    TrailingStop = Close – (TSL_MULTIPLIER_TREND * ATR14)
    ENDIF

    IF Close > MA20 THEN
    SELL AT MARKET
    ENDIF

    IF Close < MA150 AND Close > MA70 THEN
    SELLSHORT POSITION_SIZE CONTRACTS AT MARKET
    TrailingStop = Close + (TSL_MULTIPLIER_TREND * ATR14)
    ENDIF

    IF Close < MA20 THEN
    EXITSHORT AT MARKET
    ENDIF

    // — Strategi 3: Long Trend High Momentum (LTHM) (Long & Short) —
    IF MA20 > MA70 AND Close > MA150 THEN
    BUY POSITION_SIZE CONTRACTS AT MARKET
    TrailingStop = Close – (TSL_MULTIPLIER_TREND * ATR14)
    ENDIF

    IF Close < ENTRYPRICE – (SL_MULTIPLIER * ATR14) THEN
    SELL AT MARKET
    ENDIF

    IF MA20 < MA70 AND Close < MA150 THEN
    SELLSHORT POSITION_SIZE CONTRACTS AT MARKET
    TrailingStop = Close + (TSL_MULTIPLIER_TREND * ATR14)
    ENDIF

    IF Close > ENTRYPRICE + (SL_MULTIPLIER * ATR14) THEN
    EXITSHORT AT MARKET
    ENDIF

    // — Strategi 4: Hög Volatilitet (ATR) (Long & Short) —
    IF ATR14 > 1.5 * Average[50](ATR14) THEN
    BUY POSITION_SIZE CONTRACTS AT MARKET
    TrailingStop = Close – (TSL_MULTIPLIER_SHORT * ATR14)
    ENDIF

    IF BarsSinceEntry >= 5 THEN
    SELL AT MARKET
    ENDIF

    IF ATR14 < 0.8 * Average[50](ATR14) THEN
    SELLSHORT POSITION_SIZE CONTRACTS AT MARKET
    TrailingStop = Close + (TSL_MULTIPLIER_SHORT * ATR14)
    ENDIF

    IF BarsSinceEntry >= 5 THEN
    EXITSHORT AT MARKET
    ENDIF

    // — Strategi 5: Kortsiktig RSI (Long & Short) —
    IF RSI14 < 30 THEN
    BUY POSITION_SIZE CONTRACTS AT MARKET
    TrailingStop = Close – (TSL_MULTIPLIER_SHORT * ATR14)
    ENDIF

    IF RSI14 > 50 THEN
    SELL AT MARKET
    ENDIF

    IF RSI14 > 70 THEN
    SELLSHORT POSITION_SIZE CONTRACTS AT MARKET
    TrailingStop = Close + (TSL_MULTIPLIER_SHORT * ATR14)
    ENDIF

    IF RSI14 < 50 THEN
    EXITSHORT AT MARKET
    ENDIF

    // — Hantera Trailing Stop-Loss —
    IF PositionSize > 0 THEN
    IF Close – (TSL_MULTIPLIER_TREND * ATR14) > TrailingStop THEN
    TrailingStop = Close – (TSL_MULTIPLIER_TREND * ATR14)
    ENDIF
    IF Close < TrailingStop THEN
    SELL AT MARKET
    ENDIF
    ENDIF

    IF PositionSize < 0 THEN
    IF Close + (TSL_MULTIPLIER_TREND * ATR14) < TrailingStop THEN
    TrailingStop = Close + (TSL_MULTIPLIER_TREND * ATR14)
    ENDIF
    IF Close > TrailingStop THEN
    EXITSHORT AT MARKET
    ENDIF
    ENDIF

    #245395 quote
    Iván GonzálezIván González
    Moderator
    Legend

    Hi. First you should remove “_”

    druby thanked this post
    #245397 quote
    drubydruby
    Participant
    Junior

    User variables names can only use the characters A to Z, a to z and 0 to 9.

    The variable name must start with a letter, and letters are not case sensitive, ‘A’ is the same as ‘a’.

    They must be unique and not clash with any of the reserved keywords, HOUR = Hour.

    Also the RETURN keyword is reserved for indicator program code files not back-testing files.

    Donjohan, robertogozzi and Iván González thanked this post
Viewing 7 posts - 1 through 7 (of 7 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

Help with fixiing code


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Shugster @shugster Participant
Summary

This topic contains 6 replies,
has 4 voices, and was last updated by drubydruby
1 year, 6 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 03/19/2025
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...