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
Sorry, I can’t understand what you want to change or add to your code.
Well I keep running it through a back-test on daily charts for various currency pairs over 10 years of data and while it doesn’t show any errors it doesn’t seem to work according to the logic. I think I’m missing something basic here but not sre
JSParticipant
Senior
Hi,
When you use “Set Stop Loss” you are talking about a certain number of POINTS below the purchase price…
You do not use points for the Stop Loss, but a certain price level, namely:
StopLoss=Low[BuySignalBarIndex]
Instead of “Set Stop Loss” you should use the following for a price level:
Set Stop Price StopLoss
For your “TakeProfit” you use: TakeProfit=High[BuySignalBarIndex]+BuySignalBarLength*TakeProfitMultiplier
High[BuySignalBarIndex is another price level (no points) but BuySignalBarLength is again points, so you can’t add these two together…
So, your “TakeProfit” could look like this:
TakeProfit=High[BuySignalBarIndex]*TakeProfitMultiplier
Again, you should use the following for your take profit:
Set Target Price TakeProfit