crm

Login

 

Example 15 (Managing instruments 23 and 14 using the AG_AT() function code)

Categorized as:AutoGraf

» Example 15 (Managing instruments 23 and 14 using the AG_AT() function code)

Languages:Русский  中文  



Example 15 (Managing instruments 23 and 14 using the AG_AT() function code)



In the example we consider the code of the function AG_AT() written to manage the following instruments:

Planned_Order (23),
Close_All (14).



In the given example we use imported functions from the file AG_Lib.ex4.

The AG_Lib.ex4 file contains some helper-functions that service the execution of the AG_AT() automatic trading function. In general these functions are not obligatory, but we strongly recommend using them. In this example following functions are used:
  • The AG_Message() function is used to record texts of messages in the array Message[][].
  • The AG_Set_Instr() function is used to record the parameters for instruments, that are being placed, in array Manager[][].
  • The AG_Delete_Instr() function is used to record the parameters for Instruments, that are being deleted, in the array Manager[][].

Array Message[]
Array Manager[][]



The example of AG_AT() automatic trading function code

 
//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж
// AG_AT.mq4 Example 15 (instruments 14, 23)
// Custom Automatic Trade function.
// Used at the construction of automatic trade strategy for application AutoGraf 4
//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж
// It is not realization of trade strategy. It is a technical example of management ..
// .. instruments for the construction of the own automatic trade system.
//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж 0 жж

#property library
#import "AG_Lib.ex4"
   int AG_Magic_Number(); // Magic Number calculation
   int AG_Message(string& Message[], string _Text);// Text messages record in Message[]
      // Control action record in Manager[][]
   int AG_Set_Instr(double& Manager[][], int ii, double v1, double v2, double v3,
         double v4, double v5, double v6, int io , int ih);
   int AG_Delete_Instr(double&Manager[][], int ii, int io,int ih);// Deleting of instruments
#import "AG_Trade_Criterion.ex4"
   int AG_Trade_Criterion_1();// First function of trade criterion definition
   int AG_Trade_Criterion_2();// Second function of trade criterion definition
#import

//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж 1 жж
int AG_AT(int Parol_AT, double Order[][], string Object[], double Instrument[][][][],
      int Ddraw_Object[][], double& Tuning[], double& Manager[][], string& Message[])
   {

//================================================================================== 2 ==
   if (!IsDemo()) // For demo account without limits
   {
      if(Parol_AT != AccountNumber() + 1) // If incorrect password enter
         { // Example of message (max 62 symbols)
         AG_Message(Message, "Developer J.Smith, http://company.com ");
         AG_Message(Message, "Incorrect password for AT function.");
         return(0);// Exit from AT function
         } // AT function will automatic turn off by AutoGraf
      }


//================================================================================== 3 ==
   static int Count = 0; // Counter
   int MN; // MagicNumber it is recommended to calculate in AG_Magic_Number() function
   static int Ticket; // Unique identifier of order
   string Comm = "AG_AT";// Commentary (it is recommended "AG_AT")
//==================================================================================

   double Lot = NormalizeDouble(Tuning[1]],2); // Value of lots
   int Per = NormalizeDouble(Tuning[2],0); // Value of percents (integer)
   int Slip = NormalizeDouble(Tuning[3],0); // Slippage (in points)
   int SL = NormalizeDouble(Tuning[4],0); // StopLoss (in points)
   int TP = NormalizeDouble(Tuning[5],0); // TakeProfit (in points)
   int Ds = NormalizeDouble(Tuning[6],0); // Distance (in points)
   int St = NormalizeDouble(Tuning[7],0);// Modification step (in points)

//================================================================================== 4==

   if (Count==0) // It can be done one time at the beginning
      {// Example of message (max 62 symbols):
      AG_Message(Message, "Developer J.Smith, http://company.com ");
      Count++; // Amount of visits of this block
      Lot = 0.0;// 0.0 means that the lots amount is calculated in AutoGraf
      Per = 7; // .. on the basis of value percent (here 7%) of sum of balance
      Slip = 1;
      SL = 100;
      TP = 75;
      Ds = 20;
      St = 3;
/*
      Lot = 2.5; // If lots amount Lot (Tuning[1]) greater than zero, ..
      Per = 0; // .. then value of percents (Tuning[2]) in application ..
      Per = 12; // .. not taken into account and it will be calculated ..
         // .. regardless of variable Per(Tuning[2]) value.


      Lot = 0.0; // If lots amount Lot (Tuning[1]) in application ..
      Per = 7; // .. AutoGraf is required to calculate on the basis ..
         // .. of set percent (Tuning[2]) value, then ..
         // .. .. Lot (Tuning[1]) value is required to set in zero.
*/

//================================================================================== 5==

      Tuning[1] = Lot; // Lots value
      Tuning[2] = Per; // Value of percents (integer)
      Tuning[3] = Slip; // Slippage (in points)
      Tuning[4] = SL; // StopLoss (in points)
      Tuning[5] = TP; // TakeProfit (in points)
      Tuning[6] = Ds; // Distance (in points)
      Tuning[7] = St;// Modification step (in points)
      AG_Message(Message,"Changing of options from АТ..");// Example of message (max. 62 chars.)
      return(1); // Exit after retuning of parameters
      }
         // After a control from AG_AT will be returned..
         // .. in application AutoGraf, in the AG_ind indicators ..
         // ... window is possible to look new values of tunings.

//================================================================================== 6 ==

   bool BLim = false; // We consider, that BuyLimit orders are not present
   bool SLim = false; // СWe consider, that SellLimit orders are not present
   int My_Ord = NormalizeDouble(Order[0][0],0); // Orders amount in our window
   for (int i=1; i<=My_Ord; i++) // On the list of orders
      {
      if (NormalizeDouble(Order[i][6],0) == 2.0)// Order BuyLimit is found
         BLim = true;
      if (NormalizeDouble(Order[i][6],0) == 3.0)// Order SellLimit is found
         SLim = true;
      }

   bool Plan_BLim = false; // We consider, that the Planned order BuyLimit is not present
   bool Plan_SLim = false; // We consider, that the Planned order SellLimit is not present
   int Plan_Ord = NormalizeDouble(Instrument[0][1][0][0],0);// Instrument amount in a window
   for (int p=1; p<=Plan_Ord; p++) // On the list of order
      {       // If the Planned order BuyLimit is already present
      if (NormalizeDouble(Instrument[0][1][p][5],0) == 2.0)
         Plan_BLim = true; // If the Planned order SellLimit is already present
      if (NormalizeDouble(Instrument[0][1][p][5],0) == 3.0)
         Plan_SLim = true;
      }

//================================================================================== 7 ==
   int TT = TimeCurrent(); // Current time
   int Tm1 = StrToTime("15:15"); // We want to set the Planned order in a period ..
   int Tm2 = StrToTime("16:00"); // .. from 15:15 to 16:00
   int Tm = StrToTime("18:30"); // Аbsolute time of execution of instrument 23 Planned_Order
   int Tm_Cls = StrToTime("23:59"); // Absolute time of execution of instrument 14 Close_All

//================================================================================== 8 ==

   // If in a window a orders is not present, then setting of instruments
   // the Planned_Order in a set period

   if (TT > Tm1 && TT < Tm2 && NormalizeDouble(Order[0][0],0) == 0.0)
      {
      // If order BuyLimit not present and Planned_Order BuyLimit still not set
      if( BLim == false && Plan_BLim == false )
         {
         double Price = Ask - 25*Point;// Executione price of instrument
         // We set an instrument 23 Planned_Order
         AG_Set_Instr(Manager, 23, Tm, Price, 50, 20, 2, Lot*0.5, 0, 0);
         // 23 = index of instrument
         // Tm = absolute execution time of instrument
         // Price = execution price of instrument (order will be open at this price or ..
         // .. at nearest possible price if a price will go away)
         // 50 = StopLoss of opened order (points)
         // 20 = TakeProfit of opened order (points)
         // 2 = type of order BuyLimit
         // Lot*0.5 = lots amount of opened order
         // 0 = a parameter absents
         // 0 = a parameter absents
         }

//================================================================================= 9 ==
      // If order BuyLimit not present and Planned_Order BuyLimit still not set
      if( SLim == false && Plan_SLim == false )
         {
         Price = Bid + 25*Point; // Execution price of instrument
         // We set an instrument 23 Planned_Order
         AG_Set_Instr(Manager, 23, Tm, Price, 50, 20, 3, Lot*0.5, 0, 0);
         }
         // If an instrument Close_All still not set

//================================================================================= 10 ==

   if( NormalizeDouble(Instrument[0][2][32][0],0) == 0.0 )
      // 0 = push-type index for an instrument 14
      // 2 = push-type index for an instrument 14
      // 32 = push-type index for an instrument 14
      // 0 = flag of set of instrument
      // 0.0= instrument not set (1.0 = is set))
      // We set an instrument 14 Close_All
      AG_Set_Instr(Manager, 14, Tm_Cls, 30, 0, 0, 0, 0, 0, 0);
      // 14 = index of instrument
      // Tm_Cls = absolute execution time of instrument
      // 30 = percent from the height of screen
      // 0 = a parameter absents
      }

