한 번에 둘 이상의 EA 실행 - 페이지 3

 
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern bool CheckOncePerBar=true;
extern double FixedLotSize=1;
extern double SystemStopLoss=150;
extern double TakeProfit=0;
extern int Slippage=5;
extern int MagicNumber=3574;

//Global Variables
int BuyTicket=0;
int SellTicket=0;
double InternalStopLoss=0;
double CalcDigits=0;
double CalcPoint=0;
bool MABuyFanning=false;
bool MASellFanning=false;
int SelectedOrder=0;
bool Closed=false;
int ErrorCode=0;
string ErrLog="a";
double BuyStopLoss=0;
double SellStopLoss=0;
bool NewBar=false;
double ThisBarOpen=0;
double SmallMA=0;
double MediumMA=0;
double LargeMA=0;
int Counter=0;



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
CalcDigits = MarketInfo(Symbol(),MODE_DIGITS);//MODE_DIGITS is count of digits after decimal point
if(CalcDigits==0) CalcPoint=1;//Dow      
if(CalcDigits==1) CalcPoint=0.1;   
if(CalcDigits==2) CalcPoint=0.01;//Gold & Nymex
if(CalcDigits==3) CalcPoint=0.01;//Yen
if(CalcDigits==4) CalcPoint=0.0001;//Not used
if(CalcDigits==5) CalcPoint=0.0001;//Non-Yen forex
InternalStopLoss=SystemStopLoss*CalcPoint;
   
   return(INIT_SUCCEEDED);
  }
//-----------------------------------------------

void OnTick()
{

   if(CheckOncePerBar)
      {
      if(ThisBarOpen!=Open[0])
         {
         ThisBarOpen=Open[0];
         NewBar=true;
         }
      else NewBar=false;
      }
    else NewBar=true;

if(NewBar)
{


//Reset Moving Averages
SmallMA=iMA(NULL,0,8,0,1,0,0);
MediumMA=iMA(NULL,0,10,0,1,0,0);
LargeMA=iMA(NULL,0,50,0,1,0,0);


   if(SmallMA>MediumMA&&MediumMA>LargeMA) MABuyFanning=true;
   else MABuyFanning=false;
      
   if(SmallMA<MediumMA&&MediumMA<LargeMA) MASellFanning=true; 
   else MASellFanning=false;   



if(BuyTicket==0&&MABuyFanning)
 {
      RefreshRates();
      BuyStopLoss= Bid-InternalStopLoss;
   //   while(IsTradeContextBusy()) Sleep(10);
      BuyTicket=OrderSend(Symbol(),OP_BUY,FixedLotSize,Ask,Slippage,BuyStopLoss,0,"Buy Order",MagicNumber,0,Green);
          if(BuyTicket==-1)
            {
            ErrorCode=GetLastError();
            Alert("Symbol: ",Symbol(),"Error in buy routine: ",ErrorCode);
            ErrLog=StringConcatenate("Bid: ",MarketInfo(Symbol(),MODE_BID)," Ask: ",MarketInfo(Symbol(),MODE_ASK)," Lots: ",FixedLotSize," Stop Loss: ",BuyStopLoss);
            Print(ErrLog);
            //Buy ticket revert to 0 so it can try again in case of slow connection/timeout etc.
            BuyTicket=0;
            } 
 }   


if(SellTicket==0&&MASellFanning)
 {
      RefreshRates();
      SellStopLoss=Ask+InternalStopLoss;
    //  while(IsTradeContextBusy()) Sleep(10);
      SellTicket=OrderSend(Symbol(),OP_SELL,FixedLotSize,Bid,Slippage,SellStopLoss,0,"Sell Order",MagicNumber,0,Red);
          if(SellTicket==-1)
            {
            ErrorCode=GetLastError();
            Alert("Symbol: ",Symbol(),"Error in sell routine: ",ErrorCode);
            ErrLog=StringConcatenate("Bid: ",MarketInfo(Symbol(),MODE_BID)," Ask: ",MarketInfo(Symbol(),MODE_ASK)," Lots: ",FixedLotSize," Stop Loss: ",SellStopLoss);
            Print(ErrLog);
            SellTicket=0;
            } 
  }  

//Exits

         if(BuyTicket!=0)
            {
            if(SmallMA<MediumMA)
            {
            for(Counter=0; Counter<=OrdersTotal()-1;Counter++)
               {
               SelectedOrder=OrderSelect(Counter,SELECT_BY_POS);
               if(OrderMagicNumber()==MagicNumber&&OrderSymbol()==Symbol()&&OrderType()==OP_BUY)
                  {
                 // while(IsTradeContextBusy()) Sleep(10);
                  Closed=OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_BID),Slippage,Red);
                  if(Closed) BuyTicket=0;
                  else Alert("Symbol: ",Symbol()," Ticket: ",BuyTicket," unable to close buy order(s): buy ma convergence close routine");                  
                  }
            Counter--;               
                }
            }
            }

         if(SellTicket!=0)
            {
            if(SmallMA>MediumMA)
            {
            for(Counter=0;Counter<=OrdersTotal()-1;Counter++)
               {
               SelectedOrder = OrderSelect(Counter,SELECT_BY_POS);
               if(OrderMagicNumber()==MagicNumber&&OrderSymbol()==Symbol()&&OrderType()==OP_SELL)
                  {
               //   while(IsTradeContextBusy()) Sleep(10);
                  Closed=OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_ASK),Slippage,Red);
                  if(Closed) SellTicket=0;
                  else Alert("Symbol: ",Symbol()," Ticket: ",SellTicket," unable to close sell order(s): sell ma convergence close routine");                  
                  }
            Counter--;               
                }
            }
            }
}            
return;   
}
 

