Conditional / Persistent conditions coding

Post Reply
User avatar
boldtrader
Trader
Posts: 158
Joined: Fri Jun 29, 2012 10:05 pm
Location: Somewhere in the Northeast Kingdom.

Conditional / Persistent conditions coding

Post by boldtrader »

Hi Team, My warmest and deepest appreciation to all the great minds here, active and lurking. And even greater regards to Steve for this great group of talent.

I am in need of some really fresh eyes, as I am a self-taught / self learning newbie on mql4. I am hitting against some minor headwinds, but feeling like I am still sailing upstream. Any help is greatly appreciated.

I am trying to create a conditional loop(?), whereby when Condition B (which is a combination of data 1 + data 2 + data 3 + ... ) == True, then if Condition A == (which is a combination of data 4 + data 5 + data 6 + ... ) == True in look back of (x # of ) bars, on Period (), [&& no other condition (ie: Condition D) has occurred in look back period], then execute Buy trade.

Additionally, as long as Condition B == True, for each future incident of Condition A == True, after initial trade, then Buy.

For the Sell side, whereby when Condition D (combination of data 4 - data 5 - data 6 - .... ) == true, then if Condition C (which is a combination of data 1 - data 2 - data 3 - ... ) == True in look back of (20) bars, on Period (20), [&& no other condition (ie: Condition D) has occurred in look back period), then execute Sell trade.

I am also looking for ability to expanding into multiple conditions / cases.

The following has been setup:

#include <stdlib.mqh>
#include <stderror.mqh>
#include <WinUser32.mqh>

int init()
{
if statements

return(0);
}

int start()
{

if statements

return(0);
}

// Trading code starts here

if (TradeOrAlert) {


double dData1 = iCustom(Symbol(),0,"Indi Name", data, mode, # bars]); // #bars may not be needed?
double dData2 = iCustom(Symbol(),0,"Indi Name", data, mode, # bars]);
double dData3 = iCustom(Symbol(),0,"Indi Name", data, mode, # bars]);
double dData4 = iCustom(Symbol(),0,"Indi Name", data, mode, # bars]);
double dData5 = iCustom(Symbol(),0,"Indi Name", data, mode, # bars]);
double dData6 = iCustom(Symbol(),0,"Indi Name", data, mode, # bars]);
double dData7 = iCustom(Symbol(),0,"Indi Name", data, mode, # bars]);
double dConditionA = (dData1 +dData2) > (dData3 + dData4)
double dConditionB = (dData1 +dData3) > (dData2 + dData5)
double dConditionC = (dData2 +dData4) > (dData2 + dData7)
double dConditionD = (dData1 +dData3) > (dData4 + dData6)

// Condition B
while ( dConditionB == true) {

then

// Condition A

if (dConditionB == true)

{ BUY(Symbol(),B_Symbol_LS_1,B_Symbol_TP_1,B_Symbol_SL_1,B_Symbol_TS_1,"BUY Trade Condition") ;
Print ( data info = "+dCondition A+", "+dCondition B+", "...", " );

// Condition D
While (dConditionD == true){

then

// Condition C

if (dConditionC == true)

{ SELL(Symbol(),S_Symbol_LS_0,S_Symbol_TP_0,S_Symbol_SL_0,S_Symbol_TS_0,"Sell Trade Condition") ;
Print ( data info = "+dCondition C+", "+dCondition D+", "...", " );

}

return(0);
}

=====================================================================
One suggestion I received was to use Global Variables:

If the condition(s) carry through from bar to bar (which they can sometimes do), to set the conditions as global variables.
example:

If (ConditionB== true && ConditionA == true)
//check for orders / open positions
// If nothing, send order
else if (ConditionD == ture && ConditionC == true)
// check for orders / open positions
// if nothing, send order

But this brings me back to the coding of the GV.. and I understand they have to be "reset", which the "reset" will have its own terms/conditions to cause the reset to be true or false.

I am stuck on making this work out and know there is probably a better way to do this, and organize.

I have been reading about global variables, and while it seems that solution may be better, I am not sure how to code that part up.

Any help please?
"Make Your 20, Bank Your Money... WFY II"
My Mission: Help families grow income, protect assets, erase debt, and gain financial independence.[/b]
fxprotrader
Trader
Posts: 26
Joined: Sat Jun 16, 2012 11:26 pm

Re: Conditional / Persistent conditions coding

Post by fxprotrader »

boldtrader wrote:Hi Team, My warmest and deepest appreciation to all the great minds here, active and lurking. And even greater regards to Steve for this great group of talent.

I am in need of some really fresh eyes, as I am a self-taught / self learning newbie on mql4. I am hitting against some minor headwinds, but feeling like I am still sailing upstream. Any help is greatly appreciated.

I a
Any help please?
boldtrader-
Following is a function that I wrote to check multiple conditions. Not sure if this fits into what you're doing but I'm going to throw it out there, maybe something in it will help your situation or at least give you an idea.

Code: Select all

//+------------------------------------------------------------------+
//                                                                   +
//+------------------------------------------------------------------+
bool CheckBarHistory_H1Sell(double MAH0,double MAH1,double H1High1,double H1High0,double H1Low1,double H1Low0){

bool IsTrue = FALSE;
int hit=0;
//if hit remains 0 IsTrue remains false
//this looks for breach of EntryPoint1 and EntryPoint2 
//if any line is breached hit gets set to > 0 - disallowing the trade
     if(H1High1>=MAH1+H1MAEntryPoint1*Poin){hit++;}
else if(H1High0>=MAH0+H1MAEntryPoint1*Poin){hit++;}
else if(H1Low1<=MAH1-H1MAEntryPoint2*Poin) {hit++;}
else if(H1Low0<=MAH0-H1MAEntryPoint2*Poin) {hit++;}
else{IsTrue = TRUE;}

if(hit==0 && Bid < MAH0){
IsTrue = TRUE;
}

 return (IsTrue);
}
User avatar
boldtrader
Trader
Posts: 158
Joined: Fri Jun 29, 2012 10:05 pm
Location: Somewhere in the Northeast Kingdom.

Re: Conditional / Persistent conditions coding

Post by boldtrader »

Thanks for a quick reply fxprotrader. Will give it some thought as to how I may be able to use your suggestion.

Thank you.
"Make Your 20, Bank Your Money... WFY II"
My Mission: Help families grow income, protect assets, erase debt, and gain financial independence.[/b]
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

Re: Conditional / Persistent conditions coding

Post by dietcoke »

boldtrader wrote:I am trying to create a conditional loop(?), whereby when Condition B (which is a combination of data 1 + data 2 + data 3 + ... ) == True, then if Condition A == (which is a combination of data 4 + data 5 + data 6 + ... ) == True in look back of (x # of ) bars, on Period (), [&& no other condition (ie: Condition D) has occurred in look back period], then execute Buy trade.

Additionally, as long as Condition B == True, for each future incident of Condition A == True, after initial trade, then Buy.

You have two problems to solve from what I can see

as well as determining the truth or not of a number of conditions, you also need to hold data for each indicator lookback period. This is best done in arrays.

You can find a good primer on using arrays here http://www.forexfactory.com/showthread.php?t=165411


Eg.

Code: Select all


double dData1[lookbackbars];
double dData2[lookbackbars];
double dData3[lookbackbars];
double dData4[lookbackbars];
double dData5[lookbackbars];
double dData6[lookbackbars];
double dData7[lookbackbars];

bool GetData()
{
	
for(int i=0, i<=lookbackbars; i++)
{
     // Add the data values to arrays 
      dData1[i] = iCustom(Symbol(),0,"Indi Name", data, mode, # bars]);
      dData2[i] = iCustom(Symbol(),0,"Indi Name", data, mode, # bars]);
      dData3[i] = iCustom(Symbol(),0,"Indi Name", data, mode, # bars]);
      dData4[i] = iCustom(Symbol(),0,"Indi Name", data, mode, # bars]);
      dData5[i] = iCustom(Symbol(),0,"Indi Name", data, mode, # bars]);
      dData6[i] = iCustom(Symbol(),0,"Indi Name", data, mode, # bars]);
      dData7[i] = iCustom(Symbol(),0,"Indi Name", data, mode, # bars]);
}
}
I would then write a function for each condition. So, for conditionB which doesnt have any lookback complexity, you can do

Code: Select all

bool IsConditionB(double& Data1[], double& Data2[], double& Data3[], double& Data5[])
{
		//determine if  the condition  is true and return true if it is.
		if((Data1[0] + Data3[0]) > (Data2[0] + Data5[0])) return(true);
		return (false)
}
For the conditions which need to be true ovr the lookback period. you can use something like this

Code: Select all

bool IsConditionA(double& Data1[], double& Data2[], double& Data3[], double& Data4[])
{
	//cycle through the lookback period
	for(int i=0, i<=lookbackbars; i++)
	{
		//determine if  the condition for that lookback bar is false
                //we retyurn false if any bar fails the condition
		if((Data1[i] + Data2[i]) <= (Data3[i] + Data4[i]))
		// if the condition has failed then returnfalse
		return(false);
	}
	//the condition was true for all lookback bars
	return (true)
}

Image
Post Reply

Return to “Coders Hangout”