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