Help with fixiing code
Forums › ProRealTime English forum › ProBuilder support › Help with fixiing code
- This topic has 6 replies, 4 voices, and was last updated 3 weeks ago by
druby.
-
-
03/19/2025 at 10:32 PM #245068
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”03/19/2025 at 10:34 PM #24506903/19/2025 at 10:42 PM #245070// 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”03/20/2025 at 9:48 AM #245079hi! here you have the code. Be careful with “_”.
12345678910111213141516171819202122// Set the period for calculationperiod = 50// Calculate linear regression lineslope = LinearRegressionSlope[period](close)Intercept = average[period](close) - Slope * average[period](barindex)regressionline = intercept + slope * BarIndex// Calculate standard errorsumsquarederrors = 0for i = 0 to period - 1 dopredicted = intercept + slope * (BarIndex - i)sumsquarederrors = sumsquarederrors + pow((close[i] - predicted),2)nextstderror = sqrt(sumsquarederrors / period)// Upper and lower channel linesupperchannel = regressionline + stderrorlowerchannel = regressionline - stderror// Plot the linesRETURN regressionline AS "Regression Line", upperchannel AS "Upper Channel", lowerchannel AS "Lower Channel"2 users thanked author for this post.
03/28/2025 at 4:52 PM #245394I 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)
ENDIFIF Close > BOLL_TOP THEN
SELL AT MARKET
ENDIFIF Close > BOLL_TOP THEN
SELLSHORT POSITION_SIZE CONTRACTS AT MARKET
TrailingStop = Close + (TSL_MULTIPLIER_TREND * ATR14)
ENDIFIF 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)
ENDIFIF Close > MA20 THEN
SELL AT MARKET
ENDIFIF Close < MA150 AND Close > MA70 THEN
SELLSHORT POSITION_SIZE CONTRACTS AT MARKET
TrailingStop = Close + (TSL_MULTIPLIER_TREND * ATR14)
ENDIFIF 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)
ENDIFIF Close < ENTRYPRICE – (SL_MULTIPLIER * ATR14) THEN
SELL AT MARKET
ENDIFIF MA20 < MA70 AND Close < MA150 THEN
SELLSHORT POSITION_SIZE CONTRACTS AT MARKET
TrailingStop = Close + (TSL_MULTIPLIER_TREND * ATR14)
ENDIFIF 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)
ENDIFIF BarsSinceEntry >= 5 THEN
SELL AT MARKET
ENDIFIF ATR14 < 0.8 * Average[50](ATR14) THEN
SELLSHORT POSITION_SIZE CONTRACTS AT MARKET
TrailingStop = Close + (TSL_MULTIPLIER_SHORT * ATR14)
ENDIFIF 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)
ENDIFIF RSI14 > 50 THEN
SELL AT MARKET
ENDIFIF RSI14 > 70 THEN
SELLSHORT POSITION_SIZE CONTRACTS AT MARKET
TrailingStop = Close + (TSL_MULTIPLIER_SHORT * ATR14)
ENDIFIF 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
ENDIFIF 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
ENDIF03/28/2025 at 5:00 PM #24539503/28/2025 at 9:18 PM #245397User 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.
3 users thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on