//================================================================================= 11 ==
   if (NormalizeDouble(Order[0][0],0)>0.0)
      {
      for (p=1; p<=Plan_Ord; p++) // On the list of orders
         {
         // If a Planned_Order BuyLimit is set
         if (NormalizeDouble(Instrument[0][1][p][5],0) == 2.0)
            {
            AG_Delete_Instr(Manager, 23, p, 0);// We delete an instrument 23 Planned_Order
            AG_Message(Message, "АТ: We delete Planned_Order BuyLimit.");// Message
            }
         if (NormalizeDouble(Instrument[0][1][p][5],0) == 3.0)
            {
            AG_Delete_Instr(Manager, 23, p, 0);// We delete an instrument 23 Planned_Order
            AG_Message(Message, "АТ: We delete Planned_Order SellLimit.");// Message
            }
         }
      }

//================================================================================= 12 ==
   return(1); // Normal return
// return(0); // Function AT will be closed in application AutoGraf 4

   }

//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж End of module жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж 13 жж
 


The inputs and outputs of the AG_AT() function, as well as block 2-3, are described in section:

AG_AT( ) function inputs and outputs



Block 3-6 includes the new parameter values of AutoGraf 4. You can find more information here:

Array Tuning[]



The basic code implementing a trading strategy is specified in blocks 6-12. According to the strategy instruments Planned_Order are used to place BuyLimit and SellLimit pending orders. The Instruments Planned_Order are placed during the fixed period of time from 15:15 till 16:00 and only if pending orders of the specified type have not been placed yet. The execution time for every instrument is 18:30. At the end of the working day (23:59) all market orders must be closed, all pending orders must be deleted.

Block 6-7. To make a decision about placing the instruments Planned_Order, we need to know what orders are already placed in the financial instrument window where AutoGraf 4 is being executed and find out if any instances of the instrument Planned_Order have already been placed.

To detect BuyLimit and SellLimit pending orders, all orders are analyzed. For this purpose, in the cycle by the number of orders the type of every open order is verified. The total number of orders is identified by the value of Order[0][0] element. The order type is identified by the value of array Order[i][6] element. i - order index in the general list of orders in array Order[][]. 6 – array index defining the order type (according to the types of orders commonly accepted in MQL 4).

Trading operations



Chart Instruments Planned_Order are accounted in array Instrument[0][1][][]. To find out if any instances of the instrument Planned_Order have been placed on the chart, the values of elements in this array are analyzed. Indexes in the first and the second dimensions of the array (0 and 1) are unchangeable. The indexes in the third dimension define a sequence number of the instrument Planned_Order in the general list of these instruments; the indexes in the fourth dimension define the instrument parameter. The total number of instruments Planned_Order is calculated by the value of Instrument[0][1][0][0]. Instruments parameters are specified in the section:

Array Instrument[][][][]



