[ARCHIVE] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 3. - page 182

Всеволод  
NaVolne:


Why is this the case?

The EA processes every tick, and on every tick there is a check and opening of an order.... You need to remember the time of the last open position and pause for 15 minutes, or you may work by the opening or closing of a bar. It's better to be linked to bar time in the code.

double OpenBar=0; 
 
int start()
   {
    //Проверка на начало нового бара
    if (OpenBar==Time[0]) {return;} else {OpenBar=Time[0];}
    //ваш код
   }
Oleg  
dent:
How to order an advisor
Maxim Zaguzov  
OlegTs:
double OpenBar=0; 

 

int start()
   {
    //Проверка на начало нового бара
    double CurOpenBar=iOpen(NULL,PERIOD_M15,0);  
    if (OpenBar==CurOpenBar) {return;} else {OpenBar=CurOpenBar;}
    //ваш код
   }


something like that...

A bad example. Because the opening price of the candlesticks can be repeated. But the timing does not.
Oleg  
MaxZ:
Bad example. Because the opening price of the candlesticks can be repeated. But the timing does not.

I agree:)))
[Deleted]  
Thanks a lot guys, it's starting to work out :)
[Deleted]  

I'm trying to learn mcl4 and write an EA for tests. Some questions have arisen.

1) Is there a procedure for checking the analyzed history for missing minute candles for this or that pair? Maybe the procedure is already written and available on the net?

2) If there are gaps or just a small amount of history, how can the EA download the whole minute candlesticks for this or that pair from the necessary date or fill the gaps?

The idea is that the EA will check the correctness of the history while working and correct the gaps or small number of bars if necessary.
[Deleted]  

I'm learning a programming language, and I'm having trouble understanding the behaviour of the programme. I have written an EA based on the textbook, but I've twisted it to a slightly different principle of operation.

The Expert Advisor is based on 2 muwings averaging. The idea is simple, moving averages cross, order is closed and opposite one is opened. I have written an EA and it works fine. Of course it is at a slight disadvantage, but in accordance with the idea. The entire problem is as follows (in bold):

A=iMA(NULL,0,Period_MA_1,0,MODE_SMA, PRICE_CLOSE,1); // A

B=iMA(NULL,0,Period_MA_2,0,MODE_SMA,PRICE_CLOSE,1); // B

C=iMA(NULL,0,Period_MA_1,0,MODE_SMA,PRICE_CLOSE,2); // C

D=iMA(NULL,0,Period_MA_2,0,MODE_SMA,PRICE_CLOSE,2); // D

If I change the way of calculating moving averages (it can show plus sign) and put exponential method( MODE_SMA) instead of simple one (MODE_EEMA ) and one more(MODE_SMMA), so called smoothed method, everything goes wrong. The working principle becomes even more amusing (in the strategy tester) as the EA opens an order at the very beginning of the period set and closes it at the end. I.e., for a year it opens one single trade at the beginning of the period, and closes it at the end. As a result, the Expert Advisor has shown me a plus, but I do not need such a plus. I can flip a coin myself and open either Buy or Sell order and look, whether I'm winning or losing in a year. By the way, if I set MODE_SMA to MODE_LWMA (Linear Weighted Moving Average), the programme works fine.

Please, advise me, what is wrong? The principle is the same; just the way the moving averages are calculated has been changed a bit.

Here is the entire code of the program:

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

//| Based on two moving averages.mq4

//| Copyright © 2011, MetaQuotes Software Corp.

//| http://www.metaquotes.net |

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

#property copyright "Copyright © 2011, MetaQuotes Software Corp.

#property link "http://www.metaquotes.net"



//--------------------------------------------------------------- 1 --

// Numerical values for M15


extern int int Period_MA_1=6; // Period MA1

extern int Period_MA_2=15; // Period MA 2

extern double Lots =0.1; // Fixed number of lots



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

string Symb; // Name of financial instrument

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

int start()

{

int

Total, // Number of orders in the window

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

Ticket; // Order number

double

A, //Value. MA_1 value for the first bar from the end

MA_2 value for the first bar from the end

Price, // Price of the selected order

C, //Value. MA_1 for the second bar from the end

D, // Significance. MA_2 value for the second bar from the end

Lot; // Number of lots in the selected order

bool

Ans =false, // Server response after close

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.

Lot =OrderLots(); // Number of lots

}

}

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

// Trade criteria

A=iMA(NULL,0,Period_MA_1,0,MODE_SMA,PRICE_CLOSE,1); // A

B=iMA(NULL,0,Period_MA_2,0,MODE_SMA,PRICE_CLOSE,1); // B

C=iMA(NULL,0,Period_MA_1,0,MODE_SMA,PRICE_CLOSE,2); // C

D=iMA(NULL,0,Period_MA_2,0,MODE_SMA,PRICE_CLOSE,2); // D


if (A<C&C>D&B>=A) // МА1 line goes downwards,

{ // MA1 crosses MA2

Opn_B=true; // from above downward

Cls_S=true; //

}

if (B<D&D>C&&A>=B) // MA2 line goes downwards

{ // MA2 crosses MA1

Opn_S=true; // top-down

Cls_B=true; //

}

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

// Close orders

while(true) //Order closing 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("Trying 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 --

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

//Opening of orders

while(true) //Order closing 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,Lots,Ask,20,0,0, "EA 2xMA");//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,Lots,Bid,20,0,0, "EA 2xMA");//Open Sel.

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

{

Alert ("Sell order opened ",Ticket;)

return; // Exit from the start()

}

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



Всеволод  
neisseria:

I'm learning a programming language, and I'm having trouble understanding the behaviour of the programme. I have written an EA based on the tutorial, but I have changed it to a slightly different principle.



When closing the order it would be nice to decrease the Total, or, better yet, let's not overthink the program and make a function that returns the number of open orders for the symbol and magic number.

Four moving averages are too much and it is better to show them in two periods and at different offsets. It is recommended to display them on the screen and see how they behave.