This ProOrder code (strategy) implements a trend tracking system based on a crossing of the price with Bollinger-type bands and a dynamic trailing stop. It is designed to work on the CAC 40 (Spread) in a 3-minute timeframe, with hourly/daily filtering rules and risk management via stop loss and target.
The logic calculates an envelope around the average (implicit simple moving average via average) using the standard deviation (std):
A trend state (trend) and a trailing stop level (ts) are initialized on the first signal:
The trailing stop is then updated to follow the trend (max in uptrend, min in downtrend). A reversal is detected when the price crosses the trailing stop. The code also contains a time filter (stopv) to avoid certain time slots/days.
Important: in the extract provided, the variable sale is initialized to 0 and is never set to 1, while only purchase is set to 1 during a reversal (trend=1 then crossing below ts). In addition, the order executed is a SELLSHORT conditioned by sale=1. As it stands, the strategy may therefore not trigger any trades. This documentation describes the calculation as written, but it is advisable to check/complete the entry conditions.
REM CAC 40 SPREAD 1
DEFPARAM cumulateOrders = FALSE
defparam preloadbars= 10000
timeframe (3 MINUTE)
if time<090000 or time>173000 then
journee=0
else
journee=1
endif
SignalPeriod = 28
ArrowPeriod = 2
achat=0
vente=0
bbup = average[signalperiod]+std[signalperiod]*arrowperiod
bbdn = average[signalperiod]-std[signalperiod]*arrowperiod
if ts=0 then
if close crosses over bbup then
ts=bbdn
trend=1
elsif close crosses under bbdn then
ts=bbup
trend=-1
endif
endif
if trend=1 then
ts=max(ts,bbdn)
elsif trend=-1 then
ts=min(ts,bbup)
endif
if trend=1 and close crosses under ts then
trend=-1
ts=bbup
r=255
g=0
achat=1
endif
if ((dayofweek=3 or dayofweek=5) and time<093500) or (dayofweek=5 and time>105000 and time<120000)or (dayofweek=1 and time>150000 and time<153000) then
stopa=0
else
stopa=1
endif
if journee=1 and achat=1 and stopa=1 then
buy 1 contract at market
endif
if trend=-1 and close crosses over ts then
trend=1
ts=bbdn
r=0
g=255
vente=1
endif
if (time>130100 and time<143000) or (time>105500 and time<120000) or (time>145500 and time<151500) or (dayofweek=5 and time<090500) then
stopv=0
else
stopv=1
endif
if journee=1 and vente=1 and stopv=1 then
sellshort at market
endif
set target profit 9
set stop loss 70