The fact of placing instruments with specified parameters (the type of the order being opened) is analyzed in the cycle by the number of placed instruments Planned_Order. The order type, opened as a result of the instrument Planned_Order execution, is defined by the value of Instrument[0][1][p][5]. For example in the line:

 
  if (NormalizeDouble(Instrument[0][[1][p][5],0) == 2.0)
 

the following fact is calculated: that a BuyLimit order (== 2, the order type according to the classification of order types in MQL 4) will be opened as a result of the execution of this instrument (accounted with the index p). In the line:

 
      if (NormalizeDouble(Instrument[0][1][p][5],0) == 3.0)
 

the fact is calculated that a SellLimit order (== 3) will be opened as a result of this instrument execution.

According to the strategy instruments should not be placed if BuyLimit and SellLimit orders or instruments Planned_Order placing these orders are already placed on the chart.

According to the strategy, block 7-8 includes the time value when instruments should be placed and deleted as well as the time to execute all instruments Planned_Order.

Block 8-10. The direction to place instruments Planned_Order for BuyLimit orders is generated in block 8-9. The direction to place instruments Planned_Order for SellLimit orders is generated in block 9-10. If the current time falls within the range of time when instruments Planned_Order should be placed and there are no orders placed on the chart, control is passed to calculate the next criterion to place the instrument.

Block 8-9. If there are no BuyLimit orders on the chart and instruments Planned_Order for a BuyLimit order is not placed yet, the function AG_Set_Instr() is called for execution. According to the strategy the execution price is set 25 pips below the correct price (Ask). When trading this instrument parameter value (and its execution time) should be calculated according to your strategy.

When executing the AG_Set_Instr() function , parameters of the instrument that is being placed are recorded in array Manager[][]. After the AG_AT() function has been executed, this array will be read in AutoGraf 4. If parameters of instruments are specified correctly, the instruments will be placed. If AutoGraf 4 detects any errors, a message will be displayed in the sub-window, informing the user that parameter values are invalid.

Array Manager[][]



Block 9-10. Calculations to place the instrument Planned_Order for a SellLimit order is similar to the calculations in block 8-9.

Block 10-11. According to the strategy all orders must be deleted at the end of the working day. Within the range of time specified in the strategy (15:15 through 16:00) the instrument Close_All is placed, if it has not been placed before (please note, that only one instance of the instrument Close_All can be placed on the chart). The fact of placing the instrument Close_All is calculated in the line:

 
 if( NormalizeDouble(Instrument[0][2][32][0],0) == 0.0 )
 

Here 0, 2 and 32 are unchangeable indexes in the first, second and third dimensions. The index 0 in the fourth dimension of array IInstrument[0]ΐ][32][0] defines the parameter value that verifies the fact of placing an instrument on the chart. If the criterion is true, i.e. the instrument Close_All has not been placed yet, the function AG_Set_Instr() is called for execution.

When executing the function AG_Set_Instr(), parameters of the instrument that is being placed are recorded in array Manager[][]. When the AG_AT() function is executed, this array will be read in AutoGraf 4. If parameters of instruments are specified correctly, instruments will be placed. If AutoGraf 4 detects any errors, a message will be displayed in the sub-window, informing the user that parameter values are invalid.

Block 11-12. According to the strategy instruments Planned_Order placing BuyLimit and SellLimit orders are deleted when an order of any type is placed on the chart. To verify the presence of the order on the chart, a simple calculation is used:

 
   if (NormalizeDouble(Order[0][0],0)>0.0)
 

Here the value of the array Order[0][0] element is analyzed, when it is greater than zero, the search is performed to find instruments Planned_Order that place orders of the specified type.

The calculations in this cycle are similar to the calculations in block 6-7. The parameter value corresponding to the order type is analyzed. If the current analyzed instrument Planned_Order is set to open BuyLimit or SellLimit orders, the function AG_Set_Instr() is called for execution. When executing the function AG_Set_Instr(), parameters of the instrument that is being deleted are recorded in array Manager[][].

Block 12-13. After the AG_AT() function has been executed, control will be returned to AutoGraf 4. And the specified instruments will be placed and deleted based on the data recorded in array Manager[][]. To get a better understanding of how instrument parameters are indexed in array Manager[][], please refer to the following sections:

Array Instrument[][][][]
Array Manager[][]



When executing the instrument Close_All, AutoGraf 4 calls the function Close_All. While this function is being executed, no other Instruments may be executed. After all orders have been closed, the function Close_All is disabled and the automatic mode is enabled again.

AutoGraf 4 analyzes the value returned by the AG_AT() function. Under normal conditions (like in the given example) the function returns the value 1. This value is considered by the application is a direction to continue working, i.e. at each new iteration (of the major cycle) the AG_AT() function will be called for execution.

If the AG_AT() function returns the value 0, AutoGraf 4 considers it as a direction to disable the automatic mode (the AT icon will be moved downwards). Meaning, AutoGraf 4 won’t call the AG_AT() function until the user enables the automatic trading mode (the AT icon upwards).



 :   :