Hi friends,
I’m working in an automated strategy but I’m having problems with an issue.
I would like to make entries only under a condition. I would like to open shorts when last RSI min. value was between two values, and open longs when last RSI max was between another two values.
I’ve got it defining a N bars number, but I don’t want to use the bars number. I think it may be solved using ONCE or WHILE instructions but I can’t find the way.
My code now is as following:
Open longs:
c7 = ((lowest[20](RSI)) < 20) and ((highest[RSINUMBARS](RSI)) < 80)
Open shorts:
c17 = ((highest[20](RSI)) > 80) and ((lowest[RSINUMBARS](RSI)) > 20)
But I would like to obviate the [20] bars value using something similar to
while ((highest[20](RSI)) > 80) and ((lowest[RSINUMBARS](RSI)) > 20) do ...
I tried wih ONCE too but didn’t work.
Could you guide me to find a solution?
Thanks to all the community.
If you post your full code then I / we could easily run it on our Platforms and it then easier to offer help rather than us having to make above into a working System to check it out.
If you not want to share your full code then I guess some expert coder may spot an anomaly and / or offer suggestions?
I’m not sure I fully understand your problem.
First of all you need to give your RSI’s a period. For example:
RSI[14]
Why not use a simple IF THEN ENDIF?
if ((highest[20](RSI[14])) > 80) and ((lowest[RSINUMBARS](RSI[14])) > 20) then
(whatever you want to do)
endif
The code is read through once at the close of a candle and if your condition of looking back for the lowest RSI and highest RSI over your look back periods is met then whatever you want to do is carried out.
The ONCE instruction means that whatever is written after it is only read at the close of the very first candle and ignored every candle after that.
Hi GraHal of course. Also I’ve been all night working on it and I changed a few things to enhance it.
Basically I change RSI by RSX and results are much better now. However code is a bit frankenstein…don’t laugh please 🙂
It’s for DAX M1 and my main question is how to avoid RSXBARS for no limiting trades at that certain vars number. I spent much hours testing with WHILE and ONCE but I can’t solve it.
Also, any other idea to enchance strategy is welcome 🙂
//-------------------------------------------------------------------------
// Código principal : CH - 1M - TR - STO
//-------------------------------------------------------------------------
/// Definition of code parameters
defparam preloadbars = 500
DEFPARAM FlatBefore = 101000
DEFPARAM FlatAfter = 172500
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// INICIO RSX
Len=RSXLEN
if (f90 = 0.0) then
f90 = 1.0
f0 = 0.0
if (Len-1 >= 5) then
f88 = Len-1.0
else
f88 = 5.0
endif
f8 = 100.0*(Close)
f18 = 3.0 / (Len + 2.0)
f20 = 1.0 - f18
else
if (f88 <= f90) then
f90 = f88 + 1
else
f90 = f90 + 1
endif
f10 = f8
f8 = 100*Close
v8 = f8 - f10
f28 = f20 * f28 + f18 * v8
f30 = f18 * f28 + f20 * f30
vC = f28 * 1.5 - f30 * 0.5
f38 = f20 * f38 + f18 * vC
f40 = f18 * f38 + f20 * f40
v10 = f38 * 1.5 - f40 * 0.5
f48 = f20 * f48 + f18 * v10
f50 = f18 * f48 + f20 * f50
v14 = f48 * 1.5 - f50 * 0.5
f58 = f20 * f58 + f18 * Abs(v8)
f60 = f18 * f58 + f20 * f60
v18 = f58 * 1.5 - f60 * 0.5
f68 = f20 * f68 + f18 * v18
f70 = f18 * f68 + f20 * f70
v1C = f68 * 1.5 - f70 * 0.5
f78 = f20 * f78 + f18 * v1C
f80 = f18 * f78 + f20 * f80
v20 = f78 * 1.5 - f80 * 0.5
if ((f88 >= f90) and (f8 <> f10)) then
f0 = 1.0
endif
if ((f88 = f90) and (f0 = 0.0)) then
f90 = 0.0
endif
endif
if ((f88 < f90) and (v20 > 0.0000000001)) then
v4 = (v14 / v20 + 1.0) * 50.0
if (v4 > 100.0) then
v4 = 100.0
endif
if (v4 < 0.0) then
v4 = 0.0
endif
else
v4 = 50.0
endif
// FIN RSX
// General conditions
RSX=v4
RSXLEN = 14 // 34 //12
RSXVARMIN = 28 // 31 //26
RSXVARMAX = 64 //62 //66
RSXBARS = 24 //33 //24
// Conditions long
STOL = Stochastic[204,4]
STOOVERL = 25
STOUNDERL = 60
STOMANTAINL = 89
ATRPROFITL = AverageTrueRange[5] //50 //AverageTrueRange[5]
ATRPROFITMULTL = 5 //1 //4.7
ATRRISKL = AverageTrueRange[10]
ATRRISKMULTL = 3.8 //3.5
MAPROFITAVGL = 14
BBPROFITSTDAVGL = 15
//RSXL = RSX
// Conditions short
STOS = Stochastic[199,2]
STOOVERS = 70
STOUNDERS = 83
STOMANTAINS = 6
ATRPROFITS = AverageTrueRange[10]
ATRPROFITMULTS = 4.6
ATRRISKS = AverageTrueRange[4]
ATRRISKMULTS = 4.4
MAPROFITAVGS = 15
BBPROFITSTDAVGS = 18
//RSXS = RSX
once RRreached = 0
profitpipsl = ATRPROFITL*ATRPROFITMULTL // 0.1 * var:30-60 4.9
riskpipsl = ATRRISKL*ATRRISKMULTL //risk in pips
amountl = 1 //lot amount to open each trade
sdl = 0.17 //standard deviation of MA floating profit - orig: 0.25
profitpipss = ATRPROFITS*ATRPROFITMULTS // 0.1 * var:30-60 4.6
riskpipss = ATRRISKS*ATRRISKMULTS //whole account risk in percent%
amounts = 1 //lot amount to open each trade
sds = 0.17 //standard deviation of MA floating profit - orig: 0.25
// Conditions to enter long positions
c3 = (STOL > STOOVERL) // 20
c4 = (STOL < STOUNDERL) // 40
c5 = (STOL > STOL[1]) // and (STOL[1] < STOL[2])
c6 = (RSX > RSX[1]) and (RSX > RSXVARMIN)
c7 = ((lowest[RSXBARS](RSX)) < RSXVARMIN) and ((highest[RSXBARS](RSX)) < RSXVARMAX)
// Conditions to enter short positions
c13 = (STOS < STOUNDERS) // 80
c14 = (STOS > STOOVERS) // 60
c15 = (STOS < STOS[1])
c16 = (RSX < RSX[1]) and (RSX < RSXVARMAX)
c17 = ((highest[RSXBARS](RSX)) > RSXVARMAX) and ((lowest[RSXBARS](RSX)) > RSXVARMIN)
//first trade whatever condition
if NOT ONMARKET AND c3 and c4 and c5 and c6 and c7 AND NOT daysForbiddenEntry then //close>close[1]
BUY amountl LOT AT MARKET
endif
if NOT ONMARKET AND c13 and c14 and c15 and c16 and c17 AND NOT daysForbiddenEntry then //close<close[1]
SELLSHORT amounts LOT AT MARKET
endif
//money management
//liveaccountbalance = accountbalance+strategyprofit
moneyriskl = riskpipsl
if longonmarket then
onepointvaluebasketl = pointvalue*countofposition
mindistancetoclosel =(moneyriskl/onepointvaluebasketl)*pipsize
endif
moneyrisks = riskpipss
if shortonmarket then
onepointvaluebaskets = pointvalue*countofposition
mindistancetocloses =(moneyrisks/onepointvaluebaskets)*pipsize
endif
//floating profit
floatingprofitl = (((close-positionprice)*pointvalue)*countofposition)/pipsize
floatingprofits = (((close-positionprice)*pointvalue)*countofposition)/pipsize
//actual trade gains
MAfloatingprofitl = average[MAPROFITAVGL](floatingprofitl)
BBfloatingprofitl = MAfloatingprofitl - std[BBPROFITSTDAVGL](MAfloatingprofitl)*sdl
MAfloatingprofits = average[MAPROFITAVGS](floatingprofits)
BBfloatingprofits = MAfloatingprofits - std[BBPROFITSTDAVGS](MAfloatingprofits)*sds
//floating profit risk reward check
if profitpipsl>0 and floatingprofitl>profitpipsl then
RRreached=1
endif
if profitpipss>0 and floatingprofits>profitpipss then
RRreached=1
endif
//stoploss trigger when risk reward ratio is not met already
//if onmarket and RRreached=0 then
//SELL AT positionprice-mindistancetoclose STOP
//EXITSHORT AT positionprice-mindistancetoclose STOP
//endif
if longonmarket and RRreached=0 then
SELL AT positionprice-mindistancetoclosel STOP
//EXITSHORT AT positionprice-mindistancetoclose STOP
endif
if shortonmarket and RRreached=0 then
//SELL AT positionprice-mindistancetoclose STOP
EXITSHORT AT positionprice-mindistancetocloses STOP
endif
//stoploss trigger when risk reward ratio has been reached
//if onmarket and RRreached=1 then
//if floatingprofit crosses under BBfloatingprofit then
//SELL AT MARKET
//EXITSHORT AT MARKET
//endif
//endif
if longonmarket and RRreached=1 and (stol < STOMANTAINL) then
if floatingprofitl crosses under BBfloatingprofitl then
SELL AT MARKET
endif
endif
if shortonmarket and RRreached=1 and (stos > STOMANTAINS) then
if floatingprofits crosses under BBfloatingprofits then
EXITSHORT AT MARKET
endif
endif
//resetting the risk reward reached variable
if not onmarket then
RRreached = 0
endif
Thanks a lot!
Hi Vonasi, thanks a lot for your response.
The main problem is defining last condition in a certain number of bars may limit amount of trades done.
What about if RSI or RSX condition is made on last 30 bars and not 14 or 20? I don’t want to lose those trades. So I would like to save in a floating var the last point where that condition were done and open trades in base at that condition.
I’m not sure if I explain it right at all, sorry.
However code is a bit frankenstein…don’t laugh please
I’m not laughing at the code, but I am at the joke! Very good!! I’ll have to remember that one! 🙂
But I have to say I am a bit scared by the monster!
Also you must look and feel like a monster if you have been up all night coding!?
I tried it on my Platform over 100k bars on DAX 1 min and it executes nil / zero trades … so that is the first problem – no trades – Yes??
Hi GraHal,
I don’t feel like Frankie…instead I want more coding…I’m getting addict 😀
I don’t have problems with code, but I upload it to test it. In 100000 bars doesn’t have great results but yes at 10.000 and 30.000 and my idea is updating vars to adapt it at market conditions.
I attach also some performance results.
Thanks again.
I test it at IG DAX 1€ cash.
I got 1 trade on a 5 min TF over 100k bars on DAX so my conclusions would be that your code has far too many AND conditions and they are not being met all at the same time.
I tried deleting Buy conditions one by one and I got trades (on 1 min TF) but they lost loads and loads of £££s.
I think you need to put chains / ropes on Frankenstein v1.0 and start on Frank v2.0 with far less conditions?? See if you can get signs of life in a finger before putting the whole monster body together?? Sorry couldn’t resist that! 🙂 🙂
On a less jokey note … did you run the System and get trades after adding each condition or did you write lots of conditions and then run the code (apply the lightning bolt to Frank!)??
Edit / PS
I wrote above before I saw your post with results … weird that I get zero trades? I’ll try running again again and report back.
Hi GraHal,
I don’t know which may be the real problem with your tests. I’m testing in € and capital is configured in PRT strategy window.
- Initial capital: 600€
- Spread: 2
Also, my country is Spain…not sure if it may affect to tests and results, because it works from 10:10 to 17:25 to avoid high commissions and market openings.
If you see performance data attached, are very promising. Of course I would like to reduce vars and conditions but I think it works nice for me.
About my doubts, have you got any idea? It’s adding some more code but I don’t fear of it 😀
Thanks again for your comments.
Aha I try it now on my Spread Bet Platform and I get 1 trade on DJI at 1 min TF (still no trades on DAX 1 min on CFD or SB PLatform).
Bit of a mystery, maybe it is the Time settings difference between our Platforms … Ill convert the times and report back.
EDIT / PS
To get same the lightning bolt at same time as Spain, I knocked 1 hour off Flat Before / After and still the same results … no trades on DAX at 1 min. Also I used the .itf file … so I have all Franks body parts in my Test Lab also! 🙂
but yes at 10.000 and 30.000
What do you mean re above?? 10 AM and 3 PM or what?
Sorry… 10000 and 30000 BARS
In Spain we use dots [.] instead commas [,] for thousand separations.
Regards.
@komiya Are you 100% sure that the results you show in post above are results for the code in the .itf file??
I can’t understand how you get trades and I don’t with the same code on same data on same TF etc etc??
Anybody else offer any thoughts on why nil trades for me and 95 trades for komiya??
Hi, I reupload again but it’s working nice for me…
Anybody else may test it and think about my question?
Thanks a lot people!