New to PRT and PBC, so be firm and fair…
Forums › ProRealTime English forum › ProOrder support › New to PRT and PBC, so be firm and fair…
- This topic has 14 replies, 4 voices, and was last updated 40 minutes ago by
NeoTrader.
-
-
05/01/2025 at 5:18 PM #246658
Backtesting: Range optimisation code….
Hi there.
New to ProRealTime and ProBuilder Code also.
Fairly dexterous with AI (15 months), and other codes, Excel Formulae & VBA, Python…I typically create a template (Excel > NotePad), putting the bones on the code together, and run it past an AI (Using a functional method, to engage the AI).
Enclosed is the template in two sections, this serves well to an AI model for coding assistance.Section [1] is background reference only info.
Section [2[ are the component names and values that need coding (ProBuilder Code).
I would appreciate an correctional guidance, to assist in my joining the dots learning process, so please, do advise.
I tend to make code simple, yet effective.This code for review:
A random indicator being range tested, to find the best performance parameters.
ATR stop loss
The template and my annotated code should make clear my objectives – if not, hit me back, and I’ll fill-in any gapsI am not sure what the request means:
Use the ‘Insert PRT Code’ button ???
I have visual or text options !?!?// ========================================================================// STRATEGY: DMI-ADX Crossover Trading Backtest// AUTHOR: Cody AI// DATE: Thu.01.May.2025// VERSION: 1.0// ========================================================================// ========================================// 1. INDICATOR SELECTION FROM PRT LIBRARY// ========================================// DMI with default periods// ATR for stop loss calculation// ============================================// 2. INPUTS TO SET VIA PRT BACKTEST INTERFACE// ============================================RiskPercent = 5PipValue = 0.0001PositionSize = 10// ==============================// 3. VARIABLES USED IN THE CODE// ==============================DMIperiod = 14ATRperiod = 14ATRmultiplier = 2.5R1start = 1R1stop = 20R1step = 1// =======================// 4. CODING THE BACKTEST// =======================// INDICATOR CALCULATIONS// ———————-DIplus = DIPLUS[DMIperiod]DIminus = DIMINUS[DMIperiod]ATR = ATR[ATRperiod]// TRADE ENTRY EXIT LOGIC (CONDITIONS)// ————————————LongEntrySignal = CROSSOVER(DIplus, DIminus)ShortEntrySignal = CROSSOVER(DIminus, DIplus)LongExitSignal = CROSSUNDER(DIplus, DIminus)ShortExitSignal = CROSSUNDER(DIminus, DIplus)// STOP LOSS CALCULATION USING ATR// ——————————–StopLossDistance = ATR * ATRmultiplierLongStopLoss = CLOSE – StopLossDistanceShortStopLoss = CLOSE + StopLossDistance// LONG POSITION LOGIC// ——————–IF LongEntrySignal AND NOT ISLONG() THENBUY PositionSize SHARES AT MARKETSELL PositionSize SHARES ON STOP AT LongStopLoss// Draw up arrow for buy signalDRAWARROWUP(BARINDEX, LOW – (ATR * 0.5)) COLOURED(0, 255, 0)ENDIF// SHORT POSITION LOGIC// ——————–IF ShortEntrySignal AND NOT ISSHORT() THENSELLSHORT PositionSize SHARES AT MARKETBUYTOCOVER PositionSize SHARES ON STOP AT ShortStopLoss// Draw down arrow for sell signalDRAWARROWDOWN(BARINDEX, HIGH + (ATR * 0.5)) COLOURED(255, 0, 0)ENDIF// EXIT LONG POSITION ON SIGNAL/TRIGGER// ————————————IF ISLONG() AND LongExitSignal THENSELL AT MARKETENDIF// EXIT SHORT POSITION ON SIGNAL/TRIGGER// ————————————–IF ISSHORT() AND ShortExitSignal THENBUYTOCOVER AT MARKETENDIF// DISPLAY STOP LOSS LEVELS WITH COLORED BACKGROUND// ———————————————–// Color the background when in a position to show stop loss zonesIF ISLONG() THENCOLORBETWEEN(CLOSE, LongStopLoss, “lightred”)ENDIFIF ISSHORT() THENCOLORBETWEEN(CLOSE, ShortStopLoss, “lightblue”)ENDIF// END// ========================================================================// END OF STRATEGY CODEThank you in anticipation of any assistance.
NT.
Background
I am also going through the videos:
https://www.prorealcode.com/programming-with-prorealtime/
1. Training program – Introduction to programming with ProRealTime
2. ProRealTime – Advanced programming
These are helpful as a foundation to my learning.And I have printed out, and currently annotating the PBC manual.
This is my workbook when going through the videos.05/01/2025 at 6:05 PM #246667The ‘Insert PRT Code button’ allows you to post a formatted code, instead of the plain text of your code:
This is your formatted original code:1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283// ========================================================================// STRATEGY: DMI-ADX Crossover Trading Backtest// AUTHOR: Cody AI// DATE: Thu.01.May.2025// VERSION: 1.0// ========================================================================// ========================================// 1. INDICATOR SELECTION FROM PRT LIBRARY// ========================================// DMI with default periods// ATR for stop loss calculation// ============================================// 2. INPUTS TO SET VIA PRT BACKTEST INTERFACE// ============================================RiskPercent = 5PipValue = 0.0001PositionSize = 10// ==============================// 3. VARIABLES USED IN THE CODE// ==============================DMIperiod = 14ATRperiod = 14ATRmultiplier = 2.5R1start = 1R1stop = 20R1step = 1// =======================// 4. CODING THE BACKTEST// =======================// INDICATOR CALCULATIONS// ———————-DIplus = DIPLUS[DMIperiod]DIminus = DIMINUS[DMIperiod]ATR = ATR[ATRperiod]// TRADE ENTRY EXIT LOGIC (CONDITIONS)// ————————————LongEntrySignal = CROSSOVER(DIplus, DIminus)ShortEntrySignal = CROSSOVER(DIminus, DIplus)LongExitSignal = CROSSUNDER(DIplus, DIminus)ShortExitSignal = CROSSUNDER(DIminus, DIplus)// STOP LOSS CALCULATION USING ATR// ——————————–StopLossDistance = ATR * ATRmultiplierLongStopLoss = CLOSE – StopLossDistanceShortStopLoss = CLOSE + StopLossDistance// LONG POSITION LOGIC// ——————–IF LongEntrySignal AND NOT ISLONG() THENBUY PositionSize SHARES AT MARKETSELL PositionSize SHARES ON STOP AT LongStopLoss// Draw up arrow for buy signalDRAWARROWUP(BARINDEX, LOW – (ATR * 0.5)) COLOURED(0, 255, 0)ENDIF// SHORT POSITION LOGIC// ——————–IF ShortEntrySignal AND NOT ISSHORT() THENSELLSHORT PositionSize SHARES AT MARKETBUYTOCOVER PositionSize SHARES ON STOP AT ShortStopLoss// Draw down arrow for sell signalDRAWARROWDOWN(BARINDEX, HIGH + (ATR * 0.5)) COLOURED(255, 0, 0)ENDIF// EXIT LONG POSITION ON SIGNAL/TRIGGER// ————————————IF ISLONG() AND LongExitSignal THENSELL AT MARKETENDIF// EXIT SHORT POSITION ON SIGNAL/TRIGGER// ————————————–IF ISSHORT() AND ShortExitSignal THENBUYTOCOVER AT MARKETENDIF// DISPLAY STOP LOSS LEVELS WITH COLORED BACKGROUND// ———————————————–// Color the background when in a position to show stop loss zonesIF ISLONG() THENCOLORBETWEEN(CLOSE, LongStopLoss, “lightred”)ENDIFIF ISSHORT() THENCOLORBETWEEN(CLOSE, ShortStopLoss, “lightblue”)ENDIF// END// ========================================================================// END OF STRATEGY CODEAI usually print a similar pseudo code that needs some adjustments:
– BUYTOCOVER doesn’t exist, use EXITSHORT (or ExitShort), instead
– ISSHORT() needs to be replaced by ShortOnMarket for the same reason, as well as ISLONG() needs to be replaced by LongOnMarket
– DRAWARROWUP and COLORBETWEEN, as well as any other graphic instruction CANNOT be used in strategies and screeners but only in indicators
– Pending orders (like the stop loss you have set) have a different sintax (actually they may have different forms, as yiou can read in the PDF manual or online at https://www.prorealcode.com/prorealtime-documentation/
– GraphOnPrice can be used to print data on thwe chart
– Graph can be used to print data ibn the variable windows that ProBackTest opens when backtestingThis is your working code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384// ========================================================================// STRATEGY: DMI-ADX Crossover Trading Backtest// AUTHOR: Cody AI// DATE: Thu.01.May.2025// VERSION: 1.0// ========================================================================// ========================================// 1. INDICATOR SELECTION FROM PRT LIBRARY// ========================================// DMI with default periods// ATR for stop loss calculation// ============================================// 2. INPUTS TO SET VIA PRT BACKTEST INTERFACE// ============================================RiskPercent = 5myPipValue = 0.0001PositionSize = 10// ==============================// 3. VARIABLES USED IN THE CODE// ==============================DMIperiod = 14ATRperiod = 14ATRmultiplier = 2.5R1start = 1R1stop = 20R1step = 1// =======================// 4. CODING THE BACKTEST// =======================// INDICATOR CALCULATIONS// ———————-myDIplus = DIPLUS[DMIperiod]myDIminus = DIMINUS[DMIperiod]ATR = ATR[ATRperiod]// TRADE ENTRY EXIT LOGIC (CONDITIONS)// ————————————LongEntrySignal = myDIplus CROSSES OVER myDIminusShortEntrySignal = myDIminus CROSSES OVER myDIplusLongExitSignal = myDIplus CROSSES UNDER myDIminusShortExitSignal = myDIminus CROSSES UNDER myDIplus// STOP LOSS CALCULATION USING ATR// ——————————-StopLossDistance = ATR * ATRmultiplierLongStopLoss = CLOSE - StopLossDistanceShortStopLoss = CLOSE + StopLossDistance// LONG POSITION LOGIC// ——————-IF LongEntrySignal AND NOT LongOnMarket THENBUY PositionSize SHARES AT MARKETSELL PositionSize SHARES AT LongStopLoss STOPENDIF// SHORT POSITION LOGIC// ——————-IF ShortEntrySignal AND NOT ShortOnMarket THENSELLSHORT PositionSize SHARES AT MARKETEXITSHORT PositionSize SHARES AT ShortStopLoss STOPENDIF// EXIT LONG POSITION ON SIGNAL/TRIGGER// ————————————IF LongOnMarket AND LongExitSignal THENSELL AT MARKETENDIF// EXIT SHORT POSITION ON SIGNAL/TRIGGER// ————————————-IF ShortOnMarket AND ShortExitSignal THENExitShort AT MARKETENDIF// DISPLAY STOP LOSS LEVELS WITH COLORED BACKGROUND// ———————————————-// Color the background when in a position to show stop loss zonesIF LongOnMarket THEN//COLORBETWEEN(CLOSE, LongStopLoss, "lightred")ENDIFIF ShortOnMarket THEN//COLORBETWEEN(CLOSE, ShortStopLoss, "lightblue")ENDIF// END// ========================================================================// END OF STRATEGY CODE// Draw up arrow for buy signalGraphOnPrice LOW - (ATR * 0.5) COLOURED(0, 255, 0)// Draw down arrow for sell signalGraphOnPrice HIGH + (ATR * 0.5) COLOURED(255, 0, 0)05/01/2025 at 6:11 PM #246668Moreover, the names of indicators, such as DIPLUS, MACD, AVERAGE, etc… cannot be used as the name of a variable, add any char to make them differ.
Example: myDIPLUS or xDIPLUS for the indicator DIPLUS, etc…
1 user thanked author for this post.
05/01/2025 at 6:51 PM #246669Hi robertogozzi,
Thank you so much for pointing me in the right direction….
Especially with the my* variable naming, that is going to be useful.
The encyclopaedia is excellent link, and being used constantly, thank you.
https://www.prorealcode.com/prorealtime-documentation/
Dinner time now, and after, I will look to code so the range is applied for the DMIperiod.
I’ll also look into the placement of the stop loss line on the chart, as that doesn’t appear currently.
The arrows appear, I just need to work out why there are two arrows per trade entry (Orange arrow down & blue arrow up), oh, and the no.2.Thank you once again.
NT05/01/2025 at 9:30 PM #246673Hi there.
Lets see if my work with AI, is paying off?
A bit more study is needed.Hopefully, this code will allow me to set the range variables via the backtest interface.
Arrow wise, I am not so sure?
As always, any guidance is much appreciated.// ========================================================================// STRATEGY: DMI-ADX Crossover Trading Backtest// AUTHOR: Cody AI// DATE: Thu.01.May.2025// VERSION: 1.0// ========================================================================// ========================================// 1. INDICATOR SELECTION FROM PRT LIBRARY// ========================================// DMI with default periods// ATR for stop loss calculation// ============================================// 2. INPUTS TO SET VIA PRT BACKTEST INTERFACE// ============================================RiskPercent = 5myPipValue = 0.0001PositionSize = 10// ==============================// 3. VARIABLES USED IN THE CODE// ==============================DMIperiod = 14 // This will be optimized from 1 to 20 with step 1ATRperiod = 14ATRmultiplier = 2.5// =======================// 4. CODING THE BACKTEST// =======================// INDICATOR CALCULATIONS// ———————-myDIplus = DIPLUS[DMIperiod] // Using DMIperiod for optimizationmyDIminus = DIMINUS[DMIperiod] // Using DMIperiod for optimizationATR = ATR[ATRperiod]// TRADE ENTRY EXIT LOGIC (CONDITIONS)// ———————————-LongEntrySignal = myDIplus CROSSES OVER myDIminusShortEntrySignal = myDIminus CROSSES OVER myDIplusLongExitSignal = myDIplus CROSSES UNDER myDIminusShortExitSignal = myDIminus CROSSES UNDER myDIplus// STOP LOSS CALCULATION USING ATR// ——————————StopLossDistance = ATR * ATRmultiplierLongStopLoss = CLOSE – StopLossDistanceShortStopLoss = CLOSE + StopLossDistance// LONG POSITION LOGIC// ——————IF LongEntrySignal AND NOT LongOnMarket THENBUY PositionSize SHARES AT MARKETSELL PositionSize SHARES AT LongStopLoss STOPENDIF// SHORT POSITION LOGIC// ——————-IF ShortEntrySignal AND NOT ShortOnMarket THENSELLSHORT PositionSize SHARES AT MARKETEXITSHORT PositionSize SHARES AT ShortStopLoss STOPENDIF// EXIT LONG POSITION ON SIGNAL/TRIGGER// ———————————–IF LongOnMarket AND LongExitSignal THENSELL AT MARKETENDIF// EXIT SHORT POSITION ON SIGNAL/TRIGGER// ————————————IF ShortOnMarket AND ShortExitSignal THENExitShort AT MARKETENDIF// DRAW ENTRY SIGNALS ON THE CHART// ————————————// Draw up arrow for buy signal using GraphOnPriceIF LongEntrySignal THENGraphOnPrice(“▲”, LOW – (ATR * 0.5)) COLOURED(0, 255, 0)ENDIF// Draw down arrow for sell signal using GraphOnPriceIF ShortEntrySignal THENGraphOnPrice(“▼”, HIGH + (ATR * 0.5)) COLOURED(255, 0, 0)ENDIF// END// ========================================================================// END OF STRATEGY CODEMy thanks in advance of any assistance.
MT
05/01/2025 at 9:46 PM #246674Odd, just testing said code, and there is a line 1 Error: Unknown command…
Line 1 = // comment, so clearly my code is in error elsewhere !
Ah, my “Arrows” are at fault, so I have removed those,
Re-run the backtest , but it is not optimised !? Odd…
05/02/2025 at 4:09 PM #246703I thought, when you add the optimisation variable(s), if you don’t comment out the original variable in the code, that affects the results as it will override the optimisation setting.
2 users thanked author for this post.
05/02/2025 at 4:16 PM #246705druby is correct above.
For example, if you optimise DMIperiod (as DMIperiod entered in the optimiser) you would need to show it in the code as below …
DMIperiod = DMIPeriod //14 // This will be optimized from 1 to 20 with step 1
1 user thanked author for this post.
05/02/2025 at 4:28 PM #24670605/02/2025 at 4:31 PM #246707GraHal,
Very well explained, thank you also for assisting.Now it’s clear….
BTW, do you know how I can change or amend the arrows upon the chart to signal an entry and exit…
I have looked for answers, and as yet, I am not able to find such code or a suitable explanation…
05/02/2025 at 4:39 PM #246708arrows upon the chart to signal an entry and exit…
Yes there are various options in the Settings, Chart and put arrows in the search box or maybe icons.
I can’t post a screenshot right now as my Platform has just frozen!1 user thanked author for this post.
05/02/2025 at 5:11 PM #246710If you right click an entry or exit icon in the price chart and choose ‘Configure’ there are some option there to change the symbols, but you may have to expand the displayed options.
Also, if you left click on the back-test strategy name label in the chart window, and choose ‘Configure’, there is option to change the colour.
Additionally, if you left click on the ‘list icon’ that appears at the right hand side of the price panels name label (icon on price window), there is option to turn on and off various orders per strategy and manual which may appear in same chart window.
That’s all that comes to mind at moment, there may be other options in the main settings.
1 user thanked author for this post.
05/02/2025 at 5:13 PM #24671105/02/2025 at 5:16 PM #24671305/02/2025 at 5:18 PM #246714 -
AuthorPosts
Find exclusive trading pro-tools on