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
Calculate the distance in points from close to moving average [SOLVED]
Calculate the distance in points from close to moving average
- Attachments
-
- DistFrmSMA Chart.png
- (26.91 KiB) Not downloaded yet
-
- DistFrmSMA Calc.png
- (150.9 KiB) Not downloaded yet
- TJ
- Posts: 7743
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2223 times
- TJ
- Posts: 7743
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2223 times
Re: Calculate the distance in points from close to moving average
If this is a coding question,
you have to post your code.
you have to post your code.
Re: Calculate the distance in points from close to moving average
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));
- ABC
- Posts: 722
- Joined: 16 Dec 2006
- Location: www.abctradinggroup.com
- Has thanked: 125 times
- Been thanked: 410 times
- Contact:
Re: Calculate the distance in points from close to moving average
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
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
Re: Calculate the distance in points from close to moving average
Hi tonyt, you simply have to translate the row where you calculate distfromSMA after the calculation of the moving average:
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).
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);
Re: Calculate the distance in points from close to moving average
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.
- TJ
- Posts: 7743
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2223 times
Re: Calculate the distance in points from close to moving average
EasyLanguage/PowerLanguage is a Procedural Language.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.
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.
Re: Calculate the distance in points from close to moving average
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:
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.
That's what I do.
In your case it would look like:
Code: Select all
value1 = (Mod (C, SMA)) * 4;
I just set it to value1, you can ofc assign any variable you want.
Re: Calculate the distance in points from close to moving average [SOLVED]
Not familiar with that procedure, I will try it out thanks!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:
Result will be the difference in price between the 2 number and then a multiplication into the tick count.Code: Select all
value1 = (Mod (C, SMA)) * 4;
I just set it to value1, you can ofc assign any variable you want.
Re: Calculate the distance in points from close to moving average
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.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:
Result will be the difference in price between the 2 number and then a multiplication into the tick count.Code: Select all
value1 = (Mod (C, SMA)) * 4;
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!
Hope that it worked for you as well
Cheers
Riembaus
Re: Calculate the distance in points from close to moving average
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.
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!
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
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!
Re: Calculate the distance in points from close to moving average
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);
Re: Calculate the distance in points from close to moving average
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.
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.
Re: Calculate the distance in points from close to moving average
I've been told....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.
"go to the wiki
look up the keyword PRINT
study the usage example
pay attention to the explanation of
Expression:C:D"