Hi all,
Hi, I would like to ask if it is possible to have your algo with 2 TP target. Apologies if this questions have been raised. I tried to look up past topics but the topic was a while ago and the answer was the brokers cannot support partial TP at that time. If there is a relevant topic on this already appreciate if you can provide the link to me. If not, I hope you will find this qns interesting:
Simple algo is to buy 2 shares when the price cross over the lower bollinger band, and to sell 1 share when the price higher than the middle of the bollinger, and to sell another 1 share when the price is higher than the top of the bollinger.
Basically:
// Conditions to enter long positions
BBDown= BollingerDown[55](close)
c1 = (close CROSSES OVER BBDown)
IF c1 THEN
BUY 2 SHARES AT MARKET
ENDIF
// Conditions to exit long positions
TP1 = Average[55](close)
c2 = (close >= TP1)
TP2 = BollingerUp[55](close)
c3 = (close >= TP2)
IF c2 THEN
SELL 1 AT MARKET
ENDIF
IF c3 THEN
SELL 1 AT MARKET
ENDIF
The code SELL 1 AT MARKET gives an error. Can anyone advise how I should code this?
Thank you and have a great day ahead.
You’ve been returned an error because your syntax is missing the keyword CONTRACT, after 1 for the first TP.
For the second TP, I suggest that just remove 1 (instead of adding CONTRACT), as the second TP has to close ALL positions (even if only 1 is left).
Thank you Roberto, as always. In the code below, the long triggered to by 2 when the price cross over the lower bollinger band (BB) of 2 standard deviation (STD), and sell 1 to TP when the price is above the BB up of 1 STD, and sell another 1 when the price is above BB up of 2 STD. In the picture, the BB 1 std is white dotted lines and BB 2 std is magenta dotted lines. I modified the codes based on your suggestion. The result now able to sell 1 at TP1 and the remaining at TP2. However the TP2 level seems to be wrong. Can you pls help? Tks. I have attached the screenshot. Its EURUSD 10 mins chart. Tks
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = BollingerDown[55](close)
c1 = (close CROSSES OVER indicator1)
IF c1 THEN
BUY 2 SHARES AT MARKET
ENDIF
// Conditions to exit long positions
BB1STD = Average[55](close)+std[55](close)
c2 = (close >= BB1STD)
BB2STD = BollingerUp[55](close)
c3 = (close >= BB2STD)
IF c2 THEN
SELL 1 CONTRACT AT MARKET
ENDIF
IF c3 THEN
SELL AT MARKET
ENDIF
The error is in line 13.
STD is not needed for the middle line, it’s just an average:
BB1STD = Average[55](close)
Hi Roberto,
Thanks for the fast response. I change the code already. But the TP2 still not executed correctly. The TP2 shud be executed at the top of the BBBand (magenta dotted line). See the attached screen shot. Appreciate if you can help. Sorry to trouble you again.
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = BollingerDown[55](close)
c1 = (close CROSSES OVER indicator1)
IF c1 THEN
BUY 2 SHARES AT MARKET
ENDIF
// Conditions to exit long positions
BB1STD = Average[55](close)
c2 = (close >= BB1STD)
BB2STD = BollingerUp[55](close)
c3 = (close >= BB2STD)
IF c2 THEN
SELL 1 CONTRACT AT MARKET
ENDIF
IF c3 THEN
SELL AT MARKET
ENDIF
Hi Roberto et al, I think I know what the problem is. So i long 2 shares. As you can see, I have defined 2 TP conditions. So say the less aggressive TP1 target is reached first, and we sell 1 contract at TP1 and have a remaining 1 share left. Then in the next consecutive candles the program will always read the condition for TP1, and thus TP2 will not be reached as the remaining 1 share will always get hit on the TP1 condition. How do i change my code so that for my 2 shares that I buy, 1 will get executed on TP1, and the other on TP2?
Tks
At line 3 add
If Not OnMarket then
Flag = 0
Endif
Add this line between lines 9 and 10
Flag = 1
Lines 19-21 should be replaced by
IF c2 and Flag THEN
SELL 1 CONTRACT AT MARKET
Flag = 0
ENDIF
Hi Roberto, thanks lots your suggestion works wonder. But just as I thought things are as simple as they look, they are not. Apparently there is also LongOnMarket and ShortOnMarket! So if I have both BUY and SELLSHORT in the same strategy, I have to use both LongOnMarket and ShortOnMarket as shown in the code below. Is my understanding correct? Tks in advance.
If Not LongOnMarket then
FlagLong = 0
Endif
If Not ShortOnMarket then
FlagShort = 0
Endif