New to PRT and PBC, so be firm and fair…

Viewing 15 posts - 1 through 15 (of 45 total)
  • Author
    Posts
  • #246658 quote
    NeoTrader
    Participant
    New

    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 !?!?

    // ========================================================================
    // 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

    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.

    ProRealTime-ProBuilder-Code-Template.V1.0.txt
    #246667 quote
    robertogozzi
    Moderator
    Master

    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)
    NeoTrader and Iván González thanked this post
    #246668 quote
    robertogozzi
    Moderator
    Master

    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…

    NeoTrader thanked this post
    #246669 quote
    NeoTrader
    Participant
    New

    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

    #246673 quote
    NeoTrader
    Participant
    New

    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.

    // ========================================================================
    // 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     // This will be optimized from 1 to 20 with step 1
    ATRperiod = 14
    ATRmultiplier = 2.5
    // =======================
    // 4. CODING THE BACKTEST
    // =======================
    // INDICATOR CALCULATIONS
    // ———————-
    myDIplus = DIPLUS[DMIperiod]   // Using DMIperiod for optimization
    myDIminus = DIMINUS[DMIperiod] // Using DMIperiod for optimization
    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
    // DRAW ENTRY SIGNALS ON THE CHART
    // ————————————
    // Draw up arrow for buy signal using GraphOnPrice
    IF LongEntrySignal THEN
        GraphOnPrice(“▲”, LOW – (ATR * 0.5)) COLOURED(0, 255, 0)
    ENDIF
    // Draw down arrow for sell signal using GraphOnPrice
    IF ShortEntrySignal THEN
        GraphOnPrice(“▼”, HIGH + (ATR * 0.5)) COLOURED(255, 0, 0)
    ENDIF
    // END
    // ========================================================================
    // END OF STRATEGY CODE

    My thanks in advance of any assistance.

    MT

    #246674 quote
    NeoTrader
    Participant
    New

    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…

    #246703 quote
    druby
    Participant
    New

    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.

    NeoTrader and robertogozzi thanked this post
    #246705 quote
    GraHal
    Participant
    Master

    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

    NeoTrader and robertogozzi thanked this post
    #246706 quote
    NeoTrader
    Participant
    New

    Druby,
    Ah, often, the simplest of answers are the most successful…

    A lesson learned, and I thank you kindly for the correction.

    NT

    druby thanked this post
    #246707 quote
    NeoTrader
    Participant
    New

    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…

    GraHal and druby thanked this post
    #246708 quote
    GraHal
    Participant
    Master

    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!

    NeoTrader thanked this post
    #246710 quote
    druby
    Participant
    New

    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.

    NeoTrader thanked this post
    #246711 quote
    GraHal
    Participant
    Master

    Thawed out now 🙂

    See attached

    NeoTrader thanked this post
    MCcZsw9neb.png MCcZsw9neb.png
    #246713 quote
    NeoTrader
    Participant
    New

    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

    #246714 quote
    NeoTrader
    Participant
    New

    druby,
    Thank you, that is much appreciated.

    With your explanation, and GraHal image, I am looking sorted…

    Platform on, making changes, and I’ll update when the backtest is run 🙂

    GraHal thanked this post
Viewing 15 posts - 1 through 15 (of 45 total)
  • You must be logged in to reply to this topic.

New to PRT and PBC, so be firm and fair…


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
NeoTrader @neotrader Participant
Summary

This topic contains 44 replies,
has 6 voices, and was last updated by NeoTrader
9 months, 2 weeks ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 05/01/2025
Status: Active
Attachments: 9 files
Logo Logo
Loading...