ProRealCode - Trading & Coding with ProRealTime™
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 gaps
I am not sure what the request means:
Use the ‘Insert PRT Code’ button ???
I have visual or text options !?!?
Thank 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.
The ‘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:
// ========================================================================
// 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 = 5
PipValue = 0.0001
PositionSize = 10
// ==============================
// 3. VARIABLES USED IN THE CODE
// ==============================
DMIperiod = 14
ATRperiod = 14
ATRmultiplier = 2.5
R1start = 1
R1stop = 20
R1step = 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 * ATRmultiplier
LongStopLoss = CLOSE – StopLossDistance
ShortStopLoss = CLOSE + StopLossDistance
// LONG POSITION LOGIC
// ——————–
IF LongEntrySignal AND NOT ISLONG() THEN
BUY PositionSize SHARES AT MARKET
SELL PositionSize SHARES ON STOP AT LongStopLoss
// Draw up arrow for buy signal
DRAWARROWUP(BARINDEX, LOW – (ATR * 0.5)) COLOURED(0, 255, 0)
ENDIF
// SHORT POSITION LOGIC
// ——————–
IF ShortEntrySignal AND NOT ISSHORT() THEN
SELLSHORT PositionSize SHARES AT MARKET
BUYTOCOVER PositionSize SHARES ON STOP AT ShortStopLoss
// Draw down arrow for sell signal
DRAWARROWDOWN(BARINDEX, HIGH + (ATR * 0.5)) COLOURED(255, 0, 0)
ENDIF
// EXIT LONG POSITION ON SIGNAL/TRIGGER
// ————————————
IF ISLONG() AND LongExitSignal THEN
SELL AT MARKET
ENDIF
// EXIT SHORT POSITION ON SIGNAL/TRIGGER
// ————————————–
IF ISSHORT() AND ShortExitSignal THEN
BUYTOCOVER AT MARKET
ENDIF
// DISPLAY STOP LOSS LEVELS WITH COLORED BACKGROUND
// ———————————————–
// Color the background when in a position to show stop loss zones
IF ISLONG() THEN
COLORBETWEEN(CLOSE, LongStopLoss, “lightred”)
ENDIF
IF ISSHORT() THEN
COLORBETWEEN(CLOSE, ShortStopLoss, “lightblue”)
ENDIF
// END
// ========================================================================
// END OF STRATEGY CODE
AI 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 backtesting
This is your working code:
// ========================================================================
// 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 = 5
myPipValue = 0.0001
PositionSize = 10
// ==============================
// 3. VARIABLES USED IN THE CODE
// ==============================
DMIperiod = 14
ATRperiod = 14
ATRmultiplier = 2.5
R1start = 1
R1stop = 20
R1step = 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 myDIminus
ShortEntrySignal = myDIminus CROSSES OVER myDIplus
LongExitSignal = myDIplus CROSSES UNDER myDIminus
ShortExitSignal = myDIminus CROSSES UNDER myDIplus
// STOP LOSS CALCULATION USING ATR
// ——————————-
StopLossDistance = ATR * ATRmultiplier
LongStopLoss = CLOSE - StopLossDistance
ShortStopLoss = CLOSE + StopLossDistance
// LONG POSITION LOGIC
// ——————-
IF LongEntrySignal AND NOT LongOnMarket THEN
BUY PositionSize SHARES AT MARKET
SELL PositionSize SHARES AT LongStopLoss STOP
ENDIF
// SHORT POSITION LOGIC
// ——————-
IF ShortEntrySignal AND NOT ShortOnMarket THEN
SELLSHORT PositionSize SHARES AT MARKET
EXITSHORT PositionSize SHARES AT ShortStopLoss STOP
ENDIF
// EXIT LONG POSITION ON SIGNAL/TRIGGER
// ————————————
IF LongOnMarket AND LongExitSignal THEN
SELL AT MARKET
ENDIF
// EXIT SHORT POSITION ON SIGNAL/TRIGGER
// ————————————-
IF ShortOnMarket AND ShortExitSignal THEN
ExitShort AT MARKET
ENDIF
// DISPLAY STOP LOSS LEVELS WITH COLORED BACKGROUND
// ———————————————-
// Color the background when in a position to show stop loss zones
IF LongOnMarket THEN
//COLORBETWEEN(CLOSE, LongStopLoss, "lightred")
ENDIF
IF ShortOnMarket THEN
//COLORBETWEEN(CLOSE, ShortStopLoss, "lightblue")
ENDIF
// END
// ========================================================================
// END OF STRATEGY CODE
// Draw up arrow for buy signal
GraphOnPrice LOW - (ATR * 0.5) COLOURED(0, 255, 0)
// Draw down arrow for sell signal
GraphOnPrice HIGH + (ATR * 0.5) COLOURED(255, 0, 0)
Moreover, 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…
Hi 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.
NT
Hi 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.
My thanks in advance of any assistance.
MT
Odd, 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…
I 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.
druby 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
Druby,
Ah, often, the simplest of answers are the most successful…
A lesson learned, and I thank you kindly for the correction.
NT
GraHal,
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…
arrows 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!
If 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.
GraHal,
I was just about to reply to you.
That screenshot is priceless, thank you…
Platform now on, so I’ll be putting these suggestions into action…
I add, run the backtest, and report back…
NT
New to PRT and PBC, so be firm and fair…
This topic contains 44 replies,
has 6 voices, and was last updated by NeoTrader
9 months, 2 weeks ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 05/01/2025 |
| Status: | Active |
| Attachments: | 9 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.