SetScaleWeekly: close of the last week
Author: MrIzzy
Creation Date: 6/2/2009 3:09 PM
profile picture

MrIzzy

#1
Here is a Daily/Weekly question. For a daily graph, a piece of code looks like this:

CODE:
Please log in to see this code.


For backtesting purposes, this works fine. However, when I run this on the Monday, the Weekly close on the Monday is set equal to the close on that Monday. The weekly close on teh Tuesday is set equal to the close on that Tuesday and so on. It is as if Wealthlab is building the Weekly bars one day at a time.

I would have expected it to be set equal to the close of the previous Friday.

Is there any method to obtain the Close of the Last Week (since that Bar is completed)? Note that the last trading day of last week may not have been a Friday since Friday may have been a holiday.
profile picture

Eugene

#2
profile picture

MrIzzy

#3
Thank you for this , Eugene.

This works flawlessly in back testing. However, it produces strange results on the last few bars.
Here are some printouts (of the stock AA).
It shows that today (Tuesday) the close of the week was adjusted to be equal to Today's close.

Bar= 2363 Date = 5/28/2009 Close= 9.09 CloseofWeek = 8.86
Bar= 2364 Date = 5/29/2009 Close= 9.22 CloseofWeek = 9.22
Bar= 2365 Date = 6/1/2009 Close= 9.83 CloseofWeek = 9.22
Bar= 2366 Date = 6/2/2009 Close= 10.52 CloseofWeek = 10.52

Note that this just happens during the current week. If I look at a past week that has already ended, I get the correct results:

Bar= 2354 Date = 5/14/2009 Close= 8.74 CloseofWeek = 10.01
Bar= 2355 Date = 5/15/2009 Close= 9.03 CloseofWeek = 9.03
Bar= 2356 Date = 5/18/2009 Close= 9.49 CloseofWeek = 9.03
Bar= 2357 Date = 5/19/2009 Close= 9.68 CloseofWeek = 9.03

For all the weeks that have been completed, the Close of the week is modified on the last day of the week, (usually Friday) and stays that way until a new week finishes.



For the current week, it exhibits a different behavior. The point is that when I run a stratgery, it will perform differently during the week than it would if I backtest it when the dates have already past.

profile picture

Eugene

#4
QUOTE:
For the current week, it exhibits a different behavior.

For the current week, the close is not determined until the week is done - hence the variation.
profile picture

MrIzzy

#5
Hi Eugene,

The only issue I am having is with the very last bar.
For all the other bars, it gives the correct close of the week.
For the very last bar, the close of the week is set to the close of the current bar.

I think that since it is the last bar, WL thinks it is indeed, the end of the week. The fix I have is NOT elegant, but it seems to work:


CODE:
Please log in to see this code.

RunningDuringTheWeek is a Boolean which is set to True unless the last bar happens to be on the Friday or, more generally, an end of week bar.

profile picture

Eugene

#6
Looks like a good workaround, if it fits the bill.
profile picture

MrIzzy

#7
Thank you very much.
In the manual, I did find a function called "NextBarIsNewMonth" and it says that

Note, however, that
the script can check the next bar's date only if that next bar actually exists in the chart.


So I guess we have to use a workaround for the last bar.
profile picture

Cone

#8
QUOTE:
So I guess we have to use a workaround for the last bar.
You can get as complicated as you want, but why not just look at a calendar?
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).