[ARCHIVE]Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Can't go anywhere without you - 5. - page 270

 
hoz:


I didn't mean that thefi_Ticket parameter isn't needed, but why it was set to 0 (zero) by default. I would just do it that way:

I'll duplicate the source code again:

This function allows to get trading information by an instrument (without binding to an order, for example, before opening an order) and the same, but in conjunction with a specific order (when it is followed by an order). The default value is used for ease of use of the function. The negative value of fi_Ticket is used when initializing the trade library.

P.S. And first, pay attention to the variable names, or more precisely their prefixes. Variables that have an initial b, such as bs_Symbol are declared globally from the library. Most global variables are declared in b-PSI@Base.

 

Please look at EA code, can't figure out what the reason is.... At first start it trades can open a trade immediately, but after closing it stops working until next terminal restart or on/off allow EA to trade... Works with Gann 2.0 indicator

extern string     s0                = "Setup: Main";
extern int        Magic             = 1121;
extern double     lots              = 0.1;
extern int        StopLoss          = 70;
extern int        TakeProfit        = 140;
extern bool       UseTrail          = true;
extern bool       TrailWhileMinus   = false;
extern int        Trail             = 70;

extern string     s1                = "Setup: GANN indicator";
extern bool UseBeginDate = false;
extern string BeginDate = "2010.01.01 00:00";
extern int nBarsBack = 120;//250;
extern string prices = "0=close, 4=median, 5=typical";
extern int Price_Mode = 5;
extern bool Show_GridMatrix = false;
extern bool Show_GannGrid = false;
extern bool Show_HiloArrows = true;
extern bool Show_PriceArrows = true;
extern bool Show_Comments = false;
extern string ____MainGrid____ = "ooo";
extern color MainGrid_Color = Silver;//Green;//Sienna;
extern int MainGrid_Style = STYLE_DOT;
extern int MinMaxGrid_Style = STYLE_SOLID;
extern int MainGrid_Width = 1;
extern int fontSize = 8;
extern bool Draw_AllGrids = false;
extern bool Draw_AdditionalGrids = false;
extern string ____GannGrid____ = "ttt";
extern color GannGrid_Color = Silver;//Gray;
extern int GannGrid_Style = STYLE_DOT;
extern int GannGrid_Width = 177;
extern string ____Default_GridParameters____ = "Recomanded GridInterval 35 or 36";
extern int MainGrid_Intervals = 3677;   //default=35(!)
extern double GannGrid_Interval = 8.0;//with default 8.5 is the time interval not OK; and 9 is too large
extern int Text_Shift = 50;

//s1, UseBeginDate, BeginDate, nBarsBack, prices, Price_Mode, Show_GridMatrix, Show_GannGrid, Show_HiloArrows, Show_PriceArrows, Show_Comments, ____MainGrid____, MainGrid_Style, MinMaxGrid_Style, MainGrid_Width, fontSize, Draw_AllGrids, Draw_AdditionalGrids, ____GannGrid____, GannGrid_Style, GannGrid_Width, ____Default_GridParameters____, MainGrid_Intervals, GannGrid_Interval, Text_Shift

int               slip              = 3;

int Ticket[1000];

void deinit() 
{
   SemaphoreDeinit("TRADECONTEXT");

   return(0);
}


int start()
{
   static datetime TimeFlag = 0;
   datetime        TimeLast = Time[0];
   if(TimeFlag < TimeLast)
   {
      TimeFlag = TimeLast;
      
      int GANN = GetSignal_GANN();
      
      AnalyzeSignals(GANN);
      
      if(UseTrail == true) TrailAllOrders();
   }
   return(0);
}



