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

A different support and resistance indicator recode Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
kcaughron31
Posted : Tuesday, March 15, 2011 7:06:30 AM
Registered User
Joined: 3/4/2009
Posts: 160

Bruce, Can you convert this code to where it will work in SF?  Here is a screen shot link and the Metatrader code below:

http://www.earnforex.com/metatrader-indicators/Support-and-Resistance

The code:

//+------------------------------------------------------------------+
//|                                           Support and Resistance |
//|                                      Copyright © 2010, EarnForex |
//|                                        http://www.earnforex.com/ |
//|                          Based on MT4 indicator by Barry Stander |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, EarnForex"
#property link      "http://www.earnforex.com"
#property version   "1.01"
#property description "Blue and red support and resistance levels displayed directly on the chart."
#property description "Based on MT4 indicator by Barry Stander."

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_color1  Red
#property indicator_type1   DRAW_ARROW
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
#property indicator_color2  Blue
#property indicator_type2   DRAW_ARROW
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2

//---- buffers
double Resistance[];
double Support[];

void OnInit()
{
   IndicatorSetString(INDICATOR_SHORTNAME, "Support and Resistance");
   IndicatorSetInteger(INDICATOR_DIGITS, _Digits);

   PlotIndexSetInteger(0, PLOT_ARROW, 119);
   PlotIndexSetInteger(1, PLOT_ARROW, 119);

   SetIndexBuffer(0, Resistance);
   PlotIndexSetString(0, PLOT_LABEL, "Resistance");

   SetIndexBuffer(1, Support);
   PlotIndexSetString(1, PLOT_LABEL, "Support");

   ArraySetAsSeries(Resistance, true);
   ArraySetAsSeries(Support, true);
}

//+------------------------------------------------------------------+
//| Custom Support and Resistance                                    |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &High[],
                const double &Low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   ArraySetAsSeries(High, true);
   ArraySetAsSeries(Low, true);

   //Get the values of the Fractals indicator before entering the cycle
   double FractalUpperBuffer[];
   double FractalLowerBuffer[];
   int myFractal = iFractals(NULL, 0);
   CopyBuffer(myFractal, 0, 0, rates_total, FractalUpperBuffer);
   CopyBuffer(myFractal, 1, 0, rates_total, FractalLowerBuffer);
 ArraySetAsSeries(FractalUpperBuffer, true);
 ArraySetAsSeries(FractalLowerBuffer, true);
 
   for (int i = rates_total - 2; i >= 0; i--)
   {
  if (FractalUpperBuffer[i] != EMPTY_VALUE) Resistance[i] = High[i];
     else Resistance[i] = Resistance[i + 1];
 
   if (FractalLowerBuffer[i] != EMPTY_VALUE) Support[i] = Low[i];
      else Support[i] = Support[i + 1];
   }  

 return(rates_total);
}

//+------------------------------------------------------------------+

Bruce_L
Posted : Tuesday, March 15, 2011 9:19:30 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
I am not familiar with Metatrader code, but it seems to rely on an external "Fractal" Indicator of some sort. There are actually multiple Indicators by that name, but I'm going to take a guess and suggest the following RealCode Indicator could be used for Support:

RealCode for Real People: Indicators

'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:Support
'|******************************************************************
Static Support As Single
If isFirstBar Then
    Support = Single.NaN
End If
If Price.Low > Price.Low(1) AndAlso _
    Price.Low(1) > Price.Low(2) AndAlso _
    Price.Low(2) < Price.Low(3) AndAlso _
    Price.Low(3) < Price.Low(4) Then
    Support = Price.Low(2)
End If
Plot = Support

If so, the following RealCode Indicator would probably be Resistance:

'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:Resistance
'|******************************************************************
Static Resistance As Single
If isFirstBar Then
    Resistance = Single.NaN
End If
If Price.High < Price.High(1) AndAlso _
    Price.High(1) < Price.High(2) AndAlso _
    Price.High(2) > Price.High(3) AndAlso _
    Price.High(3) > Price.High(4) Then
    Resistance = Price.High(2)
End If
Plot = Resistance

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
kcaughron31
Posted : Tuesday, March 15, 2011 10:27:02 AM
Registered User
Joined: 3/4/2009
Posts: 160
Bruce the way that this plots out is there some way that it could be linked to use the Zig Zag tops andd bottoms and do the same.  I don't mean the ZZ pivot point chart but to use the ZZ tops and bottoms and have like the last 3 tops and last 3 bottoms to show up.  Wouldn't this look a whole lot like the links picture?

Just wondering when I got it ploted out and then looked a little closer to the links pic and that just sortof hit me as that might work.  I don't know anything on how the ZZ indicator is calculated or how to figure out the tops and bottoms, could you help please
kcaughron31
Posted : Tuesday, March 15, 2011 11:19:45 AM
Registered User
Joined: 3/4/2009
Posts: 160
Bruce I was looking at the ZZ top condition code and came up with this one.  Check this and see if I've come close to getting it right or if it might work.


'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:Support
'|******************************************************************
Static Support As Single
If isFirstBar Then
    Support = Single.NaN
