Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 950

 
Rustam Bikbulatov:

This function writes a number on each order, or rather lot*100.

Now we need an inverse function which shows what was the last number

Try it:

double value = StringToDouble(ObjectGetString(0,object_name,OBJPROP_TEXT)); 

If the object contains text (not numeric), you need to add StringSubstr():

double value = StringToDouble( StringSubstr(ObjectGetString(0,object_name,OBJPROP_TEXT),begin_index,length));

begin_index - index of the character in the string, starting from zero, length - length of the numeric value (how many characters the number occupies)... I think so

 
Yevhenii Levchenko:

Try this:

If the object has text (not numeric), add StringSubstr():

begin_index - index of the character in the string , starting from zero, length - length of the numeric value (how many characters the number occupies)... I think so

Well initially you would have to look for these all numbers on the graph. It just won't work that way. That's the problem. Already checked.

 
Yevhenii Levchenko:

Try this:

If object has text (not numeric), add StringSubstr():

begin_index - index of the character in the string, starting from zero, length - length of the numeric value (how many characters the number occupies)... I think so

   bool name;
        for(int i=0; i<ObjectsTotal(0,"LOTB"+OrderTicket(),OBJ_TEXT); i++)
     {
      name = ObjectSetString(0,"LOTB"+OrderTicket(),OBJ_TEXT,0);
      Comment("   ",name); 
   }

Similar to this one, but it doesn't work. It shows zero.

 
Rustam Bikbulatov:

Like this one, but it doesn't work. It shows zero.

Do you want to set a value or take a value? The ObjectSetString() function sets a text value and returns a bool value. This is not it. And in the loop head I think it's not the right one... Where ObjectsTotal()... I am guided by mt4 reference. Or is it mt5 code?

 
Yevhenii Levchenko:

Do you need to set a value or take a value? The ObjectSetString() function sets a text value and returns a bool value. This is not it. And in the head of the loop I think it's not the right one... Where ObjectsTotal()... I am guided by mt4 reference. Or is it mt5 code?

This is MT4.

 

Once again, good day everyone!

I am not getting a response to my question posed earlier, but I am asking for help with the code anyway.

This is the forum for trading, automated trading systems and strategy testing.

Any newbie questions on MQL4 and MQL5, help and discussion on algorithms and codes

MrBrooklin, 2019.08.30 14:15

Good day everyone!!!

Here is part of the script code for Metatrader5:

#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//---- показывать входные параметры
#property script_show_inputs
//---
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
CTrade         m_trade;                      // trading object
CSymbolInfo    m_symbol;                     // symbol info object
//+------------------------------------------------------------------+
//| Enum Stop or Limit                                               |
//+------------------------------------------------------------------+
enum ENUM_STOP_OR_LIMIT
  {
   stop=0,     // Buy stop and Sell stop
   limit=1     // Buy limit and Sell limit
  };
//--- input parameters
input ushort               InpUpGap          = 15;    // Gap for pending orders UP from the current price (in points)
input ushort               InpUpStep         = 30;    // Step between orders UP (in points)

input ushort               InpDownGap        = 15;    // Gap for pending orders DOWN from the current price (in points)
input ushort               InpDownStep       = 30;    // Step between orders DOWN (in points)

input ENUM_STOP_OR_LIMIT   InpPending        = stop;  // Type of pending orders

input uchar                InpUpQuantity     = 1;     // UP quantity orders
input uchar                InpDownQuantity   = 1;     // DOWN quantity orders

input double               InpLots           = 0.01;  // Lots
input ushort               InpStopLoss       = 50;    // Stop Loss (in points)
input ushort               InpTakeProfit     = 50;    // Take Profit (in points)
//---
ulong                      m_slippage=30;             // slippage

double                     ExtUpGap=0.0;
double                     ExtUpStep=0.0;

double                     ExtDownGap=0.0;
double                     ExtDownStep=0.0;

double                     ExtStopLoss=0.0;
double                     ExtTakeProfit=0.0;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   if(InpLots<=0.0)
     {
      Print("The \"Lots\" can't be smaller or equal to zero");
      return;
     }
//---
   if(!m_symbol.Name(Symbol())) // sets symbol name
      return;
   if(!RefreshRates())
      return;

   string err_text="";
   if(!CheckVolumeValue(InpLots,err_text))
     {
      Print(err_text);
      return;
     }

//---
   if(IsFillingTypeAllowed(SYMBOL_FILLING_FOK))
      m_trade.SetTypeFilling(ORDER_FILLING_FOK);
   else
      if(IsFillingTypeAllowed(SYMBOL_FILLING_IOC))
         m_trade.SetTypeFilling(ORDER_FILLING_IOC);
      else
         m_trade.SetTypeFilling(ORDER_FILLING_RETURN);

//---
   m_trade.SetDeviationInPoints(m_slippage);
   m_trade.SetAsyncMode(true);

