Correct basic logic for entries and exits

Forums ProRealTime English forum ProOrder support Correct basic logic for entries and exits

Viewing 12 posts - 1 through 12 (of 12 total)
  • #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?

    My 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.

     

     

    #252685

    ExitLongTrade = 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.
    #252686

    Hi, @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.

    1. Condition A: Price above previous close on 1 hour chart
    2. Condition B: Same again for 15-minute chart
    3. Condition C: Same again for 5-minute chart
    4. Condition D: Price above 233 double moving average with this line pointing upwards over the last 5 bars on 30 second chart.
    5. Condition E: Same again for 5 second chart
    6. 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.
    7. Trading allowed between 3.00pm and 8.30 pm UK time
    8. If all the conditions A-F are met and the time period is correct then enter trade
    9. 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.
    #252687

    To 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.
    #252688

    Are 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.
    #252689

    First thing I spotted is …

    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.
    #252690

    The multitude of conditions means that coincidence does not often occur and so not many trades are generated?

    What Instrument is this Algo intended for anyway?

    #252696

    Try appending these lines to your code, to spot any issue about them:

     

    2 users thanked author for this post.
    #252701

    not 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

    #252704

    It’s intended for the NASDAQ100.

    #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.

     

    1 user thanked author for this post.
    #252707

    in 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 then
    sell at market
    endif
    if ExitShortTrade AND SHORTonmarket then
    exitshort at market
    endif
Viewing 12 posts - 1 through 12 (of 12 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login