How to start with MQL5 - page 13

 

Example: short trade request form and full trade request form

Code: Buy Short code and  Full code.mq5

//+------------------------------------------------------------------+
//|                                Buy Short code and  Full code.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.00"
//---
#include <Trade\Trade.mqh>
CTrade         m_trade;                      // trading object
//---
#property script_show_inputs
//--- input parameters
input group             "Trading settings"
input uint     InpStopLoss          = 150;         // Stop Loss
input uint     InpTakeProfit        = 460;         // Take Profit
input group             "Additional features"
input ulong    InpDeviation         = 10;          // Deviation
input ulong    InpMagic             = 200;         // Magic number
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   m_trade.SetExpertMagicNumber(InpMagic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(Symbol());
   m_trade.SetDeviationInPoints(InpDeviation);
//--- Short code
   m_trade.Buy(0.01);
//--- Full code
   MqlTick last_tick;
   ResetLastError();
   if(SymbolInfoTick(Symbol(),last_tick))
     {
      Print(Symbol()," ",last_tick.time,": Bid = ",last_tick.bid," Ask = ",last_tick.ask);
     }
   else
     {
      Print("SymbolInfoTick() failed, error = ",GetLastError());
      return;
     }
//---
   double sl=(InpStopLoss==0)?0.0:last_tick.ask-InpStopLoss*Point();
   double tp=(InpTakeProfit==0)?0.0:last_tick.ask+InpTakeProfit*Point();
   m_trade.Buy(0.02,Symbol(),last_tick.ask,sl,tp,"Full code");
  }
//+------------------------------------------------------------------+


Result

 

Hello, I need help identifying ChartID.

I use my EA as a scanner and use ChartOpen Function to get the screened symbol, i want to use vertical lines and trend lines on those charts but i dont know how to get the chartID . i tried and it keeps getting added to my chart with the EA.

 
Ahmad861:

Hello, I need help identifying ChartID.

I use my EA as a scanner and use ChartOpen Function to get the screened symbol, i want to use vertical lines and trend lines on those charts but i dont know how to get the chartID . i tried and it keeps getting added to my chart with the EA.

Use  ChartID

Documentation on MQL5: Chart Operations / ChartID
Documentation on MQL5: Chart Operations / ChartID
  • www.mql5.com
ChartID - Chart Operations - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Vladimir Karputov:

Use  ChartID

i tried, but it will still draw lines on my chart with the EA, not the chart which just opened

 
Ahmad861 :

i tried, but it will still draw lines on my chart with the EA, not the chart which just opened

If you are using the ChartOpen Function , you need to remember: this function returns the opened chart ID. It is this identifier that must be used to draw on a new chart.

ChartOpen

Opens a new chart with the specified symbol and period.
long  ChartOpen(
   string           symbol,     // Symbol name
   ENUM_TIMEFRAMES  period      // Period
   );

Parameters

symbol

[in]  Chart symbol. NULL means the symbol of the  current chart (the Expert Advisor is attached to).

period

[in]  Chart period (timeframe). Can be one of the ENUM_TIMEFRAMES values. 0 means the current chart period.

Return Value

If successful, it returns the opened chart ID. Otherwise returns 0.

Note

The maximum possible number of simultaneously open charts in the terminal can't exceed the CHARTS_MAX value.

Documentation on MQL5: Chart Operations / ChartOpen
Documentation on MQL5: Chart Operations / ChartOpen
  • www.mql5.com
ChartOpen - Chart Operations - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Vladimir Karputov:

If you are using the ChartOpen Function , you need to remember: this function returns the opened chart ID. It is this identifier that must be used to draw on a new chart.

I didn't think of that at all .it worked, thanks a lot !!

 

Calculate Positions and Pending Orders

Code:  Calculate Positions and Pending Orders.mq5

//+------------------------------------------------------------------+
//|                       Calculate Positions and Pending Orders.mq5 |
//|                         Copyright © 2019-2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019-2020, Vladimir Karputov"
#property version   "1.003"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\OrderInfo.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
COrderInfo     m_order;                      // object of COrderInfo class
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int buys=0,sells=0;
   int buy_limits=0,sell_limits=0,buy_stops=0,sell_stops=0;
   CalculateAllPositions(buys,sells);
   CalculateAllPendingOrders(buy_limits,sell_limits,buy_stops,sell_stops);
   string text="BUY: "+IntegerToString(buys)+", SELL: "+IntegerToString(sells)+"\n"+
               "Buy limits "+IntegerToString(buy_limits)+", Sell limits "+IntegerToString(sell_limits)+
               ", Buy stops "+IntegerToString(buy_stops)+", Sell stops "+IntegerToString(sell_stops);
   Comment(text);
//---
  }
//+------------------------------------------------------------------+
//| Calculate all positions Buy and Sell                             |
//+------------------------------------------------------------------+
void CalculateAllPositions(int &count_buys,int &count_sells)
  {
   count_buys=0;
   count_sells=0;
   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         //if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
        {
         if(m_position.PositionType()==POSITION_TYPE_BUY)
            count_buys++;
         if(m_position.PositionType()==POSITION_TYPE_SELL)
            count_sells++;
        }
//---
   return;
  }
//+------------------------------------------------------------------+
//| Calculate all pending orders                                     |
//+------------------------------------------------------------------+
void CalculateAllPendingOrders(int &count_buy_limits,int &count_sell_limits,int &count_buy_stops,int &count_sell_stops)
  {
   count_buy_limits  = 0;
   count_sell_limits = 0;
   count_buy_stops   = 0;
   count_sell_stops  = 0;
   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of current orders
      if(m_order.SelectByIndex(i))     // selects the pending order by index for further access to its properties
         //if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==InpMagic)
        {
         if(m_order.OrderType()==ORDER_TYPE_BUY_LIMIT)
            count_buy_limits++;
         else
            if(m_order.OrderType()==ORDER_TYPE_SELL_LIMIT)
               count_sell_limits++;
            else
               if(m_order.OrderType()==ORDER_TYPE_BUY_STOP)
                  count_buy_stops++;
               else
                  if(m_order.OrderType()==ORDER_TYPE_SELL_STOP)
                     count_sell_stops++;
        }
  }
//+------------------------------------------------------------------+


Open positions: GBPUSD BUY 0.03 lot and USDPLN SELL 0.01

Pending orders are placed: USDPLN Sell limit 0.01 and GBPUSD Buy limit 0.03


Calculate Positions and Pendong Orders

 
ChartOpen() returns a long value, How can i access that long value without executing the ChartOpen(). I am running a multi symbol and multi timeframe EA on loops
  string symbolarray[];
  ArrayResize(symbolarray,SymbolsTotal(true));
  for(int i=0;i<ArraySize(symbolarray);i++){
  for(int j=0;j<ArraySize(TF);j++){
  string sym=SymbolName(i,true);

  // Strategy Logic

  ChartOpen(sym,TF[j]);
 
Ahmad861 :
ChartOpen() returns a long value, How can i access that long value without executing the ChartOpen(). I am running a multi symbol and multi timeframe EA on loops

Use a styler - you will immediately see your error:

string symbolarray[];
ArrayResize(symbolarray,SymbolsTotal(true));
for(int i=0; i<ArraySize(symbolarray); i++)
  {
   for(int j=0; j<ArraySize(TF); j++)
     {
      string sym=SymbolName(i,true);
      // Strategy Logic
      ChartOpen(sym,TF[j]);
      //+------------------------------------------------------------------+
 
Vladimir Karputov:

Use a styler - you will immediately see your error:

i do not understand, What do you mean ?