Can i use an extern variable to set symbols for my EA to run?

 

Hi everyone. I have a question.

I am working on a EA and the idea is to have as an input parameter the symbol i want the EA to run.

I though on a combobox but i understand that mql4 doesn't allow a combobox. Am i right?

I'm trying right now using an extern string variable. But it's not sending any order due to the error 4106 (Unknown symbol).

extern string symbol = "USDCAD";
...

OrderSend(symbol, ..., ...);

I also tried using StringConcatenate to put on the "

extern symbol = "USDCAD";

start()
{
   string orderSymbol = StringConcatenate("\"", symbol, "\"");
   OrderSend(orderSymbol, ..., ...);
}

But still the same error. Can anybody help me? Or it's not possible to set the symbol as an input parameter?

Thanks.

 
You can pass the symbol as an extern, however, the symbol must match an available symbol from your broker. By chance does your broker use USDCADn or some variant?
 

Hi serpentsnoir.

No, the broker use USDCAD. I did some test. I define symbol not as an extern variable but as a local variable:

start
{
   string symbol = "USDCAD";
   OrderSend(symbol, ..., ...);
}

And it worked just fine. But when its an extern variable i get always the same error (4106). I don't know where the problem is.

 

create and run this script and tell us what you get on a USDCAD chart (untested)

#property show_inputs

extern string symbol = "USDCAD"; 

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   Print(Symbol()," / ",symbol," / ",(Symbol() == symbol));
   return(0);
  }
 
serpentsnoir:

create and run this script and tell us what you get on a USDCAD chart (untested)


Running the script in the expert log this is what i got exactly:

test USDCAD, H1: loaded successfully

test USDCAD, H1 inputs: symbol= "USDCAD";

test USDCAD, H1: USDCAD/ USDCAD/ 1

test USDCAD, H1: uninit reason 0

test USDCAD, H1: removed

 

I better tell you the idea of the EA and why i am asking about using an extern variable for the symbol. Maybe there is a solution to my previous error.

The idea is to run the same EA in 10 different symbols or maybe more.

But, what i need is that the EA execute only 2 orders per symbol. But the orders can't be 2 sells or 2 buys. If there are 2 orders in the same symbol they must be only 1 sell and 1 buy.

This is the code i have:

start
{
   if(existSell() == false)
   {
      OrderSend(Symbol(), ..., ...);
   }
   
   if(existBuy() == false)
   {
      OrderSend(Symbol(), ..., ...);
   }
}

bool existSell()
  {
      bool exist = false;
      bool end = false;
      for(int i = 0; i < OrdersTotal() && end == false; i++)
      {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         {
            if(OrderSymbol() == Symbol())
            {
               if(OrderType() == OP_SELL || OrderType() == OP_SELLSTOP)
               {
                  exist = true;
                  end = true;
               }
            }
         }
      }
      return(exist);
   }
     
          
   bool existeOrdenBuy()
  {
      bool exist = false;
      bool end = false;
      for(int a = 0; a < OrdersTotal() && end == false; a++)
      {
         if(OrderSelect(a, SELECT_BY_POS, MODE_TRADES))
         {
            if(OrderSymbol() == Symbol())
            {
               if(OrderType() == OP_BUY || OrderType() == OP_BUYSTOP)
               {
                  exist = true;
                  end = true;
               }
            }
         }
      }
      return(exist);
   }            

This code work fine if i only use it in 1 symbol. But when is running in more it doesn't execute all the orders it should. With this code it only execute 1 order per symbol. And i don't know why.

That's why i thought to code so that the EA can find exactly if there are orders in a symbol or not. So it can execute the orders perfectly.

I hope you can help me, i will appreciate it a lot.

By the way, this is not the EA complete. I am sorry but i can tell you exactly all the filters it must use.

 

well, do you always get a sell but never a buy?

Could the trade context be busy when you send the second order?

 

Actually i get sells and buys. But never at the same time for the same symbol.

I get a trade context busy but that is only for the modifications of orders, that is another problem i have but i want to solve the execution of orders first.

 

And after declaring USDCAD as an extern variable what symbol chart are you attaching the EA to?

 

What I do is write allot of small functions which can be reused. It helps cut down on development time. Below is some example from my "for testing" template. You'll want to add Error-Checking before using something like below on a Live-Chart. Just example and not-tested.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
extern string  My_Symbs="EURUSD_USDJPY_GBPUSD_USDCHF_AUDUSD_USDCAD";
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){
    string  Symbol_Ray[]={"EURUSD","USDJPY","GBPUSD","USDCHF","AUDUSD","USDCAD"};
    int Temp_Magic=7; int Fx__Period; string  Fx__Symbol;
    for(int i=0; i<ArraySize(Symbol_Ray); i++){
        Fx__Symbol=Symbol_Ray[i]; Fx__Period=PERIOD_M1;
        if(StringFind(My_Symbs,Fx__Symbol)<0){continue;}
        if(IsTesting()){i=9999; Fx__Symbol=Symbol();}
        Origination_Algorithm_OrderSend(Temp_Magic,Fx__Symbol,Fx__Period);
        Termination_Algorithm_OrderClse(Temp_Magic,Fx__Symbol,Fx__Period);
}   }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void Origination_Algorithm_OrderSend(int Magic, string Symb, int Perd){
    MathSrand(GetTickCount()); int Direction;
    if(MathRand()%2==0){Direction=1;}else{Direction=-1;}
    for(int x=1; x>=-1; x-=2){
        if(Direction!=x){continue;}
        if(Count_Orders_Magic_Symbol_Type(Magic,Symb,x)>0){continue;}
        double Lot=MarketInfo(Symb,MODE_MINLOT); 
        double Ask_Market=     MarketInfo(Symb,MODE_ASK);
        double Bid_Market=     MarketInfo(Symb,MODE_BID);
        double OrPrice; color OrColor;
        if(x== 1){OrPrice=Ask_Market; OrColor=Blue;}
        if(x==-1){OrPrice=Bid_Market; OrColor=Red;}
        OrderSend(Symb,Ot(x),Lot,OrPrice,0,0,0,"",Magic,0,OrColor);
}   }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void Termination_Algorithm_OrderClse(int Magic, string Symb, int Perd){
    for(int x=1; x>=-1; x-=2){
        int Time_X=TimeLst_Order_Magic_Symbol_Type(Magic,Symb,x);
        double Prof=Profit_Order_Magic_Symbol_Type(Magic,Symb,x);
        if(Time_X>iTime(Symb,Perd,60) && Prof<1){continue;}
        while(Count_Orders_Magic_Symbol_Type(Magic,Symb,x)>0){
            Close_Orders_In_Type_Priority(Magic,Symb,x);
}   }   }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int Count_Orders_Magic_Symbol_Type(int Magic, string Symb, int x){
    int Ans; for(int i=OrdersTotal()-1; i>=0; i--){
        if(OrderSelect(i, SELECT_BY_POS)
        && OrderMagicNumber()==Magic
        && OrderSymbol()==Symb
        && OrderType()==Ot(x)){Ans++;}} return(Ans);}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int TimeLst_Order_Magic_Symbol_Type(int Magic, string Symb, int x){
    for(int i=OrdersTotal()-1; i>=0; i--){
        if(OrderSelect(i, SELECT_BY_POS)
        && OrderMagicNumber()==Magic
        && OrderSymbol()==Symb
        && OrderType()==Ot(x)){return(OrderOpenTime());}}}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
double Profit_Order_Magic_Symbol_Type(int Magic, string Symb, int x){
    double Ans; for(int i=OrdersTotal()-1; i>=0; i--){
        if(OrderSelect(i, SELECT_BY_POS)
        && OrderMagicNumber()==Magic
        && OrderSymbol()==Symb
        && OrderType()==Ot(x)){Ans+=OrderProfit();}} return(Ans);}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int Ot(int Value){if(Value==1){return(0);}if(Value==-1){return(1);}}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bool Close_Orders_In_Type_Priority(int Magic, string Symb, int Type){
    double LargeLot; int LargeTicket;
    for(int i=OrdersTotal()-1; i>=0; i--){
        if(OrderSelect(i, SELECT_BY_POS)
        && OrderMagicNumber()==Magic 
        && OrderSymbol()==Symb){
            if(OrderType()==Ot(Type)){
            if(OrderLots()>LargeLot){
                LargeLot=OrderLots(); LargeTicket=OrderTicket();}}}
    }Close_Order_With_Matchin_Ticket(LargeTicket);}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bool Close_Order_With_Matchin_Ticket(int Ot){
    if(OrderSelect(Ot, SELECT_BY_TICKET) && OrderCloseTime()==0){
        double Ocp=OrderClosePrice(); double Ol=OrderLots();
        if(!IsTesting()){while(IsTradeContextBusy()){Sleep(500);}}
        int Ans=OrderClose(Ot,Ol,Ocp,0,0); return(Ans);}}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
wimpy63:

I better tell you the idea of the EA and why i am asking about using an extern variable for the symbol. Maybe there is a solution to my previous error.

The idea is to run the same EA in 10 different symbols or maybe more.

But, what i need is that the EA execute only 2 orders per symbol. But the orders can't be 2 sells or 2 buys. If there are 2 orders in the same symbol they must be only 1 sell and 1 buy.

This is the code i have:

This code work fine if i only use it in 1 symbol. But when is running in more it doesn't execute all the orders it should. With this code it only execute 1 order per symbol. And i don't know why.

That's why i thought to code so that the EA can find exactly if there are orders in a symbol or not. So it can execute the orders perfectly.

I hope you can help me, i will appreciate it a lot.

By the way, this is not the EA complete. I am sorry but i can tell you exactly all the filters it must use.

You said you run this EA on multiple Symbols, do you run it also on the same symbol but different timeframes ? for example, GBPUSD H1 & GBPUSD M15 ?

Maybe your 2nd order is being opened but is them being closed very quickly, have you checked this ?

One unrelated point . . . get rid of your end variable, just use a break instead.

if(OrderType() == OP_BUY || OrderType() == OP_BUYSTOP)
               {
                  exist = true;
                  break;         //  <--  exits the loop
               }
Reason: