In this thread, we will collect and discuss useful code snippets/functions that we can incorporate in Expert Advisors to make coding process faster and easier:good:
Printable View
In this thread, we will collect and discuss useful code snippets/functions that we can incorporate in Expert Advisors to make coding process faster and easier:good:
Here is a code which you can use in an Expert Advisors to automatically adjust variables depending on if the Broker is 4 digits or 5 digits pricing.
following code should be inside the int Init() function
Code:if(Digits==5 || Digits==3){
StopLoss= Stoploss * 10; // this adjusts Stoploss variable for 5digit broker
}
Following is a custom function which when called will return total of active trades for a particular magic number:
Code:int CountAllTrades(int magic) {
int c=0;
for (int j=OrdersTotal()-1;j>=0;j--)
{
OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic) c++;
}
return(c);
}
This function will close All open trades for a particual MagicNumber of EA.
Code:void CloseAll()
{
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
if (OrderSymbol()==Symbol() && (OrderMagicNumber() == MagicNumber))
{
//-- Close open BUYs
if (type == OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,CornflowerBlue);
//-- Close open SELLS
if (type == OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,CornflowerBlue);
}
}
return;
}
If you have request for any specific code function, please feel free to post your request here and will that code posted to you here in this thread.
Of if you have own own custom codes and you think will benefit other EA programmers, please feel to post it here for the benefit of all.
Here is the code function that will return the chart period in the nicely formation text line.
Example: (if EA is running on 1hour chart)
chartperiod = periodToString(Period()) // will return 1 Hour
Code:string periodToString(int tf)
{
string tfString;
switch (tf)
{
case 1: tfString = "1 Min"; break;
case 5: tfString = "5 Min"; break;
case 15: tfString = "15 Min"; break;
case 30: tfString = "30 Min"; break;
case 60: tfString = "1 Hour"; break;
case 240: tfString = "4 Hour"; break;
case 1440: tfString = "Daily"; break;
case 10080: tfString = "Weekly"; break;
case 40320: tfString = "Monthly"; break;
default: tfString = "Unknown";
}
return (tfString);
}
Here is the Code that you can put in any EA to take a screenshot of the Chart and save it to disk, you can call this on any event like if there is any new trade taken, you can save the screenshot.
the system function to take screenshot is:
Code:WindowScreenShot(filename,size_x,size_y)
Here is the example usage:
Code:string filename= "Chart"+Symbol();
WindowScreenShot(filename,570,428)
The above will save the chart with name "ChartEURUSD". You can make the filename very dynamic so every time screenshot is taken, it would save it with different name.
Here is a code which can be saved as a script file in the Scripts Folder.
To execute the script, drage and drop the script on the current chart, this will close all open trades and also pending orders of all symbols.
Code:int start()
{
double total;
int cnt;
while(OrdersTotal()>0)
{
// close opened orders first
total = OrdersTotal();
for (cnt = total ; cnt >=0 ; cnt--)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
switch(OrderType())
{
case OP_BUY :
RefreshRates();
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Violet);break;
case OP_SELL :
RefreshRates();
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,Violet); break;
}
}
}
// and close pending
total = OrdersTotal();
for (cnt = total ; cnt >=0 ; cnt--)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
{
switch(OrderType())
{
case OP_BUYLIMIT :OrderDelete(OrderTicket()); break;
case OP_SELLLIMIT :OrderDelete(OrderTicket()); break;
case OP_BUYSTOP :OrderDelete(OrderTicket()); break;
case OP_SELLSTOP :OrderDelete(OrderTicket()); break;
}
}
}
}
return(0);
}
Here is code function which can be used say when ea needs to trade only during certain hours of the day and skip of certain day of the week :
Code:bool ValidTime()
{
if (DayOfWeek()==1 && Hour()<=6) return(false);
return(true);
}
Here is the code fuction which will return the total number of lots which are currently running in open trades :
Parameters: key is magic number, and type is the type of trades .
Code:double gettotallot(int _key, int type)
{
double _lot=0;
for(int k=OrdersTotal(); k>=0; k--)
{
if (OrderSelect(k,SELECT_BY_POS,MODE_TRADES)&& OrderSymbol()==Symbol()&& OrderMagicNumber()==_key && OrderType()==type)
_lot+=OrderLots();
}
return(_lot);
}
Thank you for you show the codes here.Now I just wonder whether you have time to teach me to write the codes.Maybe I should ask you will you teach me these codes there?I wish I can manage the code myself.
great thread, can you make an EA code based on the intersection of the RSI indicator (14, close) with Simple MA indicator (3, previous indicators data). I have tried making it but it does not go as expected because of an error writing the code for Simple MA indicators (3, previous indicators data).
can you make a code for me. i want entry market automatically just when US session. Open BUY when the price above Open Market and Open Sell when the price below Open Market. Stop lose 20 and there is no profit target just close when US session end. thanks
nice tread.
Greetings ....
I want to ask a few examples in MQL4 script, how do I know what the pair are still OP wrote.
thanks before
It's bollinger Brand MQL Code
//+------------------------------------------------------------------+
//| Bollinger MA Price.mq4 |
//| Paladin80 |
//| forevex@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Paladin80"
#property link "forevex@mail.ru"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 LightSeaGreen
#property indicator_color2 LightSeaGreen
#property indicator_color3 LightSeaGreen
//---- indicator parameters
extern int BandsPeriod=20;
extern int BandsShift=0;
extern double BandsDeviations=2.0;
extern int MA_method=0;
extern int Applied_price=0;
bool error=false;
/* MA_method: 0 - Simple moving average,
1 - Exponential moving average,
2 - Smoothed moving average,
3 - Linear weighted moving average.
Applied_price: 0 - Close price,
1 - Open price,
2 - High price,
3 - Low price,
4 - Median price,
5 - Typical price,
6 - Weighted close price, */
//---- buffers
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];
I am new in forex .all mention so that helpful for me!! i don't understand u . thanks for this post
---------- Post added at 03:47 PM ---------- Previous post was at 03:46 PM ----------
oh yes I was now save all code and trying to use.:happy:
nice tread.
Greetings .
What is usefull of mql. I not know about this usefull. Please post a thread about use of this mql system. We are helpfull to your importent post.
i have own custom codes and you itinhk will benefit other ea programers ,please feel to post it here for the benefit of all,
very useful thread. can you please give me a website to learn the basics of MQL. So that i can write my own codes and try them out. then i will able to use the codes mention in the thread too. If you have a ebook please share it
Hello friends, Nice thread. I am not a coder, I have a custom indicator that I want to add alerts. help me how to create an alert to my custom indicator . please post code here for make an alert.
nice topic...
i am a coder it may be very helpful for me...
i dont know mql though...but it will take sometime...
hi all,
it is a very nice thread. it is useful to any trader, please dont stop posting
यदि आप किसी भी विशिष्ट कोड समारोह के लिए अनुरोध किया है, तो यहां आपके अनुरोध पोस्ट करने के लिए स्वतंत्र महसूस कृपया और कोड है कि आप के लिए तैनात किया जाएगा इस धागे में यहाँ.
यदि आप अपने स्वयं के कस्टम कोड है और आपको लगता है कि अन्य EA प्रोग्रामर को लाभ होगा, यह सभी के लाभ के लिए यहाँ पोस्ट करने के लिए महसूस कृपया.
/ एनएसई एमसीएक्स realtime डेटा चार्ट | | EA कोडिंग
if u really serious to learn mql4 just read this book at this website http://book.mql4.com/ ucan downloade there and if u finish reading it that u can make ure ea
Dear agr ap mql k code snip karna chahty to to google ap ke help kary ga. Aor ap mql sekhnay k liya youtube py video tutorial dakho aur us ke jitne kar sako paractice kro taky ap jaldi sekh sako.
can you create a signal for me personally. I'd prefer gain access to market place routinely just as soon as people period. Wide open obtain if your price tag above Wide open Market and also Wide open Advertise if your price tag down below Wide open Market. stop loss 40 and also there's no earnings targeted just close as soon as people period end. when compared with.
Hi, i just learn to make expert advisor myself. my first ea are made by ea builder. it is easy but there is one problem. my ea can run but only open one position. i want it too open based on rule and no matter if there is already open position in there. can you share the script to add about this ? thankyou verymuch
MQL4 ya kisi v MQL k bre mian janna ho tu ap google main search kar hr help le skte ho ya phir youtube ki v because mujy is k bre main khuch zyda knowledge nahi he,agar kisi bahi k pas ho tu plz mujy detail se bta de is k bre main.
not at all understand the same with using MQL5 program until now so I will just use the mt4 trading only as necessary in my
and I'm not going to do a lesson in making the MQL5 program because it will make me be a busy themselves
help me is this code already right, i want to find signal for stochastic crossing but still error message.
PHP Code:
int QnSignal()
{ int result,SigStoch double Mystoch1 = iStochastic(NULL, 0, Myperiod, Dperiod, slowing, method, Pfield, MODE_MAIN, 1);
double Mystoch2 = iStochastic(NULL, 0, Myperiod, Dperiod, slowing, method, Pfield, MODE_MAIN, 2);
if (Mystoch2 > H_level && Mystoch2 > Mystoch1 && Mystoch1 < H_level) SigStoch = 2;
if (Mystoch2 < L_level && Mystoch2 < Mystoch1 && Mystoch1 > L_level) SigStoch = 1;
great thread, can you make an EA code based on the intersection of the RSI indicator (14, close) with Simple MA indicator (3, previous indicators data). I have tried making it but it does not go as expected because of an error writing the code for Simple MA indicators (3, previous indicators data).
Thank you for you show the codes here.Now I just wonder whether you have time to teach me to write the codes.Maybe I should ask you will you teach me thes
codes thereyar yua mq 4 code ka ha muaja ot es code ka bar main ko pata nahi ha yar ya to main es forum main abi newbie hoon es lya ya forum sa mq4 ka bara main nahi bata sakta hon
elsya main
Here is the code function that will return the chart period in the nicely formation text line.
Example: (if EA is running on 1hour chart)
chartperiod = periodToString(Period()) // will return 1 Hour
-----------------------------------------------------------------------------------------------
string periodToString(int tf)
{
string tfString;
switch (tf)
{
case 1: tfString = "1 Min"; break;
case 5: tfString = "5 Min"; break;
case 15: tfString = "15 Min"; break;
case 30: tfString = "30 Min"; break;
case 60: tfString = "1 Hour"; break;
case 240: tfString = "4 Hour"; break;
case 1440: tfString = "Daily"; break;
case 10080: tfString = "Weekly"; break;
case 40320: tfString = "Monthly"; break;
default: tfString = "Unknown";
}
very useful thread. can you please give me a website to learn the basics of MQL. So that i can write my own codes and try them out. then i will able to use the codes mention in the thread too.
If you have can you create a signal for me personally. I'd prefer gain access to market place routinely just as soon as people period. Wide open obtain if your price tag above Wide open
Market and also Wide open Advertise if your price tag down below Wide open Market. stop loss 40
nice thread, will you build an ea code utilizing intersection on your rsi indicator ( 14, shut ) with simple ma indicator ( 3, previous indicators information ). i've tried creating it other then it doesn't go as expected as a result of an error writing the code for simple ma indicators ( 3, previous indicators information ).
Here is a code which you can use in an Expert Advisors to automatically adjust variables depending on if the Broker is 4 digits or 5 digits pricing.