Plot horizontal line until crossed

This line starts plotting below the price. Is there a way to plot it until L<= line, then stop plotting? As indicated, it's an array.


Plot( targetarray, "", Colorgreen, stylethick, Null, Null, 0, 1, 1 );

The only thing I could think of was this, but not even close. Can I get a suggestion how to do this please?

t = IIf(L>=targetarray, targetarray,Null);
Plot(t,""....

Can you post a snapshot of what you see?

Is this a case of broken horizontal lines being plotted when price fluctuates above and below it ?

For this, the condition or description of the targetarray needs to be known

and don't answer in a hurry. Think over it and reply properly.

The yellow line of your picture is similar to creating a stop line. In order to make the yellow line to start at the green line's spot of your picture and making it to end at the cross of L you have to iterate (through BarCount).

1 Like

@C_M why do you hesitate to post your code ?

If one knows the origin of the green line, then you can intercept the point or condition of change, break its plotting and begin the continuous plot of yellow part until a 2nd condition occurs of L cross and halt the horizontal line.

otherwise it will continue only in words.

1 Like
intrade = Flip(short,buy); 
SetOption("EverybarNullCheck",True); 
stopline=IIf(intrade AND L>targetarray AND !short,targetarray,null);
Plot(stopline,"Stop Loss",colorblue,stylethick);

This is closer but I still can't get it to extend beyond the next short signal.

Latest effort.

intrade = Flip(short,buy); 
SetOption("EverybarNullCheck",True); 
stopline=IIf(intrade AND L>targetarray AND !short,targetarray,null);
Plot(stopline,"Stop Loss",colorblue,stylethick);
oldline= ValueWhen(Short,Ref(targetarray,-1));
Plot(oldline,"Stop Loss",coloryellow,stylethick);

This is clearly the wrong approach. fx, can you help with the interate through barcount please?

@C_M this article should be helpful,

http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/

2 Likes

@C_M, it would be very, very helpful if the code was complete - I've just copy/ pasted the code you've posted above, in an attempt to understand what it's doing in relation to your question, but it's missing the "Short" variable (and possible others), ie. AB reports "Error 29. Variable 'short' used without having been initialised."

How To Ask Questions The Smart Way has the following suggestions on how to provide a minimal working set of code for your question:

The most effective way to be precise about a code problem is to provide a minimal bug-demonstrating test case. What's a minimal test case? It's an illustration of the problem; just enough code to exhibit the undesirable behavior and no more. How do you make a minimal test case? If you know what line or section of code is producing the problematic behavior, make a copy of it and add just enough supporting code to produce a complete example (i.e. enough that the source is acceptable to the compiler/ interpreter/ whatever application processes it). If you can't narrow it down to a particular section, make a copy of the source and start removing chunks that don't affect the problematic behavior. The smaller your minimal test case is, the better

Generating a really small minimal test case will not always be possible, but trying to is good discipline. It may help you learn what you need to solve the problem on your own — and even when it doesn't, hackers like to see that you have tried. It will make them more cooperative.

If you simply want a code review, say as much up front, and be sure to mention what areas you think might particularly need review and why.

Thanks phase. Hopefully this helps.

I want the black line to continue to the right rather than step up/down at the next short signal. And I want it to continue until low <= targetarray.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {7/06/2017} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) \n{{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
SetBarFillColor( IIf( C > O, ParamColor( "Candle UP Color", colorGreen ), IIf( C <= O, ParamColor( "Candle Down Color", colorRed ), colorLightGrey ) ) );
Plot( C, "", IIf( C > O, ParamColor( "Wick UP Color", colorDarkGreen ), IIf( C <= O, ParamColor( "Wick Down Color", colorDarkRed ), colorLightGrey ) ), 64, 0, 0, 0, 0 );

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



Short  =  Cross(MA(C,30),C);
ShortPrice = C;

tlas =  HHV(H,10);
targetarray = Null;
stopvalue = 5*ATR(30);

for( i = 1; i < BarCount; i++ )
{
    if( Short[i] )
    {
        
        y4 = tlas[i] - stopvalue[i] ;
	        targetarray[i] = y4;
        

        for( j = i + 1; j < BarCount; j++ )
        {
            
            targetarray[j] = targetarray[i];
                   
        }
    }
}


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

Plot( targetarray, "", Colorblack, stylethick, Null, Null, 0, 1, 1 );
PlotShapes( IIf( short, shapeSMALLdownTRIANGLE, shapeNone ), colorlightyellow, 0, h, -35 );




Please , next time try to provide your code in your first post with illustrative screenshots to facilitate the other side ( any reader ) to quickly understand your request

What i had understood that you want to keep the "targetarray "
static once you have a true short signal with no further fluctuation until the price break down that line .
Until that line is broken down , you would ignore any excessive generated short signal , if what i wrote above is correct , the below modified code may be helpful to you

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {7/06/2017} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) \n{{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
SetBarFillColor( IIf( C > O, ParamColor( "Candle UP Color", colorGreen ), IIf( C <= O, ParamColor( "Candle Down Color", colorRed ), colorLightGrey ) ) );
Plot( C, "", IIf( C > O, ParamColor( "Wick UP Color", colorDarkGreen ), IIf( C <= O, ParamColor( "Wick Down Color", colorDarkRed ), colorLightGrey ) ), styleBar, 0, 0, 0, 0 );

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

SetBarsRequired(sbrAll,sbrAll);

Short  =  Cross(MA(C,30),C);
ShortPrice = C;

ji = 0;
tlas =  HHV(H,10);
targetarray = Null;
stopvalue = 5*ATR(30);

for( i = 1; i < BarCount; i++ )
{
    if( Short[i] AND i >= ji)
    {
        
        y4 = tlas[i] - stopvalue[i] ;
	    targetarray[i] = y4;
        

        for( j = i + 1; j < BarCount; j++ )
        {
            if (targetarray[i] < L[j]) {targetarray[j] = targetarray[i]; ji = j;}
            if (targetarray[i] > L[j]) {break;}
        }
    }
}


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

Plot( targetarray, "", colorWhite, styleNoRescale, Null, Null, 0, 1, 1 );
PlotShapes( IIf( short, shapeSMALLdownTRIANGLE, shapeNone ), colorlightyellow, 0, h, -35 );
1 Like

Hi Sebastian,

Targetarray stays static with each new short, yes. But I do want every short signal to generate a new line in the same manner. I'm hoping for this sort of appearance. Black arrows indicates short = true. Thanks for having a look.

x

@C_M, we’re getting closer to understanding what you want, but collectively, it’s taken 15 posts, at an average of 45min per post, to read/ understand, think about, develop code, and write a response – if you had hired someone professionally to sort this out for you, it would have cost you the equivalent of 2 days’ wages so-far, at $100/hr.

The first issue I can see, coding efficiency, is:

SetBarFillColor( IIf( C > O, ParamColor( "Candle UP Color", colorGreen ), IIf( C <= O, ParamColor( "Candle Down Color", colorRed ), colorLightGrey ) ) );
Plot( C, "", IIf( C > O, ParamColor( "Wick UP Color", colorDarkGreen ), IIf( C <= O, ParamColor( "Wick Down Color", colorDarkRed ), colorLightGrey ) ), styleBar, 0, 0, 0, 0 );

Whilst this works, it’s cluttered, takes time to untangle and understand what the code is doing, which means that in the longer-run, 6 months later, maintaining it’s going to be a problem. A cleaner way, IMO, is to:

// These variables are dedicated to holding _user selections_, and can now be referenced/ used anywhere in the code
selUpColor	= ParamColor( "UP Color", colorDarkGreen ) ;
selDnColor	= ParamColor( "Down Color", colorDarkRed ) ;
selNoChangeColor	= ParamColor( "No change Color", colorLightGrey) ;

// A dedicated variable to hold the color of the status of the Open:Close.
colorOfBar	= IIf( Close > Open, selUpColor, IIf( Close <= Open, selDnColor, selNoChangeColor)) ;

// Now bring it all together
SetBarFillColor(colorOfBar);
Plot(Close, "", colorOfBar, styleBar, 0, 0, 0, 0 );

At the bottom, I had to change the colors because nothing showed-up on my screen, and removed the Iif() from within the Plot() - you don’t need it, as you already have the status in the Short variable:

//Plot( targetarray, "", colorWhite, styleNoRescale, Null, Null, 0, 1, 1 );
//PlotShapes( IIf( short, shapeSMALLdownTRIANGLE, shapeNone ), colorlightyellow, 0, h, -35 );

// It’s always useful to provide a label for the variables that you plot, particularly during 
// the development phase, so that the value for a specific bar can be seen in the "Data" window
// enabling you to determine whether it’s appropriate or not
//
// Note: be careful with using "styleNoRescale" - it causes things to disappear from view,
// which means that you're likely to forget about them and how they affect the
// rest of the code - a better way would be to show everything until your absolutely sure that
// it's no longer necessary to do so.

Plot(targetarray, "targetarray", colorBlue, styleLine, Null, Null, 0, 1, 1 );
PlotShapes(Short * shapeSmallDownTriangle, colorGreen, 0, High, -35 );

The body of the code, within the for() loop, creates one long horizontal line (on some symbols) from the very first short signal, and it persists to the right-hand edge of the chart, 1500 bars later on one of my examples!, even though there are other signals to go Short after the first one.

From memory, you haven’t specified what you want the software to do regarding the following:

  • New Short signal – Do they close-out a prior trade?
  • Should there be multiple stop lines on the chart, one for each Short signal? This has the potential to display thousands of lines!
  • How long does the stop line persist for?
3 Likes

Thanks again phase, and to everyone on the thread who tried to help without me giving enough info.

  • New short signals should not close out a prior trade.
  • Yes, multiple lines, one for each short signal
  • Maybe 100 bars. I can see what you mean that they shouldn't plot forever.

You need to create multiple arrays then assigning each to corresponding variables , You would probably need to use some additional functions VarSet() & VarGet()

Here's an initial proposed solution

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {7/06/2017} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) \n{{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
SetBarFillColor( IIf( C > O, ParamColor( "Candle UP Color", colorGreen ), IIf( C <= O, ParamColor( "Candle Down Color", colorRed ), colorLightGrey ) ) );
Plot( C, "", IIf( C > O, ParamColor( "Wick UP Color", colorDarkGreen ), IIf( C <= O, ParamColor( "Wick Down Color", colorDarkRed ), colorLightGrey ) ), styleBar, 0, 0, 0, 0 );

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

//SetBarsRequired(sbrAll,sbrAll);

ji 			= 0;
tlas	    = HHV(H,10);
stopvalue   = ATR(30)*5;
TargetArray = tlas - stopvalue;

Short  		=  Cross(MA(C,30),C) AND TargetArray < L;
ShortPrice  = C;


for( i = 1; i < BarCount; i++ )
{
    if( Short[i] )
    {
        VarSet("targetarray"+i,Null);
		
        TargetArray    = VarGet("targetarray"+i);
        TargetArray[i] = tlas[i] - stopvalue[i];

        for( j = i; j < BarCount-1; j++ )
        {
            if (targetarray[i] < L[j+0]) {targetarray[j+0] = targetarray[i];}
            if (targetarray[i] > L[j+1]) {targetarray[j+1] = targetarray[i];		VarSet("Short_Signal_No_"+ji,Targetarray); 		ji = ji+1; 		break;}
        }
    }
    
    if ( i == BarCount-1 ) 
    {
		for ( z = 0; z <= ji; z++ )
		{
			Plot(VarGet("Short_Signal_No_"+z),"",z,styleNoRescale);
		}
	}
}


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

//Plot(  tlas - stopvalue, "", colorWhite, styleNoRescale, Null, Null, 0, 1, 1 );
PlotShapes( IIf( short, shapeSMALLdownTRIANGLE, shapeNone ), colorlightyellow, 0, h, -35 );

Note that the code would probably slow down the overall performance in case you applied it on large amount of quotes ( eg.more than 100K ) , so it may be necessary to make some amendments to reach a more elegant solution

4 Likes

That looks great, thanks Sebastian.

I notice that some short signals don't create a line and I'm not sure why. Also, how do you make the colours do that?

If your background is using dark colors and if the line corresponding
to some short signal is dark too , you will not be able to see that line and the same saying goes for the light colors , you can just change the color of line to a fixed color ( eg, colorwhite , colorblack and so on )

// The 3rd argument in this line is responsible for producing the colors
// you are seeing , you can add MTRandom() to make colors vary 
// each time the formula is executed
Plot(VarGet("Short_Signal_No_"+z),"",z,styleNoRescale);
1 Like

@C_M
You were right , some lines are not drawn because referral numbers are not assigned to their matching short signal , the reason behind this is that the price will not going to break them in the future , so the below modification is necessary to get the correct result

// This line should be 
if (targetarray[i] < L[j+0]) {targetarray[j+0] = targetarray[i];}

// Replaced by this one 
if (targetarray[i] < L[j+0]) {targetarray[j+0] = targetarray[i];	if ( j == BarCount-2 ) {VarSet("Short_Signal_No_"+ji,Targetarray); ji = ji+1;} }
2 Likes

Seb, I think this part is redundant. Maybe left over code from when you started ? :slight_smile:

        VarSet("targetarray"+i,Null);
	TargetArray    = VarGet("targetarray"+i);
// These two lines can just be
// Null assigned variable is immediately assigned back to TargetArray

        TargetArray    = Null;

Haven't yet gone through your code but will surely do.

Those would otherwise be a lot of variables :smiley:

1 Like

Excellent remark @travick , you are right i should abbreviate the first two lines of varset and varget to the one you have mentioned , thanks alot for that :hibiscus:

1 Like