MarketInfo(Symbol(),MODE_STOPLEVEL) to get the minimum Stoploss or Takeprofit level still error 130

 

From the topic u will get fast my problem I'm a beginner EA programmer, this is my first EA pls see my Code I really dont get why its still not working, I mean why do I get still "invalid error"

//+------------------------------------------------------------------+
//|                                          breakOut_TRF_v4.0.0.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
#include <stderror.mqh>
#include <stdlib.mqh>

/*
   Erster Testanlauf mal schauen wie es wird...
*/

extern double LotSize=1.00;
extern int slippage=30;
extern int magicNumber=1234;
extern double stopLoss=10;
extern double takeProfit=20;

double pips;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---   
   Comment("Expert Advisor: breakOut_TRF_v4.0.0 is loaded successful");

   pips=Point;
   if(Digits()==3 || Digits()==5){
      pips =Point*10;
   }else{
      pips=Point;
   }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CheckForSignal();
  }
//+------------------------------------------------------------------+
//| Function "CheckForSignal()"                                      |
//+------------------------------------------------------------------+
void CheckForSignal(){
   static datetime candletime=0;
   if(candletime!=Time[0]){
      double upArrow=iCustom(Symbol(),PERIOD_CURRENT,"breakOut_TRF_v4.0.0",0,1);
      if(upArrow != EMPTY_VALUE){
         EnterTrade(OP_BUY);
      }
      double downArrow=iCustom(Symbol(),PERIOD_CURRENT,"breakOut_TRF_v4.0.0",1,1);
      if(downArrow != EMPTY_VALUE){
         EnterTrade(OP_SELL);
      }
      candletime=Time[0];
   }
}
//+------------------------------------------------------------------+
//| Function "EnterTrade()"                                          |
//+------------------------------------------------------------------+
void EnterTrade(int type){

   int err=0;
   double price=0;
   double sl=0;
   double tp=0;
   int ticket=0;
   double minimumstoplevel=0;
   double minimumStopLoss=0;
   double minimumTakeProfit=0;
   string tiggerLong="";
   string triggerShort="";

   minimumstoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
   
   if(type == OP_BUY){
      price=Ask;
      minimumStopLoss=NormalizeDouble(Bid-minimumstoplevel*Point,Digits);
      minimumTakeProfit=NormalizeDouble(Bid+minimumTakeProfit*Point,Digits);
      ticket=OrderSend(Symbol(),type,LotSize,price,slippage,minimumStopLoss,minimumTakeProfit,"EA Trade",magicNumber,0,clrBlue);
   }else{
      price=Bid;
      minimumStopLoss=NormalizeDouble(Ask+minimumstoplevel*Point,Digits);
      minimumTakeProfit=NormalizeDouble(Ask-minimumstoplevel*Point,Digits);
      ticket=OrderSend(Symbol(),type,LotSize,price,slippage,minimumStopLoss,minimumTakeProfit,"EA Trade",magicNumber,0,clrRed);
   }  

   if(ticket>0){         
      if(OrderSelect(ticket,SELECT_BY_TICKET)){
         if(OrderType()==OP_BUY){
            sl=OrderOpenPrice()-(stopLoss*pips);
            tp=OrderOpenPrice()+(takeProfit*pips);
         }else if(OrderType()==OP_SELL){
            sl= OrderOpenPrice()+(stopLoss*pips);
            tp= OrderOpenPrice()-(takeProfit*pips);
         }
         if(!OrderModify(ticket,price,sl,tp,0,clrMagenta)){
            err=GetLastError();
            Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err));
         }
      }else{
         Print("Failed to Select Order",ticket);
         err=GetLastError();
         Print("Encountered an error while selecting order"+(string)ticket+" error number"+(string)err+" "+ErrorDescription(err));
      }
   }
   else{
      err=GetLastError();
      Print("Encountered an error during order placement"+(string)err+" "+ErrorDescription(err));
   }
}
//+------------------------------------------------------------------+
 
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.
 
noSkill06s:

From the topic u will get fast my problem I'm a beginner EA programmer, this is my first EA pls see my Code I really dont get why its still not working, I mean why do I get still "invalid error"

you have not said if the error is in placing the trade or modifying the trade....

have you checked what is being returned from your request?

 minimumstoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);

When you are modifying the trade you are not checking the current price only the open price this could easily lead to invalid levels.

I suggest you do some checking to see what values you are trying to set rather than just if it worked or not.

regards

 

You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
          Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

