Hello. Say I want to go long when a>b AND c>d. When both conditions are true I want only ONE entry which stops when take profit or stoploss is hit (or condition changes).
Problem is: when profit or loss is hit the system goes long AGAIN on the next candle since still a>b AND c>d. I have been suggested to stop this by setting Trend=0 when in position, but I can’t get a code wright for “position means Trend=0”. Also ONMARKET or NOTONMARKET are suggested, but I do not find these in the syntax, I get no results when using these words in the code.
For example, I tried this code but the part “AND Trend=0” is rejected….
(sorry for the Dutch in the code 🙂 )
// Opstapelen posities gedeactiveerd
DEFPARAM CumulateOrders = False
// Definitie mijnMACD
MMsnel = exponentialAverage[12](close)
MMlangzaam = exponentialAverage[26](close)
mijnMACD = MMsnel - MMlangzaam
//Definitie RSI
opwaarts = max(0, close - close[1])
neerwaarts = max(0, close[1] - close)
mmopwaarts = wilderAverage[14](opwaarts)
mmneerwaarts = wilderAverage[14](neerwaarts)
RS = mmopwaarts / mmneerwaarts
mijnRSI = 100 - 100 / (1 + RS)
//Longtrend, shorttrend
IF mijnMACD > 0 AND mijnRSI > 50 THEN
Trend = 1
ELSIF mijnMACD < 0 AND mijnRSI < 50 THEN
Trend = -1
ELSE
Trend = 0
ENDIF
//Condities
c1 = Trend = 1
c2 = Trend = -1
// Condities om long posities te openen
IF c1 THEN
BUY 1 SHARES AT MARKET AND Trend = 0
ENDIF
// Condities om long posities te sluiten
IF c2 THEN
SELL AT MARKET AND Trend = 0
ENDIF
// Condities om short posities te openen
IF c2 THEN
SELLSHORT 1 SHARES AT MARKET AND Trend = 0
ENDIF
// Condities om short posities te sluiten
IF c1 THEN
EXITSHORT AT MARKET AND Trend = 0
ENDIF
// Stops en targets
SET STOP pLOSS 20
SET TARGET pPROFIT 15
You cannot use conditions, nor instructuons, on the same line as BUY, SELL, SELLSHORT and EXITSHORT. Wrirte them on a separate line.
Moreover, you do not need to exit an open position for a Stop & Reverse, simply execute the contrary instruction. BUY will close any open short postition, then start a LONG one, the other way round for SELLSHORT.
// Opstapelen posities gedeactiveerd
DEFPARAM CumulateOrders = False
// Definitie mijnMACD
MMsnel = exponentialAverage[12](close)
MMlangzaam = exponentialAverage[26](close)
mijnMACD = MMsnel - MMlangzaam
//Definitie RSI
opwaarts = max(0, close - close[1])
neerwaarts = max(0, close[1] - close)
mmopwaarts = wilderAverage[14](opwaarts)
mmneerwaarts = wilderAverage[14](neerwaarts)
RS = mmopwaarts / mmneerwaarts
mijnRSI = 100 - 100 / (1 + RS)
//Longtrend, shorttrend
IF mijnMACD > 0 AND mijnRSI > 50 THEN
Trend = 1
ELSIF mijnMACD < 0 AND mijnRSI < 50 THEN
Trend = -1
ELSE
Trend = 0
ENDIF
//Condities
c1 = Trend = 1
c2 = Trend = -1
// Condities om long posities te openen
IF c1 AND Not LongOnMarket THEN
BUY 1 SHARES AT MARKET
Trend = 0
ENDIF
// Condities om short posities te openen
IF c2 THEN
SELLSHORT 1 SHARES AT MARKET
Trend = 0
ENDIF
// Stops en targets
SET STOP pLOSS 20
SET TARGET pPROFIT 15
OK Roberto, I understand the exit Long and exit short was not needed. Also I see you just simply put “Trend=0”. To me that is interesting, I was struggeling with conditions etc.
But, sorry, I have to inform you that this solution is not working. The system still trades on EVERY candle. When I leave “Trend=0” out of the code, the result is exactly the same… also on every candle.
When do you want to restart trading AFTER one entry?
Hello Roberto. I want to go long (only once) when RSI >50 AND MACD reaches >50. Then the trade will run until a) RSI <50 OR b) MACD < 0 (this is now not in the code, but is just to give you the whole idea) OR b) stoploss is hit OR c) profit is hit. After this I only want to trade again when there is a NEW situation, a NEW opportunity: RSI<50 and MACD<0 or a NEW situation where RSI>50 and MACD>0 again.
I do not know if it is possible to code such a thing. I think ONMARKET/NOTONMARKET will not work because once the stop or te profit is hit the system will conclude: NOTONMARKET and immediately trade again… Maybe something must de done with Trend = nr a+1 ? Thank you.
There you go:
// Opstapelen posities gedeactiveerd
DEFPARAM CumulateOrders = False
ONCE LastTrade = 0
// Definitie mijnMACD
MMsnel = exponentialAverage[12](close)
MMlangzaam = exponentialAverage[26](close)
mijnMACD = MMsnel - MMlangzaam
//Definitie RSI
opwaarts = max(0, close - close[1])
neerwaarts = max(0, close[1] - close)
mmopwaarts = wilderAverage[14](opwaarts)
mmneerwaarts = wilderAverage[14](neerwaarts)
RS = mmopwaarts / mmneerwaarts
mijnRSI = 100 - 100 / (1 + RS)
//Longtrend, shorttrend
IF mijnMACD > 0 AND mijnRSI > 50 THEN
Trend = 1
ELSIF mijnMACD < 0 AND mijnRSI < 50 THEN
Trend = -1
ELSE
Trend = 0
ENDIF
// Condities om long posities te openen
IF Trend = 1 AND Not LongOnMarket AND LastTrade <> 1 THEN
BUY 1 SHARES AT MARKET
LastTrade = 1
ENDIF
// Condities om short posities te openen
IF Trend = -1 AND Not ShortOnMarket AND LastTrade <> 2 THEN
SELLSHORT 1 SHARES AT MARKET
LastTrade = 2
ENDIF
// Stops en targets
SET STOP pLOSS 20 //120
SET TARGET pPROFIT 15 //215
//
//graph LastTrade
Thanks Roberto, this is really great!! I was looking for this for a longtime. (Sorry for late reaction, somehow I did not see the email). It is a simple code. I am even able to understand it!
I have only one question: is the variable LastTrade something you made up (can one write Apple instead?) or is it Syntax? If so – it is not in my syntax….
Thanks!
It’s not a keyword, so you can name it as best suits you, be itApple, Cat or A3. They cannot start with a digit and cannot have blanks and special characters embedded, though.
OK Clear Roberto. By the way: the line with Once is not necessary…