StopLoss doesnt working, where did i go wrong ? - page 2

 
Fernando Carreiro:
Please write in English (or at least in both languages). This is an English forum!

You right Mr Carreiro,

I wrote to Mr Mehmet; "Write your name as a "man" to Google, see your picture (as a King). You gave a time but ok i realized something about coding.  Thank you for your help." 

On the other hand, his code doesnt solve the problem. Because i need some kind of loop without SL or TP. If ma20 cross up ma50 buy, it must automatically stop ma20 cross below to ma50. Endless loop like buy-close-sell-close-buy-close etc. 


 
cuneytates:

Kesişmeden kapaması lazımdı. Kapamadı kod üstadım :(

TestResult
//+------------------------------------------------------------------+
//|                                               SLTPCuneytAtes.mq4 |
//|                                        Copyright 2021, HaskayaFx |
//|                                   https://www.haskayayazilim.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, HaskayaFx"
#property link      "https://www.haskayayazilim.net"
#property version   "1.00"
#property strict

input int Stoploss=300;
input int Profit=600;
input double Lots=0.02;
input bool AutoClose=true;
// input string BuySellComment="CunetAtesEA:"
// input int   MagicAl=123321;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }

int BuySelKontrol(int GlnSymbolType )
{
  int sonuc=0;
  int total = OrdersTotal();
   for(int i=total-1;i>=0;i--)
    {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderSymbol()!=Symbol() && OrderType()==GlnSymbolType ) sonuc=1;     
    }
 
   
    return(sonuc);
}




void OnTick()
  {
     string sinyal = "";  
     int ticket;
     
     
     double ma20 = iMA(_Symbol,PERIOD_CURRENT,20,0,MODE_SMA,PRICE_MEDIAN,0);
     double ma20prev = iMA(_Symbol,PERIOD_CURRENT,20,0,MODE_SMA,PRICE_MEDIAN,1);
     double ma50 = iMA(_Symbol,PERIOD_CURRENT,50,0,MODE_SMA,PRICE_MEDIAN,0);
     double ma50prev = iMA(_Symbol,PERIOD_CURRENT,50,0,MODE_SMA,PRICE_MEDIAN,1);

     if (ma20>ma50 && ma20prev<ma50prev && BuySelKontrol(0)==0) 
     
     {  if(AutoClose) CloseAllOrdes();
        ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,2,0,0,NULL,0,0,clrRed);
        if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
           {
            ticket=OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice()-Stoploss*Point,OrderOpenPrice()+Profit*Point,0,CLR_NONE);
           }
     }

     
     
     if (ma20<ma50 && ma20prev>ma50prev && BuySelKontrol(1)==0)
     {
        if(AutoClose) CloseAllOrdes();
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,2,0,0,NULL,0,0,clrRed);
         if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
           {
            ticket=OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice()+Stoploss*Point,OrderOpenPrice()-Profit*Point,0,CLR_NONE);
           }
     }
     
  }

//+------------------------------------------------------------------+
int CloseAllOrdes()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    bool snc=OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                          break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
       if (GetLastError()>0) Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return(0);
}


 
Mehmet Bastem:

I couldnt understand that first buy order and 244 modification which caused slowest laptop all time.

Is it normal ?



 
cuneytates:

I couldnt understand that first buy order and 244 modification which caused slowest laptop all time.

Is it normal ?



Order number does no harm to you. Please don't get stuck with this. It would be wrong to sort by order number. You have chosen to order the Order Number in reverse order. Arrow You can see it with the up arrow key. Order number 244 should be 3.