how to make macd sample can use in another chart with different pair?

[Deleted]  

can anyone help me??? iam confusing n dont understand about "magic number"

i want to make macd sample use in multiple chart (few chart, diferrent pair n 1 terminal) i need everyone help.... please master....


this is the macd that i'd modified,..

bu its not working.... ???

can anyone help me to fix my problem???

//+------------------------------------------------------------------+
//| MACD Sample.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#define MAGICNUM 140179

extern double TakeProfit = 50;
extern double Lots = 0.1;
extern double TrailingStop = 30;
extern double MACDOpenLevel=3;
extern double MACDCloseLevel=2;
extern double MATrendPeriod=26;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
   double MacdCurrent, MacdPrevious, SignalCurrent;
   double SignalPrevious, MaCurrent, MaPrevious;
   int cnt, ticket, total;
   // initial data checks
   // it is important to make sure that the expert works with a normal
   // chart and the user did not make any mistakes setting external
   // variables (Lots, StopLoss, TakeProfit,
   // TrailingStop) in our case, we check TakeProfit
   // on a chart of less than 100 bars
   if(Bars<100)
   {
      Print("bars less than 100");
   return(0);
   }
   if(TakeProfit<10)
   {
      Print("TakeProfit less than 10");
      return(0); // check TakeProfit
   }
   // to simplify the coding and speed up access
   // data are put into internal variables
   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
   SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
   MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
   MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);


   total=ActiveTradesForMagicNumber(Symbol(), MAGICNUM);
   if(total<1)

     {
      // no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
       {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);
       }
      // check for long position (BUY) possibility
      if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious &&
         MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious)
         {
            ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
         if(ticket>0)
            {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
            }
         else Print("Error opening BUY order : ",GetLastError());
         return(0);
         }
      // check for short position (SELL) possibility
      if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
         MacdCurrent>(MACDOpenLevel*Point) && MaCurrent<MaPrevious)
         {
            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Red);
         if(ticket>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 is important to enter the market correctly,
   // but it is more important to exit it correctly...
   for(cnt=0;cnt<total;cnt++)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && // check for opened position
         OrderSymbol()==Symbol()) // check for symbol
         {
            if(OrderType()==OP_BUY) // long position is opened
               {
            // should it be closed?
                  if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
                     MacdCurrent>(MACDCloseLevel*Point))
                     {
                        OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                        return(0); // exit
                     }
            // check for trailing stop
                  if(TrailingStop>0)
                     {
                        if(Bid-OrderOpenPrice()>Point*TrailingStop)
                           {
                              if(OrderStopLoss()<Bid-Point*TrailingStop)
                                 {
                                    OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                                    return(0);
                                 }
                           }
                     }
               }
            else // go to short position
               {
                  // should it be closed?
                  if(MacdCurrent<0 && MacdCurrent>SignalCurrent &&
                  MacdPrevious<SignalPrevious && MathAbs(MacdCurrent)>(MACDCloseLevel*Point))
                     {
                        OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
                        return(0); // exit
                     }
                  // check for trailing stop
                  if(TrailingStop>0)
                     {
                        if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                           {
                              if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                                 {
                                    OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                                    return(0);
                                 }
                           }
                     }
               }
         }
   }
   return(0);
}
int ActiveTradesForMagicNumber(string SymbolToCheck, int MagicNumberToCheck)
   {
      int icnt, itotal, retval;

      retval=0;
      itotal=OrdersTotal();

      for(icnt=0;icnt<itotal;icnt++)
         {
            OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
            // check for opened position, symbol & MagicNumber
            if(OrderType()<=OP_SELL && OrderSymbol()==SymbolToCheck && OrderMagicNumber()==MagicNumberToCheck)
               {

                  retval++;

            //Print("Orders opened : ",retval);

               }
         }

         return(retval);

   }

  

Simon Gniadkowski
17304
Simon Gniadkowski  
gekafx:

can anyone help me??? iam confusing n dont understand about "magic number"

Using a Magic Number allows you to tell what placed an order, a manually placed trade won't have a Magic Number. You can tell the symbol an order was placed on using OrderSymbol(), you can tell the order type, Buy or Sell, using OrderType() . . . . but you can't tell what EA placed an order or on what timeframe the order was based on.

So I use my Magic Number to tell me those 2 pieces of information, the EA number and the timeframe . . .

Simon Gniadkowski
17304
Simon Gniadkowski  
gekafx:

this is the macd that i'd modified,..

bu its not working.... ???

Can you be more specific about what it isn't doing or what it is doing incorrectly ?
Robert
418
Robert  

Gekafx,

can anyone help me??? i am confusing n don't understand about "magic number"

i want to make macd sample use in multiple chart (few chart, diferrent pair n 1 terminal) i need everyone help.... please....


RaptorUK:

Can you be more specific about what it isn't doing or what it is doing incorrectly ?

Hi Gekafx,

As Raptor indicated...there is not much information on what the problem is...so the following is just a guess based on your questions.

The key to Magic Numbers is....they have to be DIFFERENT for each EA....

In order to make this EA do that...you need to change the following code to make the Magic Number a USER setting so you can change it for each EA you use.

Here is the code you need to change:

#define MAGICNUM 140179

Change to:

extern int MAGICNUM = 140179;

* The number is just for example. Use any integer numbers you want.

Recompile your code...then run the EA on different charts...and use a different MAGICNUM setting for each EA.

Hope this helps,

Robert

Agent86
1061
Agent86  
Hi


can anyone help me??? iam confusing n dont understand about "magic number"

i want to make macd sample use in multiple chart (few chart, diferrent pair n 1 terminal) i need everyone help.... please master....


this is the macd that i'd modified,..

bu its not working.... ???

can anyone help me to fix my problem???


As cosmicbeing suggested about magicnumbers, but this would not solve the problem of using it on one chart if thats what your trying to do

If you want to use indicators data from a separate currency pair on this chart and this particular EA then you need to change your variables or ad additional variables to reflect which pair and timeframe you wish to use such as:

MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);


//NULL means current currency pair
//If you want to add additional comparisons for another currency pair then you need to change this or create another variable to include another pair //for comparison

//Such as:

MacdCurrent_2=iMACD("EUROUSD",0,12,26,9,PRICE_CLOSE,MODE_MAIN,0)  //check string symbols for your use if this is what your trying to do

//Or some other string symbol

Hope this helps

https://docs.mql4.com/indicators/iMACD

Robert
418
Robert  
gekafx:

can anyone help me??? iam confusing n dont understand about "magic number"

i want to make macd sample use in multiple chart (few chart, diferrent pair n 1 terminal) i need everyone help....

this is the macd that i'd modified,.. but its not working.... ???

can anyone help me to fix my problem???

Hi Gekafx,

In reviewing the Magic Number problem for your MACD Sample EA...it looks like there are a few additional concerns with the Magic Number in this EA...

Besides making the Magic Number an extern USER setting...

In sending your trades...you also have to replace the "hard coded" Magic Number (16384) and change it to the MAGICNUM variable :
            ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Red);
Replace the "16384" with your "MAGICNUM" variable...

            ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",MAGICNUM,0,Green);
            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",MAGICNUM,0,0,Red);

And you need to search and select your trades by the Magic Number.

The OrderSelect code below does not match the Magic Number with your trades. Add your Magic Number to the OrderSelect function below:

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol

In addition... I don't know what this code function below is for...

It may be trying to do what I just suggested above - to match trades with a Magic Number?

In any case....it seems the "MagicNumberToCheck" should be exactly the same variable as your "MAGICNUM" that you set in your USER settings?

----------------------
int ActiveTradesForMagicNumber(string SymbolToCheck, int MagicNumberToCheck)
{
int icnt, itotal, retval;
retval=0;
itotal=OrdersTotal();
for(icnt=0;icnt<itotal;icnt++)
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
// check for opened position, symbol & MagicNumber
if(OrderType()<=OP_SELL && OrderSymbol()==SymbolToCheck && OrderMagicNumber()==MagicNumberToCheck)
{
---------------------------

Just some more ideas to check out...

Hope this helps,
Robert
[Deleted]  
RaptorUK:
Can you be more specific about what it isn't doing or what it is doing incorrectly ?

hai Raptor uk, nice to meet you,.. thanks for the answer..., actually i dont know why the EA wont work exaclly, because, there is no sign, buy or sell... fiuuuhhh iam bleeding...:(
[Deleted]  
cosmicbeing:

Hi Gekafx,

As Raptor indicated...there is not much information on what the problem is...so the following is just a guess based on your questions.

The key to Magic Numbers is....they have to be DIFFERENT for each EA....

In order to make this EA do that...you need to change the following code to make the Magic Number a USER setting so you can change it for each EA you use.

Here is the code you need to change:

#define MAGICNUM 140179

Change to:

extern int MAGICNUM = 140179;

* The number is just for example. Use any integer numbers you want.

Recompile your code...then run the EA on different charts...and use a different MAGICNUM setting for each EA.

Hope this helps,

Robert



thanks for the answer robert, this is very help.. but i want to correcting more deeply inside the EA... thanks alot bro..
Simon Gniadkowski
17304
Simon Gniadkowski  
gekafx:

hai Raptor uk, nice to meet you,.. thanks for the answer..., actually i dont know why the EA wont work exaclly, because, there is no sign, buy or sell... fiuuuhhh iam bleeding...:(
Are you running this in the Strategy Tester or a Demo or Live account ?
[Deleted]  
Agent86:
Hi



As cosmicbeing suggested about magicnumbers, but this would not solve the problem of using it on one chart if thats what your trying to do

If you want to use indicators data from a separate currency pair on this chart and this particular EA then you need to change your variables or ad additional variables to reflect which pair and timeframe you wish to use such as:

Hope this helps

https://docs.mql4.com/indicators/iMACD
















thankyou verry much for your help agent86... but i want make it separately, just 1 ea, but it cant load in any chart and any pair, so if iwant EURUSD, i just load it into EURUSD chart, but if i dont want to trade EURUSD, ijust not load it... i hope you understand my bad english... :)
[Deleted]  
RaptorUK:
Are you running this in the Strategy Tester or a Demo or Live account ?

in live account ...