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
// TRINQUETE LONG DAX 5'
//----------------------------
// PARAMETERS
DEFPARAM Preloadbars = 2000
DEFPARAM 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:00h
em = (dayofweek=1 AND time<090000)
// Friday 21:00h Close ALL operations.
IF dayofweek=5 AND time=210000 THEN
SELL AT MARKET
EXITSHORT AT MARKET
ENDIF
// Position Size
positionsize=1
// Start operations
horazero= time=090000
// Defining Level ZERO
IF dayofweek=1 AND horazero THEN
levelZERO=close
ENDIF
// GRID Distance
grid=50
disgrid1 = grid*1
disgrid2 = grid*2
disgrid3 = grid*3
disgrid4 = grid*4
disgrid5 = grid*5
disgrid6 = grid*6
disgrid7 = grid*7
disgrid8 = grid*8
disgrid9 = grid*9
disgrid10 = grid*10
// GRID Levels
level10up = levelZERO + disgrid10*pipsize
level9up = levelZERO + disgrid9*pipsize
level8up = levelZERO + disgrid8*pipsize
level7up = levelZERO + disgrid7*pipsize
level6up = levelZERO + disgrid6*pipsize
level5up = levelZERO + disgrid5*pipsize
level4up = levelZERO + disgrid4*pipsize
level3up = levelZERO + disgrid3*pipsize
level2up = levelZERO + disgrid2*pipsize
level1up = levelZERO + disgrid1*pipsize
level1down = levelZERO - disgrid1*pipsize
level2down = levelZERO - disgrid2*pipsize
level3down = levelZERO - disgrid3*pipsize
level4down = levelZERO - disgrid4*pipsize
level5down = levelZERO - disgrid5*pipsize
level6down = levelZERO - disgrid6*pipsize
level7down = levelZERO - disgrid7*pipsize
level8down = levelZERO - disgrid8*pipsize
level9down = levelZERO - disgrid9*pipsize
level10down = levelZERO - disgrid10*pipsize
// ORDERS //////////////////////////////////////////////////////////////////////////////////////////////////////////
// First ORDER
IF dayofweek=1 AND horazero THEN
BUY positionsize CONTRACT AT MARKET
//level1up=0
level1down=0
ENDIF
// Subsequent ORDERS
IF NOT em AND close>level8up AND close<level9up THEN
BUY positionsize CONTRACT AT level9up STOP
//BUY positionsize CONTRACT AT level8up STOP
ENDIF
IF NOT em AND close>level7up AND close<level8up THEN
BUY positionsize CONTRACT AT level8up STOP
//BUY positionsize CONTRACT AT level7up STOP
ENDIF
IF NOT em AND close>level6up AND close<level7up THEN
BUY positionsize CONTRACT AT level7up STOP
//BUY positionsize CONTRACT AT level6up STOP
ENDIF
IF NOT em AND close>level5up AND close<level6up THEN
BUY positionsize CONTRACT AT level6up STOP
//BUY positionsize CONTRACT AT level5up STOP
ENDIF
IF NOT em AND close>level4up AND close<level5up THEN
BUY positionsize CONTRACT AT level5up STOP
//BUY positionsize CONTRACT AT level4up STOP
ENDIF
IF NOT em AND close>level3up AND close<level4up THEN
BUY positionsize CONTRACT AT level4up STOP
//BUY positionsize CONTRACT AT level3up STOP
ENDIF
IF NOT em AND close>level2up AND close<level3up THEN
BUY positionsize CONTRACT AT level3up STOP
//BUY positionsize CONTRACT AT level2up STOP
ENDIF
IF NOT em AND close>level1up AND close<level2up THEN
BUY positionsize CONTRACT AT level2up STOP
//BUY positionsize CONTRACT AT level1up STOP
ENDIF
IF NOT em AND close>levelZERO AND close<level1up THEN
BUY positionsize CONTRACT AT level1up STOP
//BUY positionsize CONTRACT AT levelZERO STOP
ENDIF
IF NOT em AND close>level1down AND close<levelZERO THEN
BUY positionsize CONTRACT AT levelZERO STOP
//BUY positionsize CONTRACT AT level1down STOP
ENDIF
IF NOT em AND close>level2down AND close<level1down THEN
BUY positionsize CONTRACT AT level1down STOP
//BUY positionsize CONTRACT AT level2down STOP
ENDIF
IF NOT em AND close>level3down AND close<level2down THEN
BUY positionsize CONTRACT AT level2down STOP
//BUY positionsize CONTRACT AT level3down STOP
ENDIF
IF NOT em AND close>level4down AND close<level3down THEN
BUY positionsize CONTRACT AT level3down STOP
//BUY positionsize CONTRACT AT level4down STOP
ENDIF
IF NOT em AND close>level5down AND close<level4down THEN
BUY positionsize CONTRACT AT level4down STOP
//BUY positionsize CONTRACT AT level5down STOP
ENDIF
IF NOT em AND close>level6down AND close<level5down THEN
BUY positionsize CONTRACT AT level5down STOP
//BUY positionsize CONTRACT AT level6down STOP
ENDIF
IF NOT em AND close>level7down AND close<level6down THEN
BUY positionsize CONTRACT AT level6down STOP
//BUY positionsize CONTRACT AT level7down STOP
ENDIF
IF NOT em AND close>level8down AND close<level7down THEN
BUY positionsize CONTRACT AT level7down STOP
//BUY positionsize CONTRACT AT level8down STOP
ENDIF
IF NOT em AND close>level9down AND close<level8down THEN
BUY positionsize CONTRACT AT level8down STOP
//BUY positionsize CONTRACT AT level9down STOP
ENDIF
IF NOT em AND close>level10down AND close<level9down THEN
BUY positionsize CONTRACT AT level9down STOP
ENDIF
IF close>level10up THEN
SELL AT MARKET
EXITSHORT AT MARKET
ENDIF
SET STOP pLOSS grid
Maybe 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!
Hi 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):
// Primera ORDEN
IF dayofweek=1 AND horazero THEN
BUY positionsize CONTRACT AT MARKET
levelZERO=0
ENDIF
// Ordenes Levels GRID
IF NOT em AND close>levelZERO AND close<level1up AND levelZERO=0 THEN
BUY positionsize CONTRACT AT level1up STOP
//BUY positionsize CONTRACT AT levelZERO STOP
levelZERO=1
level1up=0
ENDIF
IF NOT em AND close<levelZERO AND close>level1down AND levelZERO=0 THEN
BUY positionsize CONTRACT AT levelZERO STOP
//BUY positionsize CONTRACT AT level1down STOP
levelZERO=1
level1down=0
ENDIF
but, 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.
/ Primera ORDEN
IF not onmarket and dayofweek=1 AND horazero THEN
BUY positionsize CONTRACT AT MARKET
ENDIF
// Ordenes Levels GRID
IF NOT em AND close > levelZERO AND close < level1up AND OneUp = 0 THEN
BUY positionsize CONTRACT AT level1up STOP
ENDIF
IF NOT em AND close < levelZERO AND close > level1down AND OneDown=0 THEN
BUY positionsize CONTRACT AT levelZERO STOP
ENDIF
IF close crosses Level1Up then
TwoUp = 0
OneUp = 1
OneDown = 0
ENDIF
IF close crosses Level1Down then
OneUp = 0
OneDown = 1
TwoDown = 0
ENDIF
This was more the idea that I had in mind…. just off the top of my head as my platform is not open right now.
Thinking about it… why don’t you just use TRADEPRICE as your datum level and set orders to buy and sell 50 pips above and below this level?
IF not onmarket and dayofweek=1 AND horazero THEN
BUY positionsize CONTRACT AT MARKET
ENDIF
Grid = 50
Buy position contracts at tradePrice + Grid Stop
Buy position contracts at tradePrice - Grid limit
Hi 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
Sometimes, 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!
You 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
level1upFlag = 0 //initial state
.
.
IF NOT em AND close>level1up AND close<level2up AND level1upFlag = 0 THEN
BUY positionsize CONTRACT AT level2up STOP
//BUY positionsize CONTRACT AT level1up STOP
level1upFlag = 1 //ban further trades
ENDIF
.
.
Hi 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