Announcement

Collapse
No announcement yet.

Drawing a rectangle

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Drawing a rectangle

    Hi,
    Is it possible to draw a rectangle on a chart?
    Les say I want to draw a resistence zone of 1.5 points.
    On the esm8 - from 1440 to 1441.5

    Regards,
    Baruch

  • #2
    yes, it's actually quite easy. You have to draw all four lines to make the rectangle.

    Mark each x/y point with bar/price data, then draw the lines.

    A simple trick I use for this is to build a bar counter (counts every bar on the chart, then STORE/MARK (in an array or variables), the bars that identify your support/resistance. This way, you can create a simple array of the support/resistance levels your system finds.

    From this array, you can draw from any point in the array to another or just back n bars. Here is some code for you to start with.

    PHP Code:
    var vMA null;
    var 
    vMACrossArray = new Array();

    var 
    nLastRawTime null;
    var 
    NewBar false;
    var 
    BarCounter 0;


    function 
    preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("MA SR Example");
        
    setCursorLabelName("MA"0);
        
    setDefaultBarFgColor(Color.blue0);
        
    setDefaultBarThickness(1,0);
            
        var 
    fp1 = new FunctionParameter("Length"FunctionParameter.NUMBER);
        
    fp1.setLowerLimit(1);        
        
    fp1.setDefault(20); //Edit this value to set a new default
        
        
    var fp2 = new FunctionParameter("Offset"FunctionParameter.NUMBER);
        
    fp2.setDefault(0); //Edit this value to set a new default
        
        
    var fp3 = new FunctionParameter("Source"FunctionParameter.STRING);
        
    fp3.setName("Source");
        
    fp3.addOption("Close");
        
    fp3.addOption("High");
        
    fp3.addOption("Low");
        
    fp3.addOption("Open");
        
    fp3.addOption("HL/2");
        
    fp3.addOption("HLC/3");
        
    fp3.addOption("OHLC/4");
        
    fp3.setDefault("Close"); //Edit this value to set a new default    
        
        
    var fp4 = new FunctionParameter("Type"FunctionParameter.STRING);
        
    fp4.setName("Type");
        
    fp4.addOption("MAStudy.SIMPLE");
        
    fp4.addOption("MAStudy.EXPONENTIAL");
        
    fp4.addOption("MAStudy.WEIGHTED");
        
    fp4.addOption("MAStudy.VOLUMEWEIGHTED");
        
    fp4.setDefault("MAStudy.SIMPLE"); //Edit this value    to set a new default

        
    var fp5 = new FunctionParameter("SupportRange"FunctionParameter.NUMBER);
        
    fp5.setLowerLimit(0);        
        
    fp5.setDefault(1.50); //Edit this value to set a new default
        
        
    var fp6 = new FunctionParameter("ResistanceRange"FunctionParameter.NUMBER);
        
    fp6.setLowerLimit(0);        
        
    fp6.setDefault(1.50); //Edit this value to set a new default
    }

    function 
    main(Length,Offset,Source,Type,SupportRange,ResistanceRange) {
       
        if (
    vMA == nullvMA = new MAStudy(LengthOffsetSource, eval(Type));
     
    /*****************************************
    Insert your code following this text block
    Use vMA.getValue(MAStudy.MA) for your code
    ******************************************/


       
    if ((getValue("rawtime"0) != nLastRawTime) ) {
         
    nLastRawTime getValue("rawtime"0);
         
    NewBar true;
         
    BarCounter += 1;
         
       }



    //  vMA Cross
       
    if ((close(-1) > vMA.getValue(MAStudy.MA,-1)) && (close(-2) < vMA.getValue(MAStudy.MA,-2) ) ) {
          var 
    TempObj = new Object;
            
    TempObj.Price vMA.getValue(MAStudy.MA,-1);
            
    TempObj.Bar BarCounter-1;
            
    TempObj.Dir 1;
         
    vMACrossArray.push(TempObj);
         
    fFindnDrawBoxonLastTrigger(SupportRange,ResistanceRange);
       }
       if ((
    close(-1) < vMA.getValue(MAStudy.MA,-1)) && (close(-2) > vMA.getValue(MAStudy.MA,-2) ) ) {
          var 
    TempObj = new Object;
            
    TempObj.Price vMA.getValue(MAStudy.MA,-1);
            
    TempObj.Bar BarCounter-1;
            
    TempObj.Dir = -1;
         
    vMACrossArray.push(TempObj);
         
    fFindnDrawBoxonLastTrigger(SupportRange,ResistanceRange);
       }


        
        return 
    vMA.getValue(MAStudy.MA);
    }

    function 
    fFindnDrawBoxonLastTrigger(vSupportRange,vResistanceRange) {
       if (
    vMACrossArray.length >= 2) {  //  We have to have at least 2 items in the array before we start looking to draw boxes
         
    if (vMACrossArray[(vMACrossArray.length-1)].Dir 0) {
           
    //  Last MA Cross is Bullish
           // - horizontal lines
             
    drawLineRelative( (vMACrossArray[(vMACrossArray.length-2)].Bar vMACrossArray[(vMACrossArray.length-1)].Bar) ,vMACrossArray[(vMACrossArray.length-1)].Price,-1,vMACrossArray[(vMACrossArray.length-1)].PricePS_SOLID2Color.green"H1"+BarCounter);
             
    drawLineRelative( (vMACrossArray[(vMACrossArray.length-2)].Bar vMACrossArray[(vMACrossArray.length-1)].Bar) ,(vMACrossArray[(vMACrossArray.length-1)].Price-vSupportRange),-1,(vMACrossArray[(vMACrossArray.length-1)].Price-vSupportRange), PS_SOLID2Color.green"H2"+BarCounter);
           
    // - Vert lines
             
    drawLineRelative( (vMACrossArray[(vMACrossArray.length-2)].Bar vMACrossArray[(vMACrossArray.length-1)].Bar) ,(vMACrossArray[(vMACrossArray.length-1)].Price),(vMACrossArray[(vMACrossArray.length-2)].Bar vMACrossArray[(vMACrossArray.length-1)].Bar),(vMACrossArray[(vMACrossArray.length-1)].Price-vSupportRange), PS_SOLID2Color.green"V1"+BarCounter);
             
    drawLineRelative( -,(vMACrossArray[(vMACrossArray.length-1)].Price),-1,(vMACrossArray[(vMACrossArray.length-1)].Price-vSupportRange), PS_SOLID2Color.green"V2"+BarCounter);
         
         }
         if (
    vMACrossArray[(vMACrossArray.length-1)].Dir 0) {
           
    //  Last MA Cross is Bullish
         
           //  Last MA Cross is Bullish
           // - horizontal lines
             
    drawLineRelative( (vMACrossArray[(vMACrossArray.length-2)].Bar vMACrossArray[(vMACrossArray.length-1)].Bar) ,vMACrossArray[(vMACrossArray.length-1)].Price,-1,vMACrossArray[(vMACrossArray.length-1)].PricePS_SOLID2Color.red"H1"+BarCounter);
             
    drawLineRelative( (vMACrossArray[(vMACrossArray.length-2)].Bar vMACrossArray[(vMACrossArray.length-1)].Bar) ,(vMACrossArray[(vMACrossArray.length-1)].Price+vResistanceRange),-1,(vMACrossArray[(vMACrossArray.length-1)].Price+vResistanceRange), PS_SOLID2Color.red"H2"+BarCounter);
           
    // - Vert lines
             
    drawLineRelative( (vMACrossArray[(vMACrossArray.length-2)].Bar vMACrossArray[(vMACrossArray.length-1)].Bar) ,(vMACrossArray[(vMACrossArray.length-1)].Price),(vMACrossArray[(vMACrossArray.length-2)].Bar vMACrossArray[(vMACrossArray.length-1)].Bar),(vMACrossArray[(vMACrossArray.length-1)].Price+vResistanceRange), PS_SOLID2Color.red"V1"+BarCounter);
             
    drawLineRelative( -,(vMACrossArray[(vMACrossArray.length-1)].Price),-1,(vMACrossArray[(vMACrossArray.length-1)].Price+vResistanceRange), PS_SOLID2Color.red"V2"+BarCounter);
         }
       
       }


    Attached Files
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      Oops. Forgot one thing. In order for this code to operate properly in REALTIME, you have to move the conditions that catch the MACross into the NEWBAR "if" statement. It should only test for this condition ONCE for every new bar.

      Other than that, this is a good example of how to draw boxes.
      Brad Matheny
      eSignal Solution Provider since 2000

      Comment


      • #4
        RE:rectangle

        Hi,
        Thanks for the example.
        I actualy wanted to draw a rectangle by coloring the area, not drawing lines.
        Is this posible?
        Second question: Why do I get an error on line 46 of attached efs and how to correct it?

        Regards,
        Baruch
        Attached Files

        Comment

        Working...
        X