End If
 If Price.low < Price.low(1) AndAlso _
 Price.low < price.low(-1) Then
 Support = Price.low
End If
Plot = Support

Thanks
Bruce_L
Posted : Tuesday, March 15, 2011 11:24:26 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
A Zig Zag Percent based Support Indicator might look something like the following:

'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:Support
'|******************************************************************
'# RevPerc = UserInput.Single = 5
Static Top As Single
Static Bot As Single
Static Rev As Single
Static Dir As Integer
Static Sup As Single
If isFirstBar Then
    Top = Price.Last
    Bot = Price.Low
    Dir = -1
    Sup = Single.NaN
End If
If Dir = 1 Then
    If Price.High > Top Then
        Top = Price.High
        Bot = Price.Last
    Else If Price.Low < Bot Then
        Bot = Price.Low
    End If
    Rev = Top * (1 - RevPerc / 100)
    If Price.Low <= Rev Then
        Dir = -1
        Top = Price.Last
    End If
Else If Dir = -1 Then
    If Price.Low < Bot Then
        Bot = Price.Low
        Top = Price.Last
    Else If Price.High > Top Then
        Top = Price.High
    End If
    Rev = Bot * (1 + RevPerc / 100)
    If Price.High >= Rev Then
        Dir = 1
        Sup = Bot
        Bot = Price.Last
    End If
End If
Plot = Sup

The Resistance Indicator would be almost identical except that it would change what was being Plotted when Tops are detected instead of when Bottoms are detected.

'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:Resistance
'|******************************************************************
'# RevPerc = UserInput.Single = 5
Static Top As Single
Static Bot As Single
Static Rev As Single
Static Dir As Integer
Static Res As Single
If isFirstBar Then
    Top = Price.High
    Bot = Price.Last
    Dir = 1
    Res = Single.NaN
End If
If Dir = 1 Then
    If Price.High > Top Then
        Top = Price.High
        Bot = Price.Last
    Else If Price.Low < Bot Then
        Bot = Price.Low
    End If
    Rev = Top * (1 - RevPerc / 100)
    If Price.Low <= Rev Then
        Dir = -1
        Res = Top
        Top = Price.Last
    End If
Else If Dir = -1 Then
    If Price.Low < Bot Then
        Bot = Price.Low
        Top = Price.Last
    Else If Price.High > Top Then
        Top = Price.High
    End If
    Rev = Bot * (1 + RevPerc / 100)
    If Price.High >= Rev Then
        Dir = 1
        Bot = Price.Last
    End If
End If
Plot = Res

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
kcaughron31
Posted : Tuesday, March 15, 2011 12:28:55 PM
Registered User
Joined: 3/4/2009
Posts: 160
Bruce is there a way to have the plots do something like the last pivot point chart codes that we went over last week to where like the last 3 highs and last 3 lows would just plot a horizontal line with the price at the end of the chart?  If there is a way to use the ZZtop and bottom conditions for pointers to start from and use the last three of each and plot horizontaly with price at the end this would be about the best that I could hope for.
Bruce_L
Posted : Tuesday, March 15, 2011 12:46:52 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
I'm not a programmer and I'm not going to try and program Zig Zag Charts "backwards" from the right side of the Charts instead of starting at the beginning of the data.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
kcaughron31
Posted : Tuesday, March 15, 2011 1:02:51 PM
Registered User
Joined: 3/4/2009
Posts: 160

Well could someone tell me if we could use ZZTop, ZZTop(1), and ZZTop(2) and the same for the bottom and then just plot out the horizontal line with the price on that line?  Would SF RC be able to understand the defining points that we would want in this or is that something that Kuff would have to look at to see if it is even possible in the API.  Also is there a downloadable copy of the RC API somewhere so as to be able to print out the api into a booklet for referance?

Bruce_L
Posted : Tuesday, March 15, 2011 1:34:09 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
If you replace everything below the Inherits line in the Class tab of the RealCode Editor of a RealCode Indicator with the following:

    Sub New
        AutoLoop = False
        '# ZZP = chart.ZigZagPercent
        '# SupAgo = UserInput.Integer = 1
    End Sub
    Public Overrides Function Plot() As System.Single
        If ZZP.Line.Count >= 3 Then
            Dim Count As Integer = 0
            Dim Support As Single = Single.NaN
            Dim i As Integer
            For i = ZZP.Line.Count - 2 To 1 Step -1
                If ZZP.Line.Value(i) < ZZP.Line.Value(i - 1) Then
                    Count += 1
                    If Count = SupAgo Then
                        Support = ZZP.Line.Value(i)
                        Exit For
                    End If
                End If
            Next
            If Not Single.IsNaN(Support) Then
                AddToOutput(ZZP.Line.DateValue(i), Support)
                AddToOutput(ZZP.Line.DateValue(ZZP.Line.Count - 1), Support)
            End If
        End If
    End Function
End Class

You should get an Indicator which Plots a line from the most recent Support to the present. Changing the SupAgo setting would adjust how many Zig Zag bottoms ago to start Plotting Support. The following line is created by Dragging and Dropping the Zig Zag Indicator into the RealCode Editor:

        '# ZZP = chart.ZigZagPercent

A very similar Indicator could be created for Resistance:

    Sub New
        AutoLoop = False
        '# ZZP = chart.ZigZagPercent
        '# ResAgo = UserInput.Integer = 1
    End Sub
    Public Overrides Function Plot() As System.Single
        If ZZP.Line.Count >= 3 Then
            Dim Count As Integer = 0
            Dim Resistance As Single = Single.NaN
            Dim i As Integer
            For i = ZZP.Line.Count - 2 To 1 Step -1
                If ZZP.Line.Value(i) > ZZP.Line.Value(i - 1) Then
                    Count += 1
                    If Count = ResAgo Then
                        Resistance = ZZP.Line.Value(i)
                        Exit For
                    End If
                End If
            Next
            If Not Single.IsNaN(Resistance) Then
                AddToOutput(ZZP.Line.DateValue(i), Resistance)
                AddToOutput(ZZP.Line.DateValue(ZZP.Line.Count - 1), Resistance)
            End If
        End If
    End Function
End Class

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
kcaughron31
Posted : Tuesday, March 15, 2011 2:52:24 PM
Registered User
Joined: 3/4/2009
Posts: 160

Bruce I get these errors when adding the code:
ZZP not declaired. Lines 14, 15,18, 24, 25

Here is the code copied after I pasted what you gave me.


1.<WBIGuid("39876aeb-f1c5-4bd4-8ab6-8c6abe1e49c4"),FriendlyName("RealCodeIndicator"),BlockTemplateEmptyMethodOnly()>

_
2.Public partial Class RealCodeIndicator
3. Inherits RealCodeIndicator_base
4. Sub New
5.  AutoLoop = False
6.  '# ZZP = chart.ZigZagPercent
7.  '# SupAgo = UserInput.Integer = 1
8. End Sub
9. Public Overrides Function Plot() As System.Single
10.  If ZZP.Line.Count >= 3 Then
11.   Dim Count As Integer = 0
12.   Dim Support As Single = Single.NaN
13.   Dim i As Integer
14.   For i = ZZP.Line.Count - 2 To 1 Step -1
15.    If ZZP.Line.Value(i) < ZZP.Line.Value(i - 1) Then
16.     Count += 1
17.     If Count = SupAgo Then
18.      Support = ZZP.Line.Value(i)
19.      Exit For
20.     End If
21.    End If
22.   Next
23.   If Not Single.IsNaN(Support) Then
24.    AddToOutput(ZZP.Line.DateValue(i), Support)
25.    AddToOutput(ZZP.Line.DateValue(ZZP.Line.Count - 1), Support)
26.   End If
27.  End If
28. End Function
29.End Class

Any idea what I did wrong?

Bruce_L
Posted : Tuesday, March 15, 2011 2:56:55 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
I'm guessing you missed the part about Dragging and Dropping the Zig Zag Percent Indicator into the RealCode Editor to create the following line:

        '# ZZP = chart.ZigZagPercent

You have to have the ZZP Indicator on the Chart to create the Indicator (and you need to Drag and Drop it into the RealCode Editor yourself to replace the line if it does not have exactly the same name as the one I used). Once the Indicators are created and saved, you can delete the ZZP Indicator from the Chart.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
kcaughron31
Posted : Tuesday, March 15, 2011 2:59:19 PM
Registered User
Joined: 3/4/2009
Posts: 160
Bruce I think I see what might be wrong:

 Sub New
        AutoLoop = False
        '# ZZP = chart.ZigZagPercent
        '# SupAgo = UserInput.Integer = 1
    End Sub
    Public Overrides Function Plot() As System.Single
        If ZZP.Line.Count >= 3 Then

I think that the code shoule look like this at the start

 Sub New
        AutoLoop = False
 End Sub
'# ZZP = chart.ZigZagPercent
        '# SupAgo = UserInput.Integer = 1
 Public Overrides Function Plot() As System.Single
        If ZZP.Line.Count >= 3 Then
(then the rest of the code)

Does this look right to you?

Bruce_L
Posted : Tuesday, March 15, 2011 3:01:53 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
The original is correct. There is no need to change it.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
kcaughron31
Posted : Tuesday, March 15, 2011 3:09:58 PM
Registered User
Joined: 3/4/2009
Posts: 160
I found the error and it's working great, thanks
kcaughron31
Posted : Tuesday, March 15, 2011 3:26:31 PM
Registered User
Joined: 3/4/2009
Posts: 160
Bruce what code line do I need to add to have the price at the end of the line like the pivot point chart?
Could I just add the line that plots out the 0.00?
Bruce_L
Posted : Tuesday, March 15, 2011 3:38:42 PM


Worden Trainer

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

AddToOutput(ZZP.Line.DateValue(ZZP.Line.Count - 1), Resistance)

To:

AddToOutput(ZZP.Line.DateValue(ZZP.Line.Count - 1), Resistance, Resistance.ToString("0.00"))

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