We should setup a thread for this Anagoge.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
How to buy at the closing price on current bar?
Collapse
X
-
Interesting thread, and I'm struggling with a related problem. Say, I want to backtest a strategy which only trades in the last half hour of trading, but uses data from a daily chart to make decisions. In realtime trading you can access the values of the daily indicators by setting 'Calculate on bar close' on 'False'. Because it's not very likely that an EMA 200 day will fluctuate wildly in the last half hour of trading, this is a nice method to determine the EMA value in the last half hour.
However, as far as I know, in backtesting you can't access the value of the Daily chart (with 'Calculate on bar close' set to false), so you have to wait after the bar closes to know the 200 day EMA for that day. Off course, then it's to late to enter any trades.
Does anyone know of a way to access the value of the indicators on the Daily chart while the market is still trading? (i.e. access the fluctuating values on the daily chart intraday in a backtest).
Another possibility would be to expand the intraday time frame to the daily time frame (i.e. an EMA(Close, 2000000) on a 1 minute chart), but that's quite cumbersome and distorts the data. (Correct?)
Anyone perhaps with an idea how to approach this problem?
Comment
-
Hi Baruch,Originally posted by Baruch View PostHi J_O_S,
You can compute it yourself. For EMA its not so easy but for SMA:
a=SMA(199)[1];
mySMA=(a*199+Close[0])/200;
Thanks alot, that works quite good.
Thanks!
Here's how I've implemented it:
I was wondering if it also is possible to add an 2 period Daily RSI to the backtest. Perhaps this value can be can also be reversed engineerd in the same way? I've come up with this, which sadly enough doesn't work.Code:[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]double[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New] smaDag = SMA(BarsArray[[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]], [/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]199[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New])[[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]];[/FONT] [/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]double[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New] mySMA = (smaDag*[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]199[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New] + Closes[[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]][[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]])/[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]200[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][SIZE=1][FONT=Courier New][SIZE=1][SIZE=2];[/SIZE][/SIZE][/FONT] [/SIZE][/FONT]
Edit: Please see the attachment with the code; the forum doesn't allow to place the code in my post for your convience.
Anyone an idea where I've made an error?
Regards,Attached FilesLast edited by J_o_s; 08-03-2010, 04:56 AM.
Comment
-
Hi,
Your calculation of RS is wrong.
It should be:
so first step is to know how to calculate EMA

Check how NT calculates α
From WIKI
The degree of weighting decrease is expressed as a constant smoothing factor α, a number between 0 and 1. The smoothing factor may be expressed as a percentage, so a value of 10% is equivalent to α = 0.1. A higher α discounts older observations faster. Alternatively, α may be expressed in terms of N time periods, where α = 2/(N+1). For example, N = 19 is equivalent to α = 0.1. The half-life of the weights (the interval over which the weights decrease by a factor of two) is approximately N/2.8854 (within 1% if N > 5).
Comment
-
Thanks for your comment Baruch.Originally posted by Baruch View PostHi,
Your calculation of RS is wrong.
It should be:
so first step is to know how to calculate EMA

Check how NT calculates α
From WIKI
The degree of weighting decrease is expressed as a constant smoothing factor α, a number between 0 and 1. The smoothing factor may be expressed as a percentage, so a value of 10% is equivalent to α = 0.1. A higher α discounts older observations faster. Alternatively, α may be expressed in terms of N time periods, where α = 2/(N+1). For example, N = 19 is equivalent to α = 0.1. The half-life of the weights (the interval over which the weights decrease by a factor of two) is approximately N/2.8854 (within 1% if N > 5).
However, it looks like there are different ways to calculate RSI, because http://www.investopedia.com/terms/r/rsi.asp (among other resources) use a different formula.
If I look in the NinjaTrader RSI indicator code:
This code uses SMA (but perhaps I'm looking in the wrong place? )Code:[FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]if[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New] ((CurrentBar + [/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]) == Period) [/FONT] [FONT=Courier New]{[/FONT] [/FONT][FONT=Courier New][COLOR=#008000][FONT=Courier New][COLOR=#008000][FONT=Courier New][COLOR=#008000]// First averages [/COLOR][/FONT] [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]avgDown.Set([B]SMA[/B](down, Period)[[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]]);[/FONT] [FONT=Courier New]avgUp.Set([B]SMA[/B](up, Period)[[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]]);[/FONT] [FONT=Courier New]} [/FONT] [/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]else[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT] [FONT=Courier New][FONT=Courier New]{[/FONT] [/FONT][FONT=Courier New][COLOR=#008000][FONT=Courier New][COLOR=#008000][FONT=Courier New][COLOR=#008000]// Rest of averages are smoothed[/COLOR][/FONT] [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]avgDown.Set((avgDown[[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]] * (Period - [/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]) + down[[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]]) / Period);[/FONT] [FONT=Courier New]avgUp.Set((avgUp[[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]] * (Period - [/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]) + up[[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]]) / Period);[/FONT] [FONT=Courier New]}[/FONT] [/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]double[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New] rs = avgUp[[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]] / (avgDown[[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]] == [/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New] ? [/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New] : avgDown[[/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New]]);[/FONT] [/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]double[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New] rsi = [/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]100[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New] - ([/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]100[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New] / ([/FONT][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][FONT=Courier New] + rs));[/FONT] [/FONT]
But with SMA the formula then becomes (correct me if I'm wrong):
SMAtoday = SMAyesterday + alpha x (price_today - SMA_yesterday)
Where can I find alpha then? (the EMA indicator NT code doesn't give an hint to this). And how do I differentiate between 'up' and 'down' closes? (edit: well off course through Close[0] < Close[1], but I mean: should these values be put in an DataSeries object e.g. or could the formula above be simplyfied because I only use a 2 periode RSI? I'm sorry for all the questions.
)
Regards,Last edited by J_o_s; 08-03-2010, 06:56 AM.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Yesterday, 05:17 AM
|
0 responses
58 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
133 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
73 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
45 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
50 views
0 likes
|
Last Post
|

Comment