Vladimir Karputov
Moderator
286047
Ahmad861 :

im using multiple symbols and timeframes through loops, declaring handles in OnInit will not work without looping

This is your delusion. Work by the rules - there is no problem with multi-symbol trading. Until you fix the code, don't try asking questions.

Keith Watford
Moderator
32920
Ahmad861:

im using multiple symbols and timeframes through loops, declaring handles in OnInit will not work without looping

Do not double post!

I have deleted your duplicated topic.

Fix your code as Vladimir has already said and only create handles in OnInit().

The code that you are attempting to do is way too complicated for a beginner.

You are making the common beginners' error of using _Point when dealing with multiple symbols.

               request.sl = SymbolInfoDouble(sym,SYMBOL_BID) + (StopLoss * _Point);
               request.tp = SymbolInfoDouble(sym,SYMBOL_BID) - (TakeProfit * _Point);

So if the EA is placing a trade on a Japanese 3 digit symbol and the chart symbol is 5 digits, obviously the TP and SL will be 100 times smaller than you expect.

Keith Watford
Moderator
32920
Ahmad861:

Will replacing _Point with this function work properly ?

Of course not.

If _Point is incorrect for the non chart symbol, _Digits wont be correct either.

You seem to have deleted your code, but I am sure that you got the correct value elsewhere in your code.

Vladimir Karputov
Moderator
286047
Ahmad861 :


When writing a multi-symbol Expert Advisor, one should remember that the properties of each symbol can vary greatly - starting from the number of symbols after zero, and by the price.

Vladimir Karputov
Moderator
286047
Ahmad861 :

The most simple example of Multi-symbol Two iMA Simple :

Trading strategy

The strategy itself is quite trivial - it is the intersection of two iMAs (Moving Average, MA). But the implementation deserves a separate description. This is a multi-symbol Expert Advisor that trades two symbols at once. Executed as a single file. All trading code is in the ' CTradingEngine31 ' class, the main advisor in the initialization block creates two instances of advisors - one for the ' Symbol 0 ' symbol and one for the ' Symbol 1 ' symbol. After that, the main advisor needs to call OnTick and OnTradeTransaction in both instances.

The EA itself is simplified as much as possible: there is no Stop Loss, no Tape Profit, no Trailing. The appearance of any trading signal means first unconditional closing of a position and immediately opening a new position. The search for a signal only at the moment of the birth of a new bar, which means that the intersection is searched for at bar # 1, not at the current one. Position volume (' Number of minimum lots ') is set as the number of minimum lots.

Trading signals:

  • Open BUY: intersection of the 'Fast' and 'Slow' indicators from bottom to top
  • Open SELL: intersection of the 'Fast' and 'Slow' indicators from top to bottom

Features:

  • The EA can be optimized for the ' Working timeframe '
  • There can be only one 'market entry' trade on a bar

Now in more detail on each group of parameters:

Trading settings:

' Working timeframe ' - working timeframe. Timeframe on which indicators are created and on which a new bar is searched for.

Position size management (lot calculation)

The lot is specified as the number of minimum lots.

Additional features:

' Print log ' prints extended logging of all operations. The ' Freeze and StopsLevels Coefficient ' parameter sets the coefficient for stop and freeze levels for cases when these levels for a symbol are equal to zero. The recommended value is '3'.

RZAMK
16
RZAMK  

Hi Vladimir.

Thanks for your hard work.

I'm trying to get this MQL4 code back to MQL5. With this code, I get open price of last current position.

double open_price(int t)
  {
   double OpenPrice=0;
   if(OrderSelect(OrdersTotal()-t,SELECT_BY_POS,MODE_TIME))
     {
      OpenPrice=OrderOpenPrice();
     }
   return(OpenPrice);
  }
I wrote this code in MQL5 and unfortunately it does not work.
double open_price(int t)
  {
   double OpenPrice=0;
   datetime last_pos_time=0;
   double   last_pos_price;
   double   last_pos_lot;
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      if(m_position.SelectByIndex(PositionsTotal()-t))
        {
         if(m_position.Time()>last_pos_time)
           {
            last_pos_time  = m_position.Time();
            last_pos_price = m_position.PriceOpen();
            last_pos_lot   = m_position.Volume();
            OpenPrice = m_position.PriceOpen();
           }
        }
     }
   return(OpenPrice);
  }
Can you help me?
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Price Constants - Indicator Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Vladimir Karputov
Moderator
286047
RZAMK :

Hi Vladimir .

Thanks for your hard works.

I'm trying to get this MQL4 code back to MQL5. With this code, I get open price of last positions.

I wrote this code in MQL5 and unfortunately it does not work. Can you help me?

I love working with OnTradeTransaction. This is where I get all the information I need about the last trade.

RZAMK
16
RZAMK  
Vladimir Karputov:

I love working with OnTradeTransaction. This is where I get all the information I need about the last trade.

How can I use OnTradeTransactionfor catch OpenPrice of last current position?
Vladimir Karputov
Moderator
286047
RZAMK :
How can I use OnTradeTransactionfor catch OpenPrice of last current position?

Example:

//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      ResetLastError();
      if(HistoryDealSelect(trans.deal))
         m_deal.Ticket(trans.deal);
      else
        {
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","HistoryDealSelect(",trans.deal,") error: ",GetLastError());
         return;
        }
      if(m_deal.Symbol()==m_symbol.Name() && m_deal.Magic()==InpMagic)
        {
         if(m_deal.DealType()==DEAL_TYPE_BUY || m_deal.DealType()==DEAL_TYPE_SELL)
           {
            if(m_deal.Entry()==DEAL_ENTRY_IN)
              {
               Print(m_deal.Price());
              }
           }
        }
     }
  }
RZAMK
16
RZAMK  
Vladimir Karputov:

Example:

thank you.

But you brought this code in previous pages.

Oh, maybe I didnt fully explain what I meant. I dont search the history for closed positions.

I want to find the openprice of the last position that is still open and take another position or positions according to its price.