Coding 3-Point touch trendlines

 

Hey everyone I'm trying to code trendlines that only take a trade after 3 touches, i'm having a hard time coding this or finding any information on something similar..

if anyone has any examples i could work from or know where to point me that explain this sort of thing would be greatly appreciated!!

thanks!

Alex

 
amcphie: Hey everyone I'm trying to code trendlines that only take a trade after 3 touches, i'm having a hard time coding this or finding any information on something similar. if anyone has any examples i could work from or know where to point me that explain this sort of thing would be greatly appreciated!!

Search the CodeBase as there should be some example code there for that.

 
amcphie:

Hey everyone I'm trying to code trendlines that only take a trade after 3 touches, i'm having a hard time coding this or finding any information on something similar..

if anyone has any examples i could work from or know where to point me that explain this sort of thing would be greatly appreciated!!

thanks!

Alex

// Inside your loop for bars

If (high[p] >= lineprice && low[p]<=lineprice)
    ++count;
else 
      count=0;
if(count ==3)
  Print("That would be 3 touches");

Assumption made, it is a straight line.

 
Thank-god Avwerosuoghene Odukudu #:

Assumption made, it is a straight line.

Oh awesome thanks!

They are actually sloping trendlines, would that change things?

 
amcphie #: Oh awesome thanks! They are actually sloping trendlines, would that change things?

Yes, you have to calculate the trend-line price value at the bar's shift. There are at least two methods, either using the function ObjectGetValueByShift() or calculating it yourself with the equation "y = m*x+c". However, only the "y = m*x+c" calculation will work during optimisations in the strategy tester. Using graphic objects will not function during optimisations.

Forum on trading, automated trading systems and testing trading strategies

Experts: Safe Trend Scalp

Fernando Carreiro, 2022.04.25 01:13

No! Do you even understand what the ObjectGetValueByShif() does?

The function, does exactly the same thing as the "y=mx+c" equation. It calculates the price for the bars either inside or outside of the two connecting points of the trend-line.

Here is an example code of a Script, to demonstrate the two methods, that has been fully tested with log results:

#property strict
#property script_show_inputs

// Script input parameters
   input uint
      nStartPointBarShift  = 5,     // Bar shift of starting point of trend-line
      nEndPointBarShift    = 15,    // Bar shift of ending   point of trend-line
      nQueryPointBarShift  = 10;    // Bar shift of query    point of trend-line

// Script start event handler
   void OnStart()
   {
      // Check if parameters are acceptable
         if( nStartPointBarShift == nEndPointBarShift )
         {
            Print( "Invalid Parameters!" );
            return;
         };

      // Calculate values
         int
            nDeltaBarShift    = (int) nEndPointBarShift - (int) nStartPointBarShift;   // Change in bar shift

         datetime
            dtStartPointTime  = iTime( _Symbol, _Period, nStartPointBarShift ),        // Time at starting point
            dtEndPointTime    = iTime( _Symbol, _Period, nEndPointBarShift   );        // Time at ending point

         double
            dbStartPointPrice = iClose( _Symbol, _Period, nStartPointBarShift ),       // Price at starting point
            dbEndPointPrice   = iClose( _Symbol, _Period, nEndPointBarShift   ),       // Price at ending point
            dbDeltaPrice      = dbEndPointPrice - dbStartPointPrice,                   // Change in price
            dbLineSlope       = dbDeltaPrice / nDeltaBarShift,                         // Slope  is the value of "m"
            dbOffset          = dbEndPointPrice - dbLineSlope * nEndPointBarShift;     // Offset is the value of "c"

      // Draw trend-line object
         string sObjectName = "Test-Trendline";
         if( ObjectCreate( sObjectName, OBJ_TREND, 0, // Create trend-line object
               dtStartPointTime, dbStartPointPrice,   // Starting point of trend-line
               dtEndPointTime,   dbEndPointPrice ) )  // Ending   point of trend-line
         {
            // Get price by both methods
               double
                  dbValueByShift = ObjectGetValueByShift( sObjectName, nQueryPointBarShift ),
                  dbPriceByShift = dbLineSlope * nQueryPointBarShift + dbOffset;

            // Print out the values to the log
               PrintFormat( "Starting Point: %s @ %d",                            DoubleToString( dbStartPointPrice, _Digits ), nStartPointBarShift );
               PrintFormat( "Ending   Point: %s @ %d",                            DoubleToString( dbEndPointPrice,   _Digits ), nEndPointBarShift   );
               PrintFormat( "Query    Point: %s @ %d (by ObjectGetValueByShift)", DoubleToString( dbValueByShift,    _Digits ), nQueryPointBarShift );
               PrintFormat( "Query    Point: %s @ %d (by y=mx+c calculation)",    DoubleToString( dbPriceByShift,    _Digits ), nQueryPointBarShift );

            // Delete Object
               ObjectDelete( sObjectName );  // Delete trend-line object
         };
   };
2022.04.25 00:07:12.139 Compiling '(Test)\TestTrendlineValue'
2022.04.25 00:07:53.150 Script (Test)\TestTrendlineValue EURUSD,H1: loaded successfully
2022.04.25 00:07:56.080 TestTrendlineValue EURUSD,H1 inputs: nStartPointBarShift=5; nEndPointBarShift=15; nQueryPointBarShift=10; 
2022.04.25 00:07:56.111 TestTrendlineValue EURUSD,H1: initialized
2022.04.25 00:07:56.111 TestTrendlineValue EURUSD,H1: Starting Point: 1.07855 @ 5
2022.04.25 00:07:56.111 TestTrendlineValue EURUSD,H1: Ending   Point: 1.07973 @ 15
2022.04.25 00:07:56.111 TestTrendlineValue EURUSD,H1: Query    Point: 1.07914 @ 10 (by ObjectGetValueByShift)
2022.04.25 00:07:56.111 TestTrendlineValue EURUSD,H1: Query    Point: 1.07914 @ 10 (by y=mx+c calculation)
2022.04.25 00:07:56.111 TestTrendlineValue EURUSD,H1: uninit reason 0
2022.04.25 00:07:56.111 Script TestTrendlineValue EURUSD,H1: removed
 
Fernando Carreiro #:

Yes, you have to calculate the trend-line price value at the bar's shift. There are at least two methods, either using the function ObjectGetValueByShift() or calculating it yourself with the equation "y = m*x+c". However, only the "y = m*x+c" calculation will work during optimisations in the strategy tester. Using graphic objects will not function during optimisations.

Oh wow i did not know that! i had run an optimisation just for fun and was wondering why it wasnt working lol.

Thanks for the help i think im starting to make sense of this.

 
amcphie #: Oh wow i did not know that! i had run an optimisation just for fun and was wondering why it wasnt working lol. Thanks for the help i think im starting to make sense of this.
You are welcome!
 
Fernando Carreiro #:
You are welcome!

if you don't mind Fernando, i've got a question for ya. if you were me (trader of 4-5 years, learning to code over last year and a half) where would you go to learn these more advanced ways of coding EA's? i've taken a few paid courses (mql4tutorial.com,Jim Dandy's course)

watched lots of youtube, read lots.. theres alot out there, not much on this kind of stuff from what i've found however..

Thanks!

Alex

 
amcphie #: if you don't mind Fernando, i've got a question for ya. if you were me (trader of 4-5 years, learning to code over last year and a half) where would you go to learn these more advanced ways of coding EA's? i've taken a few paid courses (mql4tutorial.com,Jim Dandy's course). watched lots of youtube, read lots.. theres alot out there, not much on this kind of stuff from what i've found however.. Alex

To be brutally honest, I don't believe there are any good video courses out there for MQL. Some are actually VERY bad.

On another thread, I suggested that the person learn to code in C/C++ as means to learn more about coding itself instead of being driven by the "greed" of making EAs (which are for making money) and instead be driven by need to learn how to code.

I was then attacked by a moderator telling me that it was the absolutely worst advice to give. None the less, that is still my opinion and have not changed my mind. I will leave it up to you do decide.

 
Fernando Carreiro #:

To be brutally honest, I don't believe there are any good video courses out there for MQL. Some are actually VERY bad.

On another thread, I suggested that the person learn to code in C/C++ as means to learn more about coding itself instead of being driven by the "greed" of making EAs (which are for making money) and instead be driven by need to learn how to code.

I was then attacked by a moderator telling me that it was the absolutely worst advice to give. None the less, that is still my opinion and have not changed my mind. I will leave it up to you do decide.

Well that makes me happy to hear that & i appreciate the brutal honesty! lucky for me then i have actually been reading a C++ for dummies book lol, i figured since it's more mainstream there would be better learning resources available.

If you have any more tips you wish to pass on, im all ears!

thanks for the advice!

 
Fernando Carreiro #:

To be brutally honest, I don't believe there are any good video courses out there for MQL. Some are actually VERY bad.

On another thread, I suggested that the person learn to code in C/C++ as means to learn more about coding itself instead of being driven by the "greed" of making EAs (which are for making money) and instead be driven by need to learn how to code.

I was then attacked by a moderator telling me that it was the absolutely worst advice to give. None the less, that is still my opinion and have not changed my mind. I will leave it up to you do decide.

Am not trying to promote anybody channel but "Jim Dandy" and "Orchad Fx" helped alot when I was learning the ropes for mql, sometimes the solution is right in the forum but way above ones comprehension, but I agree as well mql is simpler when you know basics like C++ language.