void AnalyzeSignals(int GANN)
{
   static int ticket = 0;
   int sig = -1;
   bool res;
   
   ticket = RefreshTicket(ticket);
   
   if(GANN == OP_BUY)
      sig = OP_BUY;
   else if(GANN == OP_SELL)
      sig = OP_SELL;   

   if(ticket != 0)
   {
      OrderSelect(ticket, SELECT_BY_TICKET);      
      int type = OrderType();
      
      if(sig == OP_BUY && type == OP_SELL)
      {
         SemaphoreTake("TRADECONTEXT"); 
         res = OrderClose(ticket, OrderLots(), Ask, slip);
         SemaphoreReturn("TRADECONTEXT");
         if(!res) {Alert("OrderClose Error: ", GetLastError());}
         else ticket = 0;
      }
   
      else if(sig == OP_SELL && type == OP_BUY)
      {
         SemaphoreTake("TRADECONTEXT"); 
         res = OrderClose(ticket, OrderLots(), Bid, slip);
         SemaphoreReturn("TRADECONTEXT");
         if(!res) {Alert("OrderClose Error: ", GetLastError());}
         else ticket = 0;
      } 
   }   
   
   
   if(ticket == 0)
   {   
      double _lot = lots;
   
      if(sig == OP_BUY)
      {
         SemaphoreTake("TRADECONTEXT"); 
         ticket = OrderSend(Symbol(), OP_BUY, _lot, Ask, slip, Bid - StopLoss*Point, Bid + TakeProfit*Point, NULL, Magic);
         SemaphoreReturn("TRADECONTEXT");
         if(ticket < 0) {Alert("OrderSend Error: ", GetLastError());}
      }
   
      else if(sig == OP_SELL)
      {
         SemaphoreTake("TRADECONTEXT");
         ticket = OrderSend(Symbol(), OP_SELL, _lot, Bid, slip, Ask + StopLoss*Point, Ask - TakeProfit*Point, NULL, Magic);
         SemaphoreReturn("TRADECONTEXT");
         if(ticket < 0) {Alert("OrderSend Error: ", GetLastError());}
      }
   }
}

int RefreshTicket(int ticket)
{
   bool res;
   
   if(ticket <= 0)
      return(0);
   else
   {
      res = OrderSelect(ticket, SELECT_BY_TICKET);
      if(!res)
         return(0);
      else if(OrderCloseTime() != 0)
         return(0);
   }
   
   return(ticket);      //all ok, ticket still valid
}

int GetSignal_GANN()
{
   //Alert("!!: ", s1, ";", UseBeginDate, ";", BeginDate, ";", nBarsBack, ";", prices, ";", Price_Mode, ";", Show_GridMatrix, ";", Show_GannGrid, ";", Show_HiloArrows, ";", Show_PriceArrows, ";", Show_Comments, ";", ____MainGrid____, ";", MainGrid_Color, ";", MainGrid_Style, ";", MinMaxGrid_Style, ";", MainGrid_Width, ";", fontSize, ";", Draw_AllGrids, ";", Draw_AdditionalGrids, ";", ____GannGrid____, ";", GannGrid_Color, ";", GannGrid_Style, ";", GannGrid_Width, ";", ____Default_GridParameters____, ";", MainGrid_Intervals, ";", GannGrid_Interval, ";", Text_Shift);

   double ga_up = iCustom(NULL, 0, "Gann v2.0", s1, UseBeginDate, BeginDate, nBarsBack, prices, Price_Mode, Show_GridMatrix, Show_GannGrid, Show_HiloArrows, Show_PriceArrows, Show_Comments, ____MainGrid____, MainGrid_Color, MainGrid_Style, MinMaxGrid_Style, MainGrid_Width, fontSize, Draw_AllGrids, Draw_AdditionalGrids, ____GannGrid____, GannGrid_Color, GannGrid_Style, GannGrid_Width, ____Default_GridParameters____, MainGrid_Intervals, GannGrid_Interval, Text_Shift, 
                                          4, 0);
  
   double ga_dn = iCustom(NULL, 0, "Gann v2.0", s1, UseBeginDate, BeginDate, nBarsBack, prices, Price_Mode, Show_GridMatrix, Show_GannGrid, Show_HiloArrows, Show_PriceArrows, Show_Comments, ____MainGrid____, MainGrid_Color, MainGrid_Style, MinMaxGrid_Style, MainGrid_Width, fontSize, Draw_AllGrids, Draw_AdditionalGrids, ____GannGrid____, GannGrid_Color, GannGrid_Style, GannGrid_Width, ____Default_GridParameters____, MainGrid_Intervals, GannGrid_Interval, Text_Shift, 
                                          6, 0); 

   Print("ga_up = ", ga_up);
   Print("ga_dn = ", ga_dn);
   
   if(ga_up > 0.0)
      return(OP_BUY);
   else if(ga_dn > 0.0)
      return(OP_SELL);
   else
      return(-1);   
}


int TrailAllOrders()
{
   int i, total;
   
   total = CreateTicketArray(OP_BUY, Magic);
   for(i=0; i<total; i++)
      TrailingStop(Ticket[i]);

   total = CreateTicketArray(OP_SELL, Magic);
   for(i=0; i<total; i++)
      TrailingStop(Ticket[i]);
}