아직 카운터가 있습니다.

GumRai의 의견 참조 https://www.mql5.com/en/forum/151167/page2#954622

 
두 개의 연속 막대가 정확히 동일한 시가를 가질 수 있다고 생각하십니까?
   if (CheckOncePerBar){
       if (ThisBarOpen!= Open [ 0 ]){
         ThisBarOpen= Open [ 0 ];
         NewBar= true ;
         }
       else NewBar= false ;
      }
     else NewBar= true ;

if (NewBar)
{
   : // doit
항상 시간을 사용
 static datetime ThisBarTime = 0 ;
if (ThisBarTime != Time [ 0 ] || !CheckOncePerBar)
{
   ThisBarTime = Time [ 0 ];
   : // doit
이것은 초기화/초기화 주기를 처리하지 않습니다. 외부 정적 변수 - MQL4 forum
 

게시물에 감사드립니다.

저는 시간 대신 Open을 사용했습니다. 왜냐하면 막대가 열리면 영원히 고정되고 절대 바뀌지 않기 때문입니다. 두 개의 연속 막대를 찾는 것이 아닙니다. 동일한 막대이며 틱이 방금 발생한 현재 막대의 열기가 ThisBarOpen에 저장된 열기 값과 동일한지 여부를 묻습니다. 또한 Open은 단순한 데이터 조회이기 때문에 Open이 Time보다 더 간단할 것이라고 생각했지만 Time은 다른 것을 참조하고 일종의 계산을 수행해야 할 수도 있습니다.

다시 카운터--; 코드가 MagicNumber와 Symbol()이 모두 동일한지 먼저 확인 하기 때문에 다른 EA가 주문을 마감할 수 없기 때문에 변경하지 않았습니다.

나는 확실히 BuyTicket의 내용을 지우는 정전에 대한 요점을 취하지만 내 테스트 중에는 발생하지 않았으며 내 코드는 여전히 매우 느리게 실행됩니다. 그러므로 나는 그것이 어떻게 원인이 될 수 있는지 알 수 없습니다.

현재 내 주요 질문은 열린 위치가 있는 터미널의 창에서 EA를 제거하는 데 왜 그렇게 오랜 시간이 걸렸고 열린 위치가 없는 터미널에서 제거하는 데 전혀 시간이 걸리지 않았는지입니다. 우연의 일치일 수도 있지만 네 개의 창 모두?

 
지난번에 나는 같은 문제가 있었고 실제로 ea를 코딩하는 방법입니다. 어쩌면 당신의 EA가 처음부터 다시 작성되어야 할 때입니다. 예를 들어 v2.
 

이것이 귀하의 문제와 관련이 있는지는 모르겠지만 EA당 1개 구매, 1개 판매로 거래를 제한하는데 왜 이 모든 작업을 수행합니까?

   if (SellTicket!= 0 )
  { if (SmallMA>MediumMA)
   { for (Counter= 0 ;Counter<= OrdersTotal ()- 1 ;Counter++)
    {SelectedOrder = OrderSelect (Counter, SELECT_BY_POS );
     if ( OrderMagicNumber ()==MagicNumber&& OrderSymbol ()== Symbol ()&& OrderType ()== OP_SELL )
     {Closed= OrderClose ( OrderTicket (), OrderLots (), MarketInfo ( Symbol (), MODE_ASK ),Slippage,Red);
       if (Closed) SellTicket= 0 ;
       else Alert ( "Symbol: " , Symbol (), " Ticket: " ,SellTicket, " unable to close sell order(s): sell ma convergence close routine" );                  
     }
    Counter--;               
}}}} 

여기에 이미 티켓 번호가 있습니다.

 SellTicket = OrderSend ( Symbol (), OP_SELL ,FixedLotSize, Bid ,Slippage,SellStopLoss, 0 , "Sell Order" ,MagicNumber, 0 ,Red);

그것을 정적 int로 만들면 주문 풀을 통해 트롤링하지 않고 해당 티켓 번호로 명시적으로 주문을 닫을 수 있습니다.

Closed= OrderClose ( SellTicket , OrderLots (), MarketInfo ( Symbol (), MODE_ASK ),Slippage,Red);
 
Sneck55 :

게시물에 감사드립니다.


다시 카운터--; 코드가 MagicNumber와 Symbol()이 모두 동일한지 먼저 확인하기 때문에 다른 EA가 주문을 마감할 수 없기 때문에 변경하지 않았습니다.


현재 내 주요 질문은 열린 위치가 있는 터미널의 창에서 EA를 제거하는 데 왜 그렇게 오랜 시간이 걸렸고 열린 위치가 없는 터미널에서 제거하는 데 전혀 시간이 걸리지 않았는지입니다. 우연의 일치일 수도 있지만 네 개의 창 모두?


루프가 어떻게 작동하는지 이해합니까?

 if (BuyTicket!= 0 )
            {
             if (SmallMA<MediumMA)
            {
             for (Counter= 0 ; Counter<= OrdersTotal ()- 1 ; Counter++ )
               {
               SelectedOrder= OrderSelect (Counter, SELECT_BY_POS );
               if ( OrderMagicNumber ()==MagicNumber&& OrderSymbol ()== Symbol ()&& OrderType ()== OP_BUY )
                  {
                 // while(IsTradeContextBusy()) Sleep(10);
                  Closed= OrderClose ( OrderTicket (), OrderLots (), MarketInfo ( Symbol (), MODE_BID ),Slippage,Red);
                   if (Closed) BuyTicket= 0 ;
                   else Alert ( "Symbol: " , Symbol (), " Ticket: " ,BuyTicket, " unable to close buy order(s): buy ma convergence close routine" );                  
                  }
             Counter-- ;               
                }
            }
            }

3개의 주문이 열려 있다고 가정해 보겠습니다.

처음 실행할 때 counter==0이므로 인덱스가 0인 주문이 선택됩니다.

루프가 끝나면 counter를 1만큼 감소시키므로 counter== -1입니다.

루프가 다시 실행되기 전에 for 함수 의 일부로 카운터가 1 증가합니다. 그래서 카운터 ==0

그래서 다음 루프 카운터==0을 다시 실행합니다!! 등등.

주문 인덱스 0을 계속 확인하는 끝없는 루프에 갇혀 있습니다.

그것이 중지되는 유일한 방법은 미결 주문이 없는 경우입니다. 왜냐하면 OrdersTotal - 1은 -1이 되고 0은 <= -1이 아니기 때문입니다.

 
정말 감사합니다 GumRaj - 당신은 내 문제를 해결했습니다!! 지금은 잘 작동합니다. MQL4로 코드를 작성한 것은 이번이 처음이므로 학습 과정입니다. for 루프가 어떻게 작동하는지 혼란스러웠습니다.
 
Sneck55 : GumRaj - 당신이 내 문제를 해결했습니다!! 지금은 잘 작동합니다.
그리고 어떻게 해결하셨나요? 카운트다운을 하지 않는다면 여전히 문제(#3) 가 있습니다. 여러 주문 또는 여러 EA가 있을 때까지 숨겨져 있습니다.
 
풀에서 일어나는 일과 일치하도록 주문을 마감하면 감소해야 하지만 주문을 마감하지 않는 경우에는 감소해야 합니다. 주문을 닫지 않고 감소하면 무한 루프에 들어갑니다.
사유: