[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 470

 
Vinin:

How many orders are opened now?

In the tester it opens endlessly, in the demo it opens as many as necessary.
 

Hi all.

I have a question from the MQL4 tutorial.

When passing MA from above or below it does not give any message?

Please write what is wrong.

bool Fact_Up= true;
bool Fact_Dn = true;
//-----------------------------------------------------------------
int start()
   {
   double MA;
//-----------------------------------------------------------------
   MA= iMA(NULL,0,Period_MA,0,MODE_SMA,PRICE_CLOSE,0);
//-----------------------------------------------------------------
   if(Bid > MA && Fact_Up == true)
      {
      Fact_Dn = true;
      Fact_Up = false;
      Alert("Цена находится выше МА(",Period_MA,").");
      }
//------------------------------------------------------------------
   if(Bid < MA && Fact_Dn == true)
      {
      Fact_Up = true;
      Fact_Dn = false;
      Alert("Цена находится ниже МА(",Period_MA,").");
      }
//------------------------------------------------------------------
   return;
}
 
Pacman:

Hi all.

I have a question from the MQL4 tutorial.

When passing MA from above or below it does not give any message?

Please write what is wrong.


In the tester?
 
PapaYozh:

In the tester?
No, on a demo account.
 

Good afternoon,
I'm making an EA that moves the stop to breakeven after opening. That is, it modifies the order, but the problem is that it constantly modifies what looks like a trailing stop. How to make it modify once and then only once. I even inserted a variable in the code that mod1=1 but then mod1 has to be larger, but the language for some reason does not notice it.

Thank you!

while(true)
{
if (tip==0 && open_b==true && mod1<=1 && Bid>ma+15*Point)
{
Alert("popitkaa perenesti stop v b/u" ,ticket);
RefreshRates();
modify=OrderModify(ticket,Ask,Bid-mod*Point,0,0);
if(modify==true)
{
Alert("zakrit modificirovanij order",ticket);
mod1++;
return;
}
return;
}
if (tip==1 && open_s==true && mod1<=1 && Bid<ma+15*Point)
{
Alert("popitkaa perenesti stop ",ticket, "v b/u");
RefreshRates();
modify=OrderModify(ticket,Bid,Ask+mod*Point,0,0);
if(modify==true)
{
Alert("zakrit modificirovanij order",ticket);
mod1++;
return;
}
return;
}
break;
}

 
'00FF33' - invalid number C:\Alpari\experts\customer.mq4 (66, 173) ??? How do I enter the colour correctly?
 
kostikk:

Hello,
I'm making an advisor that moves the stop to breakeven after opening. That is, it modifies the order, but the problem is that it is constantly modifying what is like a trailing stop. How to make it modify once and then only once. I even added a variable in the code, mod1=1 but mod1 has to be bigger, but the language doesn't know what the problem is.

Thank you!

Already done for you and it works fine

KimIV01.11.2006 18:49

I recommend my tested code
extern int    LevelProfit  = 30;            // Уровень профита
extern int    LevelWLoss   = 2;             // Уровень безубытка
extern color  clModifyBuy  = Aqua;          // Цвет модификации покупки
extern color  clModifySell = Tomato;        // Цвет модификации продажи
 
 
//+----------------------------------------------------------------------------+
//|  Перенос уровня StopLoss                                                   |
//|  Параметры:                                                                |
//|    ldStop  - уровень StopLoss                                              |
//|    lcColor - цвет модификации                                              |
//+----------------------------------------------------------------------------+
void ModifyStopLoss(double ldStop, color lcColor=CLR_NONE) {
  double ldOpen=OrderOpenPrice();
  double ldTake=OrderTakeProfit();
 
  OrderModify(OrderTicket(), ldOpen, ldStop, ldTake, 0, lcColor);
}
 
//+----------------------------------------------------------------------------+
//|  Перенос стопа в безубыток                                                 |
//+----------------------------------------------------------------------------+
void MovingInWL() {
  int i, k=OrdersTotal();
 
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==Symbol()) {
        if (OrderType()==OP_BUY) {
          if (OrderStopLoss()-OrderOpenPrice()<LevelWLoss*Point) {
            if (Bid-OrderOpenPrice()>LevelProfit*Point) {
              ModifyStopLoss(OrderOpenPrice()+LevelWLoss*Point, clModifyBuy);
            }
          }
        }
        if (OrderType()==OP_SELL) {
          if (OrderStopLoss()==0 || OrderOpenPrice()-OrderStopLoss()<LevelWLoss*Point) {
            if (OrderOpenPrice()-Ask>LevelProfit*Point) {
              ModifyStopLoss(OrderOpenPrice()-LevelWLoss*Point, clModifySell);
            }
          }
        }
      }
    }
  }
}
//+----------------------------------------------------------------------------+



https://forum.mql4.com/ru/4689

 
Dimka-novitsek:
'00FF33' - invalid number C:\Alpari\experts\customer.mq4 (66, 173) ??? How do I enter the colour correctly?


Actually there is a special type of variables in the language - color is called. For example:

double Velichina;

MyLine;

Velichina=2.569;

MyLine=Red;

 

How do I change the name of the graph window?

 

Help! For some reason does not open SELL positions, only BUY???



#property copyright "eevviill"
#property link "itisallillusion@gmail.com"


extern string os = "Настройки лота и стопов";
extern double Lot = 0.1;
extern bool use_percent_of_deposit = false;
extern double Risk = 1;
extern int StopLoss = 0;
extern int TakeProfit = 0;

extern string slug = "Дополнительные настройки";
extern string pus1 = "";
extern bool use_exit = true;
extern int slippage = 2;
extern int magic = 4335;
extern string comment = "HA nonlag";
extern int MaxAttempts = 4;

extern string pus2 = "";
extern string V_R = "Время работы";
extern bool use_work_time = true;
extern int Start = 0;
extern int Stop = 24;

extern string pus3 = "";
extern string mar = "Мартингейл";
extern bool use_martini = false;
extern int CloseProfit_pips = 22;
extern double Lot_koef = 1.6;
extern int step = 40;

extern string pus4 = "";
extern string bez_ub = "Безубыток";
extern bool use_bezubitok = false;
extern int Bez_Ub_Level = 8;
extern int Bez_Ub_Size = 1;

extern string pus5 = "";
extern string tre_st = "Трейлинг стоп";
extern bool use_traling_stop = false;
extern int Trailing_Stop = 10;

extern string pus6 = "";
extern string ATR_p = "ATR";
extern bool use_ATR = false;
extern int ATR_period = 14;
extern double ATR_min_level = 0.0004;

extern string pus7 = "";
extern string ind1 = "HA nonlagma v4";
extern int Price = 0;
extern int Length = 21;
extern int Displace = 0;
extern int Filter = 0;
extern int Color = 1;
extern int ColorBarBack = 0;
extern double Deviation = 0;

extern double Cycle = 4;






double SLbuy=0, SLsell=0, TPbuy=0, TPsell=0;
int buy,sell,Sig_p,Sig_ex_p;
static int prevtime = 0;





//////////////////////////////////////////////////////////////
int init()
{
prevtime = iTime(Symbol(),0,0);


//ATR_level(4) && JPY_Point
if (StringFind(Symbol(), "JPY", 0) != -1) ATR_min_level *=100;


return;
}





////////////////////////////////////////////////////////////
int start(){
if (iTime(Symbol(), 0, 0) == prevtime) {Sleep(50);return(0);}
prevtime = iTime(Symbol(),0,0);

//проверка на разрешение торговли от брокера
while(!IsTradeAllowed()) Sleep(2000);

//фильтр ATR
if(use_ATR)
{
double ATR = iATR(Symbol(),0,ATR_period,1);
if(ATR<ATR_min_level) return;
}

//фильтр время
if(use_work_time)
{
if(Hour()<Start || Hour()>=Stop) return;
}

//мартингейл
if(use_martini) {use_bezubitok=false;use_traling_stop=false;use_percent_of_deposit=false;use_exit=false;}
if(use_martini) martini_f();

//закрытие всех ордеров при общем профите(пункты)- для мартини
if(use_martini)
{
if(Profit_f()>=CloseProfit_pips) Close_f();
}

//трейлинг стоп
if((Bez_Ub_Level>Trailing_Stop) && use_traling_stop) use_bezubitok=false;
if(use_traling_stop) Traling_Stop_f();

//безубыток
if(use_bezubitok) BezUb();

//SL && TP
if(!use_martini)
{
if(StopLoss>0) {SLbuy=NormalizeDouble(Ask-StopLoss*Point,Digits); SLsell=NormalizeDouble(Bid+StopLoss*Point,Digits);}
if(TakeProfit>0) {TPbuy=NormalizeDouble(Ask+TakeProfit*Point,Digits);TPsell=NormalizeDouble(Bid-TakeProfit*Point,Digits);}
}

//расчёт лота от % депозита
if(use_percent_of_deposit) Lot=GetLot();

//Сигнал на вход
Sig_p =Sig_f();
//Сигнал на выход
Sig_ex_p = Sig_ex_f();


//Выход
for(int i=OrdersTotal()-1; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS);
{
if(OrderMagicNumber()==magic)
{
bool ticket_ex;
for (int j_ex = 0;j_ex < MaxAttempts; j_ex++)
{
while(IsTradeContextBusy()) Sleep(2000);

if(OrderType()==OP_BUY && ((use_exit && Sig_p<0))) ticket_ex=OrderClose(OrderTicket(),OrderLots(),Bid,slippage,Yellow);
if(OrderType()==OP_SELL && ((use_exit && Sig_p>0))) ticket_ex=OrderClose(OrderTicket(),OrderLots(),Ask,slippage,Yellow);
if(ticket_ex==true)break;
}
}
}
}


//подсчёт открытых позиций
CountOpenPositions();

//Вход
int ticket_op=0;
for (int j_op = 0; j_op < MaxAttempts; j_op++)
{
while(IsTradeContextBusy()) Sleep(2000);

if(Sig_p>0 && buy==0&& sell==0) ticket_op=OrderSend(Symbol(),OP_BUY,Lot,Ask,slippage,SLbuy,TPbuy,comment,magic,0,Green);
if(Sig_p<0 && sell==0&& buy==0) ticket_op=OrderSend(Symbol(),OP_SELL,Lot,Bid,slippage,SLsell,TPsell,comment,magic,0,OrangeRed);
if(ticket_op>-1)break;
}





return(0);
}








//расчёт функций
/////////////////////////////////////////////////////////////////////////////////////////////
int Sig_f()
{
//объявление индикаторов
double ind1 = iCustom(Symbol(),0,"Heiken_ashi_nonlagma_v4",Price,Length,Displace,Filter,Color,ColorBarBack,Deviation,1,1); //buy
double ind2 = iCustom(Symbol(),0,"Heiken_ashi_nonlagma_v4",Price,Length,Displace,Filter,Color,ColorBarBack,Deviation,2,1); //sell


//сигнал для бай
if(ind1<50000) return(1);

//сигнал для сел
if(ind2<50000) return(-1);


return(0);
}

/////////////////////////////////////////////////////////////////////////////////////////////
int Sig_ex_f()
{
//объявление индикаторов
double ind1 = iCustom(Symbol(),0,"Heiken_ashi_nonlagma_v4",Price,Length,Displace,Filter,Color,ColorBarBack,Deviation,1,1); //exit sell
double ind2 = iCustom(Symbol(),0,"Heiken_ashi_nonlagma_v4",Price,Length,Displace,Filter,Color,ColorBarBack,Deviation,2,1); //exit buy


//сигнал для выход селл
if(ind1<50000) return(1);

//сигнал для выход бай
if(ind2<50000) return(-1);



return(0);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double GetLot()
{
double Free =AccountFreeMargin();
double One_Lot =MarketInfo(Symbol(),MODE_MARGINREQUIRED);
double Min_Lot =MarketInfo(Symbol(),MODE_MINLOT);
double Max_Lot =MarketInfo(Symbol(),MODE_MAXLOT);
double Step =MarketInfo(Symbol(),MODE_LOTSTEP);
double Lot =MathFloor(Free*Risk/100/One_Lot/Step)*Step;
if(Lot<Min_Lot) Lot=Min_Lot;
if(Lot>Max_Lot) Lot=Max_Lot;
if(Lot*One_Lot>Free) return(0.0);

return(Lot);
}

////////////////////////////////////////////////////////////////
void BezUb()
{
for (int i=OrdersTotal()-1; i>=0; i--)
{
OrderSelect(i, SELECT_BY_POS);
{
if (OrderMagicNumber()==magic)
{
if (OrderType()==OP_BUY)
{
if(OrderStopLoss()!=OrderOpenPrice()+NormalizeDouble(Bez_Ub_Size*Point,Digits))
{
if (Bid>OrderOpenPrice()+Bez_Ub_Level*Point && (OrderStopLoss()<OrderOpenPrice() || OrderStopLoss()==0))
{
OrderModify(OrderTicket(),0,OrderOpenPrice()+NormalizeDouble(Bez_Ub_Size*Point,Digits),OrderTakeProfit(),0,Blue);
}
}
}


if (OrderType()==OP_SELL)
{
if(OrderStopLoss()!=OrderOpenPrice()-NormalizeDouble(Bez_Ub_Size*Point,Digits))
{
if (Ask<OrderOpenPrice()-Bez_Ub_Level*Point && (OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()==0))
{
OrderModify(OrderTicket(),0,OrderOpenPrice()-NormalizeDouble(Bez_Ub_Size*Point,Digits),OrderTakeProfit(),0,Blue);
}
}
}
}
}
}
}

///////////////////////////////////////////////////////////////////////////
void Traling_Stop_f()
{
for (int i=OrdersTotal()-1; i>=0; i--)
{
OrderSelect(i, SELECT_BY_POS);
{
if (OrderMagicNumber()==magic)
{
double Or_St_Lo = OrderStopLoss();
double Or_Op_Pr = OrderOpenPrice();
if(Or_St_Lo==0) Or_St_Lo=Or_Op_Pr;
if((OrderStopLoss()!=Or_St_Lo+NormalizeDouble(Trailing_Stop*Point,Digits)) || (OrderStopLoss()!=Or_St_Lo-NormalizeDouble(Trailing_Stop*Point,Digits)))
{
if (OrderType()==OP_BUY && Bid-Or_St_Lo>=Trailing_Stop*Point && Bid>Or_St_Lo && ((Bid-Or_St_Lo+Trailing_Stop*Point)/Point>MarketInfo(Symbol(),MODE_STOPLEVEL))) OrderModify(OrderTicket(),0,Or_St_Lo+NormalizeDouble(Trailing_Stop*Point,Digits),OrderTakeProfit(),0,LightBlue);
if(OrderType()==OP_SELL && Or_St_Lo-Ask>=Trailing_Stop*Point && Ask<Or_St_Lo && ((Or_St_Lo-Trailing_Stop*Point-Ask)/Point>MarketInfo(Symbol(),MODE_STOPLEVEL))) OrderModify(OrderTicket(),0,Or_St_Lo-NormalizeDouble(Trailing_Stop*Point,Digits),OrderTakeProfit(),0,OrangeRed);
}
}
}
}

}

///////////////////////////////////////////////////////////////////////////////////
void martini_f()
{
int nor_lot;
if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.1) nor_lot=1;
if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.01) nor_lot=2;

CountOpenPositions();

double Mar_Lot_b=Lot*buy*Lot_koef;
double Mar_Lot_s=Lot*sell*Lot_koef;

for(int i=OrdersTotal()-1; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS);
{
if(OrderMagicNumber()==magic)
{
if(OrderSymbol()==Symbol())
{
break;
}
}
}
}



if(OrderType()==OP_BUY)
{
//
if(Bid<=OrderOpenPrice()-step*Point) OrderSend(Symbol(),OP_BUY,NormalizeDouble(Mar_Lot_b,nor_lot),Ask,slippage,0,0,comment,magic,0,Green);
}


if(OrderType()==OP_SELL)
{
//
if(Ask>=OrderOpenPrice()+step*Point) OrderSend(Symbol(),OP_SELL,NormalizeDouble(Mar_Lot_s,nor_lot),Bid,slippage,0,0,comment,magic,0,OrangeRed);
}



}

///////////////////////////////////////////////////////////////////////////////////
int Profit_f()
{
int prof;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS)==true)
{
if(OrderMagicNumber()==magic)
{
if (OrderType()==OP_BUY) prof+=(Bid - OrderOpenPrice())/Point;
if (OrderType()==OP_SELL) prof+=(OrderOpenPrice()-Ask) /Point;
}
}
}

return(prof);
}

////////////////////////////////////////////////////////////////////////////////////
void CountOpenPositions()
{
buy=0;
sell=0;

for (int i=OrdersTotal()-1; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS);
{
if(OrderMagicNumber()==magic)
{
if(OrderType()==OP_BUY) buy++;
if(OrderType()==OP_SELL) sell++;
}
}
}

}

////////////////////////////////////////////////////////////////////////////////
void Close_f()
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS);
{
if(OrderMagicNumber()==magic)
{
if(OrderSymbol()==Symbol())
{
bool ticket_ex;
for (int j_ex = 0;j_ex < MaxAttempts; j_ex++)
{
while(IsTradeContextBusy()) Sleep(2000);

if(OrderType()==OP_BUY ) ticket_ex=OrderClose(OrderTicket(),OrderLots(),Bid,slippage,Yellow);
if(OrderType()==OP_SELL) ticket_ex=OrderClose(OrderTicket(),OrderLots(),Ask,slippage,Yellow);
if(OrderType()==OP_SELLSTOP || OrderType()==OP_BUYSTOP || OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYLIMIT) ticket_ex=OrderDelete(OrderTicket(),CLR_NONE);
if(ticket_ex==true)break;
}
}
}
}
}

}

Thanks in advance!
Reason: