New to programming

Hey everyone! This is my formal greeting/required newbie question . I’ve been working through the Coders’ Guru guide and it’s making sense, although I wouldn’t say it’s all sticking. Most things I recognize and can put the pieces together, but there is one thing that isn’t clicking. It’s in the “My First EA” section and it goes something like this…

int Crossed (double line1 , double line2)
{
static int last_direction = 0;
static int current_dirction = 0;

if(line1>line2)current_dirction = 1; //up
if(line1<line2)current_dirction = 2; //down

if(current_dirction != last_direction) //changed
{
last_direction = current_dirction;
return (last_direction);
}
else
{
return (0);

This makes sense, basically what I’m taking from it is it tells the program what we see visually, a change in position (or not) of two lines relative to each other. The issue I’m having is that line1 and line2 are not declared anywhere else. The next instance Crossed is used is here…

int isCrossed = Crossed (shortEma,longEma);

What I think is happening is that the first section of code I showed is just a set up for a formula. line1 and line2 are not actually variables, they are placeholders for variables, if that makes sense. It’s more for setting Crossed up as a function, I suppose, and that you must use a double data type. Is that correct? Thank you in advance.

You need more education on basic programming structure. In particular you need to learn more about functions. In the code shown, line1 and line2 are parameters to the function Crossed and are declared as doubles. When calling Crossed, you must supply actual values or variables for these parameters. Pretty standard programming practice.

Thank you for the help, but I think I may have left a bit of important information out :o. shortEma and longEma are variables, I left that part out. I think what I am struggling with the most is that line1 and line2 seem to be placeholders for variables or values, correct? So if I call Crossed like so…

Crossed ( shortEma , longEma )

I think what happens in this example is in the function all of line1 would be replaced with shortEma and line2 would be replaced with longEma and Crossed is calculated. I just started learning about a week ago, so I appreciate all the help I can get. Thanks again.

I didn’t realize you only had a week’s worth of programming. Perhaps you moving a bit fast - functions are something to learn later on. There is lots to learn before that. Make sure you take the tutorials at mql4.com.

You are mostly right, I just don’t like the use of the word “placeholder”. The proper term is “parameter”.

I’ve been reading it like it’s my job and programming isn’t completely foreign to me, I’ve just been using the “throw it at a wall and see what sticks” method until now. The guide I’ve been using goes a bit quick and some of it is pretty broken English so another reference will be helpful. I appreciate your help, happy trading :slight_smile:

I’m not understanding one thing, I thought in this example I did define ticket, but it says I didn’t. I pulled this segment out of another EA and tweaked it so I’m not sure why it’s giving me errors? Help is always appreciated.

//----Should an order be opened?--------------------------------------------------------------------

int ticket, TakeProfit;
int opentrade = Crossdir(Bid,0.0025) ;

int Lots = PTL( PercentRisk , Stoploss ) ;

TakeProfit = 15 ;

int total = OrdersTotal();
if(total < 1)

 {
   if(opentrade == 2)
     {
        ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-Stoploss*Point,Ask+TakeProfit*Point,"My EA",12345,0,Green);
        if(ticket&gt;0)
          {
           if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
           Print("BUY order opened : ",OrderOpenPrice());
          }
        else Print("Error opening BUY order : ",GetLastError()); 
        return(0);
     }
   if(opentrade == 1)
     {

        ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+Stoploss*Point,Bid-TakeProfit*Point,"My EA",12345,0,Red);
        if(ticket&gt;0)
          {
           if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
          }
        else Print("Error opening SELL order : ",GetLastError()); 
        return(0);
     }
     return(0);
 }

It’s telling me “ticket - variable not defined” error, and I’m at a loss. Thanks in advance!


Hi LethalXxXDose,

Can you please zip your mq4 file and post that instead.

It’s impossible to see anything from the picture you have posted.

I may be able to help but will need to test the actual code myself to suggest improvements/changes.

You haven’t defined init() and start().

mql4 requires

int init()
{

init chart here

}

int start()
{

program goes here

}

just find the most basic program you can find then you’ll see how it works.

This is the short version of what I have and I had to post it as a notepad version since it wasn’t showing up in my experts folder (i can open and save to it in metaeditor?)

int init()
{ // left blank because I don’t think I need anything here
}
int deinit()
{
// a couple functions I wrote here
}
int start()
{
int ticket; \error
double Lots = PTL( PercentRisk , Stoploss ) ;
int opentrade = Crossdir(Bid,0.0025) ;
int TakeProfit = 15 ;
int total = OrdersTotal();

//---- Filter section---------------------------

//--------------------------------------------

//----Should an order be opened?---------------------------------
if(total < 1)

 {
   if(opentrade == 2)

and then continues on where my previous post started. What I don’t understand is why I’m getting the error “‘ticket’ - variable not defined” pop up on ‘ticket’ when I’m just trying to define it as an integer.

Disregard, I had AccountFreeMargin in one of my functions that was missing a (), either that or some other tweaking fixed it.

i have to say programming is pretty hard. tried it myself once, but then just gave up.

I’ve got version 0.1 up, it loses money as of now, but I’m just glad it does what I want it to do at this point :). Need to add filters and order modifications still. Here are some helpful tips I’ve discovered while coding.

-Actually writing it out on paper beforehand and going through all the decision making processes and writing pseudocode helps at the beginning. At the top of your paper, put “What I want to put into the code ==> EA ==> What I want out of the code”. That saves you from trying to formulate your plan while coding. You want a direction when you code.

-Work one step at a time. Working on multiple parts of the code at once will get you lost easily.

-Use comments. A lot. It saves you frustration later.

-Get an EA from wherever you please and bring it up in another editor window. It helps to see how they did it and you can probably pull some lines out of it.

-If you are getting compiling errors, print out your variables. You can check the journal tab later to see where the breakdown is.

-If you think there is probably a function or variable built in, look for it. More than likely it exists.

-Make sure your data types are correct. One wrong one will be hard to track down.

If anyone has any more suggestions for beginners, post them here!!

LethalXxXDose

line1 and line2 are variables, they are variables designated to hold the input parameters for the function to work on, you can pass any two values to that function, In this case the two parameters are the vaules of shortEma and longEma.

It will put them into the two variables, line1 and line2 then check to see which one is the bigger of the two.
If line1 is bigger it puts a value of 1 into the variable current_direction
If line 2 is bigger it puts a value of 2 into it.

It then compares the variable current_direction with the variable last_direction to see if it has changed since the last time it checked, if it did change it then changes last_direction to the same value as current_direction so it will be ready for next time ( last_direction and current_direction are static variables this means they hold the values they have untill next time)

The function then returns the value of last_direction to isCrossed

The line int iscrossed = Crossed (shortEma,longEma) is the call to the Crossed function telling it to use the values of shortEma and longEma as its two input values, presumably because isCrossed needs to know if one of them got bigger than the other since the last time it checked.

Thats a pretty good function for checking if two lines have crossed or not the only drawback to it I can see is it can only be used to check the same two lines every time because if you use it to check a different pair of lines the static variable last_direction would be holding the value of the result from the previous pair of lines so the function could return innacurate results for the new pair of lines

Please, is there an indicator which can time the formation of candlesticks (so that for a 5min candle for example, a timer starts counting/ticking at the beginning, and after 5min, we are sure that the candlestick will close; and another will start)?

Here you go :wink:

Candle Timer.zip (687 Bytes)

Iron heart,
Thanks for your help. You’re a good guy

Anyone have any ideas on how to identify the last down or up candle?Say 4 bars ago was up but the last 3 were down, I would like to identify it’s open/close/low/high. I’m thinking a for loop is the answer, but I’m not too familiar with them. Thanks in advance.

LethalXxXDose

   int i = 0;
   while (Open[i] > Close[i])i++;
   Print("Last Bull Candle is Candle no " + i + "/High = " + High[i] + "/Low = " + Low[i] + "/Open = " + Open[i] + "/Close = " + Close[i]);

hello everyone,
please i would like to have about four time intervals on one chart… to indicate price; can someone help me with that?

thank you

Thanks for the help kenny :). I see how that works I think. I saw you over on MBTrading forums, do you still trade with them? I’ve run into an issue with their 5 digit MT4 conversion, but I don’t know if it’s just because I’m using back data. Using Open and close seems to be giving back 4 digit quotes. I’m running into issues with that when it comes to programming lot size based off of an extern percentrisk and AccountBalance. I’d like for my EA to automatically adjust lot size so this is what I came up with…

double PTL ( double risk, int ordertype) //2=buy,1=sell --NORMALIZE!!
{
double internalstop;
double w;
double totalrisk = (AccountBalance()*risk);

  if (ordertype==2)
     {
        internalstop=(((Open[1] - Close[1])/2)*10000); //pairdependant
        
        
     }         
  if (ordertype==1)
     {
        internalstop=(((Close[1] - Open[1])/2)*10000);  //pairdependant
     }
  w = (totalrisk/internalstop); //gives me the price each pip needs to be worth
  if (w&lt;0.1) {w=0.1;}
  return(w);

}