Simple Coding Help - No Promise.

toughard

Well-Known Member
Hi amit, sr114 & others experts...

I have this old code from dear augubhai....

rl=LinearReg(C,1+BarsSince(Day()!=Ref(Day(),-1)));
rl=IIf(Day()!=Ref(Day(),-1),C,rl);

Plot( rl, "", colorCustom12, styleThick);

worked around it but could not plot ema 20

can you people try your hand?

thanks
 

toughard

Well-Known Member
Hi amit, sr114 & others experts...

I have this old code from dear augubhai....

rl=LinearReg(C,1+BarsSince(Day()!=Ref(Day(),-1)));
rl=IIf(Day()!=Ref(Day(),-1),C,rl);

Plot( rl, "", colorCustom12, styleThick);

worked around it but could not plot ema 20

can you people try your hand?

thanks

and also found

In this example we shall calculate EMA for a the price of a stock. We want a 22 day EMA which is a common enough time frame for a long EMA.

The formula for calculating EMA is as follows:

EMA = Price(t) * k + EMA(y) * (1 – k)
t = today, y = yesterday, N = number of days in EMA, k = 2/(N+1)

Use the following steps to calculate a 22 day EMA:

1) Start by calculating k for the given timeframe. 2 / (22 + 1) = 0,0869

2) Add the closing prices for the first 22 days together and divide them by 22.

3) You’re now ready to start getting the first EMA day by taking the following day’s (day 23) closing price multiplied by k, then multiply the previous day’s moving average by (1-k) and add the two.

4) Do step 3 over and over for each day that follows to get the full range of EMA.

This can of course be put into Excel or some other spreadsheet software to make the process of calculating EMA semi-automatic.

To give you an algorithmic view on how this can be accomplished, see below.

public float CalculateEMA(float todaysPrice, float numberOfDays, float EMAYesterday){
float k = 2 / (numberOfDays + 1);
return todaysPrice * k + EMAYesterday * (1 – k);
}

This method would typically be called from a loop through your data, looking something like this:
foreach (DailyRecord sdr in DataRecords){
//call the EMA calculation
ema = CalculateEMA(sdr.Close, numberOfDays, yesterdayEMA);
//put the calculated ema in an array
m_emaSeries.Items.Add(sdr.TradingDate, ema);
//make sure yesterdayEMA gets filled with the EMA we used this time around
yesterdayEMA = ema;
}

Note that this is psuedo code. You would typically need to send the yesterday CLOSE value as yesterdayEMA until the yesterdayEMA is calculated from today’s EMA. That’s happening only after the loop has run more days than the number of days you have calculated your EMA for.

For a 22 day EMA, it’s only on the 23 time in the loop and thereafter that the yesterdayEMA = ema is valid. This is no big deal, since you will need data from at least 100 trading days for a 22 day EMA to be valid.
 

amitrandive

Well-Known Member
Hi amit, sr114 & others experts...

I have this old code from dear augubhai....

rl=LinearReg(C,1+BarsSince(Day()!=Ref(Day(),-1)));
rl=IIf(Day()!=Ref(Day(),-1),C,rl);

Plot( rl, "", colorCustom12, styleThick);

worked around it but could not plot ema 20

can you people try your hand?

thanks
Simply drag an EMA indicator on this chart and change parameters as per your requirement.
 

sr114

Well-Known Member
and also found

In this example we shall calculate EMA for a the price of a stock. We want a 22 day EMA which is a common enough time frame for a long EMA.

The formula for calculating EMA is as follows:

EMA = Price(t) * k + EMA(y) * (1 – k)
t = today, y = yesterday, N = number of days in EMA, k = 2/(N+1)

Use the following steps to calculate a 22 day EMA:

1) Start by calculating k for the given timeframe. 2 / (22 + 1) = 0,0869

2) Add the closing prices for the first 22 days together and divide them by 22.

3) You’re now ready to start getting the first EMA day by taking the following day’s (day 23) closing price multiplied by k, then multiply the previous day’s moving average by (1-k) and add the two.

4) Do step 3 over and over for each day that follows to get the full range of EMA.

This can of course be put into Excel or some other spreadsheet software to make the process of calculating EMA semi-automatic.

To give you an algorithmic view on how this can be accomplished, see below.

public float CalculateEMA(float todaysPrice, float numberOfDays, float EMAYesterday){
float k = 2 / (numberOfDays + 1);
return todaysPrice * k + EMAYesterday * (1 – k);
}

This method would typically be called from a loop through your data, looking something like this:
foreach (DailyRecord sdr in DataRecords){
//call the EMA calculation
ema = CalculateEMA(sdr.Close, numberOfDays, yesterdayEMA);
//put the calculated ema in an array
m_emaSeries.Items.Add(sdr.TradingDate, ema);
//make sure yesterdayEMA gets filled with the EMA we used this time around
yesterdayEMA = ema;
}

Note that this is psuedo code. You would typically need to send the yesterday CLOSE value as yesterdayEMA until the yesterdayEMA is calculated from today’s EMA. That’s happening only after the loop has run more days than the number of days you have calculated your EMA for.

For a 22 day EMA, it’s only on the 23 time in the loop and thereafter that the yesterdayEMA = ema is valid. This is no big deal, since you will need data from at least 100 trading days for a 22 day EMA to be valid.
actually what u want? to plot ema 20 which will be plotted for today or something else?

if the answer is yes then why u r inventing the wheel in a new way.

calculate ema20 and while plotting it , just make it to plot for today. matter ends here. no loop , no more further invention needed.

simply use ema20=ema(c,20) and plot it. if u need some special calculation then it is another thing , but such a simple thing - why make it complex.

1. ema 20 plotted fully


2. ema 20 plotted only for today
 

toughard

Well-Known Member
actually what u want? to plot ema 20 which will be plotted for today or something else?

if the answer is yes then why u r inventing the wheel in a new way.

calculate ema20 and while plotting it , just make it to plot for today. matter ends here. no loop , no more further invention needed.

simply use ema20=ema(c,20) and plot it. if u need some special calculation then it is another thing , but such a simple thing - why make it complex.

1. ema 20 plotted fully


2. ema 20 plotted only for today
dear sr114, I must thank you for being there to help me and others and for your self less efforts

I understood your point.... fundamentally i am not looking for ema that is plotted for all the days... as amit said i want to plot a ema(c,20) after calculating the for the price started only today that is 9.15 and it will get plotted from 21 st bar... only hiding the previous days average will not work as any filter!!
 

toughard

Well-Known Member
Simply drag an EMA indicator on this chart and change parameters as per your requirement.

amit is it that simple bcz I dont know how to verify that!!:confused:
 

amitrandive

Well-Known Member
Hi amit, sr114 & others experts...

I have this old code from dear augubhai....

rl=LinearReg(C,1+BarsSince(Day()!=Ref(Day(),-1)));
rl=IIf(Day()!=Ref(Day(),-1),C,rl);

Plot( rl, "", colorCustom12, styleThick);

worked around it but could not plot ema 20

can you people try your hand?

thanks
Simply drag an EMA indicator on this chart and change parameters as per your requirement.

amit is it that simple bcz I dont know how to verify that!!:confused:

Posting the entire code here
.
You need not verify anything.Just try and learn some Amibroker Charting basics.

Check these links,
https://www.amibroker.com/guide/tutorial.html
https://www.amibroker.com/guide/h_indbuilder.html
Code:
rl=LinearReg(C,1+BarsSince(Day()!=Ref(Day(),-1)));
rl=IIf(Day()!=Ref(Day(),-1),C,rl);

Plot( rl, "", colorCustom12, styleThick);

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("EMA");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( EMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
_SECTION_END();
 

amitrandive

Well-Known Member
http://livetradesystem.com/mcx-crude-oil.php
i also need this afl pls any one have this afl it is on moving average crossover afl pls help

Already gave you a similar code in post # 2317.

Twist the parameters to get a similar picture.
http://www.traderji.com/amibroker/90119-simple-coding-help-no-promise-3.html#post1016739

Do not search for Holy Grails , Heiken Ashi looks easy to trade in hindsight but will not always give you a correct trade.Check this AFL with Nifty chart today.Nifty opened with a Gap up ,but Heiken Ashi cadle due to its nature shows continuous candle with buy and sell signals.

Also the website link you have given advises to trade with "500" quantity and the profits shown are as per that quantity.
 

Similar threads