Functions or Variables to Return a value based on certain parameters

 

Hi All,

 

I dont know if im having a dim moment and over looking something really simple. Im not a big time MQL coder got into this quite recently but am pretty skilled in VBA.

 

Ive build my own EA using some Custom Indicators but these indicators could use some improving so stripped them down to what i need and want to enable what i feel should be a bit of simple code.

 

In VBA if you wanted to get a Boolean result from a formula you would just do a Simple If(xxx) function to get the result but all I seem to get with MQL is using IF statements as logics rather than to set the result of a variable.

 

What im looking to do is something like the following

 bool HighToLow=IF(Ihighest > Ilowest, True,False)

 

Or

If (IHighest > ILowest)
{

bool HighToLow=False

}

Else

{

bool HighToLow=True

}

 

But then that is more a logic element that i dont think works.

 

Anyone out there able to put me straight, racking my brains over this all day. 

 

Thehallster:

bool HighToLow=IF(Ihighest > Ilowest, True,False)

Or

If (IHighest > ILowest){
   bool HighToLow=False
}
Else
{
   bool HighToLow=True
}
  1. learn to code it, or pay (Freelance) someone to code it.
    We're not going to code it for you.
    We are willing to help you when you post your attempt (using SRC) and the nature of your problem.
  2. Both work if you write it in mql4
    bool HighToLow= Ihighest > Ilowest ? true : false;

    //simplified

    bool HighToLow= Ihighest > Ilowest;

    //Or

    bool HighToLow;
    if (IHighest > ILowest){
       HighToLow=False;
    }
    else{
       HighToLow=True;
    }
 

Hey Thanks for the input, I think id just over complicated it to be fair I didnt realise it was as simple as creating the variable as boolean using the formula.

 

what I've done instead of trying to instigate the code into my EA I've edited the Indicator to do what i wanted it to helped me trim down code in the EA that way. But now it wont load to a chart but it will work in Strategy tester and doesnt debug any issues. Thought it was an Init/Deinit issue but it returns a zero value and getting no fault code.

 

Any Ideas, Just combing through it myself but not getting what i need from it atm 

extern double Fibo_Level_1 = 0.236;
extern double Fibo_Level_2 = 0.382;
extern double Fibo_Level_3 = 0.500;
extern double Fibo_Level_4 = 0.618;
extern double Fibo_Level_5 = 0.764;
extern double Fibo_Level_6 = 0.886;
extern int    StartBar     = 0;
extern int    BarsBack     = 160;
extern bool   Pause        = false;
double Fibo_Level_0 = 0.000;
double Fibo_Level_7 = 1.000;
color VerticalLinesColor = Blue;
color FiboLinesColors1 = Red;
color FiboLinesColors2 = Blue;
int lowest_barBB = iLowest(NULL,0,MODE_LOW,BarsBack,StartBar);
int highest_barBB = iHighest(NULL,0,MODE_HIGH,BarsBack,StartBar);
double f_1[];
double f_2[];
double f_3[];
double f_4[];
double f_5[];
double f_6[];
double f_7[];
double f_8[];

int init()
{
  DeleteAllObjects();
  SetIndexBuffer(0,f_1);
  SetIndexBuffer(1,f_2);
  SetIndexBuffer(2,f_3);
  SetIndexBuffer(3,f_4);
  SetIndexBuffer(4,f_5);
  SetIndexBuffer(5,f_6);
  SetIndexBuffer(6,f_7);
  SetIndexBuffer(7,f_8);
  SetIndexLabel(0,"Fibo_"+DoubleToStr(Fibo_Level_0,4));
  SetIndexLabel(1,"Fibo_"+DoubleToStr(Fibo_Level_1,4));
  SetIndexLabel(2,"Fibo_"+DoubleToStr(Fibo_Level_2,4));
  SetIndexLabel(3,"Fibo_"+DoubleToStr(Fibo_Level_3,4));
  SetIndexLabel(4,"Fibo_"+DoubleToStr(Fibo_Level_4,4));
  SetIndexLabel(5,"Fibo_"+DoubleToStr(Fibo_Level_5,4));
  SetIndexLabel(6,"Fibo_"+DoubleToStr(Fibo_Level_6,4));
  SetIndexLabel(7,"Fibo_"+DoubleToStr(Fibo_Level_7,4));
  return(0);
}

