Coding help - page 778

Игорь
346
Игорь  
Hello. Immediately apologize for my bad english. If it's not difficult, please help with the advisor code. Namely, to correct the conditions of opening and closing positions. A little about the robot, the robot opens and closes positions at the turn of the zig zag indicator.
This system is supposed to work regardless of whether or not there is a zig zag indicator in MT4.
//+------------------------------------------------------------------+
//|                                       Test advisor on ZigZag.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                         https:/goga342@yandex.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright c 2010, MQL для тебя."
#property link "http://mql4you.ru"
#property strict
extern double lots = 0.01; //
extern int  TakeProfit=60; //
extern int  StopLoss=100; //
extern int Magic=111111111;
extern int Slippage=3;
extern int ExtDepth=12;
extern int ExtDeviation=5;
extern int ExtBackstep=3;

datetime Today=0;
double SL,TP;
int ticket;
//±-----------------------------------------------------------------+
int OnInit()
  {
   if(Digits==3 || Digits==5)
     {
      TakeProfit *=10;
      StopLoss   *=10;
      Slippage   *=10;
     }
   return(INIT_SUCCEEDED);
  }
//±-----------------------------------------------------------------+
int deinit() {return(0);}
//±-----------------------------------------------------------------+
int start()
  {

   if(DayOfWeek()==0 || DayOfWeek()==6) return(0); // в выходные не работаем
   if(!IsTradeAllowed()) return(0); // пропустим тик если терминал занят
                                    //:проверим открытие новой свечи
   if(Time[0]==Today) return(0);
   Today=Time[0];
//:получим значение ZigZag на предыдущей свече
   double lZZ=iCustom(NULL,0,"ZigZag",ExtDepth,ExtDeviation,ExtBackstep,0,1);
   double lZZ_=iCustom(NULL,0,"ZigZag",ExtDepth,ExtDeviation,ExtBackstep,0,0);
// открытие продаж

   if(CountSell()==0 && lZZ==Low[0] && lZZ_==Low[0])
     {
      SL = NormalizeDouble(Bid+StopLoss*Point, Digits);
      TP = NormalizeDouble(Bid-TakeProfit*Point, Digits);
        {

         ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,Slippage,SL,TP,"macd sample",Magic,0,Red);
         if(ticket>0)
           {
            // закрытие продаж
    //        if(CountSell()>0 && iCustom(NULL,0,"ZigZag_Rosh",12,5,3,0,1)==High[0] && iCustom(NULL,0,"ZigZag_Rosh",48,20,12,0,1)==High[0])
              {
               for(int i=OrdersTotal()-1; i>=0; i--)
                 {
                  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
                    {
                     if(OrderMagicNumber()==Magic && OrderType()==OP_SELL)
                        if(OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrNONE))
                           Print("Ошибка открытия ордера на покупку !");
                    }
                 }
              }
           }
        }
     }
// открытие покупок

 //  if(CountBuy()==0 && iCustom(NULL,0,"ZigZag_Rosh",12,5,3,0,1)==High[0] && iCustom(NULL,0,"ZigZag_Rosh",48,20,12,0,1)==High[0])
     {
      TP = NormalizeDouble(Ask+TakeProfit*Point, Digits);
      SL = NormalizeDouble(Ask-StopLoss*Point, Digits);
        {
         ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,Slippage,SL,TP,"macd sample",Magic,0,Green);
         if(ticket>0)
           {
            // закрытие покупок    
       //     if(CountBuy()>0 && iCustom(NULL,0,"ZigZag_Rosh",12,5,3,1,0)==Low[0] && iCustom(NULL,0,"ZigZag_Rosh",48,20,12,1,0)==Low[0])
              {
               for(int i=OrdersTotal()-1; i>=0; i--)
                 {
                  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
                    {
                     if(OrderMagicNumber()==Magic && OrderType()==OP_BUY)
                        if(!OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrNONE))
                           Print("Ошибка открытия ордера на продажу!");
                    }
                 }
              }
           }
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+
int CountSell()//проверкна открытие ордеров на продажу
  {
   int count=0;

   for(int trade=OrdersTotal()-1; trade>=0; trade--)
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_SELL)
           {
            if(OrderType()==OP_SELL)
               count++;
           }
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
int CountBuy()// проверкна открытие ордеров на покупку
  {
   int count=0;

   for(int trade=OrdersTotal()-1; trade>=0; trade--)
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_BUY)
           {
            if(OrderType()==OP_BUY)
               count++;
           }
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+

opening sales

if(CountSell()==0 && iCustom(NULL,0,"ZigZag_Rosh",12,5,3,1,0)==Low[0] && iCustom(NULL,0,"ZigZag_Rosh",48,20,12,1,0)==Low[0])

closing sales

      if(CountSell()>0 && iCustom(NULL,0,"ZigZag_Rosh",12,5,3,0,1)==High[0] && iCustom(NULL,0,"ZigZag_Rosh",48,20,12,0,1)==High[0])
              

opening buy

if(CountBuy()==0 && iCustom(NULL,0,"ZigZag_Rosh",12,5,3,0,1)==High[0] && iCustom(NULL,0,"ZigZag_Rosh",48,20,12,0,1)==High[0])
     

closing buy

 if(CountBuy()>0 && iCustom(NULL,0,"ZigZag_Rosh",12,5,3,1,0)==Low[0] && iCustom(NULL,0,"ZigZag_Rosh",48,20,12,1,0)==Low[0])
              
Karel Nagel
1853
Karel Nagel  

Could someone show me how to make this code shorter? How to use for cycle for this?

extern double S1,S2,S3,S4,S5,S6,S7,S8,S9,S10,S11,S12,S13,S14,S15;
if (S1==1234) S1=DoubleToString(S1);
if (S2==1234) S2=DoubleToString(S2);
if (S3==1234) S3=DoubleToString(S3);
if (S4==1234) S4=DoubleToString(S4);
if (S5==1234) S5=DoubleToString(S5);
if (S6==1234) S6=DoubleToString(S6);
if (S7==1234) S7=DoubleToString(S7);
if (S8==1234) S8=DoubleToString(S8);
if (S9==1234) S9=DoubleToString(S9);

Would like something like this, but don't know how to change variable names. 

for(i=1; i<=9; i++){
if (S+i==1234) S+i=DoubleToString(S+i);
}
lippmaje
1204
lippmaje  
Karel Nagel:

Could someone show me how to make this code shorter? How to use for cycle for this?

Would like something like this, but don't know how to change variable names. 

You cannot initialize an array with a list of inputs, due to limitations of MQL. (Only constant expressions like { 2, 5, 3.1 } will do.)

But since you need to convert doubles to strings anyway you can use StringFormat. The interesting thing about this function is it accepts an arbitrary number of arguments.

With the help of StringSplit it can be used to create an array on the fly:

string line=StringFormat("%g %g %g %g %g %g %g %g %g",S1,S2,S3,S4,S5,S6,S7,S8,S9);
string numbers[];
int count=StringSplit(line,' ',numbers);
for(int i=0; i<count; i++)
  {
   if(numbers[i]=="1234") Print(" S",i+1," == 1234");
  }
amillax
8
amillax  
rsiArray[0] = iRSI(NULL,0,10,PRICE_CLOSE,1);
rsiArray[1] = iRSI(NULL,0,10,PRICE_CLOSE,2);
rsiArray[2] = iRSI(NULL,0,10,PRICE_CLOSE,3);
rsiArray[3] = iRSI(NULL,0,10,PRICE_CLOSE,4);
rsiArray[4] = iRSI(NULL,0,10,PRICE_CLOSE,5);
ArraySetAsSeries(rsiArray, true); 

upperEnv = iEnvelopesOnArray(rsiArray, 0, 5,MODE_SMA, 0, 5, MODE_UPPER, 0);
lowerEnv = iEnvelopesOnArray(rsiArray, 0, 5,MODE_SMA, 0, 5, MODE_LOWER, 0); 

Been trying to figure this out for too long now, i need help.

The envelope values aren't corresponding with rsiArray[0] or to any value i see on the charts and i just cant figure out why.


Any ideas?


Thanks

EDIT: (fixed) ok, all i needed to do was restart mt4 and the code worked fine.

asaens15
2009
asaens15  

I've spent hours trying to get an EA/utility to work correctly and made several changes until my brain is numb.  Attached is a preliminary version and I'll improve it once I get past the hurdle of basics working correctly which is to sound an alarm when an open buy/sell order is added and a different alarm when an open order buy/sell is closed.  This version sometimes works and sometimes doesn't so unreliable at this point.


//+------------------------------------------------------------------+
//|               myNotify.mq4                                       |
//|               Notify me by sound file of change in orders        |
//+------------------------------------------------------------------+

//---- input parameters
extern string OrderClosedWav = "OrderClosed.wav";
extern string OrderOpenedWav = "OrderOpened.wav";
string sVersion = "1.39";
int iOld, iNew = 0;
int i = 0;
uchar ii = 0;
long NewOrdersHash = 0;
long OldOrdersHash = 0;

int nOrders = 0;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
// +------------------------------------------------------------------+
// |  expert deinitialization function                                |
// +------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

int start()
{ 
    nOrders = 0;
    NewOrdersHash = 0;

//First Pass
    for(i=OrdersTotal()-1;i>=0;i--)
       {
         OrderSelect(i, SELECT_BY_POS);
         if (OrderType() < 2)           // NOT "OP_BUYLIMIT" || "OP_BUYSTOP" //|| "OP_SELLLIMIT" || "OP_SELLSTOP")
         {
           NewOrdersHash = NewOrdersHash + OrderTicket();
           nOrders++;
         } 
       } 

OldOrdersHash = NewOrdersHash;
iOld =nOrders;

    Comment ("Ver ",sVersion," \n",
         iOld,"\n", iNew,"\n\n", ii,"\n\n",
         OrderOpenedWav,"\n",
         OrderClosedWav,"\n\n",
         "Old:  ",OldOrdersHash,"\n",
         "New: ",NewOrdersHash,"\n\n",
         nOrders,"\n");
// Wait
         Sleep(2000);

//Second Pass to compare after short pause
    nOrders = 0;
    NewOrdersHash = 0;
    for(i=OrdersTotal()-1;i>=0;i--)
       {
         OrderSelect(i, SELECT_BY_POS);
         if (OrderType() < 2)           // NOT "OP_BUYLIMIT" || "OP_BUYSTOP" //|| "OP_SELLLIMIT" || "OP_SELLSTOP")
         {
           NewOrdersHash = NewOrdersHash + OrderTicket();
           nOrders++;
         } 
       } 
iNew = nOrders;

    if (ii > 24) {ii = 1;} else {ii++;}
       
    Comment ("Ver ",sVersion," \n",
         iOld,"\n", iNew,"\n\n", ii,"\n\n",
         OrderOpenedWav,"\n",
         OrderClosedWav,"\n\n",
         "Old:  ",OldOrdersHash,"\n",
         "New: ",NewOrdersHash,"\n\n",
         nOrders,"\n");
    if (OldOrdersHash != NewOrdersHash)
    {
     if (iNew > iOld)
       PlaySound(OrderOpenedWav);
     else
       PlaySound(OrderClosedWav);
    }

return(0);

}  

/*
void OrdersF()
   {

    nOrders = 0;
    NewOrdersHash = 0;
    for(int i=OrdersTotal()-1;i>=0;i--)
       {
         OrderSelect(i, SELECT_BY_POS);
         if (OrderType() < 2)          
         {
           NewOrdersHash = NewOrdersHash + OrderTicket();
           nOrders++;
         } 
       } 
    iNew = nOrders;
    return;
   }
*/
//+------------------------------------------------------------------+

MoRTiZz3
200
MoRTiZz3  

Hi guys, I'm new. I want to do a very simple edit on this indicator

How and where do I just simply put in the Description box of the Horizontal Line, the word,     Alert_     ?

for it to automatically appear?

Looking forward your response



//+------------------------------------------------------------------+

//|                                              horizontal line.mq4 |
//|                                                          Wdholic |
//|                            https://www.mql5.com/en/users/wdholic |
//+------------------------------------------------------------------+
#property copyright "Wdholic"
#property link      "https://www.mql5.com/en/users/wdholic"
#property version   "1.00"
#property strict

#import "cfunctions.dll" 
int      MT4_ScreenToClient(int hWnd, int& iX[]);  
#import
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  { 
    double pmax=ChartGetDouble(0,CHART_PRICE_MAX);
    double pmin=ChartGetDouble(0,CHART_PRICE_MIN);
    double Ymax=ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS)*1.0;
   //  Alert(pmin);Alert(pmax);Alert(Ymax);
   int iCoords[2] ;
    int hWin = WindowHandle( Symbol(), Period());  
   
    MT4_ScreenToClient(hWin, iCoords);    
               
    double cory=iCoords[1]*1.0;
    double range=(pmax-pmin);
    
    double XY= (cory*1.0)/Ymax;
      double P=pmax-XY*range;
     
        
   Line("test_",P,clrAqua);
   WindowRedraw();
   return;
  }
//+------------------------------------------------------------------+

void Line(string t,double P,color C )
{ int a=1;string nm;
    nm="line_"+t+string(a);
  while(ObjectFind(0,nm)==0){ a++; nm="line_"+t+string(a);}
  
if(ObjectFind(0,nm)<0){
  ObjectCreate(0,nm,OBJ_HLINE,0,0,0); 
  ObjectSet( nm,OBJPROP_COLOR,C);}
  ObjectSet( nm,OBJPROP_PRICE1,P);  
  ObjectSet( nm,OBJPROP_SELECTED,1);  
}

kerold013
7
kerold013  

Can anyone Help me to code this. Putting Rectangle as a high and low of the last 3 months as shown thank you

Files:
INDIxxx.jpg 243 kb
Abubakar Saidu
5292
Abubakar Saidu  

someone help with stoploss on previous ma cross



kisaxena726
8
kisaxena726  

Hi,

I have idea. But i don't know mq4 code much. 

Previous high is lower than 2nd previous high, then buy pending order at previous high * 2% or 1% and when price at current candle touch that order buy. 

Previous low is higher than 2nd previous low, then sell pending order at previous low * -2% or -1% and when price at current candle touch that order sell.

Any period. No target No stop loss. Trailing stop loss like opposite.

Grateful to anyone, who can make mq4 code. I need help. Only MT4.

BANSINO Money Maker
18
BANSINO Money Maker  

Hi ...

Can you make an Expert for  this strategy please

Reinforcement Expert

Settings :

1.    Option: Sell only / Buy only

2.    TP: 50

3.    Sl: 00

4.    Step: 150

5.    Start Lot: 0,1

6.    Multiplication: 1.2.4.8.16… ..

7.    Total orders: 100

8.    Magic Number: 111111

The method of work :

1-   Opening a position at the start automatically by the specified lot size.

2-   Change Tp for all trades on each reinforcement.

According to the following equation: Tp for the last double position covers us all floating loss + profit of the last position.

3-   Return to the beginning lot.