PaulParticipant
Master
I made some changes, again. Goal is use of percentage, splitted long/short and working on different markets. That combo is tricky.
By running the main code and testing with 1m bars is not working so good as some trades run super long back in time.
Hello Roberto,
I am trying to understand your great code. I have the following 2 questions:
- SellPriceX = max(max(SellPriceX,SellPrice),q1) What is the meaning of SellPriceX?
- ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) Should the ‘100’ not be ‘1’?
Best regards,
Dré
Hello Roberto,
Thank you for your quick response. I understand the code now better.
I have the following remarks:
1. r1 = ((PositionPrice – SrcL) * ProfitPerCent) I think it should be: r1 = PositionPrice – ((PositionPrice – SrcL) * ProfitPerCent)
2. ExitPriceX should be inialized to 9999999 (ExitPriceX = 9999999) otherwise the ExitPrice will always be zero (0).
Best regards,
Dré
Well spotted DreaC, you are right for both glitches.
R1 must mirror Q1 (in the opposite direction) and ExitPriceX must be like ExitPrice.
Thank you for sharing with us.
PaulParticipant
Master
Hi Roberto,
Is there a way to set basepercent also to 0, with the result when the trailingstop kicks in and there is no progress followed by retracement, that the entryprice is the exitprice, so in the statistics it’s not a win?
You should try setting a STEP of 9999.
Well spotted DreaC, you are right for both glitches.
R1 must mirror Q1 (in the opposite direction) and ExitPriceX must be like ExitPrice.
Thank you for sharing with us.
Could you please post the correct code that should be in those lines?
There you go:
//------------------------------------------------------------------------------------------------------------------------------------------------
ONCE UseCLOSE = 1 //1=use CLOSE, 0=use High/Low
srcH = close //defaults to CLOSE
srcL = close //defaults to CLOSE
IF UseCLOSE = 0 THEN
srcH = high
srcL = low
ENDIF
DirectionSwitch = (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket) //True when there's been a change in the direction (likely to be due to a Stop & Reverse)
IF Not OnMarket OR DirectionSwitch THEN
//
// when NOT OnMarket or thare's been a change in direction, reset values to their default settings
//
TrailStart = 30 //30 Start trailing profits from this point
BasePerCent = 0.200 //20.0% Profit percentage to keep when setting BerakEven
StepSize = 10 //10 Pip chunks to increase Percentage
PerCentInc = 0.100 //10.0% PerCent increment after each StepSize chunk
RoundTO = -0.5 //-0.5 rounds always to Lower integer, +0.4 rounds always to Higher integer, 0 defaults PRT behaviour
PriceDistance = 7 * pipsize //7 minimun distance from current price
y1 = 0 //reset to 0
y2 = 0 //reset to 0
ProfitPerCent = BasePerCent //reset to desired default value
//PositionCount = 0
SellPrice = 0
SellPriceX = 0
ExitPrice = 9999999
ExitPriceX = 9999999
ELSE
//------------------------------------------------------
// --- Update Stop Loss after accumulating new positions
//------------------------------------------------------
//PositionCount = max(PositionCount,abs(CountOfPosition))
//
// update Stop Loss only when PositionPrice has changed (actually when increased, we don't move it if there's been some positions exited)
//
//IF PositionCount <> PositionCount[1] AND (ExitPrice + SellPrice)<>9999999 THEN //go on only if Trailing Stop had already started trailing
IF PositionPrice <> PositionPrice[1] AND (ExitPrice + SellPrice) <> 9999999 THEN //go on only if Trailing Stop had already started trailing
IF LongOnMarket THEN
q1 = PositionPrice + ((srcH - PositionPrice) * ProfitPerCent) //calculate new SL
SellPriceX = max(max(SellPriceX,SellPrice),q1)
SellPrice = max(max(SellPriceX,SellPrice),PositionPrice + (y1 * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous one
ELSIF ShortOnMarket THEN
r1 = PositionPrice - ((PositionPrice - srcL) * ProfitPerCent) //calculate new SL
ExitPriceX = min(min(ExitPriceX,ExitPrice),r1)
ExitPrice = min(min(ExitPriceX,ExitPrice),PositionPrice - (y2 * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous one
ENDIF
ENDIF
// --- Update END
ENDIF
//
IF LongOnMarket AND srcH > (PositionPrice + (y1 * pipsize)) THEN //LONG positions
//
// compute the value of the Percentage of profits, if any, to lock in for LONG trades
//
x1 = (srcH - PositionPrice) / pipsize //convert price to pips
IF x1 >= TrailStart THEN // go ahead only if N+ pips
Diff1 = abs(TrailStart - x1) //difference from current profit and TrailStart
Chunks1 = max(0,round((Diff1 / StepSize) + RoundTO)) //number of STEPSIZE chunks
ProfitPerCent = BasePerCent + (BasePerCent * (Chunks1 * PerCentInc)) //compute new size of ProfitPerCent
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%
y1 = max(x1 * ProfitPerCent, y1) //y1 = % of max profit
ENDIF
ELSIF ShortOnMarket AND srcL < (PositionPrice - (y2 * pipsize)) THEN //SHORT positions
//
// compute the value of the Percentage of profits, if any, to lock in for SHORT trades
//
x2 = (PositionPrice - srcL) / pipsize //convert price to pips
IF x2 >= TrailStart THEN // go ahead only if N+ pips
Diff2 = abs(TrailStart - x2) //difference from current profit and TrailStart
Chunks2 = max(0,round((Diff2 / StepSize) + RoundTO)) //number of STEPSIZE chunks
ProfitPerCent = BasePerCent + (BasePerCent * (Chunks2 * PerCentInc)) //compute new size of ProfitPerCent
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%
y2 = max(x2 * ProfitPerCent, y2) //y2 = % of max profit
ENDIF
ENDIF
//------------------------------------------------------------------------------
// manage actual Exit, if needed
//------------------------------------------------------------------------------
IF y1 THEN //Place pending STOP order when y1 > 0 (LONG positions)
SellPrice = max(SellPrice,PositionPrice + (y1 * pipsize)) //convert pips to price
//
// check the minimun distance between ExitPrice and current price
//
IF abs(close - SellPrice) > PriceDistance THEN
//
// place either a LIMIT or STOP pending order according to current price positioning
//
IF close >= SellPrice THEN
SELL AT SellPrice STOP
ELSE
SELL AT SellPrice LIMIT
ENDIF
ELSE
//
//sell AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price
//
SELL AT Market
ENDIF
ENDIF
IF y2 THEN //Place pending STOP order when y2 > 0 (SHORT positions)
ExitPrice = min(ExitPrice,PositionPrice - (y2 * pipsize)) //convert pips to price
//
// check the minimun distance between ExitPrice and current price
//
IF abs(close - ExitPrice) > PriceDistance THEN
//
// place either a LIMIT or STOP pending order according to current price positioning
//
IF close <= ExitPrice THEN
EXITSHORT AT ExitPrice STOP
ELSE
EXITSHORT AT ExitPrice LIMIT
ENDIF
ELSE
//
//ExitShort AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price
//
EXITSHORT AT Market
ENDIF
ENDIF
Here’s a very minor mod, if you want to use % instead of points
ts = (tradeprice*pc)/100 // % trailing start
TrailStart = ts // Start trailing profits from this point
Thanks again Roberto for all your work on this!
I would have written
ts = (close*pc)/100 // % trailing start
because you may be out of the market at that time
Hmm … I’d say that the trailing stop has an extremely low chance of starting if you’re not onmarket. 😁
But it does make me think that perhaps it should be positionprice, to allow for cumulative orders ??
I personally struggle to fully understand Roberto’s code 🙂
But from what I read the trailing stop is set up when you are not on market or when there is a direction switch. By the way I dont really understand why, but there must be a logic behind this!