I cannot open and close orders in more than one market

 
Hello,

It trades in a single market but does not open and close positions in more than one market.


Thank you from now

//+------------------------------------------------------------------+

//|                                                          MA Tech |

//|                                        Copyright 2024, Techizmet |

//|                                         http://www.techizmet.com |

//+------------------------------------------------------------------+

#include <Trade/Trade.mqh>

#include <Trade\PositionInfo.mqh>

#include <Trade\SymbolInfo.mqh>

#include <Trade\AccountInfo.mqh>

CPositionInfo  m_position;                   

CTrade         trade;                     

CSymbolInfo    m_symbol;                    

CAccountInfo   m_account;                    

bool HesapBilgi=false;

string hesap_no = "";

string hesap_bitis = "2024.05.30";

int sinyalbir=0;

int sinyaliki=0;

int alkapat =0;

int satkapat =0;

int Sell_Pozisyon=0;

int Buy_Pozisyon=0;

input string cizgi0 = "——————————— LOT Miktarı ———————————————————"; //••• Lot Miktarı •••

input double hacimbir=0.1;

input double hacimiki=0.1;

input string cizgi1 = "——————————— MA Değerleri ———————————————————"; //••• Hareketli Ortalama •••

int birma=1;

input int ikima=50;

input int ucma=120;

input double tpp=900;

int tpoldu=0;

int selloldu=0;

int selltotal=0;

int buyoldu=0;

int buytotal=0;

input ulong            m_magic=47978073;             // magic number

ulong  bmagic=0;

input int Magic = 132412;

int handles;

int bartotal;

int OnInit()

  {

   if(!m_symbol.Name(Symbol())) // sets symbol name

      return(INIT_FAILED);

//---

   trade.SetExpertMagicNumber(m_magic);

   trade.SetMarginMode();

   trade.SetTypeFillingBySymbol(m_symbol.Name());

   trade.SetExpertMagicNumber(Magic);

   return(INIT_SUCCEEDED);

  }

void OnTick()

  {

   int bars=iBars(_Symbol,PERIOD_CURRENT);

   if(bartotal<bars)

     {

      bartotal=bars;

      kosullar(handles);

     }

  }

void kosullar(int handle)

  {

   double ikiMArray[];

   double UcMArray[];

   ArraySetAsSeries(ikiMArray,true);

   ArraySetAsSeries(UcMArray,true);

   MqlRates Fiyat[];

   ArraySetAsSeries(Fiyat,true);

   int ikiMDeger=iMA(_Symbol,_Period,ikima,0,MODE_SMA,PRICE_CLOSE);

   int UcMDeger=iMA(_Symbol,_Period,ucma,0,MODE_SMA,PRICE_CLOSE);

   CopyBuffer(ikiMDeger,0,0,3,ikiMArray);

   CopyBuffer(UcMDeger,0,0,3,UcMArray);

   CopyRates(_Symbol,_Period,0,3,Fiyat);

   double AL=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

   double SAT=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

   double birtp=AL+tpp*_Point;

   double birtpsat=SAT-tpp*_Point;

// Al Bir

   if(Fiyat[1].close>ikiMArray[1])

      sinyalbir = ORDER_TYPE_BUY;

//Sat Bir

   if(Fiyat[1].close<ikiMArray[1])

      sinyalbir = ORDER_TYPE_SELL;//

// Al İki

   if(Fiyat[1].close>UcMArray[1] && Fiyat[1].close>ikiMArray[1])  //

      sinyaliki=ORDER_TYPE_BUY;

//Sat iki

   if(Fiyat[1].close<UcMArray[1] && Fiyat[1].close<ikiMArray[1])

      sinyaliki =ORDER_TYPE_SELL ; //

   if(sinyalbir==ORDER_TYPE_SELL &&  PositionsTotal()==0)

     {

      trade.Sell(hacimbir,_Symbol,SAT,0,SAT-tpp*_Point,NULL);

        {

         bmagic=PositionGetInteger(POSITION_MAGIC);

         tpoldu=1;

         selloldu=1;

        }     }

   else

      if(sinyalbir==ORDER_TYPE_BUY &&  PositionsTotal()==0)

        {

         trade.Buy(hacimbir,_Symbol,AL,0,AL+tpp*_Point,NULL);

           {

            bmagic=PositionGetInteger(POSITION_MAGIC);

            tpoldu=1;

            buyoldu=1;

           }

        }

   if(sinyaliki==ORDER_TYPE_SELL &&  PositionsTotal()>0 && tpoldu==1)

     {

      trade.Sell(hacimiki,_Symbol);

        {

         bmagic=PositionGetInteger(POSITION_MAGIC);

         tpoldu=0;

         selltotal=2;

        }

     }

   else

      if(sinyaliki==ORDER_TYPE_BUY &&  PositionsTotal()>0 && tpoldu==1)

        {

         trade.Buy(hacimiki,_Symbol);

           {

            bmagic=PositionGetInteger(POSITION_MAGIC);

            tpoldu=0;

            buytotal=2;

           }

        }

//Açık pozisyonları kapatmak için

//--- tüm açık pozisyonları ara

   for(int i=PositionsTotal()-1; i>=0; i--)

     {

      long poztip = PositionGetInteger(POSITION_TYPE); 

      ulong  position_ticket=PositionGetTicket(i);                                      // pozisyon bileti

      string position_symbol=PositionGetString(POSITION_SYMBOL);                        // sembol

      ulong  magic=PositionGetInteger(POSITION_MAGIC);                                  // pozisyon MagicNumber'ı

      int total=PositionsTotal(); // açık pozisyonların sayısı

      if(sinyaliki==ORDER_TYPE_SELL && poztip==0)

        {

         trade.PositionClose(_Symbol,-1);

        if(sinyaliki==ORDER_TYPE_BUY && poztip==1)

         trade.PositionClose(_Symbol,-1);    

        }}

  }

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Execution of trade operations results in the opening of a position, changing of its volume and/or direction, or its disappearance. Trade operations...
 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2.    int ikiMDeger=iMA(_Symbol,_Period,ikima,0,MODE_SMA,PRICE_CLOSE);
    
       int UcMDeger=iMA(_Symbol,_Period,ucma,0,MODE_SMA,PRICE_CLOSE);
    
       CopyBuffer(ikiMDeger,0,0,3,ikiMArray);
    
       CopyBuffer(UcMDeger,0,0,3,UcMArray);

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)

Reason: