Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 1003

 

Please advise how to implement the following:

There are custom lines on the chart, let there be "n" number of them. How to get the price value when the price approaches one of the lines?

Thanks in advance!

 

Hi all ) please help - The target is (if the price is above the (100 pips) "line" then "buy" ) ) how to express it ( the price is higher by 100 pips from any value ) and also this question how to know the price of the moving average at the moment?

thanks in advance )

 
Tema97:

Hi all ) please help - The target is (if the price is above the (100 pips) "line" then "buy" ) ) how to express it ( the price is higher by 100 pips from any value ) and also this question how to know the price of the moving average at the moment?

thanks in advance )

Tema, read the documentation, iMa is described there as well as the graphical objects. There is a function ObjectGetDouble(with the corresponding parameters) to get the price of a line
 

Please advise a newcomer how to avoid reopening an order, after it has been closed, while keeping the trading conditions for opening.

For example: if stochastic crosses the 50 level upwards, open Buy. Close when stochastic reverses or when it reaches level 80 or at Take Profit. But trading conditions to open are preserved! I.e. stochastic remains above 50 and Buy is opened again!

double S_1=iStochastic(NULL,0,K,D,slowing,MODE_SMA,price_field,MODE_MAIN,0);

double S_2=iStochastic(NULL,0,K,D,slowing,MODE_SMA,price_field,MODE_MAIN,1);

if(S_1>50)

{

Opn_B=true;

}

if(S_1<S_2||S_1>80)

{

Cls_B=true;

}

I insert trade criteria into simple Expert Advisor's scheme, which is presented in MQL tutorial. Please advise which function to use to avoid this problem. Thank you!

 
if(S_1 > 50 && S_2 < 50)

This is the condition for crossing the 50 level from bottom to top.

 
AlexeyVik:

This is the condition for crossing the 50 level from bottom to top.

Thank you very much! I'll give it a try now.
 
How do I "detach" an "attached" indicator (or EA) from a chart without closing that chart?
 
Is it possible to put an AccountEquity() indicator that works in real time on the chart? Where can I get it?
 

Hello, could you please advise me? I am changing trade criteria in Expert Advisor presented in MQL4 tutorial. When I try to set a stop loss on Buy, the program gives error 130: wrong stops. Please explain why?

// Numerical values for M15

extern double StopLoss =0; // SL for the order being opened

extern double TakeProfit =0; // TP for the order being opened

extern int K = 5;

extern int D = 3;

extern int slowing = 3;

extern int price_field = 0;


extern int Level_1 = 80; // Stochastic level

extern int Level_2 = 20;

extern inttern Period_MA_2 = 31; // MA Period 2

extern double Rastvor =28.0; // Distance between MA

extern double Lots =0.01; // Fixed number of lots.

extern double Prots =0.07; // Percentage of free funds

bool Work=true; // Expert Advisor will work.

string Symb; // Name of the financial instrument

//--------------------------------------------------------------- 2 --

int start()

