ProRealCode - Trading & Coding with ProRealTime™
Hi all,
In this case:
// Previous condition to open a trade
IF close crosses over level1up THEN
level1up=1
ENDIF
// LONG
IF NOT ONMARKET AND level1up=1 THEN
BUY positionsize CONTRACT AT level2up STOP
level1up=0
ENDIF
I 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
Line 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!
Hi 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
Just 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:
ONCE level1upBAR = 0
IF level1up AND ((BarIndex - level1upBAR) >= 10) THEN
level1up=0
ENDIF
// Previous condition to open a trade
IF close crosses over level1up THEN
level1up=1
level1upBAR=BarIndex
ENDIF
// LONG
IF NOT ONMARKET AND level1up=1 THEN
BUY positionsize CONTRACT AT level2up STOP
ENDIF
Hi Roberto,
Many thanks 🙂
Juan
If the order level is the same in several bars, my systems do not close and open the same orders, it just let them open.
// TRINQUETE 1' DAX
//----------------------------
// PARAMETERS
DEFPARAM CumulateOrders = false
DEFPARAM Flatbefore = 080000
DEFPARAM Flatafter = 210000
// STRATEGYPROFIT per DAY //////////////////////////////////////////////////////////////////////////////
// It resets the variable each new day
IF INTRADAYBARINDEX = 0 THEN
stratprofit = STRATEGYPROFIT //saves the previous day Profit
nomoretrading = 0
ENDIF
// Finishing DAILY operations when reaching target profit/loss
IF (STRATEGYPROFIT-stratprofit)>=1000 THEN //current day's profit
SELL AT MARKET
EXITSHORT AT MARKET
nomoretrading=1
ENDIF
IF (STRATEGYPROFIT-stratprofit)<=-100 THEN //current day's loss
SELL AT MARKET
EXITSHORT AT MARKET
nomoretrading=1
ENDIF
// Position Size
positionsize=1
// Distancia GRID
grid=40
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
// Determinación niveles GRID
IF time=080000 THEN
maxiasian=highest[5](high)
miniasian=lowest[5](low)
amplitude=((maxiasian-miniasian)/2)+miniasian
levelZERO=amplitude
ENDIF
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
// GREEN LIGHT to operate the TREND
// Levels UP
ONCE level1upBAR = 0
IF close crosses over level1up THEN
level1up=1
level1upBAR=barindex
ENDIF
IF close crosses over level2up THEN
level2up=1
level2upBAR=barindex
ENDIF
IF close crosses over level3up THEN
level3up=1
level3upBAR=barindex
ENDIF
IF close crosses over level4up THEN
level4up=1
level4upBAR=barindex
ENDIF
IF close crosses over level5up THEN
level5up=1
level5upBAR=barindex
ENDIF
IF close crosses over level6up THEN
level6up=1
level6upBAR=barindex
ENDIF
IF close crosses over level7up THEN
level7up=1
level7upBAR=barindex
ENDIF
IF close crosses over level8up THEN
level8up=1
level8upBAR=barindex
ENDIF
IF close crosses over level9up THEN
level9up=1
level9upBAR=barindex
ENDIF
// ORDERS //////////////////////////////////////////////////////////////////////////////////////////////////////////
// LONGs
IF NOT ONMARKET AND level1up=1 AND barindex-level1upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level2up STOP
level1up=0
level2up=1
ENDIF
IF NOT ONMARKET AND level2up=1 AND barindex-level2upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level3up STOP
level2up=0
level3up=1
ENDIF
IF NOT ONMARKET AND level3up=1 AND barindex-level3upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level4up STOP
level3up=0
level4up=1
ENDIF
IF NOT ONMARKET AND level4up=1 AND barindex-level4upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level5up STOP
level4up=0
level5up=1
ENDIF
IF NOT ONMARKET AND level5up=1 AND barindex-level5upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level6up STOP
level5up=0
level6up=1
ENDIF
IF NOT ONMARKET AND level6up=1 AND barindex-level6upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level7up STOP
level6up=0
level7up=1
ENDIF
IF NOT ONMARKET AND level7up=1 AND barindex-level7upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level8up STOP
level7up=0
level8up=1
ENDIF
IF NOT ONMARKET AND level8up=1 AND barindex-level8upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level9up STOP
level8up=0
level9up=1
ENDIF
IF NOT ONMARKET AND level9up=1 AND barindex-level9upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level10up STOP
level9up=0
ENDIF
// GREEN LIGHT to operate the TREND
// Levels DOWN
ONCE level1downBAR = 0
IF close crosses under level1down THEN
level1down=1
level1downBAR=barindex
ENDIF
IF close crosses under level2down THEN
level2down=1
level2downBAR=barindex
ENDIF
IF close crosses under level3down THEN
level3down=1
level3downBAR=barindex
ENDIF
IF close crosses under level4down THEN
level4down=1
level4downBAR=barindex
ENDIF
IF close crosses under level5down THEN
level5down=1
level5downBAR=barindex
ENDIF
IF close crosses under level6down THEN
level6down=1
level6downBAR=barindex
ENDIF
IF close crosses under level7down THEN
level7down=1
level7downBAR=barindex
ENDIF
IF close crosses under level8down THEN
level8down=1
level8downBAR=barindex
ENDIF
IF close crosses under level9down THEN
level9down=1
level9downBAR=barindex
ENDIF
// ORDERS //////////////////////////////////////////////////////////////////////////////////////////////////////////
// SHORTs
IF NOT ONMARKET AND level1down=1 AND barindex-level1downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level2down STOP
level1down=0
level2down=1
ENDIF
IF NOT ONMARKET AND level2down=1 AND barindex-level2downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level3down STOP
level2down=0
level3down=1
ENDIF
IF NOT ONMARKET AND level3down=1 AND barindex-level3downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level4down STOP
level3down=0
level4down=1
ENDIF
IF NOT ONMARKET AND level4down=1 AND barindex-level4downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level5down STOP
level4down=0
level5down=1
ENDIF
IF NOT ONMARKET AND level5down=1 AND barindex-level5downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level6down STOP
level5down=0
level6down=1
ENDIF
IF NOT ONMARKET AND level6down=1 AND barindex-level6downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level7down STOP
level6down=0
level7down=1
ENDIF
IF NOT ONMARKET AND level7down=1 AND barindex-level7downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level8down STOP
level7down=0
level8down=1
ENDIF
IF NOT ONMARKET AND level8down=1 AND barindex-level8downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level9down STOP
level8down=0
level9down=1
ENDIF
IF NOT ONMARKET AND level9down=1 AND barindex-level9downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level10down STOP
level9down=0
ENDIF
// Stops y objetivos
set stop ploss 30//60//50//50//40//55
set target pprofit 40//80//15//16//50//120
startBreakeven =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 market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
// LARGOS
// test if the price have moved favourably of "startBreakeven" points already
IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
ENDIF
// CORTOS
// test if the price have moved favourably of "startBreakeven" points already
IF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
EXITSHORT AT breakevenLevel STOP
ENDIF
Hi 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.
In order to better visualise the operations, you can download the indicator that draw the lines, so you can visualise the different levels.
in post 61865, I meant LEVELZERO instead of LEVELLER. F%&$*g autocorrector :))
In line 80 you write
level1up=1
thus 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.
Damn!!! 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.
did you make the correction? how is the backtest? can you post the new code please?
Hi 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.
// TRINQUETE 1' DAX
//----------------------------
// PARAMETERS
DEFPARAM CumulateOrders = false
DEFPARAM Flatbefore = 080000
DEFPARAM Flatafter = 210000
// STRATEGYPROFIT per DAY //////////////////////////////////////////////////////////////////////////////
// It resets the variable each new day
IF INTRADAYBARINDEX = 0 THEN
stratprofit = STRATEGYPROFIT //saves the previous day Profit
nomoretrading = 0
ENDIF
// Finishing DAILY operations when reaching target profit/loss
IF (STRATEGYPROFIT-stratprofit)>=1000 THEN //current day's profit
SELL AT MARKET
EXITSHORT AT MARKET
nomoretrading=1
ENDIF
IF (STRATEGYPROFIT-stratprofit)<=-100 THEN //current day's loss
SELL AT MARKET
EXITSHORT AT MARKET
nomoretrading=1
ENDIF
// Position Size
positionsize=1
// STOPs & PROFITs
SET STOP pLOSS 20
SET TARGET pPROFIT 40
// Distancia GRID
grid=40
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
// Determinación niveles GRID
IF time=080000 THEN
maxiasian=highest[5](high)
miniasian=lowest[5](low)
amplitude=((maxiasian-miniasian)/2)+miniasian
levelZERO=amplitude
ENDIF
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
// GREEN LIGHT to operate the TREND
// Levels UP
ONCE level1upBAR = 0
IF close crosses over level1up THEN
l1up=1
level1upBAR=barindex
ENDIF
IF close crosses over level2up THEN
l2up=1
level2upBAR=barindex
ENDIF
IF close crosses over level3up THEN
l3up=1
level3upBAR=barindex
ENDIF
IF close crosses over level4up THEN
l4up=1
level4upBAR=barindex
ENDIF
IF close crosses over level5up THEN
l5up=1
level5upBAR=barindex
ENDIF
IF close crosses over level6up THEN
l6up=1
level6upBAR=barindex
ENDIF
IF close crosses over level7up THEN
l7up=1
level7upBAR=barindex
ENDIF
IF close crosses over level8up THEN
l8up=1
level8upBAR=barindex
ENDIF
IF close crosses over level9up THEN
l9up=1
level9upBAR=barindex
ENDIF
// ORDERS //////////////////////////////////////////////////////////////////////////////////////////////////////////
// LONGs
IF NOT ONMARKET AND l1up=1 AND barindex-level1upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level2up STOP
l1up=0
l2up=1
ENDIF
IF NOT ONMARKET AND l2up=1 AND barindex-level2upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level3up STOP
l2up=0
l3up=1
ENDIF
IF NOT ONMARKET AND l3up=1 AND barindex-level3upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level4up STOP
l3up=0
l4up=1
ENDIF
IF NOT ONMARKET AND l4up=1 AND barindex-level4upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level5up STOP
l4up=0
l5up=1
ENDIF
IF NOT ONMARKET AND l5up=1 AND barindex-level5upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level6up STOP
l5up=0
l6up=1
ENDIF
IF NOT ONMARKET AND l6up=1 AND barindex-level6upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level7up STOP
l6up=0
l7up=1
ENDIF
IF NOT ONMARKET AND l7up=1 AND barindex-level7upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level8up STOP
l7up=0
l8up=1
ENDIF
IF NOT ONMARKET AND l8up=1 AND barindex-level8upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level9up STOP
l8up=0
l9up=1
ENDIF
IF NOT ONMARKET AND l9up=1 AND barindex-level9upBAR<10 AND nomoretrading=0 THEN
BUY positionsize CONTRACT AT level10up STOP
l9up=0
ENDIF
// GREEN LIGHT to operate the TREND
// Levels DOWN
ONCE level1downBAR = 0
IF close crosses under level1down THEN
l1down=1
level1downBAR=barindex
ENDIF
IF close crosses under level2down THEN
l2down=1
level2downBAR=barindex
ENDIF
IF close crosses under level3down THEN
l3down=1
level3downBAR=barindex
ENDIF
IF close crosses under level4down THEN
l4down=1
level4downBAR=barindex
ENDIF
IF close crosses under level5down THEN
l5down=1
level5downBAR=barindex
ENDIF
IF close crosses under level6down THEN
l6down=1
level6downBAR=barindex
ENDIF
IF close crosses under level7down THEN
l7down=1
level7downBAR=barindex
ENDIF
IF close crosses under level8down THEN
l8down=1
level8downBAR=barindex
ENDIF
IF close crosses under level9down THEN
l9down=1
level9downBAR=barindex
ENDIF
// ORDERS //////////////////////////////////////////////////////////////////////////////////////////////////////////
// SHORTs
IF NOT ONMARKET AND l1down=1 AND barindex-level1downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level2down STOP
l1down=0
l2down=1
ENDIF
IF NOT ONMARKET AND l2down=1 AND barindex-level2downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level3down STOP
l2down=0
l3down=1
ENDIF
IF NOT ONMARKET AND l3down=1 AND barindex-level3downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level4down STOP
l3down=0
l4down=1
ENDIF
IF NOT ONMARKET AND l4down=1 AND barindex-level4downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level5down STOP
l4down=0
l5down=1
ENDIF
IF NOT ONMARKET AND l5down=1 AND barindex-level5downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level6down STOP
l5down=0
l6down=1
ENDIF
IF NOT ONMARKET AND l6down=1 AND barindex-level6downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level7down STOP
l6down=0
l7down=1
ENDIF
IF NOT ONMARKET AND l7down=1 AND barindex-level7downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level8down STOP
l7down=0
l8down=1
ENDIF
IF NOT ONMARKET AND l8down=1 AND barindex-level8downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level9down STOP
l8down=0
l9down=1
ENDIF
IF NOT ONMARKET AND l9down=1 AND barindex-level9downBAR<10 AND nomoretrading=0 THEN
SELLSHORT positionsize CONTRACT AT level10down STOP
l9down=0
ENDIF
startBreakeven =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 market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
// LARGOS
// test if the price have moved favourably of "startBreakeven" points already
IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
ENDIF
// CORTOS
// test if the price have moved favourably of "startBreakeven" points already
IF SHORTONMARKET AND tradeprice(1)-close>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
EXITSHORT AT breakevenLevel STOP
ENDIF
If 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
How to keep a condition active for more than one bar
This topic contains 13 replies,
has 4 voices, and was last updated by Juan Salas
8 years ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 02/06/2018 |
| Status: | Active |
| Attachments: | 2 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.