strange valeur of STOP LOSS

 

hello, I start programming in MQL5, and I progress little by little but I do not understand an event. I put the code below but when I run the Script and look at the log in MT5 it tells me that my stop loss is not good (stop loss = 700,000). At the beginning I thought to myself that I had coded badly so I looked at other EAs that worked well but I did not find any difference with my code which would justify such a value. Do you have an idea ?



//+------------------------------------------------------------------+
//|                                               Test_programme.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Expert/Expert.mqh>
#include <Trade\Trade.mqh>



input int Stop_Loss=30;
input int Take_Profit=70;
input string Volume_position=0.1;
input int  EA_Magic=1;
CPositionInfo M_position;
CTrade M_trade;
CSymbolInfo M_symbol;

int STP;
int TKP;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int OnInit(){
M_symbol.Name(Symbol());
M_trade.SetExpertMagicNumber(EA_Magic);

STP = Stop_Loss;
TKP = Take_Profit;
   if(M_symbol.Digits()==5 || M_symbol.Digits()==3)
     {
      STP = STP*10;
      TKP = TKP*10;
     }
   return(0);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {  
  
   MqlTick latest_price;
   bool Buy_opened=false;
   bool Sell_opened=false;


   if(PositionSelect(_Symbol)==true) // we have an opened position
     {
      if(PositionGetInteger(POSITION_MAGIC)==1)
        {
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
           {
            Buy_opened=true;  //It is a Buy
           }
         else
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
              {
               Sell_opened=true; // It is a Sell
              }
        }
     }
   if(iRSI(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE)<30)
     {
      if(Buy_opened==true)
        {
         Alert("We already have a Buy Position!!!");
         return;
        }
      //request_trade.action=TRADE_ACTION_DEAL;
      //request_trade.symbol=_Symbol;
      //request_trade.volume=Volume_position;
      //request_trade.type=ORDER_TYPE_BUY;
      //request_trade.price=NormalizeDouble(latest_price.ask,_Digits);
      //request_trade.sl=NormalizeDouble(latest_price.ask-STP*_Point,_Digits);
      //request_trade.magic=EA_Magic;
      //request_trade.tp=NormalizeDouble(latest_price.ask+TKP*_Point,_Digits);
      //request_trade.price=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
      OP_BUY();
     

     }
   if(iRSI(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE)>80)
     {
      if(Sell_opened==true)
        {
         Alert("We already have a Sell Position !!!");
         return;
        }
      
      //request_trade.action=TRADE_ACTION_DEAL;
      //request_trade.symbol=_Symbol;
      //request_trade.symbol=Volume_position;
      //request_trade.type=ORDER_TYPE_SELL;
      //request_trade.price=NormalizeDouble(latest_price.ask,_Digits);
      //request_trade.sl=NormalizeDouble(latest_price.bid-STP*_Point,_Digits);
      //request_trade.magic=NormalizeDouble(latest_price.bid+TKP*_Point,_Digits);
      //request_trade.magic=EA_Magic;
      //request_trade.price=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
      OP_SELL();
      Alert("poisition vendeuse"); 
  }
  }
 
 void OP_BUY(){
   double StopLossLevel=0.0;
   double TakeProfitLevel=0.0;
      StopLossLevel=M_symbol.NormalizePrice(M_symbol.Ask()-STP);
      TakeProfitLevel=M_symbol.NormalizePrice(M_symbol.Ask()+TKP);

   double volume=Volume_position;
   if(volume!=0.0)
      M_trade.Buy(volume,NULL,M_symbol.Ask(),StopLossLevel,TakeProfitLevel);
  }
  
  
  //-----
 void OP_SELL(){

   double StopLossLevel=0.0;
   double TakeProfitLevel=0.0;
      StopLossLevel=M_symbol.NormalizePrice(M_symbol.Bid()+TKP);
      TakeProfitLevel=M_symbol.NormalizePrice(M_symbol.Bid()-STP);
   double volume=Volume_position;
   if(volume!=0.0)
      M_trade.Sell(volume,NULL,M_symbol.Bid(),StopLossLevel,TakeProfitLevel);
  }
 

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

Forum on trading, automated trading systems and testing trading strategies

EA opens Many Trades, How to Open 1 buy & 1 sell?

Vladimir Karputov, 2022.01.10 16:03

You have a huge mistake - you create an indicator handle at every tick.

REMEMBER: The MQL5 style implies that the indicator handle MUST be created ONLY ONCE and this must be done in OnInit !!!


An example of get values from the iRSI indicator

How to start with MQL5
How to start with MQL5
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 
StopLoss should be a price …. 700.000 doesnt Look like a price… you Take your code… put it on a script … and load on diferent Markets … see if your point/tick calculation works
 
SLLevel=Ask-SL*_Point