- Calcuate Angle of a Trendline
- reading manual/visual indicators into EA
- Questions from Beginners MQL4 MT4 MetaTrader 4
By using simple maths — namely the equation "y = m * x + c" ...
Forum on trading, automated trading systems and testing trading strategies
Fernando Carreiro, 2022.04.24 12:00
y = m * x + c
Let y = price
Let x = bar index[price] = m * [index] + c
Given:
Price at Index 9 is 105 (10th bar)
Price at Index 1 is 100 (2nd bar)Therefore:
m = Δy / Δx = (105 - 100) / ( 9 - 1 ) = 5 / 8 = 0.625
c = y - m * x = 105 - m * 9 = 100 - m * 1 = 99.375
Equation:
y = 0.625 * x + 99.375
[price] = 0.625 * [index] + 99.375
Example:
What is trend-line price at bar index 5?
[price @ 5] = 0.625 * 5 + 99.375 = 102.5
Forum on trading, automated trading systems and testing trading strategies
Fernando Carreiro, 2022.04.24 12:31
Yes, obviously! It does not matter the slope as long as you clearly define the values for First and Last point.
Here is an example:
Given:Price at Index 11 is 100 (12th bar)
Price at Index 1 is 105 (2nd bar)Therefore:
m = Δy / Δx = (100 - 105) / ( 11 - 1 ) = -5 / 10 = -0.5
c = y - m * x = 100 - m * 11 = 105 - m * 1 = 105.5
Equation:
y = -0.5 * x + 105.5
[price] = -0.5 * [index] + 105.5
Example:
What is trend-line price at bar index 5?
[price @ 5] = -0.5 * 5 + 105.5 = 103
Forum on trading, automated trading systems and testing trading strategies
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
Thank you for the help, can you show me with the following data how the calculation formular must look to get the trendline price for candel index 1 or 0
Trendline data:
price 1: 1.08971
candelstick index in the chart: 55
price 2: 1.09064
candelstick index in the chart: 33
chart period is M15
is it already possible to use your formular and calculate the trendline price with this data or is more data need?
Email Account #:Thank you for the help, can you show me with the following data how the calculation formular must look to get the trendline price for candel index 1 or 0
Trendline data: price 1: 1.08971, candelstick index in the chart: 55, price 2: 1.09064, candelstick index in the chart: 33, chart period is M15
is it already possible to use your formular and calculate the trendline price with this data or is more data need?
I have already provided you with two examples of how to apply the equation, with both positive and negative slope. I even provided a code example as well.
So, please apply yourself to reading the linked threads and using the equation with your own data.
Show your own effort in presenting the calculation and post it here for review.
I have already provided you with two examples of how to apply the equation, with both positive and negative slope. I even provided a code example as well.
So, please apply yourself to reading the linked threads and using the equation with your own data.
Show your own effort in presenting the calculation and post it here for review.
i will try it, i am not sure if i will be able to build the correct calculation, i am not good in matematics if you or somebody else want to help you can give a correct calculation example i think that is somethink what everybody wants. but of course your links can be helpfull maybe.
I have already provided you with two examples of how to apply the equation, with both positive and negative slope. I even provided a code example as well.
So, please apply yourself to reading the linked threads and using the equation with your own data.
Show your own effort in presenting the calculation and post it here for review.
Now after waisting another day and trying to build the correct calculation i am back to you Fernando, i am showing you my script and it would be nice if you can tell me what is wrong in my code, you should normaly write the name of the trendline into the script setting and then it should tell you the price of the trendline at the given index, but currently my script is showing me wrong values but i dont see where i have a error, so something with my current calculation formular is wrong, maybe you find where it is wrong:
//+------------------------------------------------------------------+ //| Trendlineprice.mq4 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict #property show_inputs input string nam=""; input ENUM_TIMEFRAMES Periode=PERIOD_CURRENT; input int Priceindexbar=0; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- double price1=ObjectGet(nam,OBJPROP_PRICE1); double price2=ObjectGet(nam,OBJPROP_PRICE2); datetime time1=datetime(ObjectGet(nam,OBJPROP_TIME1)); datetime time2=datetime(ObjectGet(nam,OBJPROP_TIME2)); int bar1=iBarShift(Symbol(),Periode,time1); int bar2=iBarShift(Symbol(),Periode,time2); double value1 = price1-price2; double value2 = bar1-bar2; double value3 = value1/value2; double value4 = price2-value3*1; double price = value3 * Priceindexbar + value4; Alert("Priceindexbar ",Priceindexbar," ",price," value3 ",value3); Alert("bar1 ",bar1," bar2 ",bar2," price1 ",price1," price2 ",price2); //--- } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+
double value4 = price1 - value3 * bar1;
I suggest you use variable names that have meaning. Otherwise you will get lost in your code.
I suggest you use variable names that have meaning. Otherwise you will get lost in your code.
Thank you for the correction Fernando, now it works.
Here is the script with the correction, for everybody hoe need it.
//+------------------------------------------------------------------+ //| Trendlineprice.mq4 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict #property show_inputs input string nam=""; input ENUM_TIMEFRAMES Periode=PERIOD_CURRENT; input int Priceindexbar=0; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- double price1=ObjectGet(nam,OBJPROP_PRICE1); double price2=ObjectGet(nam,OBJPROP_PRICE2); datetime time1=datetime(ObjectGet(nam,OBJPROP_TIME1)); datetime time2=datetime(ObjectGet(nam,OBJPROP_TIME2)); int bar1=iBarShift(Symbol(),Periode,time1); int bar2=iBarShift(Symbol(),Periode,time2); double value1 = price1-price2; double value2 = bar1-bar2; double value3 = value1/value2; //double value4 = price2-value3*1; double value4 = price1 - value3 * bar1; double price = value3 * Priceindexbar + value4; Alert("Priceindexbar ",Priceindexbar," ",price," value3 ",value3); Alert("bar1 ",bar1," bar2 ",bar2," price1 ",price1," price2 ",price2); //--- } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+
If even after all the above posts, example calculations and code samples (mine and the OP's), you are still unable to do it yourself, then you will need to hire someone to do it for you.
If even after all the above posts, example calculations and code samples (mine and the OP's), you are still unable to do it yourself, then you will need to hire someone to do it for you.
As I've written I've incorporated elements of the script in something I'm working on but my question isn't about how to calculate the equation of a straight line or ObjectGetValueByShift so the code examples etc. aren't helping but I will get there. Sometimes it is better just written 'no I can't help'.
Then please explain your question in more detail, because your point did not come across. What exactly are you asking then?

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use