ProBacktest and drawing on the charts
Forums › ProRealTime English forum › ProOrder support › ProBacktest and drawing on the charts
- This topic has 9 replies, 5 voices, and was last updated 2 hours ago by
NeoTrader.
-
-
05/12/2025 at 10:18 PM #247023
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.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181// ========================================================================// 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 PlatformmyATRperiod = 14myATRmultiplier = 2.5myRiskPercent = 5 // Risk percentage (5%)myInitialBalance = 100000 // Starting capitalmyADXThreshold = 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 ENTRYmyLongSignal = myDIplus CROSSES OVER myDIminusmyShortSignal = myDIminus CROSSES OVER myDIplus// ADX trend strength filtermyADXStrong = myADX > myADXThreshold// Final signals with filter appliedmyLongSignal = myLongSignal AND myADXStrongmyShortSignal = myShortSignal AND myADXStrong// === STOP LOSS DISTANCE ===myStopLossDistance = myATR * myATRmultiplier// === RISK MANAGEMENT ===// Initialize account balance trackingONCE myCurrentBalance = myInitialBalanceONCE myPreviousEquity = myInitialBalanceONCE myProfitLoss = 0// Update account balance based on closed positionsIF BarsSince(LongOnMarket) = 0 OR BarsSince(ShortOnMarket) = 0 THEN// New position openedmyPreviousEquity = myCurrentBalanceENDIF// If position was closed on previous bar, update the balanceIF (LongOnMarket[1] AND NOT LongOnMarket) OR (ShortOnMarket[1] AND NOT ShortOnMarket) THEN// Calculate profit/loss from the closed positionIF LongOnMarket[1] THENmyProfitLoss = (close[1] - POSITIONPRICE[1]) * COUNTOFPOSITION[1]ELSEmyProfitLoss = (POSITIONPRICE[1] - close[1]) * ABS(COUNTOFPOSITION[1])ENDIF// Update current balancemyCurrentBalance = myCurrentBalance + myProfitLossENDIF// Calculate position size based on riskmyRiskAmount = myCurrentBalance * (myRiskPercent / 100)// Calculate position size based on stop loss distanceIF myStopLossDistance > 0 THENmyRiskBasedPositionSize = ROUND(myRiskAmount / myStopLossDistance)// Ensure minimum position size of 1IF myRiskBasedPositionSize < 1 THENmyRiskBasedPositionSize = 1ENDIFELSEmyRiskBasedPositionSize = 1ENDIF// === STATE FLAGS ===// Initialize these variables only once to maintain state between barsONCE myReverseToLong = 0ONCE myReverseToShort = 0ONCE myEntryPrice = 0ONCE myLongStopLevel = 0ONCE myShortStopLevel = 0ONCE myLongEntryBar = 0ONCE myShortEntryBar = 0// === POSITION MANAGEMENT ===IF LongOnMarket THEN// Store entry price when entering a positionIF BarsSince(LongOnMarket) = 0 THENmyEntryPrice = closemyLongStopLevel = myEntryPrice - myStopLossDistancemyLongEntryBar = BarIndexENDIF// 1. Check stop lossIF low <= (myEntryPrice - myStopLossDistance) THENSELL AT MARKETDRAWSEGMENT(myLongEntryBar, myLongStopLevel, BarIndex, myLongStopLevel) COLOURED(0,255,0)myLongStopLevel = undefinedELSE// 2. Check for reversalIF myShortSignal THENSELL AT MARKETDRAWSEGMENT(myLongEntryBar, myLongStopLevel, BarIndex, myLongStopLevel) COLOURED(0,255,0)myReverseToShort = 1myLongStopLevel = undefinedENDIFENDIFENDIFIF ShortOnMarket THEN// Store entry price when entering a positionIF BarsSince(ShortOnMarket) = 0 THENmyEntryPrice = closemyShortStopLevel = myEntryPrice + myStopLossDistancemyShortEntryBar = BarIndexENDIF// 1. Check stop lossIF high >= (myEntryPrice + myStopLossDistance) THENEXITSHORT AT MARKETDRAWSEGMENT(myShortEntryBar, myShortStopLevel, BarIndex, myShortStopLevel) COLOURED(255,0,0)myShortStopLevel = undefinedELSE// 2. Check for reversalIF myLongSignal THENEXITSHORT AT MARKETDRAWSEGMENT(myShortEntryBar, myShortStopLevel, BarIndex, myShortStopLevel) COLOURED(255,0,0)myReverseToLong = 1myShortStopLevel = undefinedENDIFENDIFENDIF// === ENTRY WHEN FLAT ===IF NOT LongOnMarket AND NOT ShortOnMarket THENIF myReverseToLong THENBUY myRiskBasedPositionSize SHARES AT MARKETmyReverseToLong = 0ELSEIF myReverseToShort THENSELLSHORT myRiskBasedPositionSize SHARES AT MARKETmyReverseToShort = 0ELSEIF myLongSignal THENBUY myRiskBasedPositionSize SHARES AT MARKETELSEIF myShortSignal THENSELLSHORT myRiskBasedPositionSize SHARES AT MARKETENDIFENDIFENDIFENDIFENDIF// === VISUALIZATION USING DRAWSEGMENT ===// Draw active stop loss lines while positions are openIF LongOnMarket THENDRAWSEGMENT(myLongEntryBar, myLongStopLevel, BarIndex, myLongStopLevel) COLOURED(0,255,0)ENDIFIF ShortOnMarket THENDRAWSEGMENT(myShortEntryBar, myShortStopLevel, BarIndex, myShortStopLevel) COLOURED(255,0,0)ENDIF// === PRINT TRADE INFORMATION ===// Display trade information in a table for debuggingPRINT 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 variableIF myLongSignal THENmySignalVisualization = 1ELSIF myShortSignal THENmySignalVisualization = -1ELSEmySignalVisualization = 0ENDIF// graph mySignalVisualizationThere 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,
NT05/12/2025 at 11:12 PM #24702405/12/2025 at 11:33 PM #247025In 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”.2 users thanked author for this post.
05/13/2025 at 1:38 PM #247056In 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
05/13/2025 at 4:52 PM #247065Append these lines using GraphOnPrice to your code:
12GraphOnPrice ShortStop coloured("Red")GraphOnPrice LongStop coloured("Green")1 user thanked author for this post.
05/13/2025 at 5:33 PM #247068// === 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 openIF LongOnMarket THENDRAWSEGMENT(myLongEntryBar, myLongStopLevel, BarIndex, myLongStopLevel) COLOURED(0,255,0)ENDIFIF ShortOnMarket THENDRAWSEGMENT(myShortEntryBar, myShortStopLevel, BarIndex, myShortStopLevel) COLOURED(255,0,0)ENDIF// === PRINT TRADE INFORMATION ===Just checking, as I am not sure on the code refactoring…05/13/2025 at 5:59 PM #247074There you go:
1234567891011// === VISUALIZATION USING DRAWSEGMENT ===// Draw active stop loss lines while positions are openIF LongOnMarket THEN//DRAWSEGMENT(myLongEntryBar, myLongStopLevel, BarIndex, myLongStopLevel) COLOURED(0,255,0)ENDIFIF ShortOnMarket THEN//DRAWSEGMENT(myShortEntryBar, myShortStopLevel, BarIndex, myShortStopLevel) COLOURED(255,0,0)ENDIF// === PRINT TRADE INFORMATION ===GraphOnPrice myShortStopLevel coloured("Red")GraphOnPrice myLongStopLevel coloured("Green")1 user thanked author for this post.
05/13/2025 at 6:42 PM #247077// === 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
05/13/2025 at 8:26 PM #247083There 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.
1 user thanked author for this post.
05/13/2025 at 9:19 PM #247084There 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
-
AuthorPosts
Find exclusive trading pro-tools on