Need help in coding simple multiple stops. Cannot figure out what’s wrong
Forums › ProRealTime English forum › ProOrder support › Need help in coding simple multiple stops. Cannot figure out what’s wrong
- This topic has 12 replies, 5 voices, and was last updated 3 years ago by robertogozzi.
-
-
08/16/2021 at 8:50 AM #175429
Hi all,
I tried to write a simple and straight forward code, but somehow it does not work as expected and look very off. Its relatively simple code, and I cannot quite figure out which part is the mistake. Can anyone pls help with some advise and put me out of my misery? Thanks.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758//// Long only strategy to buy 10 contracts when price crosses over SMA(55),// which is the middle of Billinger Band// and stop loss set to the swing low//// Sell 5 when price cross the upper bollinger band of 1 SD (BBUp1SD)// and stop loss set to the swing low//// Sell 3 when price cross the upper bollinger band of 2 SD (BBUp2SD)// and stop loss set to the middle of bollingerband SMA(55)//// and sell 2 when price cross the upper bollinger band of 3 SD (BBUp3SD)// and stop loss set to BBUp1SD)//DEFPARAM CumulateOrders = False//Define parametersSMA55 = average[55](close)BBUp1SD = Average[55](close) + (1*std[55](close))BBUp2SD = Average[55](close) + (2*std[55](close))BBUp3SD = Average[55](close) + (3*std[55](close))//Entry ConditionsLongCondition = close > SMA55// Take Profit conditionsTP1Condition = close Crosses over BBUp1SDTP2Condition = close > BBUp2SDTP3Condition = close > BBUp3SD// Stop Loss DefinitionSwingLow = lowest[10](low[1])If LongCondition and Not LongOnMarket Thenbuy 10 Contracts at marketFlag1 = 0Flag2 = 0Set Stop Loss SwingLowEndIfIf LongOnMarket AND TP1Condition AND Flag1 and Flag2 ThenSell 5 Contract At MarketFlag1 = 1Set Stop Loss SMA55EndIFIf LongOnMarket AND TP2Condition AND Flag2 AND NOT Flag1 ThenSell 3 Contract At MarketFlag2 = 1Set Stop Loss BBUP1SDEndIfIf LongOnMarket AND TP3Condition AND NOT FLAG1 AND NOT Flag2 thensell 2 Contracts At marketendif08/16/2021 at 9:20 AM #175435Hello phanz – What I would do make If/Else situations of all the Buy’s and Sell’s, so that you can automatically be sure that nothing happens twice (unless you explicitly want it to happen twice, of course).
Something else I see is the several StopLoss commands occurring. Maybe some insider can tell whether this is allowed. Thus in your case you might change a StopLoss (value) at each bar because of e.g. the SMA changes each bar. I envision that some broker may not be happy with that (because these commands go to the broker for real (when live of course) and for that reason PRT may not allow it. For example, possibly it sets the StopLoss only once after the order is filled.
1 user thanked author for this post.
08/16/2021 at 12:39 PM #175462Hi PeterSt,
Thank you for your suggestion. The Elsif function defnitely help. At least the entry and take profit code works as expected.
i purposely set the TP3 target to be unreachable to check if the Stop Loss order works. Unfortunately the SL order does not work. More investigations needed. If anyone can give a helping hand will be greatly appreciated. Thank you.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556//// Long only strategy to buy 10 contracts when price crosses over SMA(55),// which is the middle of Bollinger Band// and stop loss set to the swing low//// Sell 5 when price cross the upper bollinger band of 1 SD (BBUp1SD)// and stop loss set to the swing low//// Sell 3 when price cross the upper bollinger band of 2 SD (BBUp2SD)// and stop loss set to the middle of bollingerband SMA(55)//// and sell 2 when price cross the upper bollinger band of 3 SD (BBUp3SD)// and stop loss set to BBUp1SD)//DEFPARAM CumulateOrders = False//Define parametersSMA55 = average[55](close)BBUp1SD = Average[55](close) + (1*std[55](close))BBUp2SD = Average[55](close) + (2*std[55](close))BBUp3SD = Average[55](close) + (3*std[55](close))//Entry ConditionsLongCondition = close > SMA55// Take Profit conditionsTP1Condition = close Crosses over BBUp1SDTP2Condition = close > BBUp2SDTP3Condition = close > BBUp2SD// Stop Loss DefinitionSwingLow = lowest[10](low[1])If LongCondition and Not LongOnMarket Thenbuy 10 Contracts at marketFlag1 = 0Flag2 = 0Set Stop Loss SwingLowElsIf LongOnMarket AND TP1Condition AND Flag1=0 and Flag2=0 ThenSell 5 Contract At MarketFlag1 = 1Set Stop Loss SMA55ElsIf LongOnMarket AND TP2Condition AND Flag1=1 AND Flag2=0 ThenSell 3 Contract At MarketFlag2 = 1Set Stop Loss BBUP1SDElsIf LongOnMarket AND TP3Condition AND NOT FLAG1=1 AND Flag2=2 thensell 2 Contracts At marketendif08/16/2021 at 12:49 PM #17546308/16/2021 at 1:59 PM #175472the SL order does not work
Which one does not work … you have several in the code?
SL within ElsIf would only work if the conditions within respective ElsIf are met.
Just a few thoughts.
1 user thanked author for this post.
08/16/2021 at 2:34 PM #175475Hi,
Line 43 we see the condition for the system to sell 5 contracts at TP1.
Line 48 we see the condition for the system to sell 3 contracts at TP2. So after the sale of the 3 contracts, we have balance of 2 contracts left (10-5-3=2).
Pls see the attached screenshot.
And also in Line 51 we Set Stop Loss BBUp1SD. So from the screenshot, the balance 2 contracts should have been taken out at point X by the virtue of the Set Stop Loss BBUp1SD. Since TP2 is executed, the Set Stop Loss BBUp1SD shud be read by the algo right?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556//// Long only strategy to buy 10 contracts when price crosses over SMA(55),// which is the middle of Billinger Band// and stop loss set to the swing low//// Sell 5 when price cross the upper bollinger band of 1 SD (BBUp1SD)// and stop loss set to the swing low//// Sell 3 when price cross the upper bollinger band of 2 SD (BBUp2SD)// and stop loss set to the middle of bollinger band SMA(55)//// and sell 2 when price cross the upper bollinger band of 3 SD (BBUp3SD)// and stop loss set to BBUp1SD)//DEFPARAM CumulateOrders = False//Define parametersSMA55 = average[55](close)BBUp1SD = Average[55](close) + (1*std[55](close))BBUp2SD = Average[55](close) + (2*std[55](close))BBUp3SD = Average[55](close) + (3*std[55](close))//Entry ConditionsLongCondition = close > SMA55// Take Profit conditionsTP1Condition = close Crosses over BBUp1SDTP2Condition = close > BBUp2SDTP3Condition = close > BBUp3SD// Stop Loss DefinitionSwingLow = lowest[10](low[1])If LongCondition and Not LongOnMarket Thenbuy 10 Contracts at marketFlag1 = 0Flag2 = 0Set Stop Loss SwingLowElsIf LongOnMarket AND TP1Condition AND Flag1=0 and Flag2=0 ThenSell 5 Contract At MarketFlag1 = 1Set Stop Loss SMA55ElsIf LongOnMarket AND TP2Condition AND Flag1=1 AND Flag2=0 ThenSell 3 Contract At MarketFlag2 = 1Set Stop Loss BBUP1SDElsIf LongOnMarket AND TP3Condition AND FLAG1=1 AND Flag2=1 thensell 2 Contracts At marketendif08/16/2021 at 8:29 PM #17549208/16/2021 at 9:32 PM #17549408/19/2021 at 9:29 AM #175678You are using SET STOP LOSS incorrectly, as it doesn’t require a price (CLOSE, SMA55, HIGH, BollingerUP, etc…) but a price difference such as RANGE, (close – low[1]), (close – BollingerUP), (SMA55 – close), AverageTrueRange, etc…).
SET STOP pLOSS is the sane as above but it requires that the difference be expressed in pips, usually PIPSIZE, such as (Sma55 – close ) * PipSize.
The same rules apply to SET TARGET PROFIT.
1 user thanked author for this post.
08/19/2021 at 6:25 PM #175722Hi Roberto,
Thank you for taking your time to reply and correcting my mistakes. I have few qns about the nature of multiple stops.
Looking at the code below, when the code open a long trade, it sends the StopLoss (SL) order as SL1. Then after the first partial profit taking, it sends the SL orders as SL2. My question is, is the SL1 order cancelled and a new SL oda SL2 is sent? Likewise after 2nd profit taking, the algo set SL as SL3. So at any point in time, there is only one SL order, which in this case the latest one SL3 right (no more SL1 and SL2 oda). Is my understanding correct?
Also “Set Stop Loss 0 ” before exiting the endif ?
Thanks!
123456789101112131415161718192021If LongCondition and Not LongOnMarket Thenbuy 10 Contracts at marketFlag1 = 0Flag2 = 0Set Stop Loss SL1ElsIf LongOnMarket AND TP1Condition AND Flag1=0 and Flag2=0 ThenSell 5 Contract At MarketFlag1 = 1Set Stop Loss SL2ElsIf LongOnMarket AND TP2Condition AND Flag1=1 AND Flag2=0 ThenSell 3 Contract At MarketFlag2 = 1Set Stop Loss SL3ElsIf LongOnMarket AND TP3Condition AND FLAG1=1 AND Flag2=1 thensell 2 Contracts At marketSet Stop Loss 0endif08/19/2021 at 7:34 PM #17572708/20/2021 at 8:46 AM #17575408/20/2021 at 9:57 AM #175761I never tried setting different SLs that way, as I use pending STOP orders, but they seem correct. If they work, then they are! 🙂
1Set Stop Loss 0simply removes any active SL.
1 user thanked author for this post.
-
AuthorPosts