Questions from Beginners MQL4 MT4 MetaTrader 4 - page 255

 
Mihail Matkovskij #:

How do you calculate tp?

I specify in the variable double tp=50;

 
Roman Epifanov #:

I specify in the variable double tp=50;

double takeProfit;
takeProfit = NormalizeDouble(Bid - tp * Point(), Digits()); // TP для Sell
 
Mihail Matkovskij #:

now writes

2021.12.04 13:56:09.509 TestGenerator: unmatched data error (high value 1.13110 at 2021.12.03 23:45 is not reached from the lowest timeframe, high price 1.13104 mismatches)


//+------------------------------------------------------------------+
//|                                                         repa.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int i;
   double lot_sum;
   bool trend;
   double ema;
   double sl=50.0,tp=50.0;
   tp = NormalizeDouble(Bid - tp * Point(), Digits()); // TP для Sell
   double lot=0.01;
   while (i<OrdersTotal())
   {
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   lot_sum=lot_sum + OrderLots(); // сумма лотов открытых позиций
   
   
   }
   
   ema=iMA(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,0); //взяли значение средней
   if (Close[0]<ema)trend=true ;else if(Close[0]>ema)trend=false; //определили тренд
   
   if (Close[1]<Open[1]) {OrderSend(Symbol(),OP_SELL,lot,Bid,2,Ask+sl,tp,"Candle sell, Close[0]<MA");};
   
   
  }
//+------------------------------------------------------------------+
 
Mihail Matkovskij #:

put it on for a minute... it works...

but the stop level is different -

1 2021.09.16 05:59 sell 1 0.01 1.18190 51.18192 1.18140 0.00 10000.00


 
Roman Epifanov #:

now writes

2021.12.04 13:56:09.509 TestGenerator: unmatched data error (high value 1.13110 at 2021.12.03 23:45 is not reached from the lowest timeframe, high price 1.13104 mismatches)


void OnTick()
  {
//---
   int i;
   double lot_sum;
   bool trend;
   double ema;
   double sl=50.0,tp=50.0;
   double stopLoss, takeProfit, openPrice;
   double lot=0.01;
   while (i<OrdersTotal())
   {
     OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
     lot_sum=lot_sum + OrderLots(); // сумма лотов открытых позиций
   
   }
   
   ema=iMA(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,0); //взяли значение средней

   if (Close[0]<ema)
     trend=true ;
   else if(Close[0]>ema)
     trend=false; //определили тренд
    
   takeProfit = NormalizeDouble(Bid - tp * Point(), Digits()); // TP для Sell
   stopLoss = NormalizeDouble(Bid + sl * Point(), Digits()); // SL для Sell
   openPrice = NormalizeDouble(Bid, Digits());

   if (Close[1]<Open[1]) {
     OrderSend(Symbol(), OP_SELL, lot, openPrice, 2, stopLoss, takeProfit, "Candle sell, Close[0]<MA");
   }
   
  }

All rules are online, so check it yourself.

 
Mihail Matkovskij #:

Rules all online, so check for yourself.

Thank you!

 

Good evening gentlemen, could you please advise why the advisor gives an error when opening a second order?


The text of the advisor itself



//+----------------------Параметры-----------------------------------+

input int TakeProfit = 100; // Profit in pips

input int StopLoss = 50; // Loss in pips

input double Lot = 0.01; // Start lot

input double Multilot = 1.95; // multiplier for starting lot

input int Slippage =30; // Slippage

input int MagicNumber = 333 ; // Unique number


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

//| Expert initialization function |

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

double tp=0;

double sl=0;

int OnInit()

{

tp=NormalizeDouble(TakeProfit*Point(),Digits());

sl=NormalizeDouble(StopLoss*Point(),Digits());

return(INIT_SUCCEED);

}


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

//| Expert tick function |

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

void OnTick()

{

//--the loop checks for open orders when the Expert Advisor is activated and calculates them.

int buy=0; //Number of BUY orders

int sell=0; //Number of SELL orders


for(int i = OrdersTotal()-1; i>=0; i--)

if(OrderSelect(i, SELECT_BY_POS,MODE_TRADES))

if(OrderSymbol()==Symbol())

if(OrderMagicNumber()== MagicNumber)

{

if(OrderType()==OP_BUY)

buy++;

if(OrderType()==OP_SELL)

sell++;

}


//--the loop receives the data of the last closed order when the EA is activated

int type=-1; //the type of a closed order

int history_close_orders=0; //number of closed orders

double lot=0; //Lot of the last closed order

double profit=0; //Profit at which the last order was closed

datetime time=0; //Closing time of the last order


for(int i = OrdersHistoryTotal()-1; i>=0; i--)

if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))

if(OrderSymbol()==Symbol())

if(OrderMagicNumber()== MagicNumber)

if(OrderCloseTime()>time)

{

history_close_orders++;

time = OrderCloseTime();//the last order close time

lot= OrderLots();//Lot of the last closed order

type=OrderType();// Type of the last closed order

profit=OrderProfit()*OrderSwap()*OrderCommission();//Profit of the last closed order

}

//calculate the lot with which the EA will work

double Lots_Work=0;// working lot


if(history_close_orders==0 || profit>0)// if there are no closed orders or the last order closed on the profit

Lots_Work=Lot;// working lot is equal to the initial lot


if(history_close_orders>0 && profit<0) // if the last order closed in the red

Lots_Work=1;// working lot will be equal to the lot of the last order multiplied by the multiplier


//open the first buy order

if(buy==0 || profit>0)

{

int ticket=OrderSend(Symbol(),OP_BUY,Lots_Work,Ask,Slippage,0,0, "Expert Advisor Test fair",MagicNumber,0,clrGreen);

if(ticket<0)

Print("OrderSend failed with error #",GetLastError());

else

Print("OrderSend completed successfully");

}


// Opening of the second and the subsequent Buy orders

if(buy==0 && type==OP_BUY && profit<0)

{

int ticket=OrderSend(Symbol(),OP_BUY,Lots_Work,Ask,Slippage,0,0, "EA Test fair",MagicNumber,0,clrGreen);

if(ticket<0)

Print("OrderSend failed with error #",GetLastError());

else

Print("OrderSend completed successfully");

}


// Opening of the first Sell order

if(sell==0 || profit>0)

{

int ticket=OrderSend(Symbol(),OP_SELL,Lots_Work,Bid,Slippage,0,0, "EA Test fair",MagicNumber,0,clrRed);

if(ticket<0)

Print("OrderSend failed with error #",GetLastError());

else

Print("OrderSend completed successfully");

}

// Opening of the second and the subsequent Buy orders

if(sell==0 && (type==OP_SELL && profit<0))

{

int ticket=OrderSend(Symbol(),OP_SELL,Lots_Work,Bid,Slippage,0,0, "Expert Advisor Test fair",MagicNumber,0,clrRed);

if(ticket<0)

Print("OrderSend failed with error #",GetLastError());

else

Print("OrderSend completed successfully");

}


//--Modify the order by adding Take Profit and Stop Loss

for(int i = OrdersTotal()-1; i>=0; i--)

if(OrderSelect(i, SELECT_BY_POS))

if(OrderSymbol()==Symbol())

if(OrderMagicNumber()== MagicNumber)

{

if(OrderType()==OP_BUY)

{

if(OrderStopLoss()==0 || OrderTakeProfit()==0)

if(OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-sl,Digits(),NormalizeDouble(OrderOpenPrice()+tp,Digits(),0,clrGreen))

Print("OrderModify OK!");

else

Print("OrderModify BUY failed #", GetLastError());

}

if(OrderType()==OP_SELL)

{

if(OrderStopLoss()==0 || OrderTakeProfit()==0)

if(OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+sl,Digits(),NormalizeDouble(OrderOpenPrice()-tp,Digits(),0,clrRed))

Print("OrderModify OK!");

else

Print("OrderModify SELL failed #", GetLastError());

}

}


}

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


 
My old laptop with i3 processor still had WinXP 32 bit installed, unsupported MT5. Recently increased the memory to 6GB, I want to install 64 bit OS that would run MT4 and MT5. Which is preferable - Win7 or Win8.1 ?
 
atztek #:
My old laptop with i3 processor still had WinXP 32 bit installed, unsupported MT5. Recently increased memory to 6GB, I want to install 64 bit OS that would run MT4 and MT5. Which is preferable - Win7 or Win8.1 ?

10

 
Andrey Sokolov #:

10

This version is installed on a new laptop, tired of constant updates. Besides, people write that eight runs better than ten on old laptops. I would leave WinXP on my old laptop but MT5 will not run on it, moreover 3Gb of memory is not enough for XP. Still, if the choice is between 7 and 8.1, what is better for performing optimizations on MT4/5 ?
Reason: