Support&Resistance

Created at 05 Dec 2013, 23:12
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
ST

st0424

Joined 23.09.2013

Support&Resistance
05 Dec 2013, 23:12


Draw a line in the high and low prices, you want to write an indicator that displays the time and price there.


The indicators of MT4, It is written in the code below.

 

//+------------------------------------------------------------------+
//|                                           SwingPointViewEXTL.mq4 |
//|                           Copyright (c) 2012, Fai Software Corp. |
//|                                    http://d.hatena.ne.jp/fai_fx/ |
//|                                   Original is  super-signals.mq4 |
//|                Copyright ゥ 2006, Nick Bilak, beluck[AT]gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright (c) 2012, Fai Software Corp."
#property link      "http://d.hatena.ne.jp/fai_fx/"


 
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_width1 2
#property indicator_color2 YellowGreen 
#property indicator_width2 2


extern int barsLeftOfCurrent = 8;
extern int barsRightOfCurrent = 8;
/*extern*/ int SignalGap = 10;
extern bool ShowPrice = true;
extern bool ShowTime = true;
extern bool ShowSRLine = true;

extern color SupportColor = SlateBlue;
extern color ResistanceColor = DarkOrange;

extern int  MaxBars = 10000;

extern int TimeDifference = 7;
extern string FontName = "Verdana";
extern int FontSize = 7;
extern string s = "普段のチャートサイズ用に下3パラメータ調整してね";
extern int TextUpGap = 25;//High 側の空白
extern int TextDownGap = 5;//Low 側の空白
extern int TextBetweenGap = 8;//行間


int dist=24;
int MyDigits = 4;
double _Point = 0.0001;
double scale = 10;
double b1[];
double b2[];

int init()  {
 //   SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,1);
 //   SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,1);
  SetIndexStyle(0,DRAW_NONE);
  SetIndexStyle(1,DRAW_NONE);

   SetIndexArrow(1,233);
   SetIndexArrow(0,234);
   SetIndexBuffer(0,b1);
   SetIndexBuffer(1,b2);
   if(Digits == 3 || Digits == 2){//JPY
      MyDigits = 2;
      _Point = 0.01;
   }

   return(0); 
}

int deinit() {
   DeleteTimeObject();
   return(0); 
}

int start() {

   
   int counted_bars=IndicatorCounted();
   int k,i,j,limit,hhb,llb;
   
   if (counted_bars<0) return(-1);
   if (counted_bars>0) counted_bars--;
   limit=Bars-1;
   if(counted_bars>=1) limit=Bars-counted_bars-1;
   if (limit<0) limit=0;
   if(limit<WindowFirstVisibleBar())limit=WindowFirstVisibleBar()-1;
   if(limit > MaxBars ) limit = MaxBars;

   for (i=limit;i>=0;i--)   {
//      b1[i]=EMPTY_VALUE;
//      b2[i]=EMPTY_VALUE;
      b1[i]=0;
      b2[i]=0;
      delTag(Time[i],1);
      delTag(Time[i],-1);
      hhb = iHighest(0,0,MODE_HIGH,barsLeftOfCurrent + 1 + barsRightOfCurrent,i - barsRightOfCurrent);
      llb = iLowest(0,0,MODE_LOW,barsLeftOfCurrent + 1 + barsRightOfCurrent,i - barsRightOfCurrent);

      if (i==hhb){
         b1[i]=High[hhb]+SignalGap*Point;
         setTag(Time[i],High[i],ResistanceColor,1);
         }
      if (i==llb){
         b2[i]=Low[llb]-SignalGap*Point;
         setTag(Time[i],Low[i],SupportColor,-1);
         }
   }
   //
   int panes, height[10];
   static int lastheight = 500;//500pixel
   panes = GetPaneHeights(height);
   if(panes == 0 || height[0] < 20) height[0] = lastheight;
   lastheight = height[0];

   double newscale = ((WindowPriceMax(0)-WindowPriceMin(0))/height[0]);
   if(scale != newscale){
      DeleteTimeObject();
      scale = newscale;
      for (i=MaxBars;i>=0;i--)   {
         if (b1[i] != 0 && b1[i] != EMPTY_VALUE)
            setTag(Time[i],High[i],ResistanceColor,1);
         if (b2[i] != 0 && b2[i] != EMPTY_VALUE)
            setTag(Time[i],Low[i],SupportColor,-1);
      }      
   }
   //Comment(height[0]," Chart Scale is " ,scale);
   return(0);
}

void setTag(datetime setTime,double MyPrice,color MyColor,double direction){
      string MyObjectName = "SwView_"+TimeToStr(setTime,TIME_DATE|TIME_SECONDS)+"_"+DoubleToStr(direction,0);

      double position;
      if(direction > 0){
         position = MyPrice + TextUpGap*scale;
      }else{
         position = MyPrice - TextDownGap*scale;
      }
      string MyTime;
      if(Period() < PERIOD_D1){
         MyTime = TimeToStr(setTime+TimeDifference*3600,TIME_MINUTES);
      }else{
         MyTime = TimeMonth(setTime+TimeDifference*3600)+"/"+TimeDay(setTime+TimeDifference*3600);
      }
      
      if(ShowPrice){
         ObjectCreate(MyObjectName, OBJ_TEXT, 0, setTime,position);
         ObjectSetText(MyObjectName, DoubleToStr(MyPrice, MyDigits), FontSize, FontName, MyColor);
      }
      
      if(ShowTime){
         ObjectCreate(MyObjectName+"_", OBJ_TEXT, 0, setTime,position-TextBetweenGap*scale);
         ObjectSetText(MyObjectName+"_",MyTime, FontSize, FontName, MyColor);
      }
      
      if(ShowSRLine){
         setSRLine(setTime,MyPrice,MyColor,direction,MyObjectName);
      }
}
void delTag(datetime setTime,double direction){
   string MyObjectName = "SwView_"+TimeToStr(setTime,TIME_DATE|TIME_SECONDS)+"_"+DoubleToStr(direction,0);
   ObjectDelete(MyObjectName);
   ObjectDelete(MyObjectName+"_");
   ObjectDelete(MyObjectName+"_SR");
}
void DeleteTimeObject(){
   //メソッドからすべてのオブジェクトを取得して削除
   for(int i=ObjectsTotal();i>=0;i--){
      string ObjName=ObjectName(i);
      if(StringSubstr(ObjName,0,7) == "SwView_"){
         ObjectDelete(ObjName);
      }
   }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setSRLine(datetime setTime,double MyPrice,color MyColor,double direction,string MyObjectName){

datetime crossTime = GetCrossTime(setTime,MyPrice,direction);
   ObjectCreate(MyObjectName+"_SR", OBJ_TREND, 0, setTime,MyPrice,crossTime,MyPrice);
   ObjectSet(MyObjectName+"_SR",OBJPROP_COLOR,MyColor);
   ObjectSet(MyObjectName+"_SR",OBJPROP_RAY,false);
   ObjectSet(MyObjectName+"_SR",OBJPROP_WIDTH,2);
}

datetime GetCrossTime(datetime setTime,double MyPrice,double direction){
   int start = iBarShift(NULL,0,setTime);
   for(int i = start-1;i>=0;i--){
      if(High[i] > MyPrice && direction ==1 ) return(Time[i]);
      if(Low[i] < MyPrice && direction ==-1 ) return(Time[i]);
   }
   return(Time[0]);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////


/*
    GetPaneHeights: get heights in pixel of all panes in a window

    FOR WHAT:

        With the use of the built-in functions "WindowPriceMin/Max()"
        together, you can calculate appropriate spacing/margin to
        text/arrow objects (OBJ_TEXT/OBJ_ARROW), whatever size the
        window is.  See the EXAMPLE section below.

    SYNOPSIS:

        #import "GetPaneHeights.ex4"
            int GetPaneHeights(int &height[]);
        #import

        height[i] = the height in pixel of pane "i"
            i = 0: main chart
            i = 1: indicator #1
            i = 2: indicator #2
            ... (in top-to-bottom order)

        The size of "height[]" must be large enough to contain all panes.
        This function returns the number of panes.
        If it fails, the return value is zero.

    NOTE:

        This function scans the device context (DC) of a window,
        so if (the leftmost piece of) the window is covered by another
        window, then this function will fail.

    EXAMPLE:

        #import "GetPaneHeights.ex4"
            int GetPaneHeights(int &height[]);
        #import

        int         i, panes, height[10];
        double      spacing;
        string      sign;

        panes = GetPaneHeights(height);
        for (i = 0; i < panes; i++)
            Print("height[", i, "] = ", height[i], " pixels");

        // Analyzing the chart... Hmm... it's time to sell.

        if (panes == 0) {
            Alert("WARNING: cannot get the pane height!");
            height[0] = 500;
        }
        sign = "sell sign";
        spacing = 5 * (WindowPriceMax(0) - WindowPriceMin(0)) / height[0];
        ObjectCreate(sign, OBJ_ARROW, 0, Time[0], High[0] + spacing);
        ObjectSet(sign, OBJPROP_ARROWCODE, ...);
        ObjectSet(sign, OBJPROP_COLOR, ...);

        // The sell sign will be located 5 pixels above the highest price
        // of the latest bar, whatever size the window is.

    Version 1.00, Copyright (c) 2009, kartz


#property copyright     "Copyright (c) 2009, kartz"
#property library
*/

#import "user32.dll"
    int     GetDC(int hwnd);                            // HDC
    int     ReleaseDC(int hwnd, int hdc);
    int     GetClientRect(int hwnd, int &rect[4]);      // BOOL
#import "gdi32.dll"
    int     GetPixel(int hdc, int x, int y);            // COLORREF
#import

#define CLR_INVALID     0xFFFFFFFF      // Windows' constant
#define TEST_XY_COORD   10

int GetPaneHeights(int &height[])
{
   if(!IsDllsAllowed()) return(0);
    int     i, x, y, h, c, cc, frame, hwnd, hdc, rect[4];

    hwnd = WindowHandle(Symbol(), Period());
    if (hwnd == 0)
        return(0);
    if (!GetClientRect(hwnd, rect))
        return(0);
    h = rect[3];
    hdc = GetDC(hwnd);
    if (hdc == 0)
        return(0);

    i = 0;
    c = GetPixel(hdc, 0, TEST_XY_COORD);
    if (c != CLR_INVALID) {
        // scan horizontally.
        for (x = 1; x <= TEST_XY_COORD; x++) {
            frame = GetPixel(hdc, x, TEST_XY_COORD);
            if ((frame == CLR_INVALID) || (frame != c))
                break;
        }
        // "x" must be 2, but scanned just in case.
        if (frame != CLR_INVALID) {
            // scan vertically along the frame.
            for (cc = CLR_INVALID, y = 0; y < h; y++, cc = c) {
                c = GetPixel(hdc, x, y);
                if (c == CLR_INVALID) {
                    i = 0;
                    break;
                }
                if (c == frame) {
                    if (cc != frame)
                        height[i] = y;
                } else if (cc == frame) {
                    height[i] = y - height[i] - 1;
                    i++;
                }
            }
        }
    }
    ReleaseDC(hwnd, hdc);
    return(i);
}

If you try to display in MT4, it will be like this image.

 

I want to use cTrader this, but I do not know what to can write the code.

Any help would be appreciated, Thanks!

 


@st0424