How to get Y coordinate for getting price Cross alert

Hi,
I am using below AFL for buy and sell alert for price crossing study line. When I am using it with a single line it is working fine, but when I use it with rectangle box study it is not showing correct. Sometime it shows only the top line of Y axis cross or sometime it is showing only the lower line crossing.

I think it is due to the rectangle has values of startY and endY coordination. I tried to write with the help of Getting X, Y co-ordinates of Study() but I am unsuccessful.

I need the alert if:

  1. When the price touches the bottom line of rectangle (start Y)
  2. When the price is trading within the rectangle range (between start Y and end Y)
  3. When the price crossed the top line (end Y).

_SECTION_BEGIN("TL cross B/S");
SU = Study("SU", ChartID=1327 );
RE = Study("RE", ChartID=1327 );

//trendlineS = Study("SU", GetChartID() );
//trendlineR = Study("RE", GetChartID() );
//StartY = LastValue( ValueWhen( ExRem( trendlineS, 0 ), trendlineR ) );
//EndY = LastValue( ValueWhen( trendlineR, trendlineS ) );

Buy= Cross(SU, L);
Sell= Cross(H, RE);

Filter=Buy OR Sell;

AlertIf( Buy, "SOUND C:\\Windows\\Media\\Windows Ringin.wav", "Audio alert", 2 );
AlertIf( Sell, "SOUND C:\\Windows\\Media\\Windows Ringin.wav", "Audio alert", 2 );
//PlotShapes(shapeUpArrow*Buy,colorGreen,0, L,-10 );
//PlotShapes(shapeDownArrow*Sell,colorRed,0, H,-10 );
AddColumn( IIf(Buy,C,IIf(Sell,C,Null)) ,"S and D touch",1.0,colorWhite,IIf(Buy,colorGreen,IIf(Sell,colorRed,colorBlack)));

_SECTION_END();

@prakashganapathy, try this:

trendline = Study( "RE", getChartID() );    // this is a single Study named RE (a rectangle)

StartX = LastValue( ValueWhen( ExRem( trendline, 0 ), DateTime() ) );
EndX = LastValue( ValueWhen( trendline, DateTime() ) );
StartY = LastValue( ValueWhen( ExRem( trendline, 0 ), trendline ) );
EndY = LastValue( ValueWhen( trendline, trendline ) );

// Check the order of Ys to de sure that startY is lower then EndY
if( EndY < StartY )
{
    tempY = StartY;
    StartY = EndY;
    EndY = TempY;
}

Title = "{{NAME}} {{DATE}}\n" +
        "Start at X = " + WriteVal( StartX, formatDateTime ) + ", Y = " + StartY +
        "\nEnd at X = " + WriteVal( EndX, formatDateTime ) + ", Y = " + EndY;

// Check signals only in the selected date range
dt = DateTime();
rectDtRange = dt >= StartX AND dt <= EndX;

Buy = iif( rectDtRange, Cross( StartY, L ), 0 );
Sell = iif( rectDtRange, Cross( H, EndY ), 0 );
// these generate signals on the full chart, not only on the "rectangle" 
// Buy = Cross( StartY, L ); 
// Sell = Cross( H, EndY );

PlotShapes( shapeUpArrow*Buy, colorGreen, 0, L, -10 );
PlotShapes( shapeDownArrow*Sell, colorRed, 0, H, -10 );

Plot( C, "Close", colorDefault, styleCandle );

You need to add a single study (a rectangle): name it "RE".
The top of the code is essentialy the same as the KB article you referenced.
I simply added some extra lines to ensure that the rectangle Y coordinates are in the right order to act as support and resistance.
Moreover I also use StartX and EndX to get signals only when they happens in the "rectangle".

2 Likes

Hi beppe

Sorry for the late reply. Thank you for the code. Code is working good, but I need some changes and I am working on this. I will post after the changes whether it is working what I need.

1 Like