//-------------------------------------------------------------------------
// Main code : TSV 1HL 51/151 trail 1/1/1
// dax 1 euro
// timezone europe, berlin
// timeframe 1h
//-------------------------------------------------------------------------
defparam cumulateorders = false
once position = 1
ONCE p1 = 51
ONCE p2 = 151
ONCE Mtype = 0
p1 = max(1,min(999,p1))
p2 = max(1,min(999,p2))
Mtype = max(0,min(6,Mtype))
MyVol = 0
IF close > close[1] THEN
MyVol = volume * (close - close[1])
ELSIF close < close[1] THEN
MyVol = -volume * (close - close[1])
ENDIF
TSV = summation[p1](MyVol)
Signal = average[p2, MType](TSV)
td = opendayofweek = 1 or opendayofweek = 2 or opendayofweek = 3 or opendayofweek = 4 or opendayofweek = 5
tt = time >= 090000 and time <= 110000 or time >= 160000 and time <= 180000
a = 1
b = 1
c = 1
IF td and tt then
if TSV crosses over Signal and close > exponentialaverage [100] (close) then
buy position contracts at market
set stop %loss a
endif
endif
//%trailing stop function
trailingPercent = b
stepPercent = c
if onmarket then
trailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailingstart points profit
trailingstep = tradeprice(1)*(stepPercent/100) //% step to move the stoploss
endif
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN
newSL = tradeprice(1)+trailingstep
ENDIF
//next moves
IF newSL>0 AND close-newSL>trailingstep THEN
newSL = newSL+trailingstep
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN
newSL = tradeprice(1)-trailingstep
ENDIF
//next moves
IF newSL>0 AND newSL-close>trailingstep THEN
newSL = newSL-trailingstep
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF