Hi all,
another post from me. Thanks for all the help thus far, really appreciate it while I’m learning. Hope to be able to give back in due course.
I am trying to code a trailing stop loss that places stops above recent swing highs (shoreside) and swing lows (longside). I have utilised some code I found that calculates recent fractals. It works ok but there is on key missing ingredient that I am not sure how to code.
If we are following the trend then we should only move the stop up/down when a previous swing point is closed through, at which point the trailing stop would move to the closes swing high or low.
Right now using the below code the trailing stop will simply move to the next identified fractal regardless of whether price has moved in our direction and closed above (uptrend) or below (downtrend) the current swing high/low. I only want the trailing stop to advance when price closes through the current swing high/low in our direction.
Does that make sense? This is what I typically do manually when trend following intraday and works a treat. Would be awesome to automate it. Here’s the trailing stop code I currently have which is adapted from a few other sources.
Any help gratefully received
Roj
// calculate swing points
PivotBAR = 2
LookBack = 4
// Swing low
BarLookBack = PivotBAR + 1
IF low[PivotBAR] < lowest[LookBack](low)[BarLookBack] THEN
IF low[PivotBAR] = lowest[BarLookBack](low) THEN
SupportPrice = low[PivotBAR]
ENDIF
ENDIF
// Swing high
IF high[PivotBAR] > highest[LookBack](high)[BarLookBack] THEN
IF high[PivotBAR] = highest[BarLookBack](high) THEN
ResistancePrice = high[PivotBAR]//high[BarIndex - MyResistance]
ENDIF
ENDIF
// Parameters for determining when BE stop kicks in
TrailingStart = 1*AverageTrueRange[10] * pipsize //trailing will start @trailinstart points profit
TrailingStep = 0 * pipsize
// Longside
IF LONGONMARKET THEN
// move to breakeven
IF newSL = 0 AND CLOSE - TRADEPRICE(1) >= TrailingStart THEN
newSL = TRADEPRICE(1) + TrailingStep
ENDIF
// next moves
IF newSL > 0 AND close > ResistancePrice AND SupportPrice > newSL THEN
newSL = SupportPrice[1]
ENDIF
ENDIF
//Shortside
IF SHORTONMARKET THEN
// move to breakeven
IF newSL = 0 AND TRADEPRICE(1) - CLOSE >= TrailingStart THEN
newSL = TRADEPRICE(1) - TrailingStep
ENDIF
// next moves
IF newSL > 0 AND close < SupportPrice AND ResistancePrice < newSL THEN
newSL = ResistancePrice[1]
ENDIF
ENDIF
//stop order to exit the positions
IF newSL > 0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
Hi,
have you been able to solve your problem? Im looking for exactly the same solution, but I am far less gifted regarding programming than you. Would be very thankful if you could show the complete code, if you have one.
Greetings from the alps!
Line 32 should read:
newSL = max(newSL,SupportPrice[1])
and line 44 should read:
newSL = min(newSL,ResistancePrice[1])
Can I please ask another question which I can’t find the answer to, but I think is simple.
I have programmed a screener and manually select the instruments it suggests. Then I start the trade with a stop below the last swing extreme.
Afterwards the program should kick in to draw the stop automatically.
But an automatic trading system says it needs a buy and sell order. How can I make the algorithm work on an already existing trade?
I think if I try to make an indicator and link the stop-sell (or -buy) order to it, then this will be only pssible intra-day. But my trades normally last longer.
I’d be very thankful for a hint, how to solve this problem.
Perhaps it would work if the last lines are changed from:
IF newSL > 0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
to
IF newSL > 0 THEN
SELLSHORT AT newSL STOP
BUY AT newSL STOP
ENDIF
?
MODERATORS EDIT: Your post has been edited. please use the ‘Insert PRT Code’ button when putting code in your posts.
Your solution would do a Stop & Reverse OR accumulate positions.
Manual and Automatic trading cannot be combined.
You can only alter trades opened automatically, though this will shut your strategy.
Thanks again Roberto,
and it wouldn’t be possible to construct an indicator based on the swing-extreme trailing stop (which would exceed my abilities) and then link this indicator to an alarm and this alarm to a sell trigger?
Indicators can only return numeric values.
Alerts can only be set manually according to what indicators return.
Thanks again, Roberto. But I don’t really understand your sentence: Indicators can only return numeric values.
Is it not the case, that this indicator would return numeric, albeit distinct stop-loss levels?
I’m really thankful for your time and knowledge but I would really love to solve this problem As an amateur I would think that there has to be a way to automate this, since it doesn’t seem to be to complex. Do you please have any idea if I could ask somewhere or if there any other way?
Sorry for bothering you, best regards, Michael
I mean it cannot set nor trigger alerts.
Try what you want to do using the Alert Function with it’s integral Trade Open function.
Try above using a stnadard Indcator … e.g Bollinger using the Close Crosses Over BollDown on the Alert Function.
If above does what you want then you are half way there?
Hello Grahal, thank you! I think your suggestion may be the same I mentioned above (to construct an indicator based on the swing-extreme trailing stop (which would exceed my abilities) and then link this indicator to an alarm and this alarm to a sell trigger)?
If this works, the code has to be changed from trading advice to definition of a indicator and I must say that above my abilities. Could someone of you perhaps help – if this way makes sense?
Another different solution might be to do it completely via an automated trading approach using a simple trade entry advice after the screener suggested a good candidate at bar “[1]”, i.g. for a long trade:
defparam cumulateorder=false
once nbbarlimit=1
if close>close[1] and close< close[1]+0.3*atr[10] then
buy x shares at market
endif
ligand – Please always use the ‘Insert PRT Code’ button when putting code in your posts. I have edited your last post.
- The code line starting with defparam gives an error , but why?
2. Roberto, I thought you gave a hint to another error in the code from Thally besides “max(newSL,SupportPrice[1])”, but I can’t find it anymore. Did you erase this post?
Thanks, Michael