Your Ad Here

Thursday, March 12, 2009

Sample MQ4 Code

Here is a bit of code that demonstrates what I've been blogging about. It's a simple way to manage pending position requests. The idea is that a signal his been raised but that this code will let you wait for an improvement in the price before actually opening a position.
  1. // *******************************************************************
  2. // Name: AMS_PEND.MQ4
  3. // Date: 01-Mar-2009
  4. // Prog: Rookie
  5. //
  6. // Desc: A pending position queue manager. Pending positions can be
  7. // held until a period of time or some favorable conditions.
  8. // *******************************************************************
  9. #property copyright "Copyright © 2009, Rookie"
  10. #property link "http://trying-forex.blogspot.com/"
  11. static double pendlask[4096]; // Ask price for a pending long
  12. static int pendltime[4096]; // Time used to calculate pending time
  13. static int pendlqty = 0; // How many items in pendlask queue
  14. static double pendsbid[4096]; // Bid price for a pending short
  15. static int pendstime[4096]; // Time used to calculate pending time
  16. static int pendsqty = 0; // How many items in pendsbid queue
  17. // ****************************************************************************
  18. // Add a pending position entry to the long list
  19. // ****************************************************************************
  20. void addpendl(double ask)
  21. {
  22. static double last = 0;
  23. if ( Time[0] > last )
  24. {
  25. pendltime[pendlqty] = Time[0];
  26. pendlask[pendlqty] = Ask;
  27. pendlqty++;
  28. }
  29. last = Time[0];
  30. }
  31. // ****************************************************************************
  32. // Add a pending position entry to the short list
  33. // ****************************************************************************
  34. void addpends(double bid)
  35. {
  36. static double last = 0;
  37. if ( Time[0] > last )
  38. {
  39. pendstime[pendsqty] = Time[0];
  40. pendsbid[pendsqty] = Bid;
  41. pendsqty++;
  42. }
  43. last = Time[0];
  44. }
  45. // ****************************************************************************
  46. // A simple way to determining if a pending long position should be
  47. // activated.
  48. // ****************************************************************************
  49. int dopendl_simple()
  50. {
  51. int i;
  52. static int this=0;
  53. // Dead simple "drop" style pending manager
  54. // ----------------------------------------
  55. for (i=0; i"">
  56. {
  57. if ( pendltime[i]>0 )
  58. {
  59. if ( pendlask[i] > Ask )
  60. Print("Pending L #",i," ask was ",pendlask[i]," at ",pendltime[i]," now ",Ask);
  61. // Terminate items that don't fire
  62. // -------------------------------
  63. if ( Time[0] - pendltime[i] > 3000 )
  64. {
  65. Print("Pending L #",i," from ",pendltime[i]," cleared due to time ",Time[0]);
  66. pendltime[i]=0;
  67. continue;
  68. }
  69. // Fire first pending item more than N points down
  70. // -----------------------------------------------
  71. if ( pendlask[i] - Ask > 5.0*Point )
  72. {
  73. if ( Bid <>
  74. {
  75. Print("Pending L #",i," firing");
  76. pendltime[i] = 0;
  77. pendlask[i] = 0.0;
  78. return(1);
  79. }
  80. }
  81. }
  82. }
  83. return(0);
  84. }
  85. // ****************************************************************************
  86. // A simple way to determining if a pending short position should be
  87. // activated.
  88. // ****************************************************************************
  89. int dopends_simple()
  90. {
  91. int i;
  92. static int this=0;
  93. // Dead simple "drop" style pending manager
  94. // ----------------------------------------
  95. for (i=0; i"">
  96. {
  97. if ( pendstime[i]>0 )
  98. {
  99. if ( pendsbid[i] <>
  100. Print("Pending S #",i," bid was ",pendsbid[i]," at ",pendstime[i]," now ",Bid);
  101. // Terminate items that don't fire
  102. // -------------------------------
  103. if ( Time[0] - pendstime[i] > 3000 )
  104. {
  105. Print("Pending S #",i," from ",pendstime[i]," cleared due to time ",Time[0]);
  106. pendstime[i]=0;
  107. continue;
  108. }
  109. // Fire first pending item more than N points up
  110. // ---------------------------------------------
  111. if ( Bid - pendsbid[i] > 5.0*Point )
  112. {
  113. if ( Bid > iMA(this,this,5,0,MODE_SMA,PRICE_TYPICAL,0) )
  114. {
  115. Print("Pending S #",i," firing");
  116. pendstime[i] = 0;
  117. pendsbid[i] = 0.0;
  118. return(1);
  119. }
  120. }
  121. }
  122. }
  123. return(0);
  124. }
  125. // ****************************************************************************
  126. // Algorithm selection point for long position pend processing
  127. // ****************************************************************************
  128. int dopendl()
  129. {
  130. return(dopendl_simple());
  131. }
  132. // ****************************************************************************
  133. // Algorithm selection point for short position pend processing
  134. // ****************************************************************************
  135. int dopends()
  136. {
  137. return(dopends_simple());
  138. }

You'd use code like the following in the start function to see whether or not to add a pending position and whether or not to open a position based on a prior pending position.
  1. int openl;
  2. // See if you have an "open long" signal. If so, add it to the
  3. // queue.
  4. // ------------------------------------------------------------
  5. openl = ...;
  6. if ( openl>0 )
  7. {
  8. addpendl(Ask);
  9. Print("Pending LONG at ask ",Ask);
  10. }
  11. // Now, see if the queue manager suggests it's time to open a
  12. // long position from within the queue.
  13. // ----------------------------------------------------------
  14. openl = dopendl();
  15. // Open a position here if necessary
  16. // ---------------------------------
  17. if ( openl > 0 )
  18. {
  19. ...
  20. }

Notice that this design allows you to simply comment out all the pending code and open positions directly from your original signal. This makes it easy to integrate into your own code in case you want to try this in your own testing platform and see how affects your existing EA.

No comments:

Post a Comment