Questions from Beginners MQL5 MT5 MetaTrader 5 - page 812

 

How is that possible? Wasn't there a problem with the maths?

input double    T_P  =0.81;
input double    S_L  =0.35;

b_ep=price-atrH1200;
b_sl=b_ep-(b_ep*S_L)/100;
b_tp=b_ep+(b_ep*T_P)/100;

It looks like 1.05 *0.81/100 =0.008505

It gives you 35% stop and 81% tekprofit instead of 0.81% and 0.35%

SellLimit(LOT_FOR_TRADE,s_ep,symb,s_sl,s_tp,ORDER_TIME_DAY,0,"SellLimit");

BuyLimit(LOT_FOR_TRADE,b_ep,symb,b_sl,b_tp,ORDER_TIME_DAY,0,"BuyLimit");

The method from the standardSellLimit&BuyLimit library


Print(s_ep,"s_ep");
Print(s_sl,"s_sl");
Print(s_tp,"s_tp");

Print shows:

2017.12.24 07:55:27.559 2017.01.03 16:54:17 1.04236745s_ep

2017.12.24 07:55:27.559 2017.01.03 16:54:17 1.25084094s_sl

2017.12.24 07:55:27.559 2017.01.03 16:54:17 0.521183725s_tp


 
SILVERPRINT:

How is this possible? Wasn't there a problem with the maths?

It looks like 1.05 *0.81/100 =0.008505

It gives you 35% stop and 81% tekprofit instead of 0.81% and 0.35%

The method from the standardSellLimit&BuyLimit library


Print shows:

2017.12.24 07:55:27.559 2017.01.03 16:54:17 1.04236745s_ep

2017.12.24 07:55:27.559 2017.01.03 16:54:17 1.25084094s_sl

2017.12.24 07:55:27.559 2017.01.03 16:54:17 0.521183725s_tp



Rewrite the same code, but so that ALL variables are declared. I'll see what types of variables you declare. And then the right solution is close at hand.

 
Vladimir Karputov:

Rewrite the same code, but so that ALL variables are declared. I'll see what types of variables you declare. And then you'll be on your way to the right solution.


//+------------------------------------------------------------------+
//|                                                       |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
#include <Indicators\Trend.mqh>
#include <Indicators\Oscilators.mqh>
#include <Indicators\TimeSeries.mqh>
#include <Trade\Trade.mqh>
input double    T_P  =0.81;
input double    S_L  =0.35;
static int hour;
int h;
input int TIME_FOR_ACTION=16;
input int slowma=200;
input int fastwma=20;
input int mahours=20;
input double LOT_FOR_TRADE=0.1;
string symb;
MqlTick infotick;
MqlDateTime currentTime;
CiMA ValueofMA200;
CiMA ValueofMA20;
CiMA ValueofMAHour;
CiATR atr;
CTrade T_M;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   symb=_Symbol;
   ValueofMA200.Create(symb,PERIOD_D1,slowma,0,MODE_SMA,PRICE_CLOSE);
   ValueofMA20.Create(symb,PERIOD_D1,fastwma,0,MODE_SMA,PRICE_CLOSE);
   ValueofMAHour.Create(symb,PERIOD_H1,mahours,0,MODE_SMA,PRICE_CLOSE);
   atr.Create(symb,PERIOD_H1,200);


   atr.BufferResize(40);
   ValueofMA200.BufferResize(250);
   ValueofMA20.BufferResize(250);
   ValueofMAHour.BufferResize(250);

   T_M.LogLevel(LOG_LEVEL_ALL);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   RefCustom();
   SymbolInfoTick(symb,infotick);
   TimeToStruct(infotick.time,currentTime);

   if(1==1)
     {
      if(1==1)
        {
         double maD1200,maD120,maH120,atrH1200,price;

         maD1200=ValueofMA200.Main(0);
         maD120=ValueofMA20.Main(0);
         maH120=ValueofMAHour.Main(0);
         atrH1200=atr.Main(0);
         price=infotick.bid;
         double s_ep,s_sl,s_tp;
         double b_ep,b_sl,b_tp;


         if(1==1)
           {
            b_ep=price-atrH1200;
            b_sl=b_ep-(b_ep*S_L)/100;
            b_tp=b_ep+(b_ep*T_P)/100;
            T_M.BuyLimit(LOT_FOR_TRADE,b_ep,symb,b_sl,b_tp,ORDER_TIME_DAY,0,"BuyLimit");
           }
         if(1==1)
           {
            s_ep=price+atrH1200;
            s_sl=s_ep+(s_ep*S_L)/100;
            s_tp=s_ep-(s_ep*T_P)/100;

            T_M.SellLimit(LOT_FOR_TRADE,s_ep,symb,s_sl,s_tp,ORDER_TIME_DAY,0,"SellLimit");
           }
}

     }
  }
//+------------------------------------------------------------------+
//| Trade function                                                   |
//+------------------------------------------------------------------+
void OnTrade()
  {
//---

  }
//+------------------------------------------------------------------+
void RefCustom()
  {
   ValueofMA200.Refresh();
   ValueofMA20.Refresh();
   ValueofMAHour.Refresh();
   atr.Refresh();
  }
//+------------------------------------------------------------------+

 
SILVERPRINT:


Errors. Not compiling.

 
Vladimir Karputov:

Errors. It won't compile.


Sorry, try it now.

 
SILVERPRINT:

Sorry, try it now.


Probably overnight - so I don't understand at all WHAT you're trying to do.

A word of advice:

  • INITIALIZE variables when declaring
  • when dividing, divide by double, not by int (e.g. number 100.0 -> double, number 100 -> int)
  • what is this style if(1==1) - ? This is monstrous, never write it this way.
  • Last, most importantly, leave only three lines in your code,

            b_ep=price-atrH1200;
            b_sl=b_ep-(b_ep*S_L)/100;
            b_tp=b_ep+(b_ep*T_P)/100;

  • and declare and initialize variables explicitly. For example like this:

   double price=1.05848;
   double atrH1200=1.05168;
   double T_P=0.81;
   double S_L=0.35;

   double b_ep=price-atrH1200;
   double b_sl=b_ep-(b_ep*S_L)/100.0;
   double b_tp=b_ep+(b_ep*T_P)/100.0;
   DebugBreak();
 
Vladimir Karputov:

Probably overnight - so I don't understand ANYTHING you're trying to do.

A word of advice:

  • when declaring INITIALIZE variables
  • when dividing, divide by double, not by int (e.g. number 100.0 -> double, number 100 -> int)
  • what is this style if(1==1) - ? This is monstrous, never write it this way.
  • Last, most importantly, leave only three lines in your code,

  • and declare and initialize variables explicitly. For example like this:


Thank you incredibly helpful.

 
Strangely enough, the function works even if there are more orders than zero.
if(OrdersTotal()==0)

It does not make muchdifference, but where the function does not work, it may open the whole deposit.

 
SILVERPRINT:
Strange but the function, works even if there are more orders than zero.

It doesn't make muchdifference, but where the function doesn't work it can open up the whole deposit.

Open the MT5 help for the function and make sure you correctly understand what an order is in MT5

 
Pressed and pressed F1, couldn't find...

Can you please tell me how to determine _Digits for another character?
Reason: