Apologies if the format is wrong, this is my first question here.
I have a question about my script that I have been creating - the zones I have saved don't seem to be saving properly and the peak and trough detection doesn't seem to be finding the correct peaks or troughs. Also can't find out for the life of me how to only print one box/label/line rather than a new one every bar.. Here is the script:
//@version=5
strategy("Supply and Demand Zones Revised", shorttitle="Ptm", overlay=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 1, initial_capital = 1000000)
var lookback = 10
type Zone
float startPrice
float endPrice
bool isDemandZone
int barIndex
bool tradePlaced
float lastPeakPrice
float lastTroughPrice
line peakLine
line troughLine
label zoneLabel
var zones = array.new(0)
var activeZones = array.new_bool(0)
high_15m = request.security(syminfo.tickerid, "15", high[barstate.isrealtime ? 1: 0])[barstate.isrealtime ? 0:1]
low_15m = request.security(syminfo.tickerid, "15", low[barstate.isrealtime ? 1: 0])[barstate.isrealtime ? 0:1]
open_15m = request.security(syminfo.tickerid, "15", open[barstate.isrealtime ? 1: 0])[barstate.isrealtime ? 0:1]
close_15m = request.security(syminfo.tickerid, "15", close[barstate.isrealtime ? 1: 0])[barstate.isrealtime ? 0:1]
var float firstBearishCandleHigh = na
var float firstBearishCandleLow = na
var float firstBullishCandleHigh = na
var float firstBullishCandleLow = na
var float highestPriceInZones = na
var float lowestPriceInZones = na
var label zonesSizeLabel = na
largeMoveMinPct = 0.0002 // 10 pips
largeMoveMaxPct = 0.0005 // 100 pips
if close_15m[15] > open_15m[15] and (close_15m[15] - open_15m[15]) > largeMoveMinPct and (close_15m[15] - open_15m[15]) < largeMoveMaxPct
for i = 1 to lookback
if close_15m[i] < open_15m[i]
firstBearishCandleHigh := high_15m[i]
firstBearishCandleLow := low_15m[i]
break
if close_15m[15] < open_15m[15] and (open_15m[15] - close_15m[15]) > largeMoveMinPct and (open_15m[15] - close_15m[15]) < largeMoveMaxPct
for i = 1 to lookback
if close_15m[i] > open_15m[i]
firstBullishCandleHigh := high_15m[i]
firstBullishCandleLow := low_15m[i]
break
if not na(firstBearishCandleHigh) and not na(firstBearishCandleLow)
if array.size(zones) >= 5
array.shift(zones)
array.shift(activeZones)
label newLabel = label.new(x = bar_index, y = firstBearishCandleLow, text = "Start: " + str.tostring(firstBearishCandleLow) + "\nEnd: " + str.tostring(firstBearishCandleHigh) + "\nSupply Zone", style = label.style_label_down, color=color.red)
array.push(zones, Zone.new(firstBearishCandleLow, firstBearishCandleHigh, false, bar_index, false, na, na, na, na, newLabel))
array.push(activeZones, true)
if not na(firstBullishCandleHigh) and not na(firstBullishCandleLow)
if array.size(zones) >= 5
array.shift(zones)
array.shift(activeZones)
label newLabel = label.new(x = bar_index, y = firstBullishCandleLow, text = "Start: " + str.tostring(firstBullishCandleLow) + "\nEnd: " + str.tostring(firstBullishCandleHigh) + "\nDemand Zone", style = label.style_label_down, color=color.green)
array.push(zones, Zone.new(firstBullishCandleLow, firstBullishCandleHigh, true, bar_index, false, na, na, na,na,newLabel))
array.push(activeZones, true)
// Create a new label showing the size of the zones array
zonesSizeLabel := label.new(x = bar_index, y = low, text = "Zones size: " + str.tostring(array.size(zones)), style = label.style_label_up)
label.delete(zonesSizeLabel[1])
// // Variables to store the previous and current peaks and troughs
// var float prevPeak = na
// var float currPeak = na
// var float prevTrough = na
// var float currTrough = na
// // Boolean variables to indicate whether the first trough and peak are in the zone
// var bool firstTroughInZone = na
// var bool firstPeakInZone = na
// // The pip size (0.0001 for most forex pairs, adjust as needed)
// pipSize = 0.0001
// // Your existing peak() and trough() functions here...
// peak() =>
// ta.highest(high, lookback)[1] < high[0] and high[0] > ta.highest(high, lookback)[2]
// trough() =>
// ta.lowest(low, lookback)[1] > low[0] and low[0] < ta.lowest(low, lookback)[2]
// // Check whether a price is within any of the supply zones
// inZone(price) =>
// var bool isInZone = false
// if array.size(zones) > 0
// for i = 0 to array.size(zones) - 1
// zone = array.get(zones, i)
// if price > zone.startPrice and price < zone.endPrice
// isInZone := true
// break
// isInZone
// // If a new peak or trough occurs, update the previous and current values
// if peak()
// prevPeak := currPeak
// currPeak := high
// firstPeakInZone := inZone(high)
// if trough()
// prevTrough := currTrough
// currTrough := low
// firstTroughInZone := inZone(low)
// // If a peak and trough have occurred within the zone and price breaks
// // the first trough and then the first peak, place a limit order
// if not na(prevPeak) and not na(prevTrough) and not na(firstTroughInZone) and not na(firstPeakInZone)
// if firstTroughInZone and firstPeakInZone and low < prevTrough and high > prevPeak
// line.new(x1 = bar_index, y1 = prevTrough + 2 * pipSize, x2 = bar_index + 1, y2 = prevTrough + 2 * pipSize, color = color.green, width = 1)
// // Place a limit order here using the broker's API or Pine Script's strategy functions
// // Now check if price has violated the zone
// i = 0
// while i < array.size(zones)
// zone = array.get(zones, i)
// if zone.isDemandZone and high > zone.endPrice
// array.remove(zones, i)
// else if not zone.isDemandZone and low < zone.startPrice
// array.remove(zones, i)
// else
// i := i + 1
Can this be salvaged or will I have to find a new way to figure my strategy out?
In case it's not clear I want to: find a large push on the 15m chart and mark the first opposite candle before it as a zone. Then when price moves into the zone I want to check for a peak inside of the zone, a trough inside the zone, a break of the first peak, and a break of the first trough. Then I want to place a short trade at 1 pip below the high of the second peak. Reversed for long trades. I hope that makes sense!
Thanks in advance, any help is appreciated
I have tried to manage the zones differently with several arrays to manage boxes or lines,
Tried adding checks to try and manage zones more efficiently but I cannot get the desired effect.
I can't seem to get the zones to appear the same on the 15m and the 1m chart.