Avoiding repetition of operation under one condition
Forums › ProRealTime English forum › ProOrder support › Avoiding repetition of operation under one condition
- This topic has 9 replies, 3 voices, and was last updated 5 years ago by Juan Salas.
-
-
06/17/2018 at 4:39 PM #73501
Dear all, I hope you are enjoying this nice weekend,
I am working on a weekly grid strategy. Brief description: Every Monday at 09:00h, the close determines the levelZERO and marks 10 levels UP and 10 levels DOWN from level ZERO, with a 50 pips separation between each of them. Since it is a hedge strategy, I am developing two different codes (LONG and SHORT).
At 09:00h, it buys and sells at level ZERO and continue buying and selling at every level. The strategy gets rid of the losing operations after 50 pips, and continue accumulating the winning. The strategy starts at 09:00h on Monday and close ALL operations when reaching the last level (level10up or level10dow) or on Friday at 21:00h.
Obviously, it only works in trendy weeks. Nevertheless, before applying any filter to avoid lateral markets, I am having trouble with avoiding repetition of operations. Example: when the price touches level1up I want to launch a BUY and SELLSHORT, and I DO NOT want to launch any other order until it touches the next level up or down. In the current strategy, it buys and sells every time it touch the SAME level, accumulating several contracts at the same level. I have tried to avoid repetition after the first buying with level1up=0 and level1up=1, but it does not work.
I am attaching only the LONG strategy, since the SHORT it is just the opposite. I am also attaching the indicator TRINQUETE weekly 50pips levels to visualise the levels clearly.
Thanks so much in advance for any help,
Juan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177// TRINQUETE LONG DAX 5'//----------------------------// PARAMETERSDEFPARAM Preloadbars = 2000DEFPARAM CumulateOrders = true// Resetting variables//IF dayofweek=1 AND INTRADAYBARINDEX=0 THEN//level9up=0//level8up=0//level7up=0//level6up=0//level5up=0//level4up=0//level3up=0//level2up=0//level1up=0//levelZERO=0//level1down=0//level2down=0//level3down=0//level4down=0//level5down=0//level6down=0//level7down=0//level8down=0//level9down=0//ENDIF// Avoiding operations every Monday before 09:00hem = (dayofweek=1 AND time<090000)// Friday 21:00h Close ALL operations.IF dayofweek=5 AND time=210000 THENSELL AT MARKETEXITSHORT AT MARKETENDIF// Position Sizepositionsize=1// Start operationshorazero= time=090000// Defining Level ZEROIF dayofweek=1 AND horazero THENlevelZERO=closeENDIF// GRID Distancegrid=50disgrid1 = grid*1disgrid2 = grid*2disgrid3 = grid*3disgrid4 = grid*4disgrid5 = grid*5disgrid6 = grid*6disgrid7 = grid*7disgrid8 = grid*8disgrid9 = grid*9disgrid10 = grid*10// GRID Levelslevel10up = 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// ORDERS //////////////////////////////////////////////////////////////////////////////////////////////////////////// First ORDERIF dayofweek=1 AND horazero THENBUY positionsize CONTRACT AT MARKET//level1up=0level1down=0ENDIF// Subsequent ORDERSIF NOT em AND close>level8up AND close<level9up THENBUY positionsize CONTRACT AT level9up STOP//BUY positionsize CONTRACT AT level8up STOPENDIFIF NOT em AND close>level7up AND close<level8up THENBUY positionsize CONTRACT AT level8up STOP//BUY positionsize CONTRACT AT level7up STOPENDIFIF NOT em AND close>level6up AND close<level7up THENBUY positionsize CONTRACT AT level7up STOP//BUY positionsize CONTRACT AT level6up STOPENDIFIF NOT em AND close>level5up AND close<level6up THENBUY positionsize CONTRACT AT level6up STOP//BUY positionsize CONTRACT AT level5up STOPENDIFIF NOT em AND close>level4up AND close<level5up THENBUY positionsize CONTRACT AT level5up STOP//BUY positionsize CONTRACT AT level4up STOPENDIFIF NOT em AND close>level3up AND close<level4up THENBUY positionsize CONTRACT AT level4up STOP//BUY positionsize CONTRACT AT level3up STOPENDIFIF NOT em AND close>level2up AND close<level3up THENBUY positionsize CONTRACT AT level3up STOP//BUY positionsize CONTRACT AT level2up STOPENDIFIF NOT em AND close>level1up AND close<level2up THENBUY positionsize CONTRACT AT level2up STOP//BUY positionsize CONTRACT AT level1up STOPENDIFIF NOT em AND close>levelZERO AND close<level1up THENBUY positionsize CONTRACT AT level1up STOP//BUY positionsize CONTRACT AT levelZERO STOPENDIFIF NOT em AND close>level1down AND close<levelZERO THENBUY positionsize CONTRACT AT levelZERO STOP//BUY positionsize CONTRACT AT level1down STOPENDIFIF NOT em AND close>level2down AND close<level1down THENBUY positionsize CONTRACT AT level1down STOP//BUY positionsize CONTRACT AT level2down STOPENDIFIF NOT em AND close>level3down AND close<level2down THENBUY positionsize CONTRACT AT level2down STOP//BUY positionsize CONTRACT AT level3down STOPENDIFIF NOT em AND close>level4down AND close<level3down THENBUY positionsize CONTRACT AT level3down STOP//BUY positionsize CONTRACT AT level4down STOPENDIFIF NOT em AND close>level5down AND close<level4down THENBUY positionsize CONTRACT AT level4down STOP//BUY positionsize CONTRACT AT level5down STOPENDIFIF NOT em AND close>level6down AND close<level5down THENBUY positionsize CONTRACT AT level5down STOP//BUY positionsize CONTRACT AT level6down STOPENDIFIF NOT em AND close>level7down AND close<level6down THENBUY positionsize CONTRACT AT level6down STOP//BUY positionsize CONTRACT AT level7down STOPENDIFIF NOT em AND close>level8down AND close<level7down THENBUY positionsize CONTRACT AT level7down STOP//BUY positionsize CONTRACT AT level8down STOPENDIFIF NOT em AND close>level9down AND close<level8down THENBUY positionsize CONTRACT AT level8down STOP//BUY positionsize CONTRACT AT level9down STOPENDIFIF NOT em AND close>level10down AND close<level9down THENBUY positionsize CONTRACT AT level9down STOPENDIFIF close>level10up THENSELL AT MARKETEXITSHORT AT MARKETENDIFSET STOP pLOSS grid06/17/2018 at 6:46 PM #73512Maybe a separate bit of code so that if close crosses a level then a flag for that level is set and any flags for any other levels are cancelled to zero. Then only allow entry at a level if the flag for that level is zero? You may have to only let the flag be set if previous close was between levels either side.
Just thinking off the top of my head – it may not be the answer!
1 user thanked author for this post.
06/17/2018 at 7:21 PM #73516Hi Vonasi, Thanks for the idea.
I am not sure if it will work. To set up the pending STOP order the close has to be between two levels, and setting up a flag will do IMO the same that the command to set up the STOP orders. Every time the close crosses the level, even without reaching the next level, it will open another order. I have tried to put something like this (just for the two first levels UP/DOWN of levelZERO):
1234567891011121314151617181920// Primera ORDENIF dayofweek=1 AND horazero THENBUY positionsize CONTRACT AT MARKETlevelZERO=0ENDIF// Ordenes Levels GRIDIF NOT em AND close>levelZERO AND close<level1up AND levelZERO=0 THENBUY positionsize CONTRACT AT level1up STOP//BUY positionsize CONTRACT AT levelZERO STOPlevelZERO=1level1up=0ENDIFIF NOT em AND close<levelZERO AND close>level1down AND levelZERO=0 THENBUY positionsize CONTRACT AT levelZERO STOP//BUY positionsize CONTRACT AT level1down STOPlevelZERO=1level1down=0ENDIFbut, it doesn’t work. After the first initial buy at level ZERO, no order is launch again. I am trying that after the first buy at every level, we set up a variable in a way that no other order can be launch. Only after crossing to the next level, that variable is enable again, in case the price goes back to the previous level.
Right now, the backtest shows the redundant orders at every level the ruin the strategy.
Thanks anyway.
06/17/2018 at 7:34 PM #7352112345678910111213141516171819202122232425/ Primera ORDENIF not onmarket and dayofweek=1 AND horazero THENBUY positionsize CONTRACT AT MARKETENDIF// Ordenes Levels GRIDIF NOT em AND close > levelZERO AND close < level1up AND OneUp = 0 THENBUY positionsize CONTRACT AT level1up STOPENDIFIF NOT em AND close < levelZERO AND close > level1down AND OneDown=0 THENBUY positionsize CONTRACT AT levelZERO STOPENDIFIF close crosses Level1Up thenTwoUp = 0OneUp = 1OneDown = 0ENDIFIF close crosses Level1Down thenOneUp = 0OneDown = 1TwoDown = 0ENDIFThis was more the idea that I had in mind…. just off the top of my head as my platform is not open right now.
06/17/2018 at 7:46 PM #7352306/17/2018 at 8:10 PM #7352606/17/2018 at 9:09 PM #73529Hi Vonasi,
Thanks, I am going to try both options. I really like the more simple and effective second one. Sometimes, someone else can bring more simple ideas to something I was complicating too much 🙂
Thanks. I will post the code once it is finished, in case someone is interested in a grid (with hedge) strategy.
Juan
06/17/2018 at 10:02 PM #73530Sometimes, someone else can bring more simple ideas to something I was complicating too much
No problem – I was pleased with myself for coming up with a simple solution in the end as normally I take the code that was provided and never question it and just try to work with the original rather than start at the beginning again.
This time I surprised myself especially as I was thinking about the problem after a couple of glasses wine!
06/18/2018 at 8:32 AM #73550You can add a FLAG variable for each level and set it to 1 when that level is hit, then ban opening trades on that level when that flag=1
12345678910level1upFlag = 0 //initial state..IF NOT em AND close>level1up AND close<level2up AND level1upFlag = 0 THENBUY positionsize CONTRACT AT level2up STOP//BUY positionsize CONTRACT AT level1up STOPlevel1upFlag = 1 //ban further tradesENDIF..1 user thanked author for this post.
06/18/2018 at 11:06 AM #73568Hi Roberto,
Thanks. In #73516 I did something similar, but somehow I did something wrong, since it didnt work. In any case, Vonasi’s approach is working. I am trying to put on the demo account but the orders are rejected due to negative stops (minor problem).
Thanks to both.
Juan
-
AuthorPosts
Find exclusive trading pro-tools on