• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 4:27pm
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 4:27pm
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Magic numbers question 12 replies

how to draw mutiple trend lines with same slope 7 replies

Magic Numbers and Comments when submitting orders (EA or manual) 2 replies

Magic Numbers in EA's 4 replies

Running mutiple EA at the same time. 2 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
Tags: Magic Numbers for Mutiple EA
Cancel

Magic Numbers for Mutiple EA

  • Post #1
  • Quote
  • First Post: Mar 29, 2007 5:25pm Mar 29, 2007 5:25pm
  •  snowy2fx
  • | Joined Aug 2006 | Status: Member | 5 Posts
I have a demo account at InterbankFX and created an mutiple copies of an EA. Each EA has it's own Magic Number. I attached a separate EA to different charts but cannot get multiple postions to be open at the same time for different currencies. It seems like it can only run for one currency at a time. As soon as I close one position, another position is automatically opened for another currency. Is there a setting in MQ that I am missing. Thank you for any assistance. Snowy2
  • Post #2
  • Quote
  • Mar 29, 2007 6:29pm Mar 29, 2007 6:29pm
  •  tesla
  • | Joined Oct 2006 | Status: Friendly Neighborhood Programmer | 533 Posts
On blackberry, short reply.

Guessing EA not coded robustly. Post it and you'll get feedback.
 
 
  • Post #3
  • Quote
  • Mar 29, 2007 7:29pm Mar 29, 2007 7:29pm
  •  snowy2fx
  • | Joined Aug 2006 | Status: Member | 5 Posts
Coding for Magic Number.


int MagicNumber = 260384;
extern double TakeProfit = 40;
extern double Lots = 0.01;
extern double TrailingStop = 20;
extern double MACDOpenLevel=3;

...

if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious)

{
Alert("You are buying here.");

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"EAMACD",MagicNumber,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);
}

Thanks.
 
 
  • Post #4
  • Quote
  • Mar 30, 2007 3:03pm Mar 30, 2007 3:03pm
  •  tesla
  • | Joined Oct 2006 | Status: Friendly Neighborhood Programmer | 533 Posts
Not enough info there to see what the problem is. Don't be shy, post the EA.
 
 
  • Post #5
  • Quote
  • Mar 31, 2007 10:59am Mar 31, 2007 10:59am
  •  snowy2fx
  • | Joined Aug 2006 | Status: Member | 5 Posts
Hi Telsa,

Here is the EA with the Magic Number code. Again, for different currencies, even with different Magic numbers, it only runs for one currency at time. I am a lost.

Thanks,
Snowy2

//+------------------------------------------------------------------+
//| MACD Magic Number.mq4 |
//| |
//+------------------------------------------------------------------+
int MagicNumber = 260384;
extern double TakeProfit = 40;
extern double Lots = 0.01;
extern double TrailingStop = 20;
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,24,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,24,9,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMACD(NULL,0,12,24,9,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,24,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=OrdersTotal();


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)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"MagicNoTest",MagicNumber,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)

{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"MagicNoTest",MagicNumber,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() && OrderMagicNumber()==MagicNumber) // check for symbol

{
if(OrderType()==OP_BUY) // long position is opened

{

// should it be closed?
if(MacdCurrent>0 && MacdCurrent<SignalCurrent &&
MacdPrevious>SignalPrevious)

{
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)

{
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);
}
// the end.
 
 
  • Post #6
  • Quote
  • Mar 31, 2007 12:11pm Mar 31, 2007 12:11pm
  •  kmrunner
  • | Joined Dec 2005 | Status: Member | 28 Posts
Your code is telling it to only open an order if there are no other orders open. "if(total<1)" << This surrounds your open routine, it can't look for another order open condition if one order is currently open.

Also, your close routine is a little messed up.
 
 
  • Post #7
  • Quote
  • Mar 31, 2007 6:39pm Mar 31, 2007 6:39pm
  •  Poocher
  • | Joined Dec 2006 | Status: Poocher | 1,017 Posts
I'm thinking about learning to code ea's but see it would be a big job. Wish there were more documentation for the beginner with examples...or I wish Tesla wasn't so busy and could code for me!!!
 
 
  • Post #8
  • Quote
  • Mar 31, 2007 6:52pm Mar 31, 2007 6:52pm
  •  aicccia
  • | Joined Jun 2006 | Status: Carpe Diem | 854 Posts
this is how I check to see if I have any orders open for a certain currency:
Inserted Code
 for(i=0;i<total;i++)
         {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
            {
            if(OrderSymbol()==Symbol())
               {
              open++;
               }
            }
         }

Your code is checking terminal-wide to see if there are any open orders, you only want to check symbol-wide.
 
 
  • Post #9
  • Quote
  • Mar 31, 2007 7:37pm Mar 31, 2007 7:37pm
  •  snowy2fx
  • | Joined Aug 2006 | Status: Member | 5 Posts
Thank you for pointing out my mistake. I thought the testing for the open order was just for the specific EA/currency. I adjust my code.

Thanks again.
snowy2
 
 
  • Post #10
  • Quote
  • Last Post: Mar 31, 2007 11:32pm Mar 31, 2007 11:32pm
  •  tesla
  • | Joined Oct 2006 | Status: Friendly Neighborhood Programmer | 533 Posts
Here's a quick function you can reference that will return a count of orders the ea has open on the current symbol.
Inserted Code
#define    MAGIC_NUMBER    8675309

int getOpenOrders() {

    // set initial order count to zero
    int iOrders = 0;

    // iterate all open orders    
    for (int i=OrdersTotal()-1;i>=0;i--) {
    
        // select the order
        OrderSelect(i,SELECT_BY_POS);
        
        // if order doesn't match this symbol or EA, continue to next order
        if (OrderSymbol() != Symbol() || OrderMagicNumber() != MAGIC_NUMBER) continue;
        
        // it's a match, add 1 to count
        iOrders++;
    }
    
    // return the order count
    return(iOrders);
}
 
 
  • Platform Tech
  • /
  • Magic Numbers for Mutiple EA
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023