int deinit()
{
   DeleteAllObjects();
   return (0);
}

int start()
{
  if(Pause==false) CalcFibo();
  return(0);
}

void DeleteAllObjects()
{
   int objs = ObjectsTotal();
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
   {
      if(highest_barBB < lowest_barBB)
      {
         DeleteAllObjects();
         name=ObjectName(cnt);
         if (StringFind(name,"v_u_hl",0)>-1) ObjectDelete(name);
         if (StringFind(name,"v_l_hl",0)>-1) ObjectDelete(name);
         if (StringFind(name,"Fibo_hl",0)>-1) ObjectDelete(name);
         if (StringFind(name,"trend_hl",0)>-1) ObjectDelete(name);
         WindowRedraw();
      }
      else
      {
         DeleteAllObjects();
         name=ObjectName(cnt);
         if (StringFind(name,"v_u_lh",0)>-1) ObjectDelete(name);
         if (StringFind(name,"v_l_lh",0)>-1) ObjectDelete(name);
         if (StringFind(name,"Fibo_lh",0)>-1) ObjectDelete(name);
         if (StringFind(name,"trend_lh",0)>-1) ObjectDelete(name);
         WindowRedraw();
      }
   }
}

void CalcFibo()
{
  
  //DeleteAllObjects();
  
  int LowBar = 0, HighBar= 0;
  double LowValue = 0 ,HighValue = 0;
  
  int lowest_bar = iLowest(NULL,0,MODE_LOW,BarsBack,StartBar);
  int highest_bar = iHighest(NULL,0,MODE_HIGH,BarsBack,StartBar);
  
  double higher_point = 0;
  double lower_point = 0;
  HighValue=High[highest_bar];
  LowValue=Low[lowest_bar];
  ObjectsDeleteAll();
  
  if(highest_bar < lowest_bar)
  {
        DrawVerticalLine("v_u_hl",highest_bar,VerticalLinesColor);
        DrawVerticalLine("v_l_hl",lowest_bar,VerticalLinesColor);
        
        if(ObjectFind("trend_hl")==-1)
        ObjectCreate("trend_hl",OBJ_TREND,0,Time[highest_bar],HighValue,Time[lowest_bar],LowValue);
        ObjectSet("trend_hl",OBJPROP_TIME1,Time[highest_bar]);
        ObjectSet("trend_hl",OBJPROP_TIME2,Time[lowest_bar]);
        ObjectSet("trend_hl",OBJPROP_PRICE1,HighValue);
        ObjectSet("trend_hl",OBJPROP_PRICE2,LowValue);
        ObjectSet("trend_hl",OBJPROP_STYLE,STYLE_DOT);
        ObjectSet("trend_hl",OBJPROP_RAY,false);
        
        if(ObjectFind("Fibo_hl")==-1)
        ObjectCreate("Fibo_hl",OBJ_FIBO,0,0,HighValue,0,LowValue);  
        ObjectSet("Fibo_hl",OBJPROP_PRICE1,HighValue);
        ObjectSet("Fibo_hl",OBJPROP_PRICE2,LowValue);
        ObjectSet("Fibo_hl",OBJPROP_LEVELCOLOR,FiboLinesColors1);
        ObjectSet("Fibo_hl",OBJPROP_FIBOLEVELS,8);
        ObjectSet("Fibo_hl",OBJPROP_FIRSTLEVEL+0,Fibo_Level_0);   ObjectSetFiboDescription("Fibo_hl",0,DoubleToStr(Fibo_Level_0,4));
        ObjectSet("Fibo_hl",OBJPROP_FIRSTLEVEL+1,Fibo_Level_1);   ObjectSetFiboDescription("Fibo_hl",1,DoubleToStr(Fibo_Level_1,4));
        ObjectSet("Fibo_hl",OBJPROP_FIRSTLEVEL+2,Fibo_Level_2);   ObjectSetFiboDescription("Fibo_hl",2,DoubleToStr(Fibo_Level_2,4));
        ObjectSet("Fibo_hl",OBJPROP_FIRSTLEVEL+3,Fibo_Level_3);   ObjectSetFiboDescription("Fibo_hl",3,DoubleToStr(Fibo_Level_3,4));
        ObjectSet("Fibo_hl",OBJPROP_FIRSTLEVEL+4,Fibo_Level_4);   ObjectSetFiboDescription("Fibo_hl",4,DoubleToStr(Fibo_Level_4,4));
        ObjectSet("Fibo_hl",OBJPROP_FIRSTLEVEL+5,Fibo_Level_5);   ObjectSetFiboDescription("Fibo_hl",5,DoubleToStr(Fibo_Level_5,4));
        ObjectSet("Fibo_hl",OBJPROP_FIRSTLEVEL+6,Fibo_Level_6);   ObjectSetFiboDescription("Fibo_hl",6,DoubleToStr(Fibo_Level_6,4));
        ObjectSet("Fibo_hl",OBJPROP_FIRSTLEVEL+7,Fibo_Level_7);   ObjectSetFiboDescription("Fibo_hl",7,DoubleToStr(Fibo_Level_7,4));
        ObjectSet("Fibo_hl",OBJPROP_RAY,true);
        WindowRedraw();
        
        
        for(int i=0; i<100; i++)
        {
           f_8[i] = NormalizeDouble(LowValue+(HighValue-LowValue)*Fibo_Level_7,Digits);
           f_7[i] = NormalizeDouble(LowValue+(HighValue-LowValue)*Fibo_Level_6,Digits);
           f_6[i] = NormalizeDouble(LowValue+(HighValue-LowValue)*Fibo_Level_5,Digits);
           f_5[i] = NormalizeDouble(LowValue+(HighValue-LowValue)*Fibo_Level_4,Digits);
           f_4[i] = NormalizeDouble(LowValue+(HighValue-LowValue)*Fibo_Level_3,Digits);
           f_3[i] = NormalizeDouble(LowValue+(HighValue-LowValue)*Fibo_Level_2,Digits);
           f_2[i] = NormalizeDouble(LowValue+(HighValue-LowValue)*Fibo_Level_1,Digits);
           f_1[i] = NormalizeDouble(LowValue+(HighValue-LowValue)*Fibo_Level_0,Digits);
        }
  }
  else
  {
        DrawVerticalLine("v_u_lh",highest_bar,VerticalLinesColor);
        DrawVerticalLine("v_l_lh",lowest_bar,VerticalLinesColor);
        
        if(ObjectFind("trend_hl")==-1)
        ObjectCreate("trend_lh",OBJ_TREND,0,Time[lowest_bar],LowValue,Time[highest_bar],HighValue);
        ObjectSet("trend_lh",OBJPROP_TIME1,Time[lowest_bar]);
        ObjectSet("trend_lh",OBJPROP_TIME2,Time[highest_bar]);
        ObjectSet("trend_lh",OBJPROP_PRICE1,LowValue);
        ObjectSet("trend_lh",OBJPROP_PRICE2,HighValue);
        ObjectSet("trend_lh",OBJPROP_STYLE,STYLE_DOT);
        ObjectSet("trend_lh",OBJPROP_RAY,false);


        if(ObjectFind("Fibo_lh")==-1)
        ObjectCreate("Fibo_lh",OBJ_FIBO,0,0,LowValue,0,HighValue);  
        ObjectSet("Fibo_lh",OBJPROP_PRICE1,LowValue);
        ObjectSet("Fibo_lh",OBJPROP_PRICE2,HighValue);
        ObjectSet("Fibo_lh",OBJPROP_LEVELCOLOR,FiboLinesColors2);
        ObjectSet("Fibo_lh",OBJPROP_FIBOLEVELS,8);
        ObjectSet("Fibo_lh",OBJPROP_FIRSTLEVEL+0,Fibo_Level_0);   ObjectSetFiboDescription("Fibo_lh",0,DoubleToStr(Fibo_Level_0,4));
        ObjectSet("Fibo_lh",OBJPROP_FIRSTLEVEL+1,Fibo_Level_1);   ObjectSetFiboDescription("Fibo_lh",1,DoubleToStr(Fibo_Level_1,4));
        ObjectSet("Fibo_lh",OBJPROP_FIRSTLEVEL+2,Fibo_Level_2);   ObjectSetFiboDescription("Fibo_lh",2,DoubleToStr(Fibo_Level_2,4));
        ObjectSet("Fibo_lh",OBJPROP_FIRSTLEVEL+3,Fibo_Level_3);   ObjectSetFiboDescription("Fibo_lh",3,DoubleToStr(Fibo_Level_3,4));
        ObjectSet("Fibo_lh",OBJPROP_FIRSTLEVEL+4,Fibo_Level_4);   ObjectSetFiboDescription("Fibo_lh",4,DoubleToStr(Fibo_Level_4,4));
        ObjectSet("Fibo_lh",OBJPROP_FIRSTLEVEL+5,Fibo_Level_5);   ObjectSetFiboDescription("Fibo_lh",5,DoubleToStr(Fibo_Level_5,4));
        ObjectSet("Fibo_lh",OBJPROP_FIRSTLEVEL+6,Fibo_Level_6);   ObjectSetFiboDescription("Fibo_lh",6,DoubleToStr(Fibo_Level_6,4));
        ObjectSet("Fibo_lh",OBJPROP_FIRSTLEVEL+7,Fibo_Level_7);   ObjectSetFiboDescription("Fibo_lh",7,DoubleToStr(Fibo_Level_7,4));
        ObjectSet("Fibo_lh",OBJPROP_RAY,true);        
        WindowRedraw();
        
        for(i=0; i<100; i++)
        {
           f_1[i] = NormalizeDouble(HighValue,4);
           f_2[i] = NormalizeDouble(HighValue-((HighValue-LowValue)*Fibo_Level_1),Digits);
           f_3[i] = NormalizeDouble(HighValue-((HighValue-LowValue)*Fibo_Level_2),Digits);
           f_4[i] = NormalizeDouble(HighValue-((HighValue-LowValue)*Fibo_Level_3),Digits);
           f_5[i] = NormalizeDouble(HighValue-((HighValue-LowValue)*Fibo_Level_4),Digits);
           f_6[i] = NormalizeDouble(HighValue-((HighValue-LowValue)*Fibo_Level_5),Digits);
           f_7[i] = NormalizeDouble(HighValue-((HighValue-LowValue)*Fibo_Level_6),Digits);
           f_8[i] = NormalizeDouble(LowValue,4);
        }
        
  }
}

