How to keep a condition active for more than one bar
Forums › ProRealTime English forum › ProOrder support › How to keep a condition active for more than one bar
- This topic has 13 replies, 4 voices, and was last updated 6 years ago by Juan Salas.
-
-
02/06/2018 at 9:43 PM #61790
Hi all,
In this case:
12345678910// Previous condition to open a tradeIF close crosses over level1up THENlevel1up=1ENDIF// LONGIF NOT ONMARKET AND level1up=1 THENBUY positionsize CONTRACT AT level2up STOPlevel1up=0ENDIFI am trying to set up a precondition to enable a trade. Before buying at level2up, closes has to crosses over level1up. The problem is that only works when it happens in the previous bar. When it happens several bars before, it doesn’t work.
I assume that we have to include something to keep this precondition (level1up=1) active, until it reaches the buying STOP order.
Any suggestions??? ,
Many thanks in advance,
Juan
02/06/2018 at 11:01 PM #61809Line 3 sets level1up to 1, but in line 2 you are comparing that value with close, which will rarely occurs and only on istruments ranging from just below 1.0000 to slightly higher. Eur/Usd will be unlikely to result in a true condition before some time!
Maybe level1up in line 2 should be replaced with level2up?
Anyway, your problem seats on line 9, since you immediately set it to ZERO, the next bars (unless another crossing occurs) conditions in line 7 will never be true again!
1 user thanked author for this post.
02/06/2018 at 11:27 PM #61815Hi Roberto,
Thanks for your answer, but I think there is a misunderstanding.
I am building a grid system that already works. I have created horizontal levels, with 50 pips separation, called level1up, level2up, and so on. In order not to buy against trend, I want that the price, before reaching level2up, crosses level1up, to make sure the price is going up. In some cases when the price is going down and have a slight retrace and touch any of the levels from below, it goes LONG when the general trend is the opposite.
So, right now, it works, I enable the buying STOP order at level2up when previously the price has surpassed level1up, but only works when that have occurred just one bar before. When the STOP order is executed, then I disable again the level1up to zero.
So I would like to know, if I have to include something to keep level1up=1 active until it reaches level2up BUYING STOP order, in case that it happens more than 1 bar after crossing level1up.
I hope it is clear now, but in any case thanks because you are always the first one to come to my rescue :))
Regards,
Juan
02/07/2018 at 8:58 AM #61827Just move line 9 somewhere else combined with a variable that determines WHEN the pending order has to be cancelled. This example cancels pending orders conditions after 10 bars:
123456789101112131415ONCE level1upBAR = 0IF level1up AND ((BarIndex - level1upBAR) >= 10) THENlevel1up=0ENDIF// Previous condition to open a tradeIF close crosses over level1up THENlevel1up=1level1upBAR=BarIndexENDIF// LONGIF NOT ONMARKET AND level1up=1 THENBUY positionsize CONTRACT AT level2up STOPENDIF02/07/2018 at 10:19 AM #61836Hi Roberto,
Many thanks 🙂
Juan
02/07/2018 at 10:55 AM #6183902/07/2018 at 1:48 PM #61864123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285// TRINQUETE 1' DAX//----------------------------// PARAMETERSDEFPARAM CumulateOrders = falseDEFPARAM Flatbefore = 080000DEFPARAM Flatafter = 210000// STRATEGYPROFIT per DAY //////////////////////////////////////////////////////////////////////////////// It resets the variable each new dayIF INTRADAYBARINDEX = 0 THENstratprofit = STRATEGYPROFIT //saves the previous day Profitnomoretrading = 0ENDIF// Finishing DAILY operations when reaching target profit/lossIF (STRATEGYPROFIT-stratprofit)>=1000 THEN //current day's profitSELL AT MARKETEXITSHORT AT MARKETnomoretrading=1ENDIFIF (STRATEGYPROFIT-stratprofit)<=-100 THEN //current day's lossSELL AT MARKETEXITSHORT AT MARKETnomoretrading=1ENDIF// Position Sizepositionsize=1// Distancia GRIDgrid=40disgrid1 = grid*1disgrid2 = grid*2disgrid3 = grid*3disgrid4 = grid*4disgrid5 = grid*5disgrid6 = grid*6disgrid7 = grid*7disgrid8 = grid*8disgrid9 = grid*9disgrid10 = grid*10// Determinación niveles GRIDIF time=080000 THENmaxiasian=highest[5](high)miniasian=lowest[5](low)amplitude=((maxiasian-miniasian)/2)+miniasianlevelZERO=amplitudeENDIFlevel10up = levelZERO + disgrid10*pipsizelevel9up = levelZERO + disgrid9*pipsizelevel8up = levelZERO + disgrid8*pipsizelevel7up = levelZERO + disgrid7*pipsizelevel6up = levelZERO + disgrid6*pipsizelevel5up = levelZERO + disgrid5*pipsizelevel4up = levelZERO + disgrid4*pipsizelevel3up = levelZERO + disgrid3*pipsizelevel2up = levelZERO + disgrid2*pipsizelevel1up = levelZERO + disgrid1*pipsizelevel1down = levelZERO - disgrid1*pipsizelevel2down = levelZERO - disgrid2*pipsizelevel3down = levelZERO - disgrid3*pipsizelevel4down = levelZERO - disgrid4*pipsizelevel5down = levelZERO - disgrid5*pipsizelevel6down = levelZERO - disgrid6*pipsizelevel7down = levelZERO - disgrid7*pipsizelevel8down = levelZERO - disgrid8*pipsizelevel9down = levelZERO - disgrid9*pipsizelevel10down = levelZERO - disgrid10*pipsize// GREEN LIGHT to operate the TREND// Levels UPONCE level1upBAR = 0IF close crosses over level1up THENlevel1up=1level1upBAR=barindexENDIFIF close crosses over level2up THENlevel2up=1level2upBAR=barindexENDIFIF close crosses over level3up THENlevel3up=1level3upBAR=barindexENDIFIF close crosses over level4up THENlevel4up=1level4upBAR=barindexENDIFIF close crosses over level5up THENlevel5up=1level5upBAR=barindexENDIFIF close crosses over level6up THENlevel6up=1level6upBAR=barindexENDIFIF close crosses over level7up THENlevel7up=1level7upBAR=barindexENDIFIF close crosses over level8up THENlevel8up=1level8upBAR=barindexENDIFIF close crosses over level9up THENlevel9up=1level9upBAR=barindexENDIF// ORDERS //////////////////////////////////////////////////////////////////////////////////////////////////////////// LONGsIF NOT ONMARKET AND level1up=1 AND barindex-level1upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level2up STOPlevel1up=0level2up=1ENDIFIF NOT ONMARKET AND level2up=1 AND barindex-level2upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level3up STOPlevel2up=0level3up=1ENDIFIF NOT ONMARKET AND level3up=1 AND barindex-level3upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level4up STOPlevel3up=0level4up=1ENDIFIF NOT ONMARKET AND level4up=1 AND barindex-level4upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level5up STOPlevel4up=0level5up=1ENDIFIF NOT ONMARKET AND level5up=1 AND barindex-level5upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level6up STOPlevel5up=0level6up=1ENDIFIF NOT ONMARKET AND level6up=1 AND barindex-level6upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level7up STOPlevel6up=0level7up=1ENDIFIF NOT ONMARKET AND level7up=1 AND barindex-level7upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level8up STOPlevel7up=0level8up=1ENDIFIF NOT ONMARKET AND level8up=1 AND barindex-level8upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level9up STOPlevel8up=0level9up=1ENDIFIF NOT ONMARKET AND level9up=1 AND barindex-level9upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level10up STOPlevel9up=0ENDIF// GREEN LIGHT to operate the TREND// Levels DOWNONCE level1downBAR = 0IF close crosses under level1down THENlevel1down=1level1downBAR=barindexENDIFIF close crosses under level2down THENlevel2down=1level2downBAR=barindexENDIFIF close crosses under level3down THENlevel3down=1level3downBAR=barindexENDIFIF close crosses under level4down THENlevel4down=1level4downBAR=barindexENDIFIF close crosses under level5down THENlevel5down=1level5downBAR=barindexENDIFIF close crosses under level6down THENlevel6down=1level6downBAR=barindexENDIFIF close crosses under level7down THENlevel7down=1level7downBAR=barindexENDIFIF close crosses under level8down THENlevel8down=1level8downBAR=barindexENDIFIF close crosses under level9down THENlevel9down=1level9downBAR=barindexENDIF// ORDERS //////////////////////////////////////////////////////////////////////////////////////////////////////////// SHORTsIF NOT ONMARKET AND level1down=1 AND barindex-level1downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level2down STOPlevel1down=0level2down=1ENDIFIF NOT ONMARKET AND level2down=1 AND barindex-level2downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level3down STOPlevel2down=0level3down=1ENDIFIF NOT ONMARKET AND level3down=1 AND barindex-level3downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level4down STOPlevel3down=0level4down=1ENDIFIF NOT ONMARKET AND level4down=1 AND barindex-level4downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level5down STOPlevel4down=0level5down=1ENDIFIF NOT ONMARKET AND level5down=1 AND barindex-level5downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level6down STOPlevel5down=0level6down=1ENDIFIF NOT ONMARKET AND level6down=1 AND barindex-level6downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level7down STOPlevel6down=0level7down=1ENDIFIF NOT ONMARKET AND level7down=1 AND barindex-level7downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level8down STOPlevel7down=0level8down=1ENDIFIF NOT ONMARKET AND level8down=1 AND barindex-level8downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level9down STOPlevel8down=0level9down=1ENDIFIF NOT ONMARKET AND level9down=1 AND barindex-level9downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level10down STOPlevel9down=0ENDIF// Stops y objetivosset stop ploss 30//60//50//50//40//55set target pprofit 40//80//15//16//50//120startBreakeven =0//714//5//5//3//25 //how much pips/points in gain to activate y//the breakeven function?PointsToKeep =15//1//10//25 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)//reset the breakevenLevel when no trade are on marketIF NOT ONMARKET THENbreakevenLevel=0ENDIF// LARGOS// test if the price have moved favourably of "startBreakeven" points alreadyIF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)+PointsToKeep*pipsizeENDIF// CORTOS// test if the price have moved favourably of "startBreakeven" points alreadyIF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)-PointsToKeep*pipsizeENDIF//place the new stop orders on market at breakevenLevelIF breakevenLevel>0 THENSELL AT breakevenLevel STOPEXITSHORT AT breakevenLevel STOPENDIF02/07/2018 at 1:55 PM #61865Hi Leo and Roberto,
This is the code. Horizontal levels above and below the LEVELLER which is determined by the price around 080000. The levels are separated by 40 pips.
If the price goes up only BUY LONG in the levels above ZERO (levelsup), and it has to be by crossing before a previous level. Example: If price is going up and going to cross level4up, I want that first has crossed over level3up, to avoid open LONG operations when the price is going south and touch any level in a small retrace.
The same way with the SHORT operations. Only short operations in the South Hemisphere where Level ZERO is the equator.
Right now, with this change proposed by Roberto, I can still see operation that are open without previously crossing the previous level.
Any suggestions??? Any change or improvement is welcome.
Thanks.
02/07/2018 at 1:58 PM #61866In order to better visualise the operations, you can download the indicator that draw the lines, so you can visualise the different levels.
02/07/2018 at 2:13 PM #61868in post 61865, I meant LEVELZERO instead of LEVELLER. F%&$*g autocorrector :))
02/07/2018 at 4:41 PM #61875In line 80 you write
1level1up=1thus replacing the value it was assigned in line 62, but the next bar level1up, which is now 1, will be replaced again with the old value when it reaches 62, so later at line 120 the condition will be false even if in the previous bar a crossing had occurred.
I think lines 53 through 73 should have different variable names.
1 user thanked author for this post.
02/07/2018 at 4:47 PM #61876Damn!!! Well done Roberto. I didn´t realised about that. I will change the names I assigned the levels not to interfere with the variables level1up, etc.
Thanks so much.
02/07/2018 at 8:58 PM #61896did you make the correction? how is the backtest? can you post the new code please?
02/08/2018 at 12:42 AM #61933Hi Raphaelopilski,
I am attaching below the code with the changes but it is not what I am looking for. I will continue working until it does exactly what I want. In any case, thanks to everyone, especially to Roberto for his comments and ideas.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287// TRINQUETE 1' DAX//----------------------------// PARAMETERSDEFPARAM CumulateOrders = falseDEFPARAM Flatbefore = 080000DEFPARAM Flatafter = 210000// STRATEGYPROFIT per DAY //////////////////////////////////////////////////////////////////////////////// It resets the variable each new dayIF INTRADAYBARINDEX = 0 THENstratprofit = STRATEGYPROFIT //saves the previous day Profitnomoretrading = 0ENDIF// Finishing DAILY operations when reaching target profit/lossIF (STRATEGYPROFIT-stratprofit)>=1000 THEN //current day's profitSELL AT MARKETEXITSHORT AT MARKETnomoretrading=1ENDIFIF (STRATEGYPROFIT-stratprofit)<=-100 THEN //current day's lossSELL AT MARKETEXITSHORT AT MARKETnomoretrading=1ENDIF// Position Sizepositionsize=1// STOPs & PROFITsSET STOP pLOSS 20SET TARGET pPROFIT 40// Distancia GRIDgrid=40disgrid1 = grid*1disgrid2 = grid*2disgrid3 = grid*3disgrid4 = grid*4disgrid5 = grid*5disgrid6 = grid*6disgrid7 = grid*7disgrid8 = grid*8disgrid9 = grid*9disgrid10 = grid*10// Determinación niveles GRIDIF time=080000 THENmaxiasian=highest[5](high)miniasian=lowest[5](low)amplitude=((maxiasian-miniasian)/2)+miniasianlevelZERO=amplitudeENDIFlevel10up = levelZERO + disgrid10*pipsizelevel9up = levelZERO + disgrid9*pipsizelevel8up = levelZERO + disgrid8*pipsizelevel7up = levelZERO + disgrid7*pipsizelevel6up = levelZERO + disgrid6*pipsizelevel5up = levelZERO + disgrid5*pipsizelevel4up = levelZERO + disgrid4*pipsizelevel3up = levelZERO + disgrid3*pipsizelevel2up = levelZERO + disgrid2*pipsizelevel1up = levelZERO + disgrid1*pipsizelevel1down = levelZERO - disgrid1*pipsizelevel2down = levelZERO - disgrid2*pipsizelevel3down = levelZERO - disgrid3*pipsizelevel4down = levelZERO - disgrid4*pipsizelevel5down = levelZERO - disgrid5*pipsizelevel6down = levelZERO - disgrid6*pipsizelevel7down = levelZERO - disgrid7*pipsizelevel8down = levelZERO - disgrid8*pipsizelevel9down = levelZERO - disgrid9*pipsizelevel10down = levelZERO - disgrid10*pipsize// GREEN LIGHT to operate the TREND// Levels UPONCE level1upBAR = 0IF close crosses over level1up THENl1up=1level1upBAR=barindexENDIFIF close crosses over level2up THENl2up=1level2upBAR=barindexENDIFIF close crosses over level3up THENl3up=1level3upBAR=barindexENDIFIF close crosses over level4up THENl4up=1level4upBAR=barindexENDIFIF close crosses over level5up THENl5up=1level5upBAR=barindexENDIFIF close crosses over level6up THENl6up=1level6upBAR=barindexENDIFIF close crosses over level7up THENl7up=1level7upBAR=barindexENDIFIF close crosses over level8up THENl8up=1level8upBAR=barindexENDIFIF close crosses over level9up THENl9up=1level9upBAR=barindexENDIF// ORDERS //////////////////////////////////////////////////////////////////////////////////////////////////////////// LONGsIF NOT ONMARKET AND l1up=1 AND barindex-level1upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level2up STOPl1up=0l2up=1ENDIFIF NOT ONMARKET AND l2up=1 AND barindex-level2upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level3up STOPl2up=0l3up=1ENDIFIF NOT ONMARKET AND l3up=1 AND barindex-level3upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level4up STOPl3up=0l4up=1ENDIFIF NOT ONMARKET AND l4up=1 AND barindex-level4upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level5up STOPl4up=0l5up=1ENDIFIF NOT ONMARKET AND l5up=1 AND barindex-level5upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level6up STOPl5up=0l6up=1ENDIFIF NOT ONMARKET AND l6up=1 AND barindex-level6upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level7up STOPl6up=0l7up=1ENDIFIF NOT ONMARKET AND l7up=1 AND barindex-level7upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level8up STOPl7up=0l8up=1ENDIFIF NOT ONMARKET AND l8up=1 AND barindex-level8upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level9up STOPl8up=0l9up=1ENDIFIF NOT ONMARKET AND l9up=1 AND barindex-level9upBAR<10 AND nomoretrading=0 THENBUY positionsize CONTRACT AT level10up STOPl9up=0ENDIF// GREEN LIGHT to operate the TREND// Levels DOWNONCE level1downBAR = 0IF close crosses under level1down THENl1down=1level1downBAR=barindexENDIFIF close crosses under level2down THENl2down=1level2downBAR=barindexENDIFIF close crosses under level3down THENl3down=1level3downBAR=barindexENDIFIF close crosses under level4down THENl4down=1level4downBAR=barindexENDIFIF close crosses under level5down THENl5down=1level5downBAR=barindexENDIFIF close crosses under level6down THENl6down=1level6downBAR=barindexENDIFIF close crosses under level7down THENl7down=1level7downBAR=barindexENDIFIF close crosses under level8down THENl8down=1level8downBAR=barindexENDIFIF close crosses under level9down THENl9down=1level9downBAR=barindexENDIF// ORDERS //////////////////////////////////////////////////////////////////////////////////////////////////////////// SHORTsIF NOT ONMARKET AND l1down=1 AND barindex-level1downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level2down STOPl1down=0l2down=1ENDIFIF NOT ONMARKET AND l2down=1 AND barindex-level2downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level3down STOPl2down=0l3down=1ENDIFIF NOT ONMARKET AND l3down=1 AND barindex-level3downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level4down STOPl3down=0l4down=1ENDIFIF NOT ONMARKET AND l4down=1 AND barindex-level4downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level5down STOPl4down=0l5down=1ENDIFIF NOT ONMARKET AND l5down=1 AND barindex-level5downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level6down STOPl5down=0l6down=1ENDIFIF NOT ONMARKET AND l6down=1 AND barindex-level6downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level7down STOPl6down=0l7down=1ENDIFIF NOT ONMARKET AND l7down=1 AND barindex-level7downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level8down STOPl7down=0l8down=1ENDIFIF NOT ONMARKET AND l8down=1 AND barindex-level8downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level9down STOPl8down=0l9down=1ENDIFIF NOT ONMARKET AND l9down=1 AND barindex-level9downBAR<10 AND nomoretrading=0 THENSELLSHORT positionsize CONTRACT AT level10down STOPl9down=0ENDIFstartBreakeven =0//714//5//5//3//25 //how much pips/points in gain to activate y//the breakeven function?PointsToKeep =15//1//10//25 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)//reset the breakevenLevel when no trade are on marketIF NOT ONMARKET THENbreakevenLevel=0ENDIF// LARGOS// test if the price have moved favourably of "startBreakeven" points alreadyIF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)+PointsToKeep*pipsizeENDIF// CORTOS// test if the price have moved favourably of "startBreakeven" points alreadyIF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)-PointsToKeep*pipsizeENDIF//place the new stop orders on market at breakevenLevelIF breakevenLevel>0 THENSELL AT breakevenLevel STOPEXITSHORT AT breakevenLevel STOPENDIFIf you use the indicator that I have provided before it will easier to visualise/identify any flaws of the operations and what it has to be improved.
Regards,
Juan
2 users thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on