Hi Everyone,
Hoping someone can help me.
I am new to this and have written a simple code which opens a sell order when the RSI is >70 and a buy order at <30. I have included for a target point range, which for this example can be 5.
The code works fine, except, when it opens a long/short and the market moves in the required direction taking the RSI below 70 or above 30 but does not however trigger the target of 5 but instead renters the >70 or <30 area, the program gets confused and will ignore the target of 5 and then only closes the short or long at the point of <30 and long at >70. Is there a change I can make that will ensure that any opened position will close at the target value irrespective if the value goes above or below the 30/70 RSI level, and also if this were to occur then every time it does break the RSI level, it opens a subsequent position. When I back test it as it is, its doing what I want except the times where it bounces in and out of the 70/30 area.
Any advice, help or modifications to the code would be appreciated.
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
DEFPARAM FLATBEFORE = 000000
DEFPARAM FLATAFTER = 000000
//Conditions
C1 = (RSI < 30)
C2 = (RSI > 70)
// Conditions to enter long positions
IF NOT LongOnMarket AND C1 THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
// Conditions to exit long positions
If LongOnMarket AND C2 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
IF NOT ShortOnMarket AND C2 THEN
SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
// Conditions to exit short positions
IF ShortOnMarket AND C1 THEN
EXITSHORT AT MARKET
ENDIF
// Stops and targets : Enter your protection stops and profit targets here
SET STOP PLOSS 0
SET TARGET PPROFIT 5
>> For clarity of messages on ProRealCode’s forums, please use the “insert code PRT” button to separate the text of the code part! Thank you! <<
🙂
Try increasing the your TP, 5 seems to not abide by the minimum distance required.
10+ might solve it.
I don’t think that makes any difference, whilst backtesting and having found this issue, the problem isn’t with the TP its when the price drops below/above the 70/30 RSi and then revisits that area. at that point, I want the code to open another trade or if nothing else, just close out the original trade at the required TP. what actually happens is that it doesn’t do this and waits for it to open/close a trade at the opposite RSI. logically, it should close at the required TP, for whatever reason this does work, but fails if it renters the are of 70/30.
I can’t understand lines 2 and 3, They might be confusing.
Line 2 is useless, since there cannot be anything before 000000, -1 does not exist.
Line 3 means at anytime because whatever the time it will be > 0.
Your trades shouldn’t open at all, but if they do they should be immediately closed.
They do open.
In any case, the issue I have is not with times as I can change them to suit.
just trying to find a solution to the real problem, it’s not the TP.
Anyway, you can replace lines 7-8 with:
C1 = 0
C2 = 0
IF Not OnMarket THEN
C1 = (RSI < 30)
C2 = (RSI > 70)
ENDIF
this should do.