void TrailingStop(int ticket)
{
   int res;
   OrderSelect(ticket, SELECT_BY_TICKET);
   
   if(OrderType() == OP_BUY)
      if(TrailWhileMinus == true || Bid-OrderOpenPrice()>Point*Trail)         //не тралим, пока не можем достичь безубытка первым переносом стопа   
         if(Bid - OrderStopLoss() > Trail*Point)
         {
            SemaphoreTake("TRADECONTEXT");
            res = OrderModify(OrderTicket(), 0, Bid - Trail*Point, OrderTakeProfit(), 0);
            SemaphoreReturn("TRADECONTEXT"); 
            if(res<0)
               Alert("TrailingStop OrderModify Error: ", GetLastError());
        }
         
   if(OrderType() == OP_SELL)
      if(TrailWhileMinus == true || OrderOpenPrice()-Ask>Point*Trail)         //не тралим, пока не можем достичь безубытка первым переносом стопа
         if(OrderStopLoss() - Ask > Trail*Point)
         {
            SemaphoreTake("TRADECONTEXT");
            res = OrderModify(OrderTicket(), 0, Ask + Trail*Point, OrderTakeProfit(), 0);
            SemaphoreReturn("TRADECONTEXT");        
            if(res<0)
               Alert("TrailingStop OrderModify Error: ", GetLastError());
         }
}

int CreateTicketArray(int dir, int SysID)
{
   int total=OrdersTotal(), i, c=0; if (total<=0) return (0);
        for(i=0;i<total;i++) { OrderSelect(i, SELECT_BY_POS); if ((OrderType()==dir) && (OrderMagicNumber()==SysID)) { Ticket[c] = OrderTicket(); c++; } }
        return (c);
}

//------------------------------------------SEMAPHORE------------------------------------------

int critical = 0; 

void SemaphoreTake(string SEM)
{ 
   if(GlobalVariableCheck(SEM) == false)
      GlobalVariableSet(SEM, 0);
  
   while(1==1)
   {
      if(GlobalVariableSetOnCondition(SEM, 1.0, 0.0))         //получили доступ
      {
         critical = 1;
         Print("SEMAPHORE \"", SEM, "\" TAKEN. CURRENT VALUE: ", GlobalVariableGet(SEM));
         break;      //выходим из цикла ожидания доступа
      }
      else
      {
         Print("ATTEMPT TO CAPTURE SEMAPHORE \"", SEM, "\" FAILED. SEMAPHORE BUSY. WAITING 0.1 SEC. CURRENT VALUE: ", GlobalVariableGet(SEM));
         Sleep(100);
      }
   }
} 

void SemaphoreReturn(string SEM)
{
   GlobalVariableSet(SEM, 0.0);
   Print("SEMAPHORE \"", SEM, "\" RETURNED. CURRENT VALUE: ", GlobalVariableGet(SEM));
   critical = 0;
}

void SemaphoreDeinit(string SEM)    //Вставить в ф-ю  deinit()
{
   if(critical == 1)
   {
      GlobalVariableSet(SEM,      0.0);    //на случай, если советника убивают, пока он в процессе посылки запроса
   }  
}
 
chief2000:
Here's an example from the documentation of a command that will help you - figure out how it works, change the parameters:

High[iHighest(NULL,0,MODE_HIGH,20,4)];
Low[iLowest(NULL,0,MODE_LOW,10,10)];


THANK YOU
 
chief2000:
Here's an example from the documentation of a command that will help you - figure out how it works, change the parameters:

High[iHighest(NULL,0,MODE_HIGH,20,4)];
Low[iLowest(NULL,0,MODE_LOW,10,10)];


Sorry I'm late with my reply, I've been having problems with my computer.
 
chief2000:
Here's an example from the documentation of a command that will help you - figure out how it works, change the parameters:

High[iHighest(NULL,0,MODE_HIGH,20,4)];
Low[iLowest(NULL,0,MODE_LOW,10,10)];


Sorry I'm late with my reply, I've been having problems with my computer.
 
al7bar:

Please look at EA code, can't figure out what the reason is.... At first start it trades can open a trade immediately, but after closing it stops working until next terminal restart or on/off allow EA to trade... Works with Gann 2.0 indicator

No obvious logic errors seen. Please print the code step by step. The trading procedure is tied to the value of ticket variable. First of all, trace its changes. Also pay attention to the value of global variable "TRADECONTEXT" - its value must be 0.
 