//---
   ExtUpGap = m_symbol.Point() * InpUpGap;
   ExtUpStep = m_symbol.Point() * InpUpStep;

   ExtDownGap = m_symbol.Point() * InpDownGap;
   ExtDownStep = m_symbol.Point() * InpDownStep;

   ExtStopLoss = m_symbol.Point() * InpStopLoss;
   ExtTakeProfit = m_symbol.Point() * InpTakeProfit;

//--- start work
   double start_price_ask=m_symbol.Ask()-ExtUpGap;
   double start_price_bid=m_symbol.Bid()+ExtDownGap;

//--- set pending orders
   for(int i=0; i<InpUpQuantity; i++)
     {
      double price_ask = start_price_ask+i*ExtUpStep;
      double price_bid = start_price_bid+i*ExtUpStep;
      if(InpPending==stop)
        {
         double sl = (ExtStopLoss==0.0)   ? 0.0 : price_ask - ExtStopLoss;
         double tp = (ExtTakeProfit==0.0) ? 0.0 : price_ask + ExtTakeProfit;
         m_trade.BuyStop(InpLots,m_symbol.NormalizePrice(price_ask),m_symbol.Name(),
         m_symbol.NormalizePrice(sl),
         m_symbol.NormalizePrice(tp));
        }
      else
        {
         double sl = (ExtStopLoss==0.0)   ? 0.0 : price_bid + ExtStopLoss;
         double tp = (ExtTakeProfit==0.0) ? 0.0 : price_bid - ExtTakeProfit;
         m_trade.SellLimit(InpLots,m_symbol.NormalizePrice(price_bid),m_symbol.Name(),
         m_symbol.NormalizePrice(sl),
         m_symbol.NormalizePrice(tp));
        }
     }

   for(int i=0; i<InpDownQuantity; i++)
     {
      double price_ask = start_price_ask-i*ExtDownStep;
      double price_bid = start_price_bid-i*ExtDownStep;
      if(InpPending==limit)
        {
         double sl = (ExtStopLoss==0.0)   ? 0.0 : price_ask - ExtStopLoss;
         double tp = (ExtTakeProfit==0.0) ? 0.0 : price_ask + ExtTakeProfit;
         m_trade.BuyLimit(m_symbol.LotsMin(),m_symbol.NormalizePrice(price_ask),m_symbol.Name(),
         m_symbol.NormalizePrice(sl),
         m_symbol.NormalizePrice(tp));
        }
      else
        {
         double sl = (ExtStopLoss==0.0)   ? 0.0 : price_bid + ExtStopLoss;
         double tp = (ExtTakeProfit==0.0) ? 0.0 : price_bid - ExtTakeProfit;
         m_trade.SellStop(m_symbol.LotsMin(),m_symbol.NormalizePrice(price_bid),m_symbol.Name(),
         m_symbol.NormalizePrice(sl),
         m_symbol.NormalizePrice(tp));
        }
     }
  }

The script should place either pending limit orders at a certain distance from ask and bid, or stop ones. The limit pending orders are set without any problems but the stop ones are not. Please help me to understand why pending Buy Stop and Sell Stop orders are not set.

Yours sincerely, Vladimir.

Just to follow up with another question: is there any way to test the script when the market is closed (e.g. at the weekend)?

Sincerely, Vladimir.

 
Artyom Trishkin:

From which list?

And please use the styler in the editor (Ctrl+<):

What should be taken from where?

Can you give me a hint?

 
MrBrooklin:

Once again, good day everyone!

No one has responded to the question I posed earlier, but I'm still asking for help with the code.

One more question: is there any way to test the script when the market is closed (e.g. on weekends)?

Sincerely, Vladimir.

No, the script will not work in a closed market - it will not place orders.

To understand the errors, try printing into the log the value of the price relative to which you are calculating the setting levels and the values of all price levels you are sending to the trade order.

 
Hi guys. Made writing a lot next to an open order.
for(int no1=0; no1<OrdersTotal(); no1++)
  {
   if(OrderSelect(no1,SELECT_BY_POS,MODE_TRADES))
     {
      if(OrderSymbol()==Symbol() && OrderType()== OP_BUY)
        {
         ObjectCreate("LOTB"+OrderTicket(),OBJ_TEXT,0,TimeCurrent(),OrderOpenPrice());
         ObjectSetText("LOTB"+OrderTicket(),OrderLots()*100,20,"Arial",clrWheat);
         ObjectSetInteger(0,"LOTB"+OrderTicket(),OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
        }
     }
  } 
Can you tell me how to take the last text from this list in another part of the code?
 
Rustam Bikbulatov:

Can you give me a hint?

You need to find the last order that was opened. Find its ticket. Then use the list of object names to find the occurrence of a substring with the found ticket of the last order in the object name line. As soon as the ticket of the last order is found in the object name, this is the necessary graphical object. All that remains is to extract what you need from this graphical object.

Reason: