StopLoss doesnt working, where did i go wrong ?

 

Hi friends, i wrote simple moving averege crossing buy/sell robot but StopLoss doesnt working.

When mov20 cross up from mov50 it opens buy but it must be close order when ma20 croos ma50 to down. 

Please help this amateur coder :)


//+------------------------------------------------------------------+
//|                                                     MovCross.mq4 |
//|                                                      Cuneyt Ates |
//|                                                      @CuneytAtes |
//+------------------------------------------------------------------+
#property copyright "Cuneyt Ates"
#property link      "@CuneytAtes"
#property version   "1.00"




void OnTick()
  {
     string sinyal = "";  
     
     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);
     
     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 buySL = (ma20<ma50 && ma20prev>ma50prev);
     double sellSL= (ma20>ma50 && ma20prev<ma50prev);
     

     
     if (ma20>ma50 && ma20prev<ma50prev  ) 
     {
      sinyal = "Buy"; 
     
     if (ma20<ma50 && ma20prev>ma50prev)
     {
      sinyal = "Sell"; 
     }
     
     if (sinyal=="Buy" && OrdersTotal()==0)
     OrderSend (_Symbol, OP_BUY,0.02,Ask,NULL, buySL, NULL,NULL,0,0,NULL);
     if (sinyal=="Sell" && OrdersTotal()==0)
     OrderSend (_Symbol, OP_SELL,0.02,Bid,NULL, sellSL, NULL,NULL,0,0,NULL);
     
     
     }
  }

 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

You don't have any code to exit trades so why would you expect it to?

 
Keith Watford:
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

You don't have any code to exit trades so why would you expect it to?

Keith Watford, thank you for your response i ll care section furthermore.

There is a "buySL" or "sellSL" at ordersend() line at stoploss section, it is my exit.
 
double buySL = (ma20<ma50 && ma20prev>ma50prev);

You don't really expect that to work do you? The result will be a bool, not a price.

When you have a buy signal, you should close any sells.

 
Keith Watford:

You don't really expect that to work do you? The result will be a bool, not a price.

When you have a buy signal, you should close any sells.

Do you know what , i looked lot of forum subject since 2018 and realize that almost every Metatrader admin likes to reprimand or mocking the forum followers ! Yes may be you are tired of some basic questions but we dont deserve that behaviour.

I am 48 years old new coder, and trying to improve myself.

I dont need "You don't really expect that to work do you? The result will be a bool, not a price." kind of sentences, it would be more helpfull and polite like "Please use  bool buySL = (ma20<ma50 && ma20prev>ma50prev); or please look mt4 documantation bool section and if you can find the solutions come again" etc !

How hard it would be?

 
cuneytates:

Do you know what , i looked lot of forum subject since 2018 and realize that almost every Metatrader admin likes to reprimand or mocking the forum followers ! Yes may be you are tired of some basic questions but we dont deserve that behaviour.

I am 48 years old new coder, and trying to improve myself.

I dont need "You don't really expect that to work do you? The result will be a bool, not a price." kind of sentences, it would be more helpfull and polite like "Please use  bool buySL = (ma20<ma50 && ma20prev>ma50prev); or please look mt4 documantation bool section and if you can find the solutions come again" etc !

How hard it would be?

Do you know what, I don't like being referred to as Miss Watford, why you should think that I am a female I don't know! I have edited your 2 replies that you called me "Miss"!

I wasn't reprimanding or mocking you. Just trying to make you think.

Don't worry, i won't bother to try to help you in future.

 
Keith Watford:

Do you know what, I don't like being referred to as Miss Watford, why you should think that I am a female I don't know! I have edited your 2 replies that you called me "Miss"!

I wasn't reprimanding or mocking you. Just trying to make you think.

Don't worry, i won't bother to try to help you in future.

Sorry i thouht Keith is female name, Mr Watford.

On the other hand you never helped me, you didnt show me the source or solution.

Thank you.
 
cuneytates:
Sorry i thouht Keith is female name, Mr Watford.

On the other hand you never helped me, you didnt show me the source or solution.

Thank you.

I gave you the solution, you quoted my post with the solution in it.

 
cuneytates:

Hi friends, i wrote simple moving averege crossing buy/sell robot but StopLoss doesnt working.

When mov20 cross up from mov50 it opens buy but it must be close order when ma20 croos ma50 to down. 

Please help this amateur coder :)


//|                                   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)
  {
//---
   
  }



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 && OrdersTotal()==0 ) 
     {
        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 && OrdersTotal()==0 )
     {
         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--)
  {
    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);
}
@ cuneytates
 
Mehmet Bastem :
@ cuneytates

It had to be closed without intersecting. Couldn't close my code master :(

 
cuneytates:

Google gir, "adam" yaz, fotoğrafın çıkıyor Mehmet kardeşim.

Uğraştırdım seni ama çok iyi oldu, şimdi kafamda oturdu birşeyler.

Çok teşekkür ediyorum, iyi bayramlar diliyorum.

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