Widen stoploss overnight
Forums › ProRealTime English forum › ProOrder support › Widen stoploss overnight
- This topic has 20 replies, 4 voices, and was last updated 5 years ago by GraHal.
-
-
08/09/2018 at 11:46 AM #77849
Hello, I am trying to code a stoploss that widens when the DAX futures market closes. I have set the chart of the DAX CFD from IG to custom trading hours 08:00:00 – 22:00:00 (because this is the data I want to use for indicators). I tried it with this code:
1234567if longonmarket and time >= 080000 and time < 215500 thensell at SLtight stopelseif longonmarket thensell at SLwide stopendifendifAs can be seen in the picture in the attachment (this is activity from the demo account), the code doesn’t do what it’s supposed to do. It widens the stoploss at 21:55 and at 22:00 it takes the stoploss out entirely. Than at 01:00 it puts back the wide stop and 08:05 it goes back to the tight stop. Ideal would be stoploss wider at 22:00 and stoploss back to tight at 08:00, when the DAX futures open. Can it be done?
08/09/2018 at 12:01 PM #77851Why not use a SET order to place the orders just once and then modify them at the desired time?
12345678910SLTight = 50SLWide = 100if onmarket and time = 080000 thenset stop ploss SLTightendifif onmarket and time = 220000 thenset stop ploss SLWideendifIf you have a limited risk account then your variable stop distance will not work because you cannot move a stop further away on a limited risk account.
08/09/2018 at 12:33 PM #77854Thank you for your reply Vonasi, The reason that I don’t use the ploss function is because I use an indicator based trailing stop. I tried coding with the ploss function in the past, but I believe the reason I didn’t use it is that it set faulty stops when there was a gap between the open and close of two candles.
08/09/2018 at 12:45 PM #77857If you calculate the number of pips in the stop distance with POSITIONPRICE – (your indicators stop price) then it would be set at the correct level.
It would not be possible to calculate this for one bar so you could use a STOP order for the first bar and then use the SET instruction afterwards just to widen and narrow the stop distance twice a day.
08/09/2018 at 12:57 PM #7786108/09/2018 at 1:03 PM #77862Yes – no reason why not.
POSITIONPRICE is the average of all open positions. TRADEPRICE is the price of the last position opened. You need to be aware that if you have multiple positions open then when using the SET instruction each one will have a separate stop order set at whatever distance you calculate. With the pending STOP order you were first using then just one level is set and all positions closed at this price.
08/09/2018 at 1:11 PM #7786408/09/2018 at 2:36 PM #77869To make my question more concrete, how can you achieve the trailing stoploss of the following code with the SET STOP PLOSS function? (to be able to widen the stop at 22:00)
Code from https://www.prorealcode.com/blog/learning/moving-stoploss-dynamic-informations-proorder/12345678910111213141516171819202122232425defparam preloadbars = 150defparam cumulateorders = falsemm = average[100](close)ATRsl = mm - highest[50](averagetruerange[100](close)*2)condition = close crosses over mmif condition thenbuy 1 lot at market//first stoploss:SL = close-ATRslset stop ploss SLdynamicSL = SLendif//dynamicSLif longonmarket thenif(ATRsl>dynamicSL) thendynamicSL=ATRslendifsell at dynamicSL stopendifGRAPH dynamicSL as "dyn"08/09/2018 at 3:02 PM #77873Sorry but I’m a bit too busy at the moment to look into your code with too much detail but I think it is as I explained before. Every time your buy condition is met then the stop loss levels for all of the already open positions will be altered to the newly calculated level from their opening prices – so they will all have different levels of stops. For your example due to the scaling in SET is not the right instruction to use.
You will need to go back to pending stop orders and work out why they are not working for you.
08/10/2018 at 9:38 AM #77934Alright, that’s a shame. The documentation quotes “Set a stop loss x units from the average position price.” for the SET STOP LOSS function. This is a bit confusing than, since “average” position price suggests the position consists of multiple prices (a scaled-in position).
08/10/2018 at 9:51 AM #77935Your line 12-13
12SL = close-ATRslset stop ploss SLworks with DAX, but not with FX pairs, since PLOSS requires PIPS, while SL is a difference in price. You should either use, in line 13:
1set stop ploss SL/pipsizeor use LOSS, instead:
1set stop loss SLMoreover, line 22 is trying to exit a LONG position SELLing at a difference in price (say 0085), instead of a price (say 12510). Pending orders require a PRICE to enter/exit at some point.
08/10/2018 at 10:21 AM #77943Thank you Roberto, I’m testing with DAX, so the FX issue is not causing any problems. For stoplosses I’ve up ’till now used the technique from the code I posted yesterday, which I got from a PRC blog post (link is above the code). (i.e.:)
123if longonmarket thensell at SL stopendifThis works fine for indicator based trailing stops (that only move your way) in combination with scaling in. I just define at what price level SL has to be and than the whole position closes at that level if price gets there.
From Vonasi’s posts I believe that with the SET STOP function indicator based trailing stops can be achieved, but not if you scale-in. I’m testing it in my demo account now, but this takes a while.
08/10/2018 at 10:45 AM #77948My logic is that if you want to make an indicator based trailing stop with the SET STOP function, (Positionprice – trailing indicator level) becomes a negative amount if the trailing indicator gets beyond your entry price (for a long). You are using a negative number for SET STOP than. Because you are not taking a loss but a profit at the trailing level. Correct?
08/10/2018 at 11:11 AM #77951That’s correct. You can use ABS() to make sure you have a positive result.
08/10/2018 at 11:15 AM #77952 -
AuthorPosts