crm

Login

 

Example 9 (Using the AG_AT() function code to manage Instrument 12)

Categorized as:AutoGraf

» Example 9 (Using the AG_AT() function code to manage Instrument 12)

Languages:中文  Русский  



Example 9 (Using the AG_AT() function code to manage Instrument 12)



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

Drag_Object (12).



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.

The AG_Message() function is used to record texts of messages in the array Message[].

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 Manager[][]



The example of AG_AT() automatic trading function code

 
//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж
// AG_AT.mq4 Example 9 (instruments 12)
// 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;


//================================================================================== 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 Channal = false; // The object of the set type is not present
   bool Manage_12 = false;
   // The object of the set type is not appointed under the management of instrument 1
   int Obj_Total_AG = StrToInteger(Object[0]);// Amount of objects, examined in AG 4
   for(int k=1; k<=Obj_Total_AG; k++) // We will walk on the examined objects
      {
      // (for the practical use more thorough analysis is required)
      string His_Name = Object[k]; // Name of current object
      if (ObjectType(His_Name) == OBJ_STDDEVCHANNEL)// it is possible also OBJ_REGRESSION
         {
         // On the array of objects, guided by an instrument 12
         for(int i=1; i<=Ddraw_Object[0][0]; i++)
            {
            if(k == Ddraw_Object[i][0])
            // If an object is already appointed under the management of instrument 12
               {
               Manage_12 = true;
               // Fact, that this object already under the management of instrument 12
               break; // An object is found, farther to search there is not a necessity
               }
            }
         Channal = true; // Fact, that the object of such type already is present in a window
         break; // We terminate a loop of search of objects (we take away an index k)
         }
      }

//================================================================================== 7 ==
   if (Channal == false) // If the object of the set type is not present
      {
      string My_Chan_Name = "Channel_for_AT"; // Name of the created channel
      ObjectCreate (My_Chan_Name, OBJ_STDDEVCHANNEL,0,0,0); // We create him ..
      int Сoor_X2 = Time[0];// 1-st point on the left, 2-nd point on the right
      int Сoor_X1 = Сoor_X2 - 24*60*60; // Duration of channel is twenty-four hours
   //Channel for the practical use it is necessary to calculate through bars
      ObjectSet(My_Chan_Name, OBJPROP_TIME1, Сoor_X1); // We set co-ordinates
      ObjectSet(My_Chan_Name, OBJPROP_TIME2, Сoor_X2); // We set co-ordinates
      ObjectSet(My_Chan_Name, OBJPROP_COLOR, Orange); // We set co-ordinates
      AG_Message(Message, "АТ: We set an object OBJ_STDDEVCHANNEL.");// Message
      return(1); // It is necessary, that the set channel was accounting in AG 4
      }
   if (Manage_12 == false) // An object is, but not appointed under management of instrument 12
      { // We set an instrument 12 Pull_Object on a channel
      AG_Set_Instr(Manager, 12, 0, 0, 0, 0, 0, 0, k, 0);
      AG_Message(Message, "АТ: We set an instrument Pull_Object.");// Message
      }

//================================================================================== 8 ==
   return(1);
   }
//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж End of module жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж 9 жж
 


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 indicated in blocks 6-9.

In this example we consider placing the instrument 12 Drag_Object on a standard deviation channel. Placing the chart instrument Drag_Object does not require any orders placed in the financial instrument window. To get a better understanding of the code in the given example, you need to know what data is contained in arrays Object[] and Ddraw_Object[][]. Please note, that array Object[] contains the list of object names considered by AutoGraf 4, and array Ddraw_Object[][] contains information about objects on which the instrument 12 Drag_Object is placed.

Arrays Object[] and Ddraw_Object[][]



Block 6-7. In this example we assume that only one channel is used and the instrument Drag_Object must be placed on this channel.

At the beginning of calculation in this block we assume that the channel has not been placed on the chart yet and the instrument has not been placed on the channel either. To verify that it’s true, the necessary analysis is performed. The total number of objects considered by AutoGraf 4 is contained in the element of array Object[0]. In the cycle the objects, considered by Autograf 4, are searched through by the number of objects. If it appears, that the type for one of the objects is a standard deviation channel, it means that the object we are interested in is already placed on the chart and we don’t need to place it again.

The next step is to find out if the Instrument Drag_Object has already been placed on any of the objects with the specified type, that have been found. For this purpose data in array Ddraw_Object[][] is analyzed in the internal cycle. The value of array element Ddraw_Object[0][0] contains the total number of objects on which the instrument Drag_Object has already been placed. In this cycle a search is performed through all objects, considered in array Ddraw_Object[][], (from the first to the last one).

During the calculation, the value of the variable k is available for analysis in the internal cycle. This is the index assigned to the object (standard deviation channel) in array Object[]. The analysis performed in the internal cycle aims at finding the answer to a simple question: is there is a record in the array Ddraw_Object[][], that confirms that the instrument Drag_Object has been already placed on this object. To answer this question, a simple calculation needs to be performed, i.e. the value of the index k needs to be compared to the value of the array element Ddraw_Object[i][0], where i is the index of the current line in the internal search cycle.

Please note, that the index k can be different from the index i. The explanation why this may happen is simple: Array Object[] registers all objects that can be recognized by AutoGraf 4, not only channels, but also lines, fans, etc. For example, the channel, we are looking for, can be registered in the general list under number 17 (k=17). If this channel is the only one on which the instrument Drag_Object is placed, this channel will be registered in the first line (i=1) of array Ddraw_Object[][].

When creating strategies, one key rule of AutoGraf 4 technology should always be kept in mind – before the custom function AG_AT() is called, all arrays are updated. At the moment when the AG_AT() function is called for execution, none of the arrays contain empty lines. Meaning, that if the user has deleted or placed any objects, indexes in arrays Object[] and Ddraw_Object[][] may change. That is why, when registering objects in the AG_AT() function, calculations should not be associated with any specific numerical index values; the necessary indexes must be calculated at each execution of the AG_AT() function.

The values of variables Manage_12 and Channal are the result of calculations in block 6-7. Manage_12 and Channal values correspond to the presence of the necessary object and the fact of placing the instrument Drag_Object on this object.

Block 7-8. Depending on the values of variables Manage_12 and Channal, an object of the specified type and the instrument Drag_Object may be placed. If the value of the variable Manage_12 shows that the object of the specified type hasn’t been placed yet, such object will be placed with the specific parameters according to user’s strategy.

After an object has been placed and before the instrument Drag_Object is placed on this object, control must be returned to AutoGraf 4 in order to update arrays. Otherwise, the instrument will be placed incorrectly, because indexes of arraysObject[] and Ddraw_Object[][] haven't been changed yet (i.e. a newly placed object hasn't been registered). After arrays have been updated, AutoGraf 4 calls the AG_AT() function once again. Only at this point the instrument Drag_Object can be placed on a channel. Generally, control should be returned to AutoGraf 4 each time after an object or an order has been placed i.e. before executing any other operations connected with orders, objects and instruments.

At the next execution of the AG_AT() function, the placed instrument will be correctly registered in arrays Object[] and Ddraw_Object[][]. Meaning, it’s possible to execute the part of the program code, responsible for placing instrument Drag_Object. If calculations in block 6-7 show that the instrument has nоt been placed yet, the function AG_Set_Instr() is called for execution. The parameters of this function include the index k, that defines unambiguously on which object the instrument Drag_Object should be placed.

Block 8-9. 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:

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



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, that AutoGraf 4 will not call the AG_AT() function for execution until the user enables the automatic trading mode (the AT icon upwards).

 :   :