Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

1st candle, hourly chart, last 20 days Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
miamiblues
Posted : Monday, July 27, 2009 8:49:05 AM
Registered User
Joined: 7/1/2009
Posts: 27
I should know how to this by now....but it is ugly,

How do i look at the prior 20 days on an hourly chart to check for the first candle of the day - and create a rule that passes if that candle is an up candle? And how do hourly charts work on a daily basis - is the first hour really 30 minutes?

thanks,

sergio
Bruce_L
Posted : Monday, July 27, 2009 9:32:44 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
I'm not quite sure how the prior 20 days comes into the request. It would seem that you would just want to return True during the day if the first Candle of the day is an "up candle" and False during the day if the first Candle of the Day is not an "up candle". The following assumes an "up candle" is based on the Net Change. If the Close of the Candle is higher than the Close of the previous Candle, it is an "up candle":

Static Trigger As Boolean
Static UpCandle As Boolean
If isFirstBar Then
    Trigger = False
Else If Price.DateValue.DayOfYear <> Price.DateValue(1).DayOfYear Then
    Trigger = True
    If Price.Last > Price.Last(1) Then
        UpCandle = True
    Else
        UpCandle = False
    End If
End If
If Trigger = True Then
    If UpCandle = True Then
        Pass
    End If
Else
    SetIndexInvalid
End If

If you want an "up candle" to be based on the Close being higher than the Open instead, change the following line from:

    If Price.Last > Price.Last(1) Then

To:

    If Price.Last > Price.Open Then

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
miamiblues
Posted : Monday, July 27, 2009 12:33:51 PM
Registered User
Joined: 7/1/2009
Posts: 27
Fantastic! Thank you so much.sergio
Bruce_L
Posted : Monday, July 27, 2009 12:41:40 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
You're welcome.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
jas0501
Posted : Monday, July 27, 2009 1:24:57 PM
Registered User
Joined: 12/31/2005
Posts: 2,499
QUOTE (Bruce_L)
I'm not quite sure how the prior 20 days comes into the request. It would seem that you would just want to return True during the day if the first Candle of the day is an "up candle" and False during the day if the first Candle of the Day is not an "up candle". The following assumes an "up candle" is based on the Net Change. If the Close of the Candle is higher than the Close of the previous Candle, it is an "up candle":

Static Trigger As Boolean
Static UpCandle As Boolean
If isFirstBar Then
    Trigger = False
Else If Price.DateValue.DayOfYear <> Price.DateValue(1).DayOfYear Then
    Trigger = True
    If Price.Last > Price.Last(1) Then
        UpCandle = True
    Else
        UpCandle = False
    End If
else
    Trigger = False
End If
If Trigger = True Then
    If UpCandle = True Then
        Pass
    End If
Else
    SetIndexInvalid
End If

If you want an "up candle" to be based on the Close being higher than the Open instead, change the following line from:

    If Price.Last > Price.Last(1) Then

To:

    If Price.Last > Price.Open Then


I think you need to set the trigger = false when not the first bar of the day.
Bruce_L
Posted : Monday, July 27, 2009 1:41:52 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
That is only True if you only wish to return only one True or False per day. The RealCode Rule as written is designed to return True or False for all of the Bars during a day based on if the first Bar of the day meets the requirements. The Trigger variable is there only to make sure that the Dragged and Dropped RealCode Rule will use enough data for its calculations.

If you want the Rule to return True for only the first Bar of the day, but return False on other Bars during the day, I would use an Else UpCandle = False type structure instead of an Else Trigger = False type structure:

Static Trigger As Boolean
Static UpCandle As Boolean
If isFirstBar Then
    Trigger = False
Else If Price.DateValue.DayOfYear <> Price.DateValue(1).DayOfYear Then
    Trigger = True
    If Price.Last > Price.Last(1) Then
        UpCandle = True
    Else
        UpCandle = False
    End If
Else
    UpCandle = False
End If
If Trigger = True Then
    If UpCandle = True Then
        Pass
    End If
Else
    SetIndexInvalid
End If

If you want an "up candle" to be based on the Close being higher than the Open instead, change the following line from:

    If Price.Last > Price.Last(1) Then

To:

    If Price.Last > Price.Open Then

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
jas0501
Posted : Monday, July 27, 2009 2:59:11 PM
Registered User
Joined: 12/31/2005
Posts: 2,499

Bruce,

Since your code declares Trigger as static the first day Price.DateValue.DayOfYear <> Price.DateValue(1).DayOfYear occurs will set Trigger, for all future bars. Declaring UpCandle = False on day where  Price.DateValue.DayOfYear = Price.DateValue is confusing and overloading the UpCandle variable. Not good practice. Maybe a different name would be clearer.

I'm not sure I appreciate the use of, and need or Trigger. How would the code below display and or behave differently without any trigger?
1. As displayed on the chart?
2. As referenced in another indicator or rule via drag and drop?

My question, I expect, relates to SetIndexInvalid vs. Fail vs. not setting a value.



'
' this displays pass for all bars on an up day
'

Static upDay as Boolean

if isfirstBar then
      UpDay = false
If Price.DateValue.DayOfYear <> Price.DateValue(1).DayOfYear Then
    '
    ' adjust to taste
    '
    ' could be
    '
    '    If Price.Last > Price.Open Then
    '
    If Price.Last > Price.Last(1) Then
        UpDay = true
   Else
       UpDay = False
     End If
