KMParticipant
New
Hi, can somebody help me with my strategy. I need some modification to optimize it. The change that I need is to make it open a position only if it crosses the middle bollinger line before hand. Example: If I have an open long position and it hits target profit then it only opens a new long position if it crosses the middle bollinger line again, if the price still above the middle bollinger then it should stay inactive.
Sorry if my English is bad.
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = ExponentialAverage[50](close)
c1 = (close > indicator1)
indicator2 = ExponentialAverage[50](close)+1.5*std[50](close)
c2 = (high[7] >= indicator2)
IF c1 AND c2 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
// Conditions to exit long positions
indicator3 = ExponentialAverage[50](close)-1.5*std[50](close)
c3 = (low <= indicator3)
IF c3 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator4 = ExponentialAverage[50](close)
c4 = (close < indicator4)
indicator5 = ExponentialAverage[50](close)-1.5*std[50](close)
c5 = (low[7] <= indicator5)
IF c4 AND c5 THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
// Conditions to exit short positions
indicator6 = ExponentialAverage[50](close)+1.5*std[50](close)
c6 = (high >= indicator6)
IF c6 THEN
EXITSHORT AT MARKET
ENDIF
// Stops and targets
set target %profit 0.4
Hello, in your conditions variables ‘condition1’ and ‘condition4’ , you should test a crossover with the EMA instead of only testing if close is above or under it. I’ll post codes if you don’t find how to get it.
Here is the short tricks to made it works like you want it :
For Long position :
c1 = (close crosses over indicator1)
For Short position :
c4 = (close crosses under indicator4)

KMParticipant
New
Thanks for the reply, but that isn’t what I’m looking for. If I make those changes, then it will trigger an Entry only if it crosses the middle bollinger line and the second conditions kicks in. I need it to cross beforehand. So it will be like this:
Conditions:
- The price is over middle bollinger band
- Seven days before the high has to be over upper bollinger band
Enter Long.
- If previous position hits target/stop then enter only if the prise moves under the middle bollinger band, then up and condition 1 and 2 kicks in then go long again.
So your ‘c1’ condition need only to be tested once? At ProOrder strategy startup? If so, all you have to do is to “flag” your first trade with a variable and then make 2 different ‘c1’ conditions :
- first ‘c1’ : tradedbefore=0 so test only if “Close>EMA50”
- second ‘c1’ : tradedbefore=1 so test a crossover
KMParticipant
New
Thank you Nicolas, I’ll try it out.
KMParticipant
New
Sorry Nicolas, I can’t make it work. Can you please write the code for me?
Here is the final code, I have flagged the first trade with the “tradedbefore” variable set to 1 if a first trade has occurred. If then, the next trade would be taken only if the price crosses above or below the middle bollinger band (a moving average 50 periods in this case).
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
DEFPARAM preloadbars = 0
once tradedbefore=0
GRAPH tradedbefore as "tradedbefore"
// Conditions to enter long positions
indicator1 = ExponentialAverage[50](close)
if tradedbefore=0 then
c1 = (close > indicator1)
elsif tradedbefore=1 then
c1 = close crosses over indicator1
endif
indicator2 = ExponentialAverage[50](close)+1.5*std[50](close)
c2 = (high[7] >= indicator2)
IF c1 AND c2 THEN
BUY 1 CONTRACT AT MARKET
tradedbefore = 1
ENDIF
// Conditions to exit long positions
indicator3 = ExponentialAverage[50](close)-1.5*std[50](close)
c3 = (low <= indicator3)
IF c3 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator4 = ExponentialAverage[50](close)
if tradedbefore=0 then
c4 = (close < indicator4)
elsif tradedbefore = 1 then
c4 = close crosses under indicator4
endif
indicator5 = ExponentialAverage[50](close)-1.5*std[50](close)
c5 = (low[7] <= indicator5)
IF c4 AND c5 THEN
SELLSHORT 1 CONTRACT AT MARKET
tradedbefore = 1
ENDIF
// Conditions to exit short positions
indicator6 = ExponentialAverage[50](close)+1.5*std[50](close)
c6 = (high >= indicator6)
IF c6 THEN
EXITSHORT AT MARKET
ENDIF
// Stops and targets
set target %profit 0.4
You are welcome, hope it will help you.