Calculate the distance in points from close to moving average  [SOLVED]

Questions about MultiCharts and user contributed studies.
tonyt
Posts: 48
Joined: 21 May 2021
Has thanked: 20 times
Been thanked: 1 time

Calculate the distance in points from close to moving average

Postby tonyt » 06 Feb 2022

Hello,

I am attempting to get an accurate measurement of the distance in points from the current close of a futures value, in this example the e-mini S&P, to a simple moving average. I am using Absvalue(close - SMA) as the calculation. When I print the output, I get a number that is close, but not accurate, for some reason it appears to be rounded up or down. In the last instance of the e-mini for example, the close is 4485.5 and the SMA is 4430.46, for a total points value distance of 55.04.

When I print the results of the equation, why do I get 55.10 as the distance?

download/file.php?mode=view&id=12819

download/file.php?mode=view&id=12820
Attachments
DistFrmSMA Chart.png
(26.91 KiB) Not downloaded yet
DistFrmSMA Calc.png
(150.9 KiB) Not downloaded yet

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Calculate the distance in points from close to moving average

Postby TJ » 06 Feb 2022

Your PRINT statement is wrong.

tonyt
Posts: 48
Joined: 21 May 2021
Has thanked: 20 times
Been thanked: 1 time

Re: Calculate the distance in points from close to moving average

Postby tonyt » 06 Feb 2022

thank you TJ, can you elaborate?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Calculate the distance in points from close to moving average

Postby TJ » 07 Feb 2022

If this is a coding question,
you have to post your code.

tonyt
Posts: 48
Joined: 21 May 2021
Has thanked: 20 times
Been thanked: 1 time

Re: Calculate the distance in points from close to moving average

Postby tonyt » 07 Feb 2022

Code: Select all

input: SMALength ( 10 ), TrendGuide ( 233 ), CloseLookBack ( 10 ), ATRLength ( 5 ), StopLong ( 0 ), StopShort ( 0 ), ProfitLong ( 0 ), ProfitShort ( 0 ), minfromSMA ( 1 ), maxfromSMA ( 500 ), //FirstProfX ( 0 ), TradeCut ( 0 ); var: distfromSMA ( 0 ), ATR ( 0 ), MP ( 0 ), DayCount ( 0 ), SLB ( false ), TrendSMA ( 0 ), real_body ( 0 ), real_body_sma ( 0 ); distfromSMA = absvalue(close - TrendSMA); ATR = averagetruerange(ATRLength); MP = marketposition; SLB = sessionlastbar; TrendSMA = AverageFC(close,TrendGuide); real_body = close - open; real_body_sma = averagefc(real_body,smalength); if SLB[1] then DayCount = daycount + 1; if MP <> MP[1] and MP <> 0 then daycount = 0; if real_body_sma crosses over 0 and close > Close[CloseLookBack] and close > TrendSMA and distfromSMA < maxfromSMA and distfromSMA > minfromSMA then buy ("Real_Bdy_LE") next bar market; If real_body_sma crosses under 0 and close < close[CloseLookBack] and close < TrendSMA and distfromSMA < maxfromSMA and distfromSMA > minfromSMA then sellshort ("Real_Bdy_SE") next bar market; input: TrTrigger(0), TrRetr(0), TrTriggerShort ( 0 ), // activate trailing if TrTrigger profit is reached TrRetrShort ( 0 ); //max retracement % from max profit reached var: DistOpenRunup(0); DistOpenRunup = OpenEntryMaxProfitPerContract/bigpointvalue; Setstopcontract; if OpenEntryMaxProfitPerContract>=TrTrigger{*ATR*BigPointValue} and TrTrigger>0 and TrRetr>0 and barssinceentry>1 then begin if MP=1 then sell("LX_trail") next bar entryprice + DistOpenRunup*(1-TrRetr/100) stop; end; if OpenEntryMaxProfitPerContract>=TrTriggerShort{*ATR*BigPointValue} and TrTriggerShort>0 and TrRetrShort>0 and barssinceentry>1 then begin if MP=-1 then buytocover ("SX_trail") next bar entryprice - DistOpenRunup*(1-TrRetrShort/100) stop; end; if TradeCut > 0 and daycount > TradeCut then begin if MP = 1 then sell ("TradeCut_LX") next bar market; if MP = -1 then buytocover ("TradeCut_SX") next bar market; end; { if FirstProfX > 0 and daycount > FirstProfX and openpositionprofit > 0 then begin if MP = 1 then sell ("FirstProf_LX") next bar market; if MP = -1 then buytocover ("FirstProf_SX") next bar market; end; } if MP = 1 and Profitlong > 0 then setprofittarget(ProfitLong);//*ATR*BigPointValue); if MP = -1 and ProfitShort > 0 then setprofittarget(ProfitShort);//*ATR*BigPointValue); if MP = 1 and StopLong > 0 then setstoploss(stopLong);//*ATR*BigPointValue); if MP = -1 and StopShort > 0 then setstoploss(stopshort);//*ATR*BigPointValue); Input: TitanExportMode ( 0 ); var: bin ( 0 ); if TitanExportMode = 1 then bin = WriteDailiesCTitanReports(1100101, TitanExportMode); //print(distfromSMA," ",close," ",TrendSMA," ",datetimetostring(datetime));