endif

'
' The question relates to use of SetIndexInvalid vs Fail vs. no else condition

'

if upDay then
      pass
else
     '
     ' could be SetIndexInvalue
      ' or no else condifition
      '
      Fail
endif

Thanks


jas0501
Posted : Monday, July 27, 2009 3:04:00 PM
Registered User
Joined: 12/31/2005
Posts: 2,499
QUOTE (jas0501)

...
...


if isfirstBar then
      UpDay = false
ElseIf Price.DateValue.DayOfYear <> Price.DateValue(1).DayOfYear Then
 ...
...


Needs missing Else
Bruce_L
Posted : Monday, July 27, 2009 4:41:35 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
QUOTE (jas0501)
Since your code declares Trigger as static the first day Price.DateValue.DayOfYear <> Price.DateValue(1).DayOfYear occurs will set Trigger, for all future bars.

Yes, that is the intent. Trigger serves a different purpose than UpCandle. The Trigger variable is designed to start returning True at the first Bar where output of the Rule is Valid. While this could be on the very first bar of the RealCode does not test for this possibility. The first time it knows that the output is valid is the first time that DayOfYear changes between Bars. The primary reason for this is to make sure there is enough data to calculate a Value True or False result if the RealCode Rule is Dragged and Dropped for use in the Watchlist.

QUOTE (jas0501)
Declaring UpCandle = False on day where  Price.DateValue.DayOfYear = Price.DateValue is confusing and overloading the UpCandle variable. Not good practice. Maybe a different name would be clearer.

I'm not a programmer, but I like my variable names (and I'm sorry you don't). I tend to alternate between Trigger, Start and Valid for the Variable I use to determine if I should outputting data or not. The UpCandle name was just a one-off indicating if the first bar of the day met the requirements or not.

In my original RealCode Rule, the intent was to return True or False for every Bar in a day based on if the first Bar of the day met the conditions or not.

In your version, a True or False result is only returned once a Day during the first Bar of the Day. There is no result returned for any of the other Bars during the day as setting Trigger to False means that SetIndexInvalid is returned instead of True or False. This is a legitimate use of SetIndexInvalid (you are essentially creating Daily Bars with the odd trait of being timed at the beginning instead of the end of the day), but was not the original intent of the Trigger variable when I created it for this RealCode.

In my second version, I decided to return True or False based on if the individual Bar met the Conditions, not on if the first Bar of the day met the Conditions (meaning the only Bar to return True would be the first Bar of the day, since being the first Bar of the day is now one of the requirements). The rest of the Bars by definition return False. We still need to wait for Trigger to be True to start returning Values if we want a Drag and Drop version of the RealCode Rule to display correctly in the Watchlist, but this becomes a somewhat odd requirement as the Watchlist Column will only be able to return True during the first Bar of any given day. Even two Bars of data would be enough data to accurately calculate the Watchlist Column.

I think this second version is where you decided I exercised bad practice in overloading the UpCandle variable but I can't be sure. Again, I'm not a programmer, so I guess I'll need that explained to me in better detail. It seems that the variable needs to become False at least on the Bar after it returned True unless significant changes are made to the base algorithm if you want to only return True on the first Bar of the day. It also seems like a waste of code to test for "being at the Bar after it returned True" as any test is probably going to take more cycles than just setting a variable.

QUOTE (jas0501)
How would the code below display and or behave differently without any trigger?
1. As displayed on the chart?

I wouldn't expect it to make any difference at all.

QUOTE (jas0501)
2. As referenced in another indicator or rule via drag and drop?

Try the following RealCode Rule with the first line being a Dragged and Dropped RealCode Rule called My Rule using the RealCode given in my Monday, July 27, 2009 9:32:44 AM ET post:

'# MR = condition.MyRule
If MR.Value(2) = True Then Pass

Plot this Rule on the Chart. Then replace the My Rule RealCode with the RealCode given in your Monday, July 27, 2009 1:24:57 PM ET post. You'll see that the RealCode Rule based on the Dragged and Dropped RealCode Rule changes rather significantly.

QUOTE (jas0501)
My question, I expect, relates to SetIndexInvalid vs. Fail vs. not setting a value.

I'll start with the question you didn't ask. Your RealCode Rule looks fine when Plotted on the Chart. But Drag and Drop your RealCode Rule from your Monday, July 27, 2009 2:59:11 PM ET post to the Watchlist and select Scan with a 1-Minute Chart up. Nothing is going to return True in the Watchlist as I write this (after market close) because there isn't enough data used in the Watchlist Columns for the calculations. The same is True if you don't have an Else Condition.

If you change the Fail line to SetIndexInvalid, you are going to get results in the Watchlist, but these results are going to be True for any symbol where the results have ever been True. The reason for this is that False is never returned by RealCode Rule. It is either True or nothing is returned. This will also affect Plotting the Indicator on the Chart as nothing ever turns the Plot to False after it starts returning True.

The reason I have both a Trigger variable and a UpCandle variable in my version is to keep the concept of having enough data and if the requirements are met or not separate. If you Drag and Drop my original RealCode Rule to the Watchlist and select Scan on a 1-minute Chart, it lights up for symbols where the first Bar of the day was an UpCandle and doesn't light up for symbols where the first Bar of the day wasn't an UpCandle.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Users browsing this topic
Guest-1

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.