How to get prices of a trendline in mql5

 

Please i need help, i have a simple trendline that draws a line from lowest low at a point to the current low of the most recent candle.

So what i want is to get price of the trendline at any given candlestick, for example let say my trendline crosses at the center of a candlestick i want to be able to get the price at that point.

This is code so far

 void OnTick() {

//Get the first candle visible on chart
int CandleOnChart = ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0);

//create variable for the lowest candle
int LowestCandle;

//create array for the lowest candle prices
double low[];

//sort arrays downwards from the current candle
ArraySetAsSeries(low,true);

//fill array with data for 30 candles
CopyLow(_Symbol, _Period,0,CandleOnChart,low);
 //Print("Count = ",CandleOnChart);

//calculate the lowest candle
LowestCandle = ArrayMinimum(low,0,CandleOnChart);

//create arrays for prices
MqlRates PriceInformation[];

//sort it from current candle to oldest candle
ArraySetAsSeries(PriceInformation,true);

//copy prices data into the array
int Data = CopyRates(_Symbol, _Period, 0, CandleOnChart,PriceInformation);

//delete former lines
ObjectDelete(_Symbol,"SimpleLowTrendLine");

//create object
ObjectCreate(
       _Symbol, //current symbol
       "SimpleLowTrendLine", //object name
       OBJ_TREND,          //obect type
       0,                    //main window
       PriceInformation[LowestCandle].time, //for all candles on the chart
       PriceInformation[LowestCandle].low,//from the low of the current lowest candle
       PriceInformation[0].time, //draw to candle 0
       PriceInformation[0].low //draw to candle 0 low price
 );
 
 //set the object color
 ObjectSetInteger(0,"SimpleLowTrendLine",OBJPROP_STYLE,STYLE_SOLID);
 
 //set the object style
 ObjectSetInteger(0,"SimpleLowTrendLine",OBJPROP_COLOR,Red);
 
 //set the object width
 ObjectSetInteger(0,"SimpleLowTrendLine",OBJPROP_WIDTH,1);
 
 //set the object ray
 ObjectSetInteger(0,"SimpleLowTrendLine",OBJPROP_RAY_RIGHT,true);
 
 ObjectSetInteger(0,"SimpleLowTrendLine",OBJPROP_PRICE_SCALE,true);
 
 int TrendPrices = ObjectGetInteger(0,"SimpleLowTrendLine",OBJPROP_PRICE_SCALE,true);


}

I attach an image below, from the image how can i get the price in that popup on the trendline.

Files:
 
5000543585: Please i need help, … So what i want is to get price of the trendline at any given candlestick, …
  1. Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
              No free help (2017)

  2. Perhaps you should read the manual. ObjectGetValueByTime - Object Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

 

I would do it like this but  I only coded EA that I don't know how to handle the visual effect. So you may need to search the api yourself. 

1.count the bar difference between two bars

2. Divide the price difference by the bar difference count so that you get the approximate increment of each bar

3. Thus you could get the trendline value on each bar.

4. Store the last Ask or Bid price, and compare the  last Ask or Bid price with the trendline value, and the current Ask or Bid price with the trendline value. 

5. If the last Ask/Bid value is smaller than the trendline value and the current one is larger than the trendline value, or vice versa. Then it's a cross and you could use the value in your popup. 

 
William William #:

I would do it like this but  I only coded EA that I don't know how to handle the visual effect. So you may need to search the api yourself. 

1.count the bar difference between two bars

2. Divide the price difference by the bar difference count so that you get the approximate increment of each bar

3. Thus you could get the trendline value on each bar.

4. Store the last Ask or Bid price, and compare the  last Ask or Bid price with the trendline value, and the current Ask or Bid price with the trendline value. 

5. If the last Ask/Bid value is smaller than the trendline value and the current one is larger than the trendline value, or vice versa. Then it's a cross and you could use the value in your popup. 

My major problem is getting the trendline values  "

If the last Ask/Bid value is smaller than the trendline value and the current one is larger than the trendline value, or vice versa."
 
William Roeder #:
  1. Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
              No free help (2017)

  2. Perhaps you should read the manual. ObjectGetValueByTime - Object Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

Ok but i don't if am wrong, this line of code should store the trendline prices 

 int TrendPrices = ObjectGetInteger(0,"SimpleLowTrendLine",OBJPROP_PRICE_SCALE,true);

Now how will i extract the prices so that i can access them as arrays

 
5000543585 #:

My major problem is getting the trendline values  "

If the last Ask/Bid value is smaller than the trendline value and the current one is larger than the trendline value, or vice versa."

Step 1-3 is how I would do to get the trendline value on each bar. You may not easily get the bar count from API calls.  You may use Low[y] and Low[x] if you draw the line using lows. 

I am not sure if you could get the total bar counts on both Low[y] and Low[x] so that you could get both bars difference and the price difference to get the increment of the trendline value on each bar. I never tried that in my codes. 

 
5000543585 #: Ok but i don't if am wrong, this line of code should store the trendline prices 

Doesn't store anything. Turns on the display of prices via the mouse.

5000543585 #: Now how will i extract the prices so that i can access them as arrays

Answered in #1.2

 
5000543585: Please i need help, i have a simple trendline that draws a line from lowest low at a point to the current low of the most recent candle. So what i want is to get price of the trendline at any given candlestick, for example let say my trendline crosses at the center of a candlestick i want to be able to get the price at that point.

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
Reason: