Download software Tutorial videos
Subscription & data-feed pricing Class schedule


New account application Trading resources
Margin rates Stock & option commissions

Attention: Discussion forums are read-only for extended maintenance until further notice.
Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

Opening Gap Up? Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
m_badger
Posted : Monday, March 30, 2009 2:32:18 PM
Registered User
Joined: 9/13/2008
Posts: 99
Hello,
For some reason the forum's search function is being whacky.

I'd like some guidance on how to create RealCode for the following signal:

On a 3-min chart:

If stock opens gap up 4% or more then pass.

Thank you in advance.
_mad
m_badger
Posted : Monday, March 30, 2009 3:13:42 PM
Registered User
Joined: 9/13/2008
Posts: 99

This appears to work:

If price.datevalue.dayofyear <> price.datevalue(1).dayofyear AndAlso _
price.Low >= price.close(1) * 1.03 Then
pass
End If
Bruce_L
Posted : Monday, March 30, 2009 3:25:51 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
If the Gap is being measured using the current Open as it relates to the previous Close and you only want the Rule to return True on the first Bar of the day, you could try the following RealCode Rule:

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

If the Gap is being measured using the current Open as it relates to the previous Close and you want the Rule to return True for the remainder of the day, you could try the following RealCode Rule:

Static Valid As Boolean
Static Passed As Boolean
If isFirstBar Then
    Valid = False
    Passed = False
Else
    If Price.DateValue.DayOfYear <> Price.DateValue(1).DayOfYear Then
        Valid = True
        If Price.Last > Price.Last(1) * 1.04 Then
            Passed = True
        Else
            Passed = False
        End If
    End If
End If
If Valid = True Then
    If Passed = True Then Pass
Else
    SetIndexInvalid
End If

If the Gap is being measured using the current Open as it relates to the previous High and you want the Rule to return True on the first Bar of the day, you could try the following RealCode Rule:

Static Valid As Boolean
Static Count As Integer
Static PrevHigh As Single
If isFirstBar Then
    Valid = False
    Count = 0
    PrevHigh = Price.High
Else
    If Price.DateValue.DayOfYear <> Price.DateValue(1).DayOfYear Then
        Count += 1
        If Count >= 2 Then
            Valid = True
        End If
        If Price.Last > PrevHigh * 1.04 AndAlso _
            Valid = True Then
            Pass
        End If
        PrevHigh = Price.High
    Else
        PrevHigh = System.Math.Max(PrevHigh, Price.High)
    End If
End If
If Valid = False Then
    SetIndexInvalid
End If

If the Gap is being measured using the current Open as it relates to the previous High and you want the Rule to return True for the remainder of the day, you could try the following RealCode Rule:

Static Valid As Boolean
Static Passed As Boolean
Static Count As Integer
Static PrevHigh As Single
If isFirstBar Then
    Valid = False
    Passed = False
    Count = 0
    PrevHigh = Price.High
Else
    If Price.DateValue.DayOfYear <> Price.DateValue(1).DayOfYear Then
        Count += 1
        If Count >= 2 Then
            Valid = True
        End If
        If Price.Last > PrevHigh * 1.04 Then
            Passed = True
        Else
            Passed = False
        End If
        PrevHigh = Price.High
    Else
        PrevHigh = System.Math.Max(PrevHigh, Price.High)
    End If
End If
If Valid = True Then
    If Passed = True Then Pass
Else
    SetIndexInvalid
End If

Your Monday, March 30, 2009 3:13:42 PM ET post brings up two possibilities I would not have thought of regarding an "Opening Gap Up." If you want to create a RealCode Rule that returns True as long as the Low for the Day is at least 4% higher than the Close of the previous Day, you could try the following:

Static Valid As Boolean
Static Count As Integer
Static CurrLow As Single
Static PrevClose As Single
If isFirstBar Then
    Valid = False
    Count = 0
    CurrLow = Price.Low
    PrevClose = Price.Last
Else
    If Price.DateValue.DayOfYear <> Price.DateValue(1).DayOfYear Then
        Count += 1
        CurrLow = Price.Low
        PrevClose = Price.Last(1)
        If Count >= 2 Then
            Valid = True
        End If
    Else
        CurrLow = System.Math.Min(CurrLow, Price.Low)
    End If
End If
If Valid Then
    If CurrLow > PrevClose * 1.04 AndAlso _
        Valid = True Then
        Pass
    End If
Else
    SetIndexInvalid
End If

And if you want to create a RealCode Rule that returns True as long as the Low for the Day is at least 4% higher than the High of the previous Day, you could try the following instead:

Static Valid As Boolean
Static Count As Integer
Static CurrLow As Single
Static DailyHigh(1) As Single
If isFirstBar Then
    Valid = False
    Count = 0
    CurrLow = Price.Low
    DailyHigh(0) = Price.High
Else
    If Price.DateValue.DayOfYear <> Price.DateValue(1).DayOfYear Then
        Count += 1
        CurrLow = Price.Low
        DailyHigh(1) = DailyHigh(0)
        DailyHigh(0) = Price.High
        If Count >= 2 Then
            Valid = True
        End If
    Else
        CurrLow = System.Math.Min(CurrLow, Price.Low)
        DailyHigh(0) = System.Math.Max(DailyHigh(0), Price.High)
    End If
End If
If Valid Then
    If CurrLow > DailyHigh(1) * 1.04 AndAlso _
        Valid = True Then
        Pass
    End If
Else
    SetIndexInvalid
End If

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
m_badger
Posted : Monday, March 30, 2009 3:56:16 PM
Registered User
Joined: 9/13/2008
Posts: 99
QUOTE (Bruce_L)
If the Gap is being measured using the current Open as it relates to the previous Close and you want the Rule to return True for the remainder of the day, you could try the following RealCode Rule:

Static Valid As Boolean
Static Passed As Boolean
If isFirstBar Then
    Valid = False
    Passed = False
Else
    If Price.DateValue.DayOfYear <> Price.DateValue(1).DayOfYear Then
        Valid = True
        If Price.Last > Price.Last(1) * 1.04 Then
            Passed = True
        Else
            Passed = False
        End If
    End If
End If
If Valid = True Then
    If Passed = True Then Pass
Else
    SetIndexInvalid
End If


Thanks
Bruce_L
Posted : Monday, March 30, 2009 3:58:55 PM


Worden Trainer

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

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
m_badger
Posted : Monday, March 30, 2009 4:06:23 PM
Registered User
Joined: 9/13/2008
Posts: 99
Unfortunately there is a problem Bruce.
While it appears the rule is passing, and it is shown as passing on the chart, the watchlist light does not light up.  Any ideas?  This is odd, chart shows passing, print out to debug log indicates its getting there, yet no watchlist light.
Bruce_L
Posted : Monday, March 30, 2009 4:10:28 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
I get the Watchlist Lights for all of the Rules that continue returning True for the rest of the Day (you won't get Watchlist Lights for the versions where the Rule only returns True on the opening Bar of the day except while the Opening Bar is still the current Bar). That's the main reason why the Valid and SetIndexInvalid stuff is there in the first place. Is the Watchlist still Scanning the Rule?

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
m_badger
Posted : Monday, March 30, 2009 4:18:38 PM
Registered User
Joined: 9/13/2008
Posts: 99
Yes it is still scanning the rule.  There must be something wrong with my rule's logic.  But that doesn't make sense either, why would it pass correctly and show on the chart and log, while not showing up on the lights?

Here is my entire rule:

Static Dim hod As Single
Static Dim passed As Boolean
Static Dim valid As Boolean

If isFirstBar Then
passed = False
valid = False
hod = price.High
Else
If price.datevalue.dayofyear <> price.datevalue(1).dayofyear Then
valid = True
If price.Last >= price.last(1) * 1.03 Then
passed = True
hod = price.High
Else
passed = False
End If
End If
End If

' Find high of day for first 30 min
If Me.currentdate.Hour = 9 Then
If price.High > hod Then
hod = price.High
End If
End If


If valid Then
If passed Then
' after 10est, if stock goes higher than opening 30min high, pass
If Me.currentdate.Hour >= 10 Then
If price.high > hod Then
pass
If isLastBar Then
Me.log.info("30bob: " & Me.currentsymbol)
LokiStatic.playsound("C:\WINDOWS\Media\notify.wav")
End If
hod = price.High
End If
End If
Else
SetIndexInvalid
End If
End If

The signal I'm trying to write is the opening gap up signal:

 - stock gaps up more than x%
-  after 30 min, stock makes a new hod, breaking through old 30 min hod

 
Bruce_L
Posted : Monday, March 30, 2009 4:47:22 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
So you weren't actually noticing anything wrong with my RealCode but with your altered version? I did not understand that based on your Monday, March 30, 2009 4:06:23 PM ET post. I'm not a programmer and I'm really not qualified to debug other people's code.

My question about still scanning was intended to determine if the scan was completed or not. If it is not completed, the Watchlist Lights might not be visible yet.

You need to keep using SetIndexInvalid until the first point at which the Rule could legitimately return a result. In this case, this is not the first opening bar, it is the first bar with an hour of 10 after the first opening bar.

You also have the nesting wrong on the last bit in that you are returning SetIndexInvalid only after 10 and only when there wasn't an opening gap up.

I'm not quite sure if it is intentional or not, but the following line is not strictly true as the High of Day continues to increment up after 10:

' after 10est, if stock goes higher than opening 30min high, pass

In any case, this is what I came up with after correcting the nesting and setting Valid at what I think is the correct point (I haven't attempted to determine if there might be other issues):

Static Dim hod As Single
Static Dim passed As Boolean
Static Dim valid As Boolean
Static Dim count As Integer

If isFirstBar Then
    passed = False
    valid = False
    hod = price.High
    count = 0
Else
    If price.datevalue.dayofyear <> price.datevalue(1).dayofyear Then
        count += 1
        If price.Last >= price.last(1) * 1.03 Then
            passed = True
            hod = price.High
        Else
            passed = False
        End If   
    End If
End If

' Find high of day for first 30 min
If Me.currentdate.Hour = 9 Then
    If price.High > hod Then
        hod = price.High
    End If
End If

If count >= 1 AndAlso Price.DateValue.Hour >= 10 Then Valid = True

If valid Then
    If passed Then
        ' after 10est, if stock goes higher than opening 30min high, pass
        If currentdate.Hour >= 10 Then
            If price.high > hod Then
                pass
                If isLastBar Then
                    Me.log.info("30bob: " & Me.currentsymbol)
                    LokiStatic.playsound("C:\WINDOWS\Media\notify.wav")
                End If
                hod = price.High
            End If
        End If
    End If
Else
    SetIndexInvalid
End If

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
m_badger
Posted : Monday, March 30, 2009 7:22:35 PM
Registered User
Joined: 9/13/2008
Posts: 99
QUOTE (Bruce_L)
So you weren't actually noticing anything wrong with my RealCode
Correct, your code was fine.

QUOTE
I'm not quite sure if it is intentional or not, but the following line is not strictly true as the High of Day continues to increment up after 10:
Yes, that is intentional.  As new hod's occur, keep updating the hod variable to the newest hod.

QUOTE
In any case, this is what I came up with after correcting the nesting and setting Valid at what I think is the correct point (I haven't attempted to determine if there might be other issues)
This appears to be the desired result.  Will test tomorrow during market hours.

Thank you for your help, again.
_mad
Bruce_L
Posted : Monday, March 30, 2009 7:52:14 PM


Worden Trainer

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

-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.