New to programming, need to know what’s wrong with my code
Forums › ProRealTime English forum › ProOrder support › New to programming, need to know what’s wrong with my code
- This topic has 6 replies, 4 voices, and was last updated 4 hours ago by
robertogozzi.
-
-
04/10/2025 at 6:16 AM #245769
Hi! I’m new to trading and new to programming as well. I’m trying to create a simple stop losses system depending on how many point the stock moves.
The issue is that the stop losses don’t trigger as I wish. Can someone see what’s wrong with the code?
I wish:
Initial stop loss at 100
When the stock moves 100+, the stop loss moves to 50.
A fuurther movement of 50 point, the stop loss moves another 50 point
After 200 points of market move, the trailingstop of 100 point kicks in.
123456789101112131415161718192021222324252627282930313233343536373839// **Lång strategi för Nasdaq**DEFPARAM PRELOADBARS = 1000ONCE StopAdjustedNASDAQLong = 0ONCE StopAdjustedNASDAQLong150 = 0ONCE TrailingNASDAQLong = 0IF NOT LongOnMarket AND NOT ShortOnMarket THENIF Time = 083000 OR Time = 153000 THENBUY 0.5 CONTRACTS AT MARKETSET STOP LOSS 100StopAdjustedNASDAQLong = 0StopAdjustedNASDAQLong150 = 0TrailingNASDAQLong = 0ENDIFENDIF// Justera stop-loss om priset har gått +100 punkter från TradePriceIF LongOnMarket AND Close >= TradePrice + 100 AND StopAdjustedNASDAQLong = 0 THENSET STOP LOSS TradePrice + 50StopAdjustedNASDAQLong = 1ENDIF// Justera stop-loss om priset har gått +150 punkter från TradePriceIF LongOnMarket AND Close >= TradePrice + 150 AND StopAdjustedNASDAQLong150 = 0 THENSET STOP LOSS TradePrice + 100StopAdjustedNASDAQLong150 = 1ENDIF// Aktivera trailing stop-loss om priset har gått +200 punkterIF LongOnMarket AND Close >= TradePrice + 200 AND TrailingNASDAQLong = 0 THENSET STOP TRAILING 100TrailingNASDAQLong = 1ENDIF// Stäng alla långa positioner innan stängningIF LongOnMarket AND Time = 215700 THENSELL AT MARKETENDIF04/10/2025 at 7:37 AM #245770Hi.
I see you’re using incorrect expressions when defining the stop loss. If you want to set the stop at X points, you should use the expression
SET STOP pLOSS X
.Here are some examples:
https://www.prorealcode.com/documentation/ploss-2/
04/11/2025 at 5:50 AM #24578604/11/2025 at 7:52 AM #245790In principle, yes. Perhaps I would change the last expression to this one:
123IF LongOnMarket AND openTime >= 215700 THENSELL AT MARKETENDIFOr you could place this at the beginning of the code:
1defparam flatafter = 2157001 user thanked author for this post.
04/11/2025 at 8:18 AM #245791Hello,
as you mention you’re new to this, it’s probably worth mentionning what might seem obvious for those familiar with the platform:
1) the instructions “time” and “opentime” are respectively the closing time and the opening time of a candle
2) a proorder code is only read at candle close, and if orders are sent they are executed immediatly after, namely at the open of the following candle
These 2 points together in mind, when you say in the code:
12IF Time = 083000 OR Time = 153000 THENBUY 0.5 CONTRACTS AT MARKETif you run your code say on a one hour timeframe rather than a smaller timeframe, you wouldn’t have any candle closing at 8:30 or 15:30 so the orders would never be sent..Similarly, when you say :12IF LongOnMarket AND Time = 215700 THENSELL AT MARKETif you are on a 5mn timeframe, or even just a 2mn timeframe, there is no candle closing at 21:57, so you would need to run such code on a 1mn timeframe or x seconds providing it divides 1mn (30s,15s,…)
.Maybe you were already doing that, but I don’t see it in your post, so that’s why I specify it, just in case… because, if you’re running on a timeframe not compatible with those times and you see no orders sent, you might be looking for an error in the code when in fact, it’s an error in timeframe choice (or you might choose to keep a certain timeframe but then need to amend time values in the code to make them compatible with chosen timeframe).04/12/2025 at 11:56 AM #245848I want to delve deeper into setting a TARGET and a STOP LOSS.
SET STOP LOSS and SET TARGET PROFIT both require a value, be it a constant or a variable or an expression, expressed as a price, thus that value must be converted from a number into a price by multiplying it by PIPSIZE (or its equivalent POINTSIZE). Should you use a difference between prices, such as the expression highest[10](high) – lowest[10](low), then NO conversion should be performed as they ARE prices, so this is correct:
12SET STOP LOSS highest[10](high) - lowest[10](low)SET TARGET PROFIT highest[10](high) - lowest[10](low)pSTOP and pPROFIT both require a value that is the number of PIPS, be it a constant or a variable or an expression, thus that value must be a number which doesn’t require any conversion. A conversion would be needed if you would like to use a value expressed as a price, such as the expression highest[10](high) – lowest[10](low). These statements are both correct:
12SET STOP pLOSS 100 //Pips are assumed by defaultSET STOP pLOSS (highest[10](high) - lowest[10](low) / PipSize //any price expression must be converted into Pipsany price, be it a constant or a variable or a price expression, must be converted into Pips by DIVIDING that price by PIPSIZE (or its equivalent POINTSIZE).
What has been said about LOSS/TARGET and pLOSS/pTARGET is also true for TRAILING/pTRAILING.
A different case are SET STOP price and SET TARGET price, as they only require a price, so the statements:
12SET STOP PRICE TradePriceSET STOP PRICE TradePrice + 50*PipSizeare both correct and only the latter one requires the numeric constant to be converted into a price, while TRADEPRICE is a price by itself.
1 user thanked author for this post.
04/12/2025 at 2:59 PM #245851My bad, the line:
What has been said about LOSS/
TARGETand pLOSS/pTARGETis also true for TRAILING/pTRAILINGmust be replaced by:
What has been said about LOSS/PROFIT and pLOSS/pPROFIT is also true for TRAILING/pTRAILING.
-
AuthorPosts