TarasBY:

This function allows you to get trading information for an instrument (without linking it to an order, for example, before opening an order) and the same, but in conjunction with a specific order (when accompanying it). The default value is used for ease of use of the function. The negative value of fi_Ticket is used when initializing the trade library.

P.S. And first, pay attention to the variable names, or more precisely their prefixes. Variables that have an initial b, such as bs_Symbol are declared globally from the library. Most global variables are declared in b-PSI@Base.


Igor, I understand that. But the point is, as I understand it, to get market information for a given symbol, we don't need to link it to an order at all. If there is an instrument, an order is not needed... Not necessary at all. After all, you only need the name of the instrument to get market information. Don't you agree?

This variable I was asking aboutbs_Symbol is first mentioned in the inclusion of b-PSY@Base.mqh, here is the string:

  bs_Symbol,                            // текущий инструмент

But, nothing is assigned to it. Then:

if (fs_Symbol != bs_Symbol || fi_Ticket < 0)

There's an expression:

fs_Symbol != bs_Symbol

Should be understood as:fs_Symbol != 0 right?

TarasBY:

P.S. And first, pay attention to the names of variables, or more precisely, their prefixes. Variables that have an initial b, such as bs_Symbol are declared globally from the library. Most global variables are declared in b-PSI@Base.


Hm. I usually use g_ to denote global variables (from the word global). Do you use l(e.g. li_cnt) to mean local variables?
 

Friends! I need help in fine-tuning an EA.

There is some code that opens an order under certain if condition. The execution of the condition can take place every tick because it is placed in the body of int start().

We need:

1. After the execution of a condition (and opening of an order), we should pause for the opening of new orders, pause for n bars. For example, if my Expert Advisor is on timeframe M30 and I need to pass 3 hours, then the pause should be 6 bars.

2. The program should work in the strategy tester.

What code is needed? And where would be the right place to insert it.

int start()
  { 
   ...

   if (...)                                                          //условие
      {
       OrderSend(Symbol(),OP_BUY,lot,Ask,0,0,0,"Order BUY",0,0);     //открытие ордера
       ...
      }

   ...
  }
 
hoz:


Igor, I understand that. But the point is, as I understand it, to get market information on a given instrument, you don't need to link it to an order at all. If there is an instrument, an order is not needed... Not necessary at all. After all, you only need the name of the instrument to get market information. Don't you agree?

This variable I was asking aboutbs_Symbol is first mentioned in the inclusion of b-PSY@Base.mqh, here is the string:

But, nothing is assigned to it. Then:

There's an expression:

Should be understood as:fs_Symbol != 0 right?


Hm. I usually use g_ to denote global variables (from the word global) and l(e.g. li_cnt) to mean local?

You still have a lot to learn. So far your assumptions are not true. My functions can serve different purposes at the same time (in order to reduce the code itself), in particular fGet_MarketInfo(). Everything is repeatedly thought out and nothing needs to be changed!

Start, for example, with an experiment: declare a global variable in the EA body and try to call this variable from the linked library. Try to link the results to variable names that have a prefix starting with b, for example bs_Symbol.

P.S. I don't need to be checked: you either use my libraries, or you don't. If something (some of my library) doesn't work correctly - let me know and I will correct it. And to ask questions - "Why does it work that way? - you need more knowledge, because "the obviousness is not on the surface".

 
TarasBY:

You still have a lot to learn. So far, your assumptions are not true. My functions can serve different purposes at the same time (in order to reduce the code itself), in particular fGet_MarketInfo(). Everything is repeatedly thought out and nothing needs to be changed!

I understand what's there. But it's not clear why it's implemented this way.

TarasBY:

For example, start with an experiment: declare a global variable in the EA body and try to call this variable from the linked library. Try to link the results to variable names that have a prefix starting with b, for example bs_Symbol.

You will not be able to use your libraries in their original form anyway, i.e. everything is bound there. Every function on a pack of others...

TarasBY:

P.S. I don't need to be checked: you either use my libraries, or you don't. If something (some library of mine) isn't working properly - let me know and I'll fix it. And to ask questions - "Why does it work that way? - you need more knowledge, because "the obviousness doesn't lie on the surface".

I didn't mean to test it in any way... In fact, there's no doubt that it's written very competently. It's just that I don't quite understand everything, that's why I said I don't think so... I look, I think... But I don't understand everything. That's why I asked.

P.S. With bs_Symbol I still don't get it.

Reason: