New to programming

Pipperazzi, I don’t quite understand your question. Price will be the same on all time frames for the same pair…

Can you describe what you would like to see?

LethalXxXDose,

Check out this code which does the same thing.

You will see that using mathabs, you can avoid having to differentiate between a buy and a sell and that simplifies the code. This code is all round a bit longer but it is far more comprehensive in that it will deal with mini accounts as well. It also ensure that the tradesizes are within the broker limits.

Note the comment where I provided an alternative way of checking the tradesizes are within limits using the mathmax and mathmin functions.

I used AccountEquity, but you can use balance just as easily if you prefer.

Yup, I still trade with MB Trading. They have some problems from time to time, but I find that they are good for scalping. With respect to your 4 digit return of quotes, I seem to recall having seen this before. Just try your code on an alternative broker.

int
   Digit_Factor = 1,
   LotDigits = 1;

int init()
{
   if (Digits == 3 || Digits == 5)Digit_Factor = 10;
   if (MarketInfo(Symbol(),MODE_LOTSTEP) == 0.01)LotDigits = 2;
}

int start()
{
   double Lots = PTL(5);
   Print("Lotsize = " + DoubleToStr(Lots,LotDigits));
}
//+------------------------------------------------------------------+
double PTL ( double risk)
{
double 
   One_Tick = MarketInfo(Symbol(),MODE_TICKVALUE) * Digit_Factor, //determine the $ value of 1 tick
   internalstop = MathAbs(Open[1] - Close[1])/2,                  //stoploss in pips
   Risk_In_Money = (internalstop/Point/Digit_Factor) * One_Tick,  //stoploss in $
   tradesize = NormalizeDouble(((AccountEquity() * risk/100)/Risk_In_Money),LotDigits);
   if (tradesize > MarketInfo(Symbol(),MODE_MAXLOT))tradesize = MarketInfo(Symbol(),MODE_MAXLOT);//make sure tradesize is within broker min & max
   if (tradesize < MarketInfo(Symbol(),MODE_MINLOT))tradesize = MarketInfo(Symbol(),MODE_MINLOT);
   //tradesize = MathMax(MarketInfo(Symbol(),MODE_MAXLOT),MathMin(tradesize,MarketInfo(Symbol(),MODE_MINLOT)));Max & min could be done like this too
//----
   return(tradesize);
}

Ahhh, you opened me up to a whole new world of syntax :). Thanks again, I’ll send you a copy of my program to check out if I ever get it where I want it.

Edit – It’s not working correctly though :S Switching out my old PTL function with yours makes a divide by zero error come up. Any ideas?

Ah, there could be a couple of reason, but off the top of my head, is the Digit_Factor variable declared at a global level?

Its the only var that could go wrong, I think.

  • If you have compiling errors double click the error and it will show you where it is in your code (most of the time)

Good contribution, SDC! I would like to put this list together to help take out frustration for those who are just starting out programming. Anyone is welcome to add in helpful tips or universal truths about MT4 programming.

Kenny, I declared Digit_Factor up with all my external variables. This is where global variables go, right?

–Edit-- I don’t have time to check right now, but i think internalstop may be the issues. Doji’s and very small movements will kill it.

You need a template to start coding. It gives you the right structure. Try an expert advisor builder. Here the ones I like:

sufx.core.t3-ism.net/ExpertAdvisorBuilder
molanis.com

LethalXxXDose are you a member of mql4.com forum ? Its a wealth of information that reminds me I have another tip to add to your list I read an article about this on that forum.

-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

  • If you have compiling errors double click the error and it will show you where it is in your code (most of the time). SDC

  • If you have the dreaded “end of program/enequal parenthesis” error which wont show you where it is by clicking on the error icon, use / and / to “comment out” whole blocks of code even whole functions and recompile to use a process of ellimination to pin down where the error is. Article Here. SDC

Pls, i need to contact someone who can write an EA for me (for free). If you know any good programmer (or if you can help), be kind to link me…
Thanx.

If you have the dreaded “end of program/enequal parenthesis” error which wont show you where it is by clicking on the error icon, use / and / to “comment out” whole blocks of code even whole functions and recompile to use a process of ellimination to pin down where the error is. Article Here. SDC

Do a google search for Notepad2. Its the best tool out for finding those missing brackets.

Hi,
I just want to know… Can expert advisors trade offline or they have to be connected to the internet to trade?

Thanks.

Try fxalgotrader - google them. they do configurable pivot systems, indicators and custom EA development work. They’re a bit choosy what they code up though - if it’s got any merit they’ll do it - if it’s rubbish they won’t!

Pipperazzi,

Yes, you must be connected to the internet and the EA must be running in order for it to work. If you do not want to constantly have your computer on, research into VPS’s. I cannot recommend any, however, as I have no experience using them.

Tarrentino & LethalXx, many thanks to you guys.