How to add close_above_open for a buy indicator in MQL4? - algorithmic-trading

Is that possible to add a close_above_open condition too in the pattern of iHigh() && iClose() && iLow()?
I.e. I would like to add the close_above_open in the below code. Can you help me to know the method?
const int timePeriodD1=PERIOD_D1;
if(iHigh(symbol,timePeriodD1,1)>iHigh(symbol,timePeriodD1,2) && iClose(symbol,timePeriodD1,1) >iClose(symbol,timePeriodD1,2) &&
iLow(symbol,timePeriodD1,1)>iLow(symbol,timePeriodD1,2)){
//ObjectSetText(d1label,"UP",11, "Verdana", Aqua);
d1result="UP";
}else if(iHigh(symbol,timePeriodD1,1)<iHigh(symbol,timePeriodD1,2) && iClose(symbol,timePeriodD1,1) < iClose(symbol,timePeriodD1,2) &&
iLow(symbol,timePeriodD1,1)< iLow(symbol,timePeriodD1,2)){
//ObjectSetText(d1label,"DOWN",11, "Verdana", Yellow);
d1result="DOWN";
}else{
//ObjectSetText(d1label,"MIXED",11, "Verdana", White);
d1result="MIXED";
}
Simply asking is there any way to write as below:
if(iHigh(symbol,timePeriodD1,1)>iHigh(symbol,timePeriodD1,2) && iClose(symbol,timePeriodD1,1) >iClose(symbol,timePeriodD1,2) &&
iLow(symbol,timePeriodD1,1)>iLow(symbol,timePeriodD1,2) &&
CloseAboveOpen(symbol,timePeriodD1,1)>CloseAboveOpen(symbol,timePeriodD1,2)

Q1 : Is that possible to add a close_above_open condition too in the pattern of iHigh() && iClose() && iLow()?
Yes, it is:
...
if ( iHigh( symbol, timePeriodD1, 1 ) > iHigh( symbol, timePeriodD1, 2 )
&& iClose( symbol, timePeriodD1, 1 ) > iClose( symbol, timePeriodD1, 2 )
&& iLow( symbol, timePeriodD1, 1 ) > iLow( symbol, timePeriodD1, 2 )
&& CloseAboveOpen( symbol, timePeriodD1, 1 ) > CloseAboveOpen( symbol, timePeriodD1, 2 )
){ ... }
...
Q2 : Simply asking is there any way to write it as below...?
Yes, there is.
May try to customize this template to match your further needs:
int CloseAboveOpen( string aSymbolNAME, /// [DOC-me] Ret-s {+|-}n-pts above Open
int aTimeFRAME,
int nOffsetBARs ) {
return( (int)( ( iClose( aSymbolNAME, aTimeFRAME, nOffsetBARs )
- iOpen( aSymbolNAME, aTimeFRAME, nOffsetBARs )
)
* MathPow( 10, (int)MarketInfo( aSymbolNAME, MODE_DIGITS ) )
)
);
}

Related

MT4 - PERIOD 's

I use below code for #1 screenshot.
int _objfnd = ObjectFind( name );
if ( _objfnd == -1 )
{
ObjectCreate ( _vlineName, OBJ_VLINE, 0, Time[i], 0 );
...
}
and I use below code for #2 screenshot.
int _objfnd = ObjectFind( name );
if ( _objfnd == -1 )
{
datetime _dayTime = Time[i] ;
int _dayNext = PeriodSeconds( _Session_Breaks_Day_Prd ) ;
_dayTime = _dayTime - ( _dayTime % _dayNext ) + _dayNext ;
ObjectCreate ( _vlineName, OBJ_VLINE, 0, _dayTime, 0 ) ;
}
If you figure out my concern,please give me good advice, much appreciate.
A: the code has not handled Daylight Saving Time Shift 2016, Nov-06

MQL5: how do I automatically delete all un-triggered pending orders before placing new orders?

I am working on a Project that requires me to place a BUYSTOP and a SELLSTOP pair of orders and then on the next bar if those orders are not triggered, then delete them and place fresh ones.
Here is my code:
if(logic == true && OrdersTotal() == 0)
{bool res = OrderSend(....);}
if(OrdersTotal() != 0)
{
if(ordertype == OP_BUY || ordertype == OP_SELL)
{
bool del = OrderDelete(....);
}
}
This code is properly placing orders and deleting them as well when I am testing.
But when the EA is active on the live server, it does not open orders because the platform already has orders of other instruments open.
I'm sure there would be quite an easy way to get around this but since I am a novice, I'm not able to figure that out.
it is not clear whether you use magic number and symbol check.
you should check sth like
int _ordersTotal = OrdersTotal()-1;
for (int i = _ordersTotal; i >= 0; i--){
if (OrderSymbol() != Symbol() || OrderMagicNumber() != magic) continue;
....
}
in different realization, i.e. you can create a function(String Symbol) that checks if you have some working orders placed of the indicated Symbol.
Old proverb says:Measure twice before cut onceThere are actually four values ( three for pendings ) to check before OrderDelete()
As your definition states to handle { OP_{BUY|SELL}STOP } orders, there are following three items to check:
Symbol() match ( not causing unwanted side-effects by deleting other EA's or manual orders )
OrderType() match ( not ignoring the actual state { PENDING | AT_MARKET } and direction { BUY | SELL } of the order )
OrderMagicNumber() match ( not ignoring the UUID selector utility available to be set for each individual OrderSend() )
So, let's sketch the detection process:
int myEaContextAwareMagicNUMBER = ...;
for ( int ii = OrdersTotal();
ii >= 0;
ii--
)
if OrderSelect( ii, SELECT_BY_POS, MODE_TRADES )
{
if ( OrderSymbol() != _Symbol
&& OrderMagicNumber() != myEaContextAwareMagicNUMBER
&& OrderOpenTime() >= Time[1] // Prev. Bar
&& !( OrderType() == OP_BUYSTOP
|| OrderType() == OP_SELLSTOP
)
) continue; // __^ __^ __^ __^ __^ __^ loop for next test
// -------------------------------------------+
// FINALLY PROCESS THE MATCHING OrderDelete() |
// -------------------------------------------+
...
..
.
// -------------------------------------------+
}
else Print( "WARN: OrderSelect() failed at db.POOL.SELECT(), RECORD_NUMBER == ", ii );
So how to delete un-triggered pendings is done.
Next comes the remark about
"... when the ea is active on the live server, it does not open orders because the platform already has orders of other instruments open."
There could hardly be any advice provided without delivering the exact { GetLastError() | _LastError } values.
Some Brokers for some account types do indeed restrict OrderSend() acceptance policies, and thus besides the GetLastError() value the respective Broker Terms and Conditions apply.
Do not hesitate to ask more & may enjoy other Questions/Answers in MQL4 domain.

LINQ to Entities grouped logical operations in Where

I'm trying to execute the following linq query and it seems to be ignoring order of operations. (Parentheses first)
var result = _repo.Transactions.Where(t =>
t.DateEntered > EntityFunctions.AddDays(DateTime.Now, -7)
&& ( t.TranDesc != "BALANCE FORWARD" && t.Withdrawl == 0 && t.Deposit == 0 ) );
Here's the where clause that generates:
WHERE ([Extent1].[dateentered] > (DATEADD (day, -7, SysDateTime())))
AND (N'BALANCE FORWARD' <> [Extent1].[TranDesc])
AND (cast(0 as decimal(18)) = [Extent1].[Withdrawl])
AND (cast(0 as decimal(18)) = [Extent1].[Deposit])
Any ideas what I'm doing wrong?
EDIT:
This is actually what I wanted and it solved my problem. Just didn't think it all the way though. Thanks.
var result = _repo.Transactions.Where(t =>
t.dateentered > EntityFunctions.AddDays(DateTime.Now, -7)
&& ((t.TranDesc == "BALANCE FORWARD" && (t.Withdrawl != 0 || t.Deposit != 0))
|| (t.TranDesc != "BALANCE FORWARD")));
There is no difference between a && (b && c && d) and a && b && c && d, and that's why parentheses within generated SQL are not exactly the same as in your LINQ query. But at the end there is no difference between these queries.

Uncrustify to align cascaded "if conditions"

the combination of UniversalIndentGUI and Uncrustify works for me very fine and saves me lots of time to format the C source codes.
But I have a small extra question and want to know whether someone can help.
Is it possible to correct the combined if conditions from:
if ( (a > 0)
&& (b > 0)
&& (c > 0))
{
...
}
to
if ( (a > 0)
&& (b > 0)
&& (c > 0)
)
{
...
}
This may help the readability of the codes to some extend if more conditions are embedded together.
Thanks
I doubt there is a setting for that. It would be good idea for uncrustify to offer it.
One not too good of a suggestion would be to define a macro or function:
#define ___(arg) arg
and then you could have:
if ( ___( a > 0 )
&& ( b > 0 )
)
{
}
but be aware the standard reserves macros that start with '_'. See:
What are the rules about using an underscore in a C++ identifier?

Silly question about how you format long if statements

On long if statements where they take up more than one line, do you put the conditions like AND or OR on a new line like this:
if (something
&& something else)
Or like this:
if (something &&
something else)
For complex conditions, consider extracting it into a function or a variable:
if (complexCondition(foo)) { ..
As a bonus, the name of the function or variable can be used to communicate what the condition means. This makes your code easier to read.
I typically do it the second way, since I can line up the statements. However, either way is fine when you're writing code, as long as you're consistent.
I prefer a rendition of the first. My reasoning is that deleting a condition via cut/paste/comment for any testing purposes is easier. It's a lot easier to comment out a line than it is to delete the and from the line above and comment out a line. This is more when I'm doing where clauses in SQL than in an if statement in any other given language, but is similar.
Given my druthers, I'd avoid long if tests in the first place. I'd rather do something like:
bool fTest1 = A == B ;
bool fTest2 = C ;
bool fTest3 = f(1,2,3) ;
bool fSuccess = ( fTest1 | ftest2 ) & fTest3 ;
if ( fSuccess )
...
Otherwise something like this:
if ( A == B
&& ( C == D
|| E == F
)
&& Z > Y
) {
...
}
else
{
...
}
YMMV, of course.
The former is far easier to debug, test, log, etc.
I usually format using the IDE formatter and then rearrange a bit to make it look beautiful.
I'm working in VSC and recently managed to not only write, but make readable very long conditions in nested if statements. Just use brackets and new lines like this. It should make automatic indentations:
foreach ($panstwa as $key => $value) {
if (is_object($value)) {
if (
(
($value->checkbox1 === true) && (is_string($value->panstwoZListy)) && ($value->panstwoZListy !== 'none') && ($value->panstwo === '') && ($value->panstwoZListy !== '')
) ||
(
(
($value->checkbox2 === true &&
($value->checkbox2_1 === true || $value->checkbox2_2 === true || $value->checkbox2_3 === true || $value->checkbox2_4 === true || $value->checkbox2_5 === true || $value->checkbox2_6 === true)
) ||
($value->checkbox3 === true &&
($value->checkbox3_1 === true || $value->checkbox3_2 === true)
) ||
($value->checkbox4 === true &&
(
(
($value->checkbox4_1 === true || $value->checkbox4_2 === true || $value->checkbox4_3 === true || $value->checkbox4_4 === true || $value->checkbox4_5 === true || $value->checkbox4_6 === true || $value->checkbox4_7 === true) && ($value->checkbox4_8 === false)
) ||
(
($value->checkbox4_1 === false && $value->checkbox4_2 === false && $value->checkbox4_3 === false && $value->checkbox4_4 === false && $value->checkbox4_5 === false && $value->checkbox4_6 === false && $value->checkbox4_7 === false) && ($value->checkbox4_8 === true) && (sprawdzRegexTextInput($value->prawnieUzasadnionyInteres)) && (is_object($value->dokumentacjaOceny) || is_string($value->dokumentacjaOceny))
)
)
)
) &&
(is_string($value->panstwo)) && ($value->panstwoZListy === 'none') && ($value->panstwo !== '') && (sprawdzRegexTextInput($value->panstwo)
)
) &&
((is_int($value->panstwoid) && is_numeric($value->panstwoid)) || (is_bool($value->panstwoid) && $value->panstwoid === false)) &&
(is_bool($value->zmiana))
) {
echo "ok";
//nie robiÄ™ nic
} else {
$flagaPanstwa = false;
}
} else {
$flagaPanstwa = false;
}
}

Resources