My first attempt at coding so bear with me please. This is a buy trigger system I’ve been using to take quick 1:1 profits but I’m struggling in converting the system into Pro Real code for full and broader back testing. This is jus ta buy-leg version with single positions for now. My challenge is that it keep triggering an entry off the incorrect bar. The idea is that the buy conditions establish the signal bar and then all targets and stops are set around that bar but the trigger for the buy should be on any subsequent bar only when he high of the bear is breached. I’m struggling to convert that logic into the code below. Appreciate thoughts and help
DEFPARAM CumulateOrders = False // Only allow one position in the same direction
// Define multipliers for Stop Loss and Take Profit (1:1 reward-risk ratio)
StopLossMultiplier = 1.0
TakeProfitMultiplier = 1.0
// Define the Bullish Reversal Bar conditions for buy signals
BuyCondition1 = ABS(Open – Close) > AverageTrueRange[7]
BuyCondition2 = Close < ExponentialAverage[20]
BuySignalBar = BuyCondition1 AND BuyCondition2
// Calculate the length of the signal bars
BuySignalBarLength = High – Low
// Initialize variables once
ONCE BuySignalBarIndex = 0
ONCE SignalTriggered = 0 // Use 0 and 1 for boolean logic
ONCE StopLoss = 0
ONCE TakeProfit = 0
// Buy Signal
IF BuySignalBar THEN
BuySignalBarIndex = BarIndex
SignalTriggered = 1 // Mark signal as triggered
ENDIF
// Ensure the trigger can occur at any point after the signal bar
IF SignalTriggered = 1 AND BarIndex > BuySignalBarIndex AND NOT LONGONMARKET THEN
IF High >= High[BuySignalBarIndex] THEN
BUY 1 CONTRACT AT High[BuySignalBarIndex] STOP
// Calculate Stop Loss and Take Profit dynamically based on the reversal bar’s high and low
StopLoss = Low[BuySignalBarIndex]
TakeProfit = High[BuySignalBarIndex] + BuySignalBarLength * TakeProfitMultiplier
// Set Stop Loss and Take Profit
SET STOP LOSS StopLoss
SET TARGET PROFIT TakeProfit
SignalTriggered = 0 // Reset the signal trigger
ENDIF
ENDIF
// Close long position at Stop Loss or Take Profit
IF LONGONMARKET THEN
IF Low <= StopLoss THEN
SELL AT MARKET
ENDIF
IF High >= TakeProfit THEN
SELL AT MARKET
ENDIF
ENDIF
// Reset the buy signal if a new one prints and no trade is open
IF BuySignalBar AND BarIndex > BuySignalBarIndex AND NOT LONGONMARKET THEN
BuySignalBarIndex = BarIndex
SignalTriggered = 1 // Mark new signal as triggered
ENDIF