{

int

K_level,

Total, // Number of orders in the window

Tip=-1, // Order type selected (B=0,S=1)

Ticket; // Order number

double S_1,

S_2,

MA_1_t, // MA_1_t value. MA_1 current value

MA_2_t, // Value. MA_2 current MA_2_t value // Number of lots selected in order

Lots, //Lots Quantity in Order currently selected

Lts, //Number of lots in an open order

Min_Lot, // Minimum number of lots

Step, // Step of lot size change

Free, // Current free funds

One_Lot, // Value of one lot

Price, // Price of the selected order

SL, // SL price of the selected order

TP; // TP of the selected order

bool

Ans =false, // Server answer after the closing

Cls_B=false, // Criterion for closing Buy

Cls_S=false, // Criterion for closing Sell

Opn_B=false, // Criterion for opening Buy

Opn__S=false; // criterion for opening Sell

//--------------------------------------------------------------- 3 --

//Preprocessing

if(Bars < Period_MA_2) // Not enough bars

{

Alert("Not enough bars in the window. Expert Advisor is not working.");

return; // Exit start()

}

if(Work==false) // Critical error.

{

Alert("Critical error. Expert Advisor is not working.");

return; // Exit from start()

}

//--------------------------------------------------------------- 4 --

// Order counting

Symb=Symbol(); // Name of financial instrument.

Total=0; // Number of orders

for(int i=1; i<=OrdersTotal(); i++) // Order loop

{

if(OrderSelect(i-1,SELECT_BY_POS)==true) // If the following

{ // Order analysis:

if (OrderSymbol()!=Symb)continue; // Not our financial instrument

if (OrderType()>1) // We have a pending order

{

Alert("Pending order detected. Expert Advisor is not working;)

return; // Exit()

}

Total++; // Market order counter

if (Total>1) // No more than one order

{

Alert("Several market orders. Expert Advisor is not working;)

return; // Exit()

}

Ticket=OrderTicket(); // Order number selected.

Tip =OrderType(); // Type of the selected order.

Price =OrderOpenPrice(); // Price of the selected order.

SL =OrderStopLoss(); // SL of the selected order.

TP =OrderTakeProfit(); // TP of the selected order.

Lot =OrderLots(); // Number of lots

}

}

//--------------------------------------------------------------- 5 --

// Trade criteria

S_1=iStochastic(NULL,0,K,D,slowing,MODE_SMA,price_field,MODE_MAIN,0);


S_2=iStochastic(NULL,0,K,D,slowing,MODE_SMA,price_field,MODE_MAIN,1);


if(S_1 > 50 && S_2 < 50)


{


Opn_B=true;


}


if(S_1<S_2||(S_1 < 80 && S_2 > 80))


{


Cls_B=true;


}

if(S_1 < 50 && S_2 > 50)


{


Opn_S=true;


}


if(S_1>S_2||(S_1 > 20 && S_2 < 20))


{


Cls_S=true;


}

//--------------------------------------------------------------- 6 --

// Close orders

while(true) // Close order loop.

{

if (Tip==0 && Cls_B==true) //Open Buy order.

{ //there is a closing criterion

Alert("Trying to close Buy ",Ticket,";)

RefreshRates(); // Refresh data

Ans=OrderClose(Ticket,Lot,Bid,2); // Close Buy

if (Ans==true) // It worked :)

{

Alert ("Closed Buy order ",Ticket;)

break; // Exit from the close loop

}

if (Fun_Error(GetLastError())==1) // Error handling

continue; // retry

return; // Exit from start()

}

if (Tip==1 && Cls_S==true) // Sell order opened.

{ // there is a close criterion

Alert("Attempting to close Sell ",Ticket,";)

RefreshRates(); // Refresh data

Ans=OrderClose(Ticket,Lot,Ask,2); // Close Sell

if (Ans==true) // It worked :)

{

Alert ("Sell order closed ",Ticket;)

break; // Exit from the close loop

}

if (Fun_Error(GetLastError())==1) // Error handling

continue; // retry

return; // Exit from start()

}

break; // Exit while

}

//--------------------------------------------------------------- 7 --

// Order values

RefreshRates(); // Refresh data

Min_Lot=MarketInfo(Symb,MODE_MINLOT); // Minimal number of lots

Free =AccountFreeMargin(); // Free funds

One_Lot=MarketInfo(Symb,MODE_MARGINREQUIRED);// Value of 1 lot

Step =MarketInfo(Symb,MODE_LOTSTEP); // Step change size

if (Lots > 0) // If lots are specified

Lts =Lots; // we work with them

else // % of free funds

Lts=MathFloor(Free*Prots/One_Lot/Step)*Step;// To open

if(Lts < Min_Lot) Lts=Min_Lot; // Not less than Min_Lot; // Not less than Min_Lot

if(Lts*One_Lot > Free) // Lots more expensive than Free

{

Alert(" Not enough money ", Lts," lots);

return; // Exit start()

}

//--------------------------------------------------------------- 8 --

//Opening of orders

while(true) //Order close loop.

{

if (Total==0 && Opn_B==true) // No open orders

{ //open Buy criterion.

RefreshRates(); // Update data

Alert("Trying to open Buy. Waiting for reply...");

Ticket=OrderSend(Symb,OP_BUY,Lts,Ask,2,StopLoss,TakeProfit);//open Buy

if (Ticket > 0) // it worked :)

{

Alert ("Buy order opened ",Ticket;)

return; //Exit order

}

if (Fun_Error(GetLastError())==1) // Error handling

continue; // retry

return; // Exit from start()

}

if (Total==0 && Opn_S==true) // no open orders

{ //open Sell criterion.

RefreshRates(); // Update data

Alert("Attempting to open Sell. Waiting for reply...");

Ticket=OrderSend(Symb,OP_SELL,Lts,Bid,2,StopLoss,TakeProfit);//Open Sel.

if (Ticket > 0) // It worked :)

{

Alert ("Sell order opened ",Ticket;)

return; //Exit order

}

if (Fun_Error(GetLastError())==1) // Error handling

continue; // retry

return; // Exit from start()

}

break; // Exit while

}

//--------------------------------------------------------------- 9 --

return; //exit from start()

}


//-------------------------------------------------------------- 10 --

int Fun_Error(int Error) // Error handling fie

{

switch(Error)

{ // Insurmountable errors.

case 4: Alert("The trade server is busy. Try again...");

Sleep(3000); // Simple solution.

return(1); // Exit function.

case 135:Alert("Price changed. Try again...");

RefreshRates(); // Refresh data.

return(1); // Exit from the function.

case 136:Alert("No price. Waiting for a new tick...");

while(RefreshRates()==false) // Till a new tick

Sleep(1); // Delay in the loop

return(1); // Exit from the function

case 137:Alert("Broker is busy. Try again...");

Sleep(3000); // Simple solution.

return(1); // Exit from the function.

case 146:Alert("The trading subsystem is busy. Try again...");

Sleep(500); // Simple decision

return(1); // Exit from the function

// Critical errors

case 2: Alert("General error;)

return(0); // Exit from the function.

case 5: Alert("Older version of the terminal.");

Work=false; // No longer work

return(0); // Exit from the function

case 64: Alert("Account blocked.");

Work=false; // No longer work

return(0); // Exit from the function

case 133:Alert("Trading prohibited.");

return(0); // Exit from the function

case 134:Alert("Not enough money to execute the transaction.");

return(0); // Exit from the function

default: Alert("An error has occurred ",Error); // Other options

return(0); // Exit from the function

}

}

//-------------------------------------------------------------- 11 --

int New_Stop(int Parametr) // Check stop-prick.

{

int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);// Min. distance

if (Parametr < Min_Dist) // If less than allowed.

{

Parametr=Min_Dist; // Set tolerance.

Alert("Stop distance increased;)

}

return(Parametr); // Return value.

}

 
Hello all! There was an indicator on the forum
https://forum.mql4.com/ru/56236/page933,Подскажите please how to open it in the terminal?

#property  indicator_separate_window
#property  indicator_buffers 3
#property   indicator_color1  Aqua
#property   indicator_width1  1
#property   indicator_color2  Red
#property   indicator_width2  1
#property   indicator_color3  Yellow
#property   indicator_width3  1

extern int       Fperiod=1;
extern int       Speriod=2;
extern int       Method=3;
extern int       PRICE_MODE=PRICE_WEIGHTED;

double   Buf0[];
double   Buf1[];                                
double   Buf2[];                                
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
   {
    SetIndexBuffer(0,Buf0);
    SetIndexBuffer(1,Buf1);
    SetIndexBuffer(2,Buf2);

    SetIndexStyle(0,DRAW_LINE);
    SetIndexStyle(1,DRAW_LINE);
    SetIndexStyle(2,DRAW_LINE);

    SetIndexEmptyValue(1,EMPTY_VALUE);   
    SetIndexEmptyValue(2,EMPTY_VALUE);   

    IndicatorShortName("Proba ");                
   
    SetIndexDrawBegin(0, 0);  
    SetIndexDrawBegin(1, 0);
    SetIndexDrawBegin(2, 0);

    return(0);
   }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
   {
    int i; 
    int limit;
    int counted_bars=IndicatorCounted();
    if(counted_bars<0) return(-1);
    if(counted_bars>0) counted_bars--;
    limit=Bars-counted_bars;
    for (i = limit;i>=0;i--)
        {  
         Buf0[i]=iMA(NULL,0,Fperiod,0,Method,PRICE_MODE,i)-iMA(NULL,0,Speriod,0,Method,PRICE_MODE,i);
         if(Buf0[i] > Buf0[i+1])
             {
              Buf1[i] = Buf0[i];
//              if(Buf0[i+1] > Buf0[i+2])
//                   Buf1[i+1] = Buf0[i+1];
//              else Buf2[i+1] = Buf0[i+1];
             }
         else
             {
              Buf2[i] = Buf0[i];
//              if(Buf0[i+1] < Buf0[i+2])
//                   Buf2[i+1] = Buf0[i+1];
//              else Buf1[i+1] = Buf0[i+1];
             }
        }              
    return(0);
   }

Reason: