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 |

Debugging Support Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
danielbender
Posted : Tuesday, November 5, 2013 11:28:33 AM
Registered User
Joined: 1/14/2006
Posts: 436

 

Bruce

I need some help debugging the attached code.   Take GMCR for example.  On 2/1/2007 the code identifies a gap greater than 2%, its first gap, ie. gapnumber = 1..  The intent is to keep track of the number of days “sincegap(1)” that the gapnumber 1 remains open, ie. the number of days the stock remains above the high price on the day prior to February 1.  The problem I am having is that “sincegap(1) never goes above 1, even though thegapnumber 1 should remain open until 2/27/2007.  Thus sincegap(1) should be incrementing itself by one day for each of the 26 days between the gap open and close dates (2/1 through 2/27).  I need  another set of eyes to help me identify the fault in my logic.

Thanks…. Dan

 

'# Cumulative
'# OutputType = UserInput.Integer = 1
' Type 1 = gap size and percent of closure / Type 2 = bars since gap and % HoD close
'# MinGapPerc = UserInput.Single = 2
' Default setting for watchlist is currently 1%
'# GapType = UserInput.Integer = 1
' Type 1 = up gap / Type 2 = down gap
 
Dim gap(50) As Single
Dim sinceGap(50) As Integer
Dim gapclosed(50) As Integer
Static sign As Integer 
Static gapnumber As Integer
Dim Gapretrace(50) As Single
Dim PrMnLowSncGap(50) As Single
Static PrMxHighSncGap(50) As Single
Dim outputgapnumber As Integer
 
' New Gap logic required to handle prior gaps
If isFirstBar Then
gap(1) = Single.NaN
gapretrace(1) = Single.NaN
' sinceGap(1) = 0
gapnumber = 0
gapclosed(1) = 1
PrMxHighSncGap(1) = 0
PrMnLowSncGap(1) = 0
Else If (1 - Price.High / Price.Low(1)) >= MinGapPerc / 100 And Price.High < Price.Low(1) Then
gapnumber = gapnumber + 1
gap(gapnumber) = -100 * (1 - Price.High / Price.Low(1)) ' new gap down logic
sinceGap(gapnumber) = 0 ' Revision 1
gapclosed(gapnumber) = 0
Else If (Price.Low / Price.High(1) - 1) >= MinGapPerc / 100 And Price.High(1) < Price.Low Then
gapnumber = gapnumber + 1
gap(gapnumber) = 100 * (Price.Low / Price.High(1) - 1) ' new gap up logic
sinceGap(gapnumber) = 0 ' Revision 1
gapclosed(gapnumber) = 0
End If
'Label = sincegap(1)
'Label = Sincegap(1)=0
'Label = gapclosed(1)=0
'Label = gapnumber '= 1 continuously
'Label = price.low
 
For i As Integer = 1 To System.Math.Max(gapnumber, 1)  'Increments Sincegap here
If Gapnumber = 0 Then 
Exit For
Else If Gapclosed(i) = 0 Then
'sinceGap(i) += 1
'Label = sincegap(1)=0
'label = gapclosed(1)=0
sincegap(i) = sincegap(i) + 1
'Label = Sincegap(1) = 1
End If
Next
'Label = sincegap(1)=1
 
' Code Speedup stuff to replace Price.MaxHigh(sincegap)and Price.MinLow(sincegap)
For i As Integer = 1 To System.Math.Max(gapnumber, 1)
If Gapnumber = 0 Then Exit For
If gapclosed(i) = 0 Then
If sincegap(i) = 0 Then
PrMxHighSncGap(i) = Price.High
PrMnLowSncGap(i) = Price.Low
Else If sincegap(i) = 1 Then
PrMxHighSncGap(i) = Price.High
PrMnLowSncGap(i) = Price.Low
Else If sincegap(i) > 1 Then
If Price.High > PRMxHighSncGap(i) Then PrMxHighSncGap(i) = Price.High
If Price.Low < PrMnLowSncGap(i) Then PrMnLowSncGap(i) = Price.low
End If
End If
Next
 
' Current Gap Closed (If close, reset sincegap zero)
For i As Integer = 1 To System.Math.Max(gapnumber, 1)
If gapnumber = 0 Then Exit For
If gap(i) < 0 And PrMxHighSncGap(i) > Price.Low(sincegap(i) + 1 - 1) Then
sincegap(i) = 0 ' gap down closed
gap(i) = 0
gapclosed(i) = 1
Else If gap(i) > 0 And PrMnLowSncGap(i) < Price.High(sincegap(i) + 1 - 1) Then 
sincegap(i) = 0 ' gap up closed
gap(i) = 0
gapclosed(i) = 1
End If
Next
'Label = PrMnLowSncGap(1)
'Label = Price.High(sincegap(1))
'Label = gapclosed(1)=0
'Label = sincegap(1)=1
 
Plot = sincegap(1)
Bruce_L
Posted : Tuesday, November 5, 2013 11:32:18 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

While I am not a programmer and am not really qualified to do debugging, I will both take a look at your code and run it to try and figure out what is happening. I will do my best to see if I can figure out a way to get it to do what you want.



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
danielbender
Posted : Tuesday, November 5, 2013 12:11:28 PM
Registered User
Joined: 1/14/2006
Posts: 436

Bruce - you may not be a programmer, but you could fool me.  Based on my experieince, your coding is terrific - efficient and creative.  As far as debugging, I have no doubt you can pinpoint my coding issue(s).  I also have no doubt that you can improve/optimize my code as well.   Thanks again.  Dan

jas0501
Posted : Tuesday, November 5, 2013 8:18:24 PM
Registered User
Joined: 12/31/2005
Posts: 2,499

This might be it:: Accidental comment 
'sinceGap(i) += 1

For i As Integer = 1 To System.Math.Max(gapnumber, 1)  'Increments Sincegap here
    If Gapnumber = 0 Then 
        Exit For
    Else If Gapclosed(i) = 0 Then
        'sinceGap(i) += 1
        'Label = sincegap(1)=0
        'label = gapclosed(1)=0
        sincegap(i) = sincegap(i) + 1
       'Label = Sincegap(1) = 1
     End If
Next
'Label = sincegap(1)=1

 

A couple minor optimization comments:

1. Avoid divides and multiplies when possible.
Replace MinGapPerc / 100 with gapSize

Dim gapSize as single

If isfirstbar
      gapSize = MinGapPerc / 100 
...
...
Else If (1 - Price.High / Price.Low(1)) >= gapSize And Price.High < Price.Low(1) Then

========================================

2. The only way the gapSize can be statisfied is if Price.High < Price.Low(1)  so you don't need to check if High is less than yesterday's low yielding a simplified expression

Else If (1 - Price.High / Price.Low(1)) >= gapSize then

 

danielbender
Posted : Tuesday, November 5, 2013 10:36:05 PM
Registered User
Joined: 1/14/2006
Posts: 436

Bruce,

Eliminating the "accidental comment" 

'sinceGap(i) += 1

does not correct the issue of sincegap(i) getting stuck at 1.0 (not incrementing by 1 for each additional day since the initial gap).  Any other thoughts??????  For the ife of me, I cannot find my logic error.

By the way, thanks for the optimization suggestions.  

Dan

Bruce_L
Posted : Wednesday, November 6, 2013 9:17:37 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

The suggestions by jas0501 are actually from another customer who is quite good at writing RealCode. I have personally found previous suggestions to be quite helpful.

I have looked at, run and made some optimizations to your RealCode. I will post what I have so far, but there aren't actually calculations for all of the variables yet and some of the calculations which are made are not used. I probably won't make any additional changes without some feedback from you as I did not completely understand what your intent might be with some of the RealCode.

Are you interested in the RealCode working with symbols which might have negative values such as some index values? I ask because it makes a difference as to if we should implement one of the optimizations suggested by jas0501.

'# Cumulative
'# OutputType = UserInput.Integer = 1
' Type 1 = gap size and percent of closure / Type 2 = bars since gap and % HoD close
'# MinGapPerc = UserInput.Single = 2
' Default setting for watchlist is currently 1%
'# GapType = UserInput.Integer = 1
' Type 1 = up gap / Type 2 = down gap
 
Static gap As New List(Of Single)
Static gapUp As New List(Of Boolean)
Static gapBar As New List(Of Integer)
Static gapClose As New List(Of Integer)
Static gapRetrace As New List(Of Single)
Static gapMax As New List(Of Single)
Static gapMin As New List(Of Single)
 
' New Gap logic required to handle prior gaps
If isFirstBar Then
	gap.Clear
	gapUp.Clear
	gapBar.Clear
	gapClose.Clear
	gapRetrace.Clear
	gapMax.Clear
	gapMin.Clear
Else If Price.High < Price.Low(1) AndAlso _
	Price.High <= Price.Low(1) * (1 - MinGapPerc / 100) Then
	gap.Add(-100 * (1 - Price.High / Price.Low(1))) ' new gap down logic
	gapUp.Add(False)
	gapBar.Add(CurrentIndex)
	gapClose.Add(0)
	gapMax.Add(Price.High)
	gapMin.Add(Price.Low)
	gapRetrace.Add(0)
Else If Price.Low > Price.High(1) AndAlso _
	Price.Low >= Price.High(1) * (1 + MinGapPerc / 100) Then
	gap.Add(100 * (Price.Low / Price.High(1) - 1)) ' new gap up logic
	gapUp.Add(True)
	gapBar.Add(CurrentIndex)
	gapClose.Add(0)
	gapMax.Add(Price.High)
	gapMin.Add(Price.Low)
	gapRetrace.Add(0)
End If
If gap.Count > 0 Then
	For i As Integer = 0 To gap.Count - 1
		If gapClose(i) = 0 Then
			gapMax(i) = System.Math.Max(gapMax(i), Price.High)
			gapMin(i) = System.Math.Min(gapMin(i), Price.Low)
			If gapUp(i) = True Then
				If gapMin(i) < Price.High(CurrentIndex - gapBar(i) + 1) Then
					gapClose(i) = CurrentIndex
					Label = "Gap " & i + 1 & " Closed " & CurrentIndex - gapBar(i) + 1 & " Up"
				End If
			Else
				If gapMax(i) > Price.Low(CurrentIndex - gapBar(i) + 1) Then
					gapClose(i) = CurrentIndex
					Label = "Gap " & i + 1 & " Closed " & CurrentIndex - gapBar(i) + 1 & " Down" 
				End If
			End If
			Plot = CurrentIndex - gapBar(gapBar.Count - 1) + 1
		Else
			Plot = 0
		End If
	Next
Else
	Plot = 0
End If

If isLastBar Then
	gap.Clear
	gapUp.Clear
	gapBar.Clear
	gapClose.Clear
	gapRetrace.Clear
	gapMax.Clear
	gapMin.Clear
End If


-Bruce
Personal Criteria Formulas
TC2000 Support Articles
danielbender
Posted : Wednesday, November 6, 2013 12:52:20 PM
Registered User
Joined: 1/14/2006
Posts: 436

Bruce

Wow.  Impressive.

Regarding the unused variables - they are referenced in some code I did not provide in my initial post.  Here is the entire indicator code that includes use of "Retrace" and other output variables.

Thanks.... Dan

+++++++++++

 

'# Cumulative
'# OutputType = UserInput.Integer = 2
' Type 1 = gap size and percent of closure / Type 2 = bars since gap and % HoD close
'# MinGapPerc = UserInput.Single = 2
' Default setting for watchlist is currently 1%
'# GapType = UserInput.Integer = 2
' Type 1 = up gap / Type 2 = down gap
 
Dim gap(50) As Single
Dim sinceGap(50) As Integer
Dim gapclosed(50) As Integer
Static sign As Integer 
Static gapnumber As Integer
Dim Gapretrace(50) As Single
Dim PrMnLowSncGap(50) As Single
Static PrMxHighSncGap(50) As Single
Dim outputgapnumber As Integer
 
' New Gap occurred (logic only handles recent gaps)
' NEW LOGIC RQRD: Dim(20), gap(i), sincegap(i), sincegapclosed(i), PrMxHighSncGgap(i), PRMxLowSncGap(i)
If isFirstBar Then
gap(1) = Single.NaN
gapretrace(1) = Single.NaN
' sinceGap(1) = 0
gapnumber = 0
gapclosed(1) = 1
PrMxHighSncGap(1) = 0
PrMnLowSncGap(1) = 0
Else If (1 - Price.High / Price.Low(1)) >= MinGapPerc / 100 And Price.High < Price.Low(1) Then
gapnumber = gapnumber + 1
gap(gapnumber) = -100 * (1 - Price.High / Price.Low(1)) ' new gap down logic
sinceGap(gapnumber) = 0 ' Revision 1
gapclosed(gapnumber) = 0
Else If (Price.Low / Price.High(1) - 1) >= MinGapPerc / 100 And Price.High(1) < Price.Low Then
gapnumber = gapnumber + 1
gap(gapnumber) = 100 * (Price.Low / Price.High(1) - 1) ' new gap up logic
sinceGap(gapnumber) = 0 ' Revision 1
gapclosed(gapnumber) = 0
End If
'Label = sincegap(1)
'Label = Sincegap(1)=0
'Label = gapclosed(1)=0
'Label = gapnumber '= 1 continuously
'Label = price.low
 
For i As Integer = 1 To System.Math.Max(gapnumber, 1)  'NOT INCREMENTING SinceGAp ???? WHY NOT ?????????? GAPCLOSED(i) = 0 ???? GAP(i) RESET TO ZERO AFTER FIRST BAR?????
If Gapnumber = 0 Then 
Exit For
Else If Gapclosed(i) = 0 Then
'sinceGap(i) += 1
'Label = sincegap(1)=0
'label = gapclosed(1)=0
sincegap(i) = sincegap(i) + 1
'Label = Sincegap(1) = 1
End If
Next
'Label = sincegap(1)=1
 
' Code Speedup stuff to replace Price.MaxHigh(sincegap)and Price.MinLow(sincegap)
For i As Integer = 1 To System.Math.Max(gapnumber, 1)
If Gapnumber = 0 Then Exit For
If gapclosed(i) = 0 Then
If sincegap(i) = 0 Then
PrMxHighSncGap(i) = Price.High
PrMnLowSncGap(i) = Price.Low
Else If sincegap(i) = 1 Then
PrMxHighSncGap(i) = Price.High
PrMnLowSncGap(i) = Price.Low
Else If sincegap(i) > 1 Then
If Price.High > PRMxHighSncGap(i) Then PrMxHighSncGap(i) = Price.High
If Price.Low < PrMnLowSncGap(i) Then PrMnLowSncGap(i) = Price.low
End If
End If
Next
 
' Current Gap Closed / sincegap reset to zero
For i As Integer = 1 To System.Math.Max(gapnumber, 1)
If gapnumber = 0 Then Exit For
If gap(i) < 0 And PrMxHighSncGap(i) > Price.Low(sincegap(i) + 1 - 1) Then
sincegap(i) = 0 ' gap down closed
gap(i) = 0
gapclosed(i) = 1
Else If gap(i) > 0 And PrMnLowSncGap(i) < Price.High(sincegap(i) + 1 - 1) Then 
sincegap(i) = 0 ' gap up closed
gap(i) = 0
gapclosed(i) = 1
End If
Next
'Label = PrMnLowSncGap(1)
'Label = Price.High(sincegap(1))
'Label = gapclosed(1)=0
'Label = sincegap(1)=1
 
 
 
' Calculate Gap retracement
For i As Integer = 1 To System.Math.Max(gapnumber, 1)
If gapnumber = 0 Then Exit For
If sincegap(i) > 0 AndAlso gapclosed(i) = 0 Then
If gap(i) < 0 And PrMxHighSncGap(i) < Price.Low(sincegap(i) + 1 - 1)Then ' gap down logic
If sincegap(i) = 1 Then
Gapretrace(i) = (price.Last - price.low) / (price.Low(sincegap(i)) - price.high) 'Gap Day >> Use Open instead of high ??
Else
Gapretrace(i) = (Price.Last - Price.High(sinceGap(i) - 1)) / _ 
(Price.Low(sinceGap(i) + 1 - 1) - Price.High(sinceGap(i) - 1))  ' Note: *** Might want to use Price.high instead of price.last
End If
Else If gap(i) > 0 And PrMnLowSncGap(i) > Price.High(sincegap(i) + 1 - 1)Then ' gap up logic
If sincegap(i) = 1 Then
Gapretrace(i) = -(price.Last - price.high) / (price.High(sincegap(i)) - price.Low) 'Gap Day >> Use Open instead of Low?? 
Else
Gapretrace(i) = -(Price.Last - Price.Low(sinceGap(i) - 1)) / _   
(Price.High(sinceGap(i) + 1 - 1) - Price.Low(sinceGap(i) - 1))   ' Note: *** Might want to use Price.low instead of price.last
End If
Else
Gapretrace(i) = 0 ' Put Continuation logic here >> do I want Abs% from gap or relative% from gap (based on gap size)??? Want consistency for easy analysis.
End If
End If
Next
 
' Find most recent unclosed gap 
Outputgapnumber = 0
For i As Integer = 1 To System.Math.Max(gapnumber, 1)
If Gapnumber = 0 Then Exit For
If Gapclosed(Gapnumber + 1 - i) = 0 Then
Outputgapnumber = Gapnumber + 1 - i
Exit For
End If
Next
'Label = sincegap(1)=1
'Label = outputgapnumber = 1
 
' Output Plot logic during period Gap is still Open / plot gap size and percent of current gap closure
If outputgapnumber > 0 Then
If GapType = 2 And sincegap(outputgapnumber) > 0  Then 'Gap Down
If gap(outputgapnumber) < 0 And (gapretrace(outputgapnumber) > 0 Or sincegap(outputgapnumber) = 1) Then
If PrMxHighSncGap(outputgapnumber) = Price.High Then 
Sign = 1 ' Todays high is new high since gap
Else 
Sign = -1 'Todays high is not a new high since gap
End If
If outputtype = 1 Then ' 1st day retracement issue
If sincegap(outputgapnumber) = 1 Then 
'Plot = System.Math.Round(sign * -gap, 0) + sign * system.math.abs(gapretrace)
Plot = System.Math.Round(gap(outputgapnumber), 0) - System.Math.Abs(gapretrace(outputgapnumber))
Else 
Plot = System.Math.Round(Sign * -gap(outputgapnumber), 0) + Sign * Gapretrace(outputgapnumber)
End If
Else If outputtype = 2 Then 
Plot = (sincegap(outputgapnumber) + ((Price.Close - Price.Low) / (Price.High - Price.Low)) / 10) * System.Math.Sign(price.NetChange)
End If
End If
Else If GapType = 1 And sincegap(outputgapnumber) > 0  Then ' Gap Up
If gap(outputgapnumber) > 0 And (gapretrace(outputgapnumber) < 0 Or sincegap(outputgapnumber) = 1) Then
If PrMnLowSncGap(outputgapnumber) = Price.Low Then  ' Todays low is new low since gap
Sign = -1
Else 
Sign = 1  'Todays low is not a new low since gap
End If
If outputtype = 1 Then ' 1st day retracement issue
If sincegap(outputgapnumber) = 1 Then 
'Plot = System.Math.Round(sign * gap, 0) + sign * system.math.abs(gapretrace)
Plot = System.Math.Round(gap(outputgapnumber), 0) + System.Math.Abs(gapretrace(outputgapnumber)) 'Plot = 2.5
Else 
Plot = System.Math.Round(Sign * gap(outputgapnumber), 0) - Sign * Gapretrace(outputgapnumber)
End If
Else If outputtype = 2 Then 
Plot = (sincegap(outputgapnumber) + ((Price.Close - Price.Low) / (Price.High - Price.Low)) / 10) * System.Math.Sign(price.NetChange)
End If
End If
Else Plot = 0
End If
Else Plot = 0
End If
danielbender
Posted : Wednesday, November 6, 2013 2:55:29 PM
Registered User
Joined: 1/14/2006
Posts: 436

Bruce - in response to your previous inquiry, I do not think I am interested in negative index values. thanks - Dan

danielbender
Posted : Wednesday, November 6, 2013 3:06:30 PM
Registered User
Joined: 1/14/2006
Posts: 436

Bruce - the code I sent was a work in progress.  My ultimate intent regarding output (Plot =) is to provide relevant information referring to the one current open gap (there may be several open gaps) where the current price is closest to the gap closing price.  The "Retrace" variable was used to determine the status of the current gap closure as a percentage of the current open gap magnitude on day one (day of gap open).  Hope this helps... Dan

Bruce_L
Posted : Wednesday, November 6, 2013 7:11:39 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

If you are not interested in working with negative index values, then you should take jas0501's advice and remove the Price.High < Price.Low(1) and Price.Low > Price.High(1) sections from the RealCode.



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
danielbender
Posted : Thursday, November 7, 2013 12:17:09 PM
Registered User
Joined: 1/14/2006
Posts: 436

Bruce

Where in your real code is the variable "gap.count" incremented?

Thanks... Dan

Bruce_L
Posted : Thursday, November 7, 2013 12:36:08 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138

This value is automatically incremented when you use gap.Add() because it actually returns the number of items in the gap list.



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