This is a simplified and clean version (v2.1) of the trading system:
TEST-KD Mean Reverting version (v1.3).
I have optimized the trailing stop, added a splitPosition (thanks to Roberto Gozzi's code) and added an additional filter for the exit.
//TS KD Mean Reverting v2.1 – Nasdaq 15 min – cfd 1 contract
// spread 2 points
DEFPARAM CUMULATEORDERS = FALSE
PositionSize=1
//——————————————————– SETUP
avgHull=average[150,7] //150-7
volBars=15 //15
vol = Volume
lowBars=10 //10
c1 = close>avgHull // average Hull (filter)
c2 = vol < vol[volBars] // volume
c3 = close > lowest [lowBars] (low) // close – lowest low
If c1 and c2 and c3 and openDayOfWeek <> 5 and tradeAllowed=1 then //TS LONG ONLY ENTRY
Buy PositionSize CONTRACTS AT MARKET
ENDIF
//——————————————————— ADDED EXIT FILTERS
myrsiM5=rsi[14](close)
if myrsiM5<30 and longonmarket and close>positionprice then //RSI Exit
sell at market
endif
if myrsiM5>70 and shortonmarket and close<positionprice then
exitshort at market
endif
//—————————————————————-
maxDailyLoss = 250 // Max Loss Intraday Exit (Euro)
realPosition=positionPerf*positionPrice/pointSize*pointValue
once tradeAllowed = 1
if intradayBarIndex=0 then
myProfit=strategyProfit
tradeAllowed=1
endif
if (strategyProfit+realPosition) <= (myProfit-maxDailyLoss) then
tradeAllowed=0
endif
///——————————————————— MONEY MANAGEMENT
SET STOP pLOSS 130 //SL 100 //SL (stop loss) – TP (take profit)
SET TARGET pPROFIT 190 //TP 175
//———————————————————
pointToReachLong=35*pointSize // 30 //TrP (trailingProfit)
pointToKeepLong=10*pointSize // 12
If not onMarket then
newSL=0
endif
If longOnMarket then
If newSL=0 and high-tradePrice(1)>pointToReachLong then
newSL=tradePrice(1)+ pointToKeepLong
endif
If newSL>0 and close-newSL>pointToReachLong then
newSL = newSL+pointToKeepLong
endif
endif
If newSL>0 then
sell at newSL STOP
endif
//————————————————————- SPLIT winning position
once partialcloseGain = 1
If partialcloseGain then
ONCE PerCent = 0.5 //close 1/2 size
PipsGain = 160 //PositionPrice * PerCentGain / PipSize
ONCE MinLotSize = 0.5
ExitQuantity = abs(CountOfPosition) * PerCent
LeftQty = max(MinLotSize,abs(CountOfPosition) – ExitQuantity)
CloseQuantity = max(0,abs(CountOfPosition) – LeftQty)
TempGain = PositionPerf * PositionPrice / PipSize
IF Not OnMarket THEN
Flag = 1
ENDIF
IF partialcloseGain AND LongOnMarket and TempGain >= PipsGain*pipsize AND Flag THEN
SELL CloseQuantity Contracts AT Market
Flag = 0
endif
IF partialcloseGain AND ShortOnMarket and TempGain >= PipsGain*pipsize AND Flag THEN
exitshort CloseQuantity Contracts AT Market
Flag = 0
endif
endif
//—————————————————————————————————————————————–