defparam cumulateorders=false
// --- settings
balance = 10000 //balance of the strategy when activated the first time
minlot = 1 //minimum lot size for the current instrument (example: 1 for DAX)
riskpercent = 2 //risk percent per trade
atr = averagetruerange(21)
hh = highest[21](high)
ll = lowest[21](low)
// Long
p1=13
p2=26
//p3=63
p4=26
tenkan=(highest[p1](high)+lowest[p1](low))/2
kijun=(highest[p2](high)+lowest[p2](low))/2
SpanA=(tenkan[p4]+kijun[p4])/2
//SpanB=(highest[p3](high[p4])+lowest[p3](low[p4]))/2
//c3=close[0] crosses over spana and close[0] crosses over spanb
//c4=close[0] crosses over kijun and close[0]>open[0]
x=close > kijun
x1=close crosses over spana
x2= kijun > kijun[1]
///short
p1=13
p2=26
//p3=63
p4=26
tenkan=(highest[p1](high)+lowest[p1](low))/2
kijun=(highest[p2](high)+lowest[p2](low))/2
SpanA=(tenkan[p4]+kijun[p4])/2
//SpanB=(highest[p3](high[p4])+lowest[p3](low[p4]))/2
//c5=close[0] crosses under spana and close[0] crosses under spanb
//c6=close[0]crosses under kijun and close[0]<open[0]
y=close < kijun
y1=close[0] crosses under Spana
y2= kijun < kijun[1]
finalcondition1 = x and x1 and x2
finalcondition2 = y and y1 and y2
////
if intradaybarindex=0 then
alreadytraded = 0
case = 0
levelhi = 0
levello = 0
endif
if onmarket or (onmarket[1] and not onmarket) or (currentprofit<>strategyprofit) then
alreadytraded = 1
endif
//case 1 : If price is above the Ichy then only trade long BUT only above the highest high of the past 21 weeks
if finalcondition1 then
case = 1
levelhi = hh[1]
//levelhi = CALL"#floor and ceil"[high,0.10,1]
//levello = CALL"#floor and ceil"[low,0.10,-1]
endif
//case 2 : If price is above the Ichy then only trade short BUT only below the lowest low of the past 21 weeks
if finalcondition2 then
case = 2
levello = ll[1]
endif
///
if alreadytraded = 0 then
//money management
StopLoss = 1*ATR
endif
Risk = riskpercent/100
//calculate contracts
equity = balance + StrategyProfit
maxrisk = round(equity*Risk)
size = max(minlot,abs(round((maxrisk/StopLoss)/PointValue)*pipsize))
//in all cases put pending orders on market
while case <> 0 do
if levelhi>0 then
buy size contract at levelhi stop
endif
if levello>0 then
sellshort size contract at levello stop
endif
wend
//set target and profit
if onmarket then
set target profit 3*ATR
set stop loss StopLoss
endif
currentprofit = strategyprofit
//debugging
//graph case as "case"