On some ECN type brokers the value might be zero (the broker doesn't know). Use a minimum of two (2) PIPs.

 
William Roeder:

You can't move stops (or pending prices) closer to the market than the minimum: MODE_STOPLEVEL * _Point or SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).
          Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

On some ECN type brokers the value might be zero (the broker doesn't know). Use a minimum of two (2) PIPs.

I tried following thing Sir, I didn't use Stoploss or Takeprofit anymore I'm opening the Trade without Stoploss and Takeprofit(I know if connection fails I will have no protection but only for testing now) I coded a for loop who calculates the profit distance stoploss and takeprofit now I can use also a pseudo takeprofit or stoploss of 1 pip on gbpusd which asked for a stoploss of 50 pips before, but now I have a another problem sometimes my self created pseudo stoploss and takeprofit works and sometimes not I coded also a Getlasterror which returns 0 but the code isn't working properly like I said sometimes it works sometimes not, and my question can u pls watch my code do you see and mistakes? my last hope is it doesn't work because I dont use magic numbers I use a standard magic number but not generated magic numbers.

//+------------------------------------------------------------------+
//|                                          breakOut_TRF_v4.0.0.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
#include <stderror.mqh>
#include <stdlib.mqh>

int StopLoss=3;
int TakeProfit=20;
int Slippage=2;
int Magic=280456;
double myPoint;
int countBar=0;
double TP=0,SL=0,TR;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---   

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CheckForSignal();
  }
//+------------------------------------------------------------------+
//| Function "CheckForSignal()"                                      |
//+------------------------------------------------------------------+
void CheckForSignal(){
   static datetime candletime=0;
   if(candletime!=Time[0]){
      double upArrow=iCustom(Symbol(),PERIOD_CURRENT,"breakOut_TRF_v4.0.0",0,1);
      if(upArrow != EMPTY_VALUE){
         buyEnterTrade(OP_BUY,0.1,Ask,2,0,0);
      }
      double downArrow=iCustom(Symbol(),PERIOD_CURRENT,"breakOut_TRF_v4.0.0",1,1);
      if(downArrow != EMPTY_VALUE){
         sellEnterTrade(OP_SELL,0.1,Bid,2,0,0);
      }
      candletime=Time[0];
   }
}
//+------------------------------------------------------------------+
//| buyEnterTrade function                                           |
//+------------------------------------------------------------------+
void buyEnterTrade(int type,double volumen,double price,int slippage,double stoploss,double takeprofit){
   int longTicket=OrderSend(Symbol(),type,volumen,price,slippage,stoploss,takeprofit,"Funktion: buyEnterTrade",Magic,0,clrGreen);
   stopLoss();
}
//+------------------------------------------------------------------+
//| sellEnterTrade function                                          |
//+------------------------------------------------------------------+
void sellEnterTrade(int type,double volumen,double price, int slippage,double stoploss, double takeprofit){
   int shortTicket=OrderSend(Symbol(),type,volumen,price,slippage,stoploss,takeprofit,"Funktion: sellEnterTrade",Magic,clrRed);
   stopLoss();
}
//+------------------------------------------------------------------+
//| stopLoss function                                          |
//+------------------------------------------------------------------+
void stopLoss()
  {
   double MyPoint=Point;
   if(Digits==3 || Digits==5) MyPoint=Point*10;
   TP=TakeProfit*MyPoint;
   SL=StopLoss*MyPoint;

   double OrdP=0,OrdTP=0,OrdSL=0;
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==Magic && Symbol()==OrderSymbol())
           {
            OrdP=OrderProfit()-MathAbs(OrderSwap())-MathAbs(OrderCommission());
            OrdSL=(-1)*SL*OrderLots()*MarketInfo(OrderSymbol(),MODE_TICKVALUE)/Point;
            OrdTP=TP*OrderLots()*MarketInfo(OrderSymbol(),MODE_TICKVALUE)/Point;
            int error=0;
            Print("Checking 'OrdP>OrdTP OR OrdP<OrdSL'",Symbol());
            if(OrdP>OrdTP || OrdP<OrdSL)
              {
               if(OrderType()==OP_BUY){
                  bool OrdClP=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrYellow);
               }else{
                  error=GetLastError();
                  Print("Long OrderClose() error: "+(string)error+" "+ErrorDescription(error));
               }
                  
               if(OrderType()==OP_SELL){
                  bool OrdClL=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrYellow);                  
               }else{
                  error=GetLastError();
                  Print("Short OrderClose() error: "+(string)error+" "+ErrorDescription(error));
               }
              }
           }
     }
  }
//+------------------------------------------------------------------+
 
Paul Anscombe:

you have not said if the error is in placing the trade or modifying the trade....

have you checked what is being returned from your request?

When you are modifying the trade you are not checking the current price only the open price this could easily lead to invalid levels.

I suggest you do some checking to see what values you are trying to set rather than just if it worked or not.

regards

can u pls see my last post need urgent help

 
noSkill06s:

From the topic u will get fast my problem I'm a beginner EA programmer, this is my first EA pls see my Code I really dont get why its still not working, I mean why do I get still "invalid error"

hi, 

this code for SL and TP is enough, you have only some bugs;

   if(type == OP_BUY){
      price=Ask;
      minimumStopLoss=NormalizeDouble(price-minimumstoplevel*Point,Digits);
      minimumTakeProfit=NormalizeDouble(price+minimumTakeProfit*Point,Digits);
      ticket=OrderSend(Symbol(),type,LotSize,price,slippage,minimumStopLoss,minimumTakeProfit,"EA Trade",magicNumber,0,clrBlue);
   }else{
      price=Bid;
      minimumStopLoss=NormalizeDouble(price+minimumstoplevel*Point,Digits);
      minimumTakeProfit=NormalizeDouble(price-minimumstoplevel*Point,Digits);
      ticket=OrderSend(Symbol(),type,LotSize,price,slippage,minimumStopLoss,minimumTakeProfit,"EA Trade",magicNumber,0,clrRed);
   }  

test this one :)

 
      price=Ask;
      minimumStopLoss=NormalizeDouble(price-minimumstoplevel*Point,Digits);
      minimumTakeProfit=NormalizeDouble(price+minimumTakeProfit*Point,Digits);

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

Reason: