![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Attached mql4 code, please convert to mql5 code, regards:
-------------------------
#property copyright "Copyright © 2010, rulefxoneday"
#property link "fxpartrocks@gmail.com"
extern double minlots=0.05;
extern double maxlots=0.10;
extern string Symbol1 = "XAUUSD";
extern string Symbol2 = "XAGUSD";
extern string Symbol3 = "WTI";
extern double smalldollars=2.5;
extern double bigdollars=20;
extern int smallmagic=11;
extern int bigmagic=22;
bool basketdollars=true; // enable basket dollars
double smallbuyorderprofit,smallsellorderprofit,bigbuyorderprofit,bigsellorderprofit;
extern int CorPeriod =5;
bool runnable=true;
bool init=true;
bool result;
double pBid, pAsk, pp;
int i=0;
datetime timeprev=0;
double pt;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{ // Find the correltion coefficient between EUR USD & USD CHF
bool bulls=false;
bool bears=false;
double correlationOne=0;
double correlationSecond=0;
double correlationThree=0;
bool buyHedge=0;
bool sellHedge=0;
Print("change EU,UCHF,GU:",Change(Symbol1),Change(Symbol2),Change(Symbol3));
if (Change(Symbol1) > 0.1 && Change(Symbol3) > 0.1 && Change(Symbol2) < -0.1) {bulls = true;}
if (Change(Symbol1) < -0.1&& Change(Symbol3) > 0.1 && Change(Symbol2) > 0.1) {bulls=true;}
if (Change(Symbol1) < -0.1 && Change(Symbol3) < -0.1 && Change(Symbol2) > 0.1 ) {bears = true;}
if (Change(Symbol1) > 0.1 && Change(Symbol3) < -0.1 && Change(Symbol2) < -0.1) {bears =true;}
correlationOne=Cor(Symbol1,Symbol2);
Print("Correlation of "+Symbol1+" and "+Symbol2+" = ",correlationOne);
// Find the correltion coefficient between EUR USD & GBP USD
correlationSecond=Cor(Symbol1,Symbol3);
Print("Correlation of "+Symbol1+" and "+Symbol3+" = ",correlationSecond);
correlationThree=Cor(Symbol3,Symbol2);
Print("Correlation of "+Symbol3+" and "+Symbol2+" = ",correlationThree);
if (correlationOne <= -0.5 && correlationSecond >= 0.5 && correlationThree <= -0.4 && CountOrdersThisPairSmallBUY() == 0 && Hour() >= 0 && Hour() <= 24 && bulls == true) {
OrderSend(Symbol1,OP_BUY,minlots,Ask,3,0,0,"Micro Magic",smallmagic,0,Green);
OrderSend(Symbol3,OP_BUY,minlots,Ask,3,0,0,"Micro Magic",smallmagic,0,Green);
OrderSend(Symbol2,OP_BUY,maxlots,Ask,3,0,0,"Micro Magic",smallmagic,0,Green);
if (CountOrdersThisPairBigSELL() == 0 && Hour() >=11 && Hour() <=19 && smallbuyorderprofit <= -(AccountBalance() *0.06) ) {
OrderSend(Symbol1,OP_SELL,minlots,Ask,3,0,0,"Micro Magic",bigmagic,0,Green);
OrderSend(Symbol3,OP_SELL,minlots,Ask,3,0,0,"Micro Magic",bigmagic,0,Green);
OrderSend(Symbol2,OP_SELL,maxlots,Ask,3,0,0,"Micro Magic",bigmagic,0,Green);
}
}
if (correlationOne >= -0.5 && correlationSecond <= 0.5 && correlationThree >= -0.4 && CountOrdersThisPairSmallSELL() == 0 && Hour() >= 0 && Hour() <= 24 && bears == true) {
OrderSend(Symbol1,OP_SELL,minlots,Ask,3,0,0,"Micro Magic",smallmagic,0,Green);
OrderSend(Symbol3,OP_SELL,minlots,Ask,3,0,0,"Micro Magic",smallmagic,0,Green);
OrderSend(Symbol2,OP_SELL,maxlots,Ask,3,0,0,"Micro Magic",smallmagic,0,Green);
if (CountOrdersThisPairBigBUY() == 0 && Hour() >=11 && Hour() <=19 && smallsellorderprofit <= -(AccountBalance() *0.06) )
{
OrderSend(Symbol1,OP_BUY,minlots,Ask,3,0,0,"Micro Magic",bigmagic,0,Green);
OrderSend(Symbol3,OP_BUY,minlots,Ask,3,0,0,"Micro Magic",bigmagic,0,Green);
OrderSend(Symbol2,OP_BUY,maxlots,Ask,3,0,0,"Micro Magic",bigmagic,0,Green);
}
}
if(basketdollars){
smallbuyorderprofit=0;
smallsellorderprofit=0;
bigbuyorderprofit=0;
bigsellorderprofit=0;
}
if(OrdersTotal()>0){
for(i=0;i<=OrdersTotal();i++){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY && OrderMagicNumber()== smallmagic){
if(basketdollars) smallbuyorderprofit=smallbuyorderprofit+ OrderProfit();
}
if(OrderType()==OP_SELL && OrderMagicNumber()== smallmagic){
if(basketdollars) smallsellorderprofit=smallsellorderprofit+ OrderProfit();
}
if(OrderType()==OP_BUY && OrderMagicNumber()== bigmagic){
if(basketdollars) bigbuyorderprofit=bigbuyorderprofit+ OrderProfit();
}
if(OrderType()==OP_SELL && OrderMagicNumber()== bigmagic){
if(basketdollars) bigsellorderprofit=bigsellorderprofit+ OrderProfit();
}
}
}
if(basketdollars) {
if (smallbuyorderprofit>=smalldollars) { closeallsmallbuyorders();
}
if (smallsellorderprofit>=smalldollars) { closeallsmallsellorders();
}
if (bigbuyorderprofit>=bigdollars) { closeallbigbuyorders();
}
if (bigsellorderprofit>=bigdollars) { closeallbigsellorders();
}
}
return(0);
}
double Change(string Pair)
{
double TemporaryArray[][6];
int OpenHour = 7;
int OpenMinute = 30;
ArrayCopyRates(TemporaryArray, Pair, PERIOD_M30);
int BarPositionBegin = 0;
for(int i=0;i<1440;i++)
{
if(TimeHour(TemporaryArray[i][0]) == OpenHour && TimeMinute(TemporaryArray[i][0]) == OpenMinute)
{
BarPositionBegin = i;
break;
}
}
double PercentChange = (TemporaryArray[0][4] * 100) / TemporaryArray[BarPositionBegin][1];
double RealChange = PercentChange - 100;
return (RealChange);
}
int CountOrdersThisPairSmallBUY() {
int l_count_0;
for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
OrderSelect(l_pos_4, SELECT_BY_POS);
if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == smallmagic) l_count_0++;
}
return (l_count_0);
}
int CountOrdersThisPairSmallSELL() {
int l_count_0 = 0;
for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
OrderSelect(l_pos_4, SELECT_BY_POS);
if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderMagicNumber() == smallmagic) l_count_0++;
}
return (l_count_0);
}
int CountOrdersThisPairBigBUY() {
int l_count_0;
for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
OrderSelect(l_pos_4, SELECT_BY_POS);
if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == bigmagic) l_count_0++;
}
return (l_count_0);
}
int CountOrdersThisPairBigSELL() {
int l_count_0 = 0;
for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
OrderSelect(l_pos_4, SELECT_BY_POS);
if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderMagicNumber() == bigmagic) l_count_0++;
}
return (l_count_0);
}
// Close all buyopen trades when in profit
void closeallsmallbuyorders(){
for (i=OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if (OrderType()==OP_BUY && OrderMagicNumber()== smallmagic)result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,CLR_NONE);
if(result!=TRUE) Print("LastError = ", GetLastError());
}
else Print( "Error when order select ", GetLastError());
}
}
// Close all buyopen trades when in profit
void closeallbigbuyorders(){
for (i=OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if (OrderType()==OP_BUY && OrderMagicNumber() == bigmagic)result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,CLR_NONE);
if(result!=TRUE) Print("LastError = ", GetLastError());
}
else Print( "Error when order select ", GetLastError());
}
}
// Close all sell open trades when in profit
void closeallsmallsellorders(){
for (i=OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if (OrderType()==OP_SELL && OrderMagicNumber()== smallmagic)result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,CLR_NONE);
if(result!=TRUE) Print("LastError = ", GetLastError());
}
else Print( "Error when order select ", GetLastError());
}
}
// Close all sell open trades when in profit
void closeallbigsellorders(){
for (i=OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if (OrderType()==OP_SELL && OrderMagicNumber()== bigmagic)result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,CLR_NONE);
if(result!=TRUE) Print("LastError = ", GetLastError());
}
else Print( "Error when order select ", GetLastError());
}
}
//+------------------------------------------------------------------+
//| CORRELATION |
//+------------------------------------------------------------------+
double symboldif(string symbol, int shift)
{
return(iClose(symbol, PERIOD_M30, shift) -
iMA(symbol, PERIOD_M30, CorPeriod, 0, MODE_SMA,
PRICE_CLOSE, shift));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double powdif(double val)
{
return(MathPow(val, 2));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double u(double val1,double val2)
{
return((val1*val2));
}
//+------------------------------------------------------------------+
//| The real correlation function to call is here. |
//+------------------------------------------------------------------+
double Cor(string base, string hedge)
{
double u1=0,l1=0,s1=0;
for(int i = CorPeriod - 1; i >= 0; i--)
{
u1 += u(symboldif(base, i), symboldif(hedge, i));
l1 += powdif(symboldif(base, i));
s1 += powdif(symboldif(hedge, i));
}
if(l1*s1 > 0)
return(u1 / MathSqrt(l1*s1));
}
//+------------------------------------------------------------------+