void DrawVerticalLine(string name , int bar , color clr)
{
   if(ObjectFind(name)==-1)
   {
      ObjectCreate(name,OBJ_VLINE,0,Time[bar],0);
      ObjectSet(name,OBJPROP_COLOR,clr);
      ObjectSet(name,OBJPROP_STYLE,STYLE_DASH);
      ObjectSet(name,OBJPROP_WIDTH,1);
      WindowRedraw();
   }
   else
   {
      ObjectDelete(name);
      ObjectCreate(name,OBJ_VLINE,0,Time[bar],0);
      ObjectSet(name,OBJPROP_COLOR,clr);
      ObjectSet(name,OBJPROP_STYLE,STYLE_DASH);
      ObjectSet(name,OBJPROP_WIDTH,1);
      WindowRedraw();
   }

}

 

Sorry, I accidently deleted your post when removing the other pointless posts. I have pasted it back again now

 

Just skimming through the code here and a few things jumped out at me:

void CalcFibo()
{
  
  //DeleteAllObjects();
  
  int LowBar = 0, HighBar= 0;
  double LowValue = 0 ,HighValue = 0;
  
  int lowest_bar = iLowest(NULL,0,MODE_LOW,BarsBack,StartBar);
  int highest_bar = iHighest(NULL,0,MODE_HIGH,BarsBack,StartBar);
  
  double higher_point = 0;
  double lower_point = 0;
  HighValue=High[highest_bar];
  LowValue=Low[lowest_bar];
  ObjectsDeleteAll();

Just be aware that ObjectsDeleteAll() can have some unwanted effects on other indicators that might be running on the chart.

This seems a little circular... and you call it in init():

void DeleteAllObjects()
{
   int objs = ObjectsTotal();
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
   {
      if(highest_barBB < lowest_barBB)
      {
         DeleteAllObjects();
         name=ObjectName(cnt);
         if (StringFind(name,"v_u_hl",0)>-1) ObjectDelete(name);
         if (StringFind(name,"v_l_hl",0)>-1) ObjectDelete(name);
         if (StringFind(name,"Fibo_hl",0)>-1) ObjectDelete(name);
         if (StringFind(name,"trend_hl",0)>-1) ObjectDelete(name);
         WindowRedraw();
      }
      else
      {
         DeleteAllObjects();
         name=ObjectName(cnt);
         if (StringFind(name,"v_u_lh",0)>-1) ObjectDelete(name);
         if (StringFind(name,"v_l_lh",0)>-1) ObjectDelete(name);
         if (StringFind(name,"Fibo_lh",0)>-1) ObjectDelete(name);
         if (StringFind(name,"trend_lh",0)>-1) ObjectDelete(name);
         WindowRedraw();
      }
   }
}



 

 
Hey thanks for the input.

Do you think that this would cause it not to load though?

It's worth mentioning I have only adapted some existing code that was already running and recompiling and working.

I think the problem lays at the top. Where I have added the Ihighest/Ilowest function to be the Operator of the First If logic.

The indicator originally had an external true or false element. What I need to do is make this use the Ihighest > Ilowest as true (might have that wrong way around, can't see my code as on my phone)

But since adding that I now can't get it to work.

But the indicator works perfectly as I want it to in strategy tester...............

Indicator was made back in 2010 but if I amend say colours used and recompile it works fine so it's got to be an issue with changing that first logic.
 

To be truthful, I'm just skim reading it (time for bed!) but this a definite no-no: 

extern double Fibo_Level_1 = 0.236;
extern double Fibo_Level_2 = 0.382;
extern double Fibo_Level_3 = 0.500;
extern double Fibo_Level_4 = 0.618;
extern double Fibo_Level_5 = 0.764;
extern double Fibo_Level_6 = 0.886;
extern int    StartBar     = 0;
extern int    BarsBack     = 160;
extern bool   Pause        = false;
double Fibo_Level_0 = 0.000;
double Fibo_Level_7 = 1.000;
color VerticalLinesColor = Blue;
color FiboLinesColors1 = Red;
color FiboLinesColors2 = Blue;
int lowest_barBB = iLowest(NULL,0,MODE_LOW,BarsBack,StartBar);
int highest_barBB = iHighest(NULL,0,MODE_HIGH,BarsBack,StartBar);

double f_1[];
double f_2[];
double f_3[];
double f_4[];
double f_5[];
double f_6[];
double f_7[];
double f_8[];

You shouldn't be trying to get price-related data at that point. Not even really in init(). 

 
Thanks knave, any suggestions of how to use that though.

As this is like the first thing it does in order to draw the Fib scales. Could I get away with adding like a sleep value after the Init deinit code, then chuck this in before the If logic?
 

Where im at with this now is that its the code as a whole is the issue.

 

this compiles but doesnt run because the code was written in old language on an old build so got a non modified version re-compiled and get uninit reason 8 error.

 

any ideas how to fix or complete restructuring of the code required? 

 

It could certainly do with some tidying up / updating. What exactly is your goal with this?

A large chunk of that code is to do with creating and deleting objects, but in your first post you indicated you just wanted to streamline it for use in an EA. In which case, you probably only need the buffers and can dispense with the objects altogether?

 
honest_knave:

It could certainly do with some tidying up / updating. What exactly is your goal with this?

A large chunk of that code is to do with creating and deleting objects, but in your first post you indicated you just wanted to streamline it for use in an EA. In which case, you probably only need the buffers and can dispense with the objects altogether?

Hey Knave,

 

Basically this indicator draws a dynamic fib scale on the chart using the BarsBack input to define what you would expect how many bars back to use.

 

In My EA, Based on Stochastic and RSI it places a Buystop or Sell Stop at the Fib Level relevant to where the current market price is. its working well at the moment on H4 but I have a couple of problems.

I need to trim down the amount of code in the EA so it runs quicker and sleeker. at the moment I have to run 2 sets of Fib Scales 1 for Sells and 1 for Buys.

I also then on a trade open or close the EA takes a screenshot and emails it as a signal that I share.

So both elements are required.

Visually it needs to make sense I have attached a working (attachable) version. The issue looks like its to do with the init/Init/OnInit bit returning a non value but within that code its giving me errors about StackOverflow problems.

 In the attached examples Im now getting it working and visually it works is doing exactly what i want it to do delete the old lines flip the scale to the correct way and colours for buys and sells. but in the data window i get no values for by buffers. Ive Also attached the original Fibos V5 indicator I have been using. Feels like im almost there with it but must be a code age issue with this thats all I can think of.

No Values With Values