Correct basic logic for entries and exits
Forums › ProRealTime English forum › ProOrder support › Correct basic logic for entries and exits
- This topic has 11 replies, 4 voices, and was last updated 2 days ago by
GraHal.
-
-
10/16/2025 at 5:01 PM #252679
Hi everyone,
Assuming that conditions A and B and the trading hours are correctly defined, can anyone see anything wrong with the following logic?
123456789101112131415161718192021222324252627// === Strategy Setup ===defparam preloadbars = 10000defparam cumulateorders = falsex = 1 // Number of contracts to trade — adjust as needed// === Final Entry Logic ===if conditionA AND conditionB AND withinTradingHours AND not onmarket thenbuy x contracts at marketendifif conditionAshort AND conditionBshort AND withinTradingHours AND not onmarket thensellshort x contracts at marketendif// === Exit Logic (Layered) ===ExitLongTrade = close < dema377ExitShortTrade = close > dema377if ExitLongTrade AND onmarket thensell at marketendifif ExitShortTrade AND onmarket thenexitshort at marketendifMy bot is making some very erratic trades and I already know that ConditionA and ConditionB are defined correctly, along with the trading hours. Most notably, it’s not exiting according to these instrcutions.
Are some bots simply too complex for Prorealtime to handle?
Many thanks in advance.
10/16/2025 at 6:47 PM #252685ExitLongTrade = close < dema377
Close would need to be < dema377 at the Close of a bar of whatever Timeframe your code is running on … but I’m sure you know this.
The Sell / Exit Long would occur / be seen at the very start of the next bar (during this bar Close may not be < dema377) … maybe you know this also.
I can’t see anything weird with the code, but we can’t see if the entry and exit conditions may cause weirdness?
1 user thanked author for this post.
10/17/2025 at 12:44 AM #252686Hi, @GraHal and anyone else who is interested in this post,
I gave a simplified version of my strategy because I didn’t want everyone to simply lose the will to live! However, if you or anyone else viewing this post has the time and patience to view the full code and the intended strategy then this is greatly appreciated and I thank you in advance!
Here it is! There are conditions A-F across various timeframes: not just A&B. The 1 second chart is the default timeframe. There is also a ‘kill switch’ if more than 5% capital is lost. I have also tried to enforce just one trade at a time with my use of ‘onmarket’, ‘not onmarket’ and ‘defparam cumulateorders = false’ conditions.
Here is what I intended for the strategy to do for long trades with mirror logic for short trades.
- Condition A: Price above previous close on 1 hour chart
- Condition B: Same again for 15-minute chart
- Condition C: Same again for 5-minute chart
- Condition D: Price above 233 double moving average with this line pointing upwards over the last 5 bars on 30 second chart.
- Condition E: Same again for 5 second chart
- Condition F: Double moving average stack on the 1 second chart, periods, 377, 233, 144,89,55,21. This is not a ribbon but the gradient of each smaller double moving average must be of greater magnitude than the next smallest. So, the 21 has the highest magnitude of gradient whilst the 377 has the lowest magnitude. These gradients are measured over 5 bars. All lines must be sloping upwards.
- Trading allowed between 3.00pm and 8.30 pm UK time
- If all the conditions A-F are met and the time period is correct then enter trade
- Exit trade if price crosses under the 377 double moving average on the 1 second chart.
The above was the intention but the bot is behaving very erratically.
The ‘insert code’ button to present my code neatly in a box, seems to have disappeared, so I’ve written it such as it is.
// === Strategy Setup ===
defparam preloadbars = 10000
defparam cumulateorders = false
x = 1 // Number of contracts to trade — adjust as needed
// === Emergency Stop Setup ===
capital = 10000 // Starting capital (£10,000)
PercentLoss = 5 // Max loss threshold (%)
if strategyprofit < 0 then
iloss = abs(strategyprofit / capital)
if iloss >= PercentLoss / 100 then
quit
endif
endif
// === Trading Hours Filter (UTC) ===
// British Summer Time: 15:00 to 20:45 → UTC: 14:00 to 19:45
withinTradingHours = time >= 140000 AND time <= 194500
// === Timeframe: 1 Hour ===
timeframe(1 hour)
conditionA = close > close[1]
conditionAshort = close < close[1]
// === Timeframe: 15 Minutes ===
timeframe(15 minutes)
conditionB = close > close[1]
conditionBshort = close < close[1]
// === Timeframe: 5 Minutes ===
timeframe(5 minutes)
conditionC = close > close[1]
conditionCshort = close < close[1]
// === Timeframe: 30 Seconds ===
timeframe(30 seconds)
dema233a = dema[233](close)
slope233a = dema233a – dema233a[5]
conditionD = close > dema233a AND slope233a > 0
conditionDshort = close < dema233a AND slope233a < 0
// === Timeframe: 5 Seconds ===
timeframe(5 seconds)
dema233b = dema[233](close)
slope233b = dema233b – dema233b[5]
conditionE = close > dema233b AND slope233b > 0
conditionEshort = close < dema233b AND slope233b < 0
// === 1-Second Chart (Base timeframe) ===
// DEMA calculations
dema21 = dema[21](close)
dema55 = dema[55](close)
dema89 = dema[89](close)
dema144 = dema[144](close)
dema233 = dema[233](close)
dema377 = dema[377](close)
// Slope over 5 candles
slope21 = dema21 – dema21[5]
slope55 = dema55 – dema55[5]
slope89 = dema89 – dema89[5]
slope144 = dema144 – dema144[5]
slope233 = dema233 – dema233[5]
slope377 = dema377 – dema377[5]
// Long preribbon: all slopes up + descending gradient
allSlopesUp = slope21 > 0 AND slope55 > 0 AND slope89 > 0 AND slope144 > 0 AND slope233 > 0 AND slope377 > 0
gradCheck1 = slope21 > slope55
gradCheck2 = slope55 > slope89
gradCheck3 = slope89 > slope144
gradCheck4 = slope144 > slope233
gradCheck5 = slope233 > slope377
gradientOrder = gradCheck1 AND gradCheck2 AND gradCheck3 AND gradCheck4 AND gradCheck5
conditionF = allSlopesUp AND gradientOrder
// Short preribbon: all slopes down + ascending gradient
allSlopesDown = slope21 < 0 AND slope55 < 0 AND slope89 < 0 AND slope144 < 0 AND slope233 < 0 AND slope377 < 0
gradCheck1s = slope21 < slope55
gradCheck2s = slope55 < slope89
gradCheck3s = slope89 < slope144
gradCheck4s = slope144 < slope233
gradCheck5s = slope233 < slope377
gradientOrderS = gradCheck1s AND gradCheck2s AND gradCheck3s AND gradCheck4s AND gradCheck5s
conditionFshort = allSlopesDown AND gradientOrderS
// === Final Entry Logic ===
if conditionA AND conditionB AND conditionC AND conditionD AND conditionE AND conditionF AND withinTradingHours AND not onmarket then
buy x contracts at market
endif
if conditionAshort AND conditionBshort AND conditionCshort AND conditionDshort AND conditionEshort AND conditionFshort AND withinTradingHours AND not onmarket then
sellshort x contracts at market
endif
// === Exit Logic (Layered) ===
ExitLongTrade = close < dema377
ExitShortTrade = close > dema377
if ExitLongTrade AND onmarket then
sell at market
endif
if ExitShortTrade AND onmarket then
exitshort at market
endif
1 user thanked author for this post.
10/17/2025 at 8:22 AM #252687To encourage somebody (and save their time) to spot the problem you are experiencing, post a screenshot showing an ‘out of step’ trade together with words describing what you think that particular trade should be doing.
1 user thanked author for this post.
10/17/2025 at 8:35 AM #252688Are some bots simply too complex for Prorealtime to handle?
No, not at all.
But you should learn to make the strategies yourself instead of let ChatGPT do it.
🙂Edit : I better had said : You should not let ChatGPT do this because that never works out well. So the “you should learn” is inappropriate and I did not mean that really.
1 user thanked author for this post.
10/17/2025 at 8:38 AM #252689First thing I spotted is …
12345// === 1-Second Chart (Base timeframe) ===// DEMA calculationsdema21 = dema[21](close)Line 101, you state 1 second, but all the following lines are under the 5 second Timeframe.
Maybe you are just saying that 1 second is the Chart Timeframe?
1 user thanked author for this post.
10/17/2025 at 8:48 AM #25269010/17/2025 at 2:31 PM #25269610/17/2025 at 3:28 PM #252701not many trades are generated?
Wrong GraHal, remove the Time Constraint and loads of Trades are generated … see attached.
I might have a go at some of the variable values later to see if that curve can be improved
10/17/2025 at 4:19 PM #25270410/17/2025 at 4:42 PM #252705@GraHal, you were absolutely right about declaring the 1 second time frame properly instead of just writing it in commentary. All of the programming intended for the 1 second time was indeed sitting under the 5 second timeframe block. Thank you so much for bringing this to my attention.
@Everyone, I’ve realised something else too. I’ve named exit conditions as ‘exitLongTrade’ and ‘exitShortTrade’ but the they’re only linked to the condition ‘onmarket’ regardless of whether I am long or short. These need to be bounded so that the short exit conditions only apply when a short trade is active and the long exit conditions only apply when a long trade is active. I don’t think the algorithm can ‘see’ whether the active trade is long or short in order to apply the appropriate exit condtion.
I need to enforce just one trade at a time. I think I have two options but I may be mistaken. What are your opinions?
Option 1: Keep the current program and try to bind the long entry conditons to the long exit conditions (likewise with the short conditions). I’ll have to swat up on how to do this.
Option 2: Write 2 trading bots: a ‘long bot’ and a ‘short bot’. However both bots still need to be able to ‘see whether or not a trade is active regardless of which bot placed it so as not to open a new trade whilst one is active. Can I make the long bot only reponsible for exiting long trades (same for the short bot)?
Many thanks to those who have engaged with this thread, and thanks in advance to those who respond to thesse latest comment.
The modified code I have so far123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110// === Strategy Setup ===defparam preloadbars = 10000defparam cumulateorders = falsex = 1 // Number of contracts to trade — adjust as needed// === Emergency Stop Setup ===capital = 10000 // Starting capital (£10,000)PercentLoss = 5 // Max loss threshold (%)if strategyprofit < 0 theniloss = abs(strategyprofit / capital)if iloss >= PercentLoss / 100 thenquitendifendif// === Trading Hours Filter (UTC) ===// British Summer Time: 15:00 to 20:45 → UTC: 14:00 to 19:45withinTradingHours = time >= 140000 AND time <= 194500// === Timeframe: 1 Hour ===timeframe(1 hour)conditionA = close > close[1]conditionAshort = close < close[1]// === Timeframe: 15 Minutes ===timeframe(15 minutes)conditionB = close > close[1]conditionBshort = close < close[1]// === Timeframe: 5 Minutes ===timeframe(5 minutes)conditionC = close > close[1]conditionCshort = close < close[1]// === Timeframe: 30 Seconds ===timeframe(30 seconds)dema233a = dema[233](close)slope233a = dema233a - dema233a[5]conditionD = close > dema233a AND slope233a > 0conditionDshort = close < dema233a AND slope233a < 0// === Timeframe: 5 Seconds ===timeframe(5 seconds)dema233b = dema[233](close)slope233b = dema233b - dema233b[5]conditionE = close > dema233b AND slope233b > 0conditionEshort = close < dema233b AND slope233b < 0// === 1-Second Chart (Base timeframe) ===timeframe(default)// DEMA calculationsdema21 = dema[21](close)dema55 = dema[55](close)dema89 = dema[89](close)dema144 = dema[144](close)dema233 = dema[233](close)dema377 = dema[377](close)// Slope over 5 candlesslope21 = dema21 - dema21[5]slope55 = dema55 - dema55[5]slope89 = dema89 - dema89[5]slope144 = dema144 - dema144[5]slope233 = dema233 - dema233[5]slope377 = dema377 - dema377[5]// Long preribbon: all slopes up + ascending gradientallSlopesUp = slope21 > 0 AND slope55 > 0 AND slope89 > 0 AND slope144 > 0 AND slope233 > 0 AND slope377 > 0gradCheck1 = slope21 > slope55gradCheck2 = slope55 > slope89gradCheck3 = slope89 > slope144gradCheck4 = slope144 > slope233gradCheck5 = slope233 > slope377gradientOrder = gradCheck1 AND gradCheck2 AND gradCheck3 AND gradCheck4 AND gradCheck5conditionF = allSlopesUp AND gradientOrder// Short preribbon: all slopes down + descending gradientallSlopesDown = slope21 < 0 AND slope55 < 0 AND slope89 < 0 AND slope144 < 0 AND slope233 < 0 AND slope377 < 0gradCheck1s = slope21 < slope55gradCheck2s = slope55 < slope89gradCheck3s = slope89 < slope144gradCheck4s = slope144 < slope233gradCheck5s = slope233 < slope377gradientOrderS = gradCheck1s AND gradCheck2s AND gradCheck3s AND gradCheck4s AND gradCheck5sconditionFshort = allSlopesDown AND gradientOrderS// === Final Entry Logic ===if conditionA AND conditionB AND conditionC AND conditionD AND conditionE AND conditionF AND withinTradingHours AND not onmarket thenbuy x contracts at marketendifif conditionAshort AND conditionBshort AND conditionCshort AND conditionDshort AND conditionEshort AND conditionFshort AND withinTradingHours AND not onmarket thensellshort x contracts at marketendif// === Exit Logic (Layered) ===ExitLongTrade = close < dema377ExitShortTrade = close > dema377if ExitLongTrade AND onmarket thensell at marketendifif ExitShortTrade AND onmarket thenexitshort at marketendif1 user thanked author for this post.
10/17/2025 at 5:55 PM #252707in order to apply the appropriate exit condtion.
You could do below, but I don’t think it will make any difference as a Sell is an Exit of Long only and ExitShort is an Exit of Short only.
if ExitLongTrade AND LONGonmarket thensell at marketendifif ExitShortTrade AND SHORTonmarket thenexitshort at marketendif -
AuthorPosts