tonyt
Posts: 48
Joined: 21 May 2021
Has thanked: 20 times
Been thanked: 1 time

Re: Calculate the distance in points from close to moving average

Postby tonyt » 07 Feb 2022

If this is a coding question,
you have to post your code.
Here you go thank you TJ!

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: Calculate the distance in points from close to moving average

Postby ABC » 07 Feb 2022

tonyt,

your code measures the distance before you compute the average. Consequently, you are using the SMA value from the previous code calculation and not the most recent SMA value.

Regards,

ABC

tonyt
Posts: 48
Joined: 21 May 2021
Has thanked: 20 times
Been thanked: 1 time

Re: Calculate the distance in points from close to moving average

Postby tonyt » 07 Feb 2022

thank you, so I am effectively using SMA[1] for the calculation?

tonyt
Posts: 48
Joined: 21 May 2021
Has thanked: 20 times
Been thanked: 1 time

Re: Calculate the distance in points from close to moving average

Postby tonyt » 07 Feb 2022

any advice as to how I would make that current in the absvalue(close - TrendSMA) calc?

Barbo
Posts: 7
Joined: 03 Nov 2021
Has thanked: 4 times
Been thanked: 1 time

Re: Calculate the distance in points from close to moving average

Postby Barbo » 07 Feb 2022

Hi tonyt, you simply have to translate the row where you calculate distfromSMA after the calculation of the moving average:

Code: Select all

ATR = averagetruerange(ATRLength); MP = marketposition; SLB = sessionlastbar; TrendSMA = AverageFC(close,TrendGuide); distfromSMA = absvalue(close - TrendSMA); {moved here} real_body = close - open; real_body_sma = averagefc(real_body,smalength);
because if you calculate "distfromSMA" before "TrendSMAP" it will use "TrendSMAP" calculated at the previous bar (because TrendSMAP is not yet updated to the current bar).

tonyt
Posts: 48
Joined: 21 May 2021
Has thanked: 20 times
Been thanked: 1 time

Re: Calculate the distance in points from close to moving average

Postby tonyt » 07 Feb 2022

wow thank you Barbo, I did not know that the order of the variables definitions affected the order of their processing, I thought it was just a list so to speak.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Calculate the distance in points from close to moving average

Postby TJ » 07 Feb 2022

wow thank you Barbo, I did not know that the order of the variables definitions affected the order of their processing, I thought it was just a list so to speak.
EasyLanguage/PowerLanguage is a Procedural Language.

ie the codes are processed and executed one line at a time.
Except the declarations (ie the Input and Variable declarations, etc), which are processed at the time of loading the study.

User avatar
Riembaus
Posts: 18
Joined: 28 May 2021
Been thanked: 2 times

Re: Calculate the distance in points from close to moving average

Postby Riembaus » 09 Feb 2022

Wouldn't it be easier to modulate the 2 numbers and multiply it by 4?

That's what I do.

In your case it would look like:

Code: Select all

value1 = (Mod (C, SMA)) * 4;
Result will be the difference in price between the 2 number and then a multiplication into the tick count.
I just set it to value1, you can ofc assign any variable you want.

tonyt
Posts: 48
Joined: 21 May 2021
Has thanked: 20 times
Been thanked: 1 time

Re: Calculate the distance in points from close to moving average  [SOLVED]

Postby tonyt » 09 Feb 2022

Wouldn't it be easier to modulate the 2 numbers and multiply it by 4?

That's what I do.

In your case it would look like:

Code: Select all

value1 = (Mod (C, SMA)) * 4;
Result will be the difference in price between the 2 number and then a multiplication into the tick count.
I just set it to value1, you can ofc assign any variable you want.
Not familiar with that procedure, I will try it out thanks!

User avatar
Riembaus
Posts: 18
Joined: 28 May 2021
Been thanked: 2 times

Re: Calculate the distance in points from close to moving average

Postby Riembaus » 10 Feb 2022

Wouldn't it be easier to modulate the 2 numbers and multiply it by 4?

That's what I do.

In your case it would look like:

Code: Select all

value1 = (Mod (C, SMA)) * 4;
Result will be the difference in price between the 2 number and then a multiplication into the tick count.
I just set it to value1, you can ofc assign any variable you want.

Not familiar with that procedure, I will try it out thanks!
You know I have coded that calculation for the longest time in so many different ways, all of them a bit janky. It wasn't until I started studying C++ that I found this very simple method. Modulation is used a lot in C++, so I went looking for it in PowerLanguage, as it is a basic mathematical method. I found it, tried it out, as I use the difference between entryprice and lowestLow to calculate profit targets and stops. All you need to know is how may ticks there are in a point and multiply the result of the modulation and whammo, you have the number you want to place it in your strategy.

Hope that it worked for you as well :)

Cheers
Riembaus

tonyt
Posts: 48
Joined: 21 May 2021
Has thanked: 20 times
Been thanked: 1 time

Re: Calculate the distance in points from close to moving average

Postby tonyt » 15 Mar 2022

Hello,

Neither of these formats appears to accomplish what I need, which is to determine the distance in point / ticks between the close and the Moving Average. When I print the output for 'DistFromSMA' for the EUROfx (EC) for example, I get 00.00 and a price of 1.10 for the Close and the same value, 1.10 for the SMA.

Code: Select all

0.00 1.10 1.10
3/15/22 12:52PST:

The SMA is currently 1.09937

The Close is currently 1.09890

this 'distance' here is 0.00047.

How do I accurately calculate this distance?

thank you!

User avatar
Riembaus
Posts: 18
Joined: 28 May 2021
Been thanked: 2 times

Re: Calculate the distance in points from close to moving average

Postby Riembaus » 16 Mar 2022

I just tried doing this for the e-micro on 5 minute charts, using SMA20 with the following code and it comes back correct.

Code: Select all

variables: SMA(0), dist(0); SMA = Average(C, 20); if C > SMA then dist = Mod(C, SMA) else dist = Mod(SMA,C); print(Date, " - ", Time, ": Close = ", C, " SMA = ", SMA, " dist = ", dist);

User avatar
Riembaus
Posts: 18
Joined: 28 May 2021
Been thanked: 2 times

Re: Calculate the distance in points from close to moving average

Postby Riembaus » 16 Mar 2022

Hmmmm. I tried doing the same on EURUSD and for more reason the signal only reads 2 decimal points, which in turn is why it doesn't get the calculation right.

From what I've found this needs to be addressed in the settings for the symbol somehow, but I'm not quite clear on how to do that.

tonyt
Posts: 48
Joined: 21 May 2021
Has thanked: 20 times
Been thanked: 1 time

Re: Calculate the distance in points from close to moving average

Postby tonyt » 16 Mar 2022

Hmmmm. I tried doing the same on EURUSD and for more reason the signal only reads 2 decimal points, which in turn is why it doesn't get the calculation right.

From what I've found this needs to be addressed in the settings for the symbol somehow, but I'm not quite clear on how to do that.
I've been told....

"go to the wiki

look up the keyword PRINT

study the usage example

pay attention to the explanation of
Expression:C:D"


Return to “MultiCharts”