ProBacktest and drawing on the charts

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #247023 quote
    NeoTrader
    Participant
    New

    Hi there.

    I am either making a classic syntax error here or I am missing a piece of the code puzzle!….
    Backtesting only.

    I am looking for a visual line between the trade entry bar and the trade exit bar (all entered trades).
    Long trade entry/exit = green line
    Short trade entry/exit = red line
    The line needs to remain on the chart, for a visual evaluation of the backtest process.

    
    // ========================================================================
    // STRATEGY: DMI-ADX Crossover with Stop Loss + Opposite Signal Exit
    // ========================================================================
    // BT-DMI-testV3.0.3viasupport
    
    // === INPUTS ===
    myDMIperiod = myDMIperiod // 10 // optimize from 1 to 20 via PRT Platform
    myATRperiod = 14
    myATRmultiplier = 2.5
    myRiskPercent = 5 // Risk percentage (5%)
    myInitialBalance = 100000 // Starting capital
    myADXThreshold = 20 // Minimum ADX value for trend strength confirmation
    
    // === INDICATORS ===
    myDIplus = DIPLUS[myDMIperiod]
    myDIminus = DIMINUS[myDMIperiod]
    myADX = ADX[myDMIperiod]
    myATR = AVERAGETRUERANGE[myATRperiod]
    
    // === SIGNALS ===
    // Basic DMI crossover signals - SAME BAR ENTRY
    myLongSignal = myDIplus CROSSES OVER myDIminus
    myShortSignal = myDIminus CROSSES OVER myDIplus
    
    // ADX trend strength filter
    myADXStrong = myADX > myADXThreshold
    
    // Final signals with filter applied
    myLongSignal = myLongSignal AND myADXStrong
    myShortSignal = myShortSignal AND myADXStrong
    
    // === STOP LOSS DISTANCE ===
    myStopLossDistance = myATR * myATRmultiplier
    
    // === RISK MANAGEMENT ===
    // Initialize account balance tracking
    ONCE myCurrentBalance = myInitialBalance
    ONCE myPreviousEquity = myInitialBalance
    ONCE myProfitLoss = 0
    
    // Update account balance based on closed positions
    IF BarsSince(LongOnMarket) = 0 OR BarsSince(ShortOnMarket) = 0 THEN
    // New position opened
    myPreviousEquity = myCurrentBalance
    ENDIF
    
    // If position was closed on previous bar, update the balance
    IF (LongOnMarket[1] AND NOT LongOnMarket) OR (ShortOnMarket[1] AND NOT ShortOnMarket) THEN
    // Calculate profit/loss from the closed position
    IF LongOnMarket[1] THEN
    myProfitLoss = (close[1] - POSITIONPRICE[1]) * COUNTOFPOSITION[1]
    ELSE
    myProfitLoss = (POSITIONPRICE[1] - close[1]) * ABS(COUNTOFPOSITION[1])
    ENDIF
        
    // Update current balance
    myCurrentBalance = myCurrentBalance + myProfitLoss
    ENDIF
    
    // Calculate position size based on risk
    myRiskAmount = myCurrentBalance * (myRiskPercent / 100)
    
    // Calculate position size based on stop loss distance
    IF myStopLossDistance > 0 THEN
    myRiskBasedPositionSize = ROUND(myRiskAmount / myStopLossDistance)
    // Ensure minimum position size of 1
    IF myRiskBasedPositionSize < 1 THEN
    myRiskBasedPositionSize = 1
    ENDIF
    ELSE
    myRiskBasedPositionSize = 1
    ENDIF
    
    // === STATE FLAGS ===
    // Initialize these variables only once to maintain state between bars
    ONCE myReverseToLong = 0
    ONCE myReverseToShort = 0
    ONCE myEntryPrice = 0
    ONCE myLongStopLevel = 0
    ONCE myShortStopLevel = 0
    ONCE myLongEntryBar = 0
    ONCE myShortEntryBar = 0
    
    // === POSITION MANAGEMENT ===
    IF LongOnMarket THEN
    // Store entry price when entering a position
    IF BarsSince(LongOnMarket) = 0 THEN
    myEntryPrice = close
    myLongStopLevel = myEntryPrice - myStopLossDistance
    myLongEntryBar = BarIndex
    ENDIF
        
    // 1. Check stop loss
    IF low <= (myEntryPrice - myStopLossDistance) THEN
    SELL AT MARKET
    DRAWSEGMENT(myLongEntryBar, myLongStopLevel, BarIndex, myLongStopLevel) COLOURED(0,255,0)
    myLongStopLevel = undefined
    ELSE
    // 2. Check for reversal
    IF myShortSignal THEN
    SELL AT MARKET
    DRAWSEGMENT(myLongEntryBar, myLongStopLevel, BarIndex, myLongStopLevel) COLOURED(0,255,0)
    myReverseToShort = 1
    myLongStopLevel = undefined
    ENDIF
    ENDIF
    ENDIF
    
    IF ShortOnMarket THEN
    // Store entry price when entering a position
    IF BarsSince(ShortOnMarket) = 0 THEN
    myEntryPrice = close
    myShortStopLevel = myEntryPrice + myStopLossDistance
    myShortEntryBar = BarIndex
    ENDIF
        
    // 1. Check stop loss
    IF high >= (myEntryPrice + myStopLossDistance) THEN
    EXITSHORT AT MARKET
    DRAWSEGMENT(myShortEntryBar, myShortStopLevel, BarIndex, myShortStopLevel) COLOURED(255,0,0)
    myShortStopLevel = undefined
    ELSE
    // 2. Check for reversal
    IF myLongSignal THEN
    EXITSHORT AT MARKET
    DRAWSEGMENT(myShortEntryBar, myShortStopLevel, BarIndex, myShortStopLevel) COLOURED(255,0,0)
    myReverseToLong = 1
    myShortStopLevel = undefined
    ENDIF
    ENDIF
    ENDIF
    
    // === ENTRY WHEN FLAT ===
    IF NOT LongOnMarket AND NOT ShortOnMarket THEN
    IF myReverseToLong THEN
    BUY myRiskBasedPositionSize SHARES AT MARKET
    myReverseToLong = 0
    ELSE
    IF myReverseToShort THEN
    SELLSHORT myRiskBasedPositionSize SHARES AT MARKET
    myReverseToShort = 0
    ELSE
    IF myLongSignal THEN
    BUY myRiskBasedPositionSize SHARES AT MARKET
    ELSE
    IF myShortSignal THEN
    SELLSHORT myRiskBasedPositionSize SHARES AT MARKET
    ENDIF
    ENDIF
    ENDIF
    ENDIF
    ENDIF
    
    // === VISUALIZATION USING DRAWSEGMENT ===
    // Draw active stop loss lines while positions are open
    IF LongOnMarket THEN
    DRAWSEGMENT(myLongEntryBar, myLongStopLevel, BarIndex, myLongStopLevel) COLOURED(0,255,0)
    ENDIF
    
    IF ShortOnMarket THEN
    DRAWSEGMENT(myShortEntryBar, myShortStopLevel, BarIndex, myShortStopLevel) COLOURED(255,0,0)
    ENDIF
    
    // === PRINT TRADE INFORMATION ===
    // Display trade information in a table for debugging
    PRINT myEntryPrice AS "Entry Price" COLOURED(255,255,255) FILLCOLOR(0,0,128)
    PRINT myLongStopLevel AS "Long Stop" COLOURED(255,255,255) FILLCOLOR(0,128,0)
    PRINT myShortStopLevel AS "Short Stop" COLOURED(255,255,255) FILLCOLOR(128,0,0)
    PRINT myRiskBasedPositionSize AS "Position Size" COLOURED(255,255,255) FILLCOLOR(0,0,128)
    PRINT myCurrentBalance AS "Account Balance" COLOURED(255,255,255) FILLCOLOR(0,0,128)
    
    // Signal visualization variable
    IF myLongSignal THEN
    mySignalVisualization = 1
    ELSIF myShortSignal THEN
    mySignalVisualization = -1
    ELSE
    mySignalVisualization = 0
    ENDIF
    
    // graph mySignalVisualization
    
    

    There are syntax errors on the DRAWSEGMENT x2 and the ENDIF !!!

    I cant see why the error occur?
    As always, any code guidance is much appreciated.

    With thanks,
    NT

    #247024 quote
    PeterSt
    Participant
    Master

    DrawSegment is a command for an Indicator. And you apply this to Strategy Code. That implies a Syntax Error.
    Sorry …
    🙂

    JS, NeoTrader and Iván González thanked this post
    #247025 quote
    JS
    Participant
    Senior

    In ProBackTest/ProOrder, graphical instructions are not allowed. For debugging purposes, you can use “Graph” (to visualize variable values in a separate window), “GraphOnPrice” (to display variable values on the price chart), and “Print” (to output variable values in a tabular format) within ProBackTest.
    When the system is uploaded to the PRT servers (Live), the entire code must be “clean” — meaning it must not include any use of “Graph”, “GraphOnPrice”, or “Print”.

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

    In ProBackTest/ProOrder, graphical instructions are not allowed….

    Hey JS, or perhaps if anyone else can assist ☺️

    Thank you very much for the clarification.

    Would you kindly show me how to add said graph text to the code, so that it displays (on the stop loss line), the level applied (as the stop loss level).
    Ideally the stop loss value would be placed on the trade entry bar (See attached with ‘*’ ).

    With thanks in anticipation.

    NT

    PRT-PBC-stop-loss-level-text-required-Screenshot-2025-05-13-133527.png PRT-PBC-stop-loss-level-text-required-Screenshot-2025-05-13-133527.png
    #247065 quote
    robertogozzi
    Moderator
    Master

    Append these lines using GraphOnPrice to your code:

    GraphOnPrice ShortStop coloured("Red")
    GraphOnPrice LongStop  coloured("Green")
    NeoTrader thanked this post
    #247068 quote
    NeoTrader
    Participant
    New

    // === VISUALIZATION USING DRAWSEGMENT === // Draw active stop loss lines while positions are open IF LongOnMarket THEN DRAWSEGMENT(myLongEntryBar, myLongStopLevel, BarIndex, myLongStopLevel) COLOURED(0,255,0) ENDIF   IF ShortOnMarket THEN DRAWSEGMENT(myShortEntryBar, myShortStopLevel, BarIndex, myShortStopLevel) COLOURED(255,0,0) ENDIF   // === PRINT TRADE INFORMATION ===

    Do i replace this code with yours Roberto?

    // === VISUALIZATION USING DRAWSEGMENT ===
    // Draw active stop loss lines while positions are open
    IF LongOnMarket THEN
    DRAWSEGMENT(myLongEntryBar, myLongStopLevel, BarIndex, myLongStopLevel) COLOURED(0,255,0)
    ENDIF
    IF ShortOnMarket THEN
    DRAWSEGMENT(myShortEntryBar, myShortStopLevel, BarIndex, myShortStopLevel) COLOURED(255,0,0)
    ENDIF
    // === PRINT TRADE INFORMATION ===
    Just checking, as I am not sure on the code refactoring…
    #247074 quote
    robertogozzi
    Moderator
    Master

    There you go:

    // === VISUALIZATION USING DRAWSEGMENT ===
    // Draw active stop loss lines while positions are open
    IF LongOnMarket THEN
       //DRAWSEGMENT(myLongEntryBar, myLongStopLevel, BarIndex, myLongStopLevel) COLOURED(0,255,0)
    ENDIF
    IF ShortOnMarket THEN
       //DRAWSEGMENT(myShortEntryBar, myShortStopLevel, BarIndex, myShortStopLevel) COLOURED(255,0,0)
    ENDIF
    // === PRINT TRADE INFORMATION ===
    GraphOnPrice myShortStopLevel coloured("Red")
    GraphOnPrice myLongStopLevel  coloured("Green")
    NeoTrader and Iván González thanked this post
    #247077 quote
    NeoTrader
    Participant
    New

    // === VISUALIZATION USING DRAWSEGMENT ===

    Hey robertogozzi
    Just checking…
    The code works… but, it does not display the stop loss ‘level’, currently….

    Originally, I was asking for:

    Would you kindly show me how to add said graph text to the code, so that it displays (on the stop loss line), the stop loss level.
    Ideally the stop loss value would be placed on the trade entry bar (See attached with ‘*’ ).

    I hope that is of use, can you kindly show me the code needed to place said stop loss level.

    That would be much appreciated.

    NT

    PRT-PBC-stop-loss-level-text-required-Screenshot-2025-05-13-133527-1.png PRT-PBC-stop-loss-level-text-required-Screenshot-2025-05-13-133527-1.png
    #247083 quote
    druby
    Participant
    New

    There no way to display text on the graph in back-test. The only way to associate text with a line is via the AS”label” command after the GRAPH variable.

    This only shows up in the cursor info box with open; close, etc.

    If the variable has a logical name like  ‘longStoploss’ then you don’t need the AS “” since the variable name is shown as default and says it all.

    NeoTrader and Iván González thanked this post
    #247084 quote
    NeoTrader
    Participant
    New

    There no way to display text on the graph in back-test….

    Hey  druby,

    Thanks for following up with this….
    I am a little confused right now, having applied the code suggested….

    The stop loss line as is, is workable (Given the backtest code limitations).

    So, I understand, you are saying, that I cannot apply the stop loss ‘level’ (value) to the chart, as per my attached image?

    The name is not needed, just the value of the stop loss level on the bar where there is a trade entry…

     

    Just when I think I am getting to understand the probacktest code, I get another curve ball lol….

    The working code is attached in txt file (which works well for coding)

    Aside, I do appreciate your follow-up help on this….

    my kindest regards,

    NT

    Iván González thanked this post
    BT-DMI-test-V3.0.3-VS-RG.txt
    #247102 quote
    JS
    Participant
    Senior

    Function Comparison: ProBackTest/ProOrder vs. ProBuilder

    Function Category ProBackTest / ProOrder ProBuilder
    Graphical functions (DRAWTEXT, DRAWLINE, etc.) Not allowed Allowed
    NeoTrader thanked this post
    #247104 quote
    NeoTrader
    Participant
    New

    Function Comparison:…..

    Hey JS,

    Yep, ive stripped out said DRAW* code, etc…. And I have added said code to my use / do not use sheet !

    But I did find a way to show the exit types….

    1. Stop loss
    2. Opposite Signal

    It is on the code, you have also engaged: https://www.prorealcode.com/topic/backtesting-and-the-correct-use-of-stops-code-that-is/#post-247103

    Thank you for the heads up.

    It is much appreciated.

    NT.

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.

ProBacktest and drawing on the charts


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
NeoTrader @neotrader Participant
Summary

This topic contains 11 replies,
has 5 voices, and was last updated by NeoTrader
9 months, 1 week ago.

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