How to draw lines between every trade

I want to draw lines between every trades, but I've got a continuous line connecting every buy and sell points with following code:

Buy = TimeNum() == 093000;
Selll = TimeNum() == 160000;
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow, IIf( Buy, colorGreen, colorBlue ) );

buyIn = ShortIn = False;
xs0 = ys0 = xs1 = ys1 = 0;	// short and cover coordinates
xb0 = yb0 = xb1 = yb1 = 0;	// buy and sell coordinates
CombinedLine = Null;
line = Null;
for(i=0; i<BarCount; i++)
{
	if (Short[i]) {
		xs0 = i;
		ys0 = shortPrice[i];
		shortIn = True;
	}
	if (shortIn && Cover[i]) {
		xs1 = i;
		ys1 = coverPrice[i];
		shortIn = False;
	}
	if (Buy[i]) {
		xb0 = i;
		yb0 = BuyPrice[i];
		buyIn = True;
	}
	if (buyIn && Sell[i]) {
		xb1 = i;
		yb1 = SellPrice[i];
		buyIn = False;
	}
	if (ys0 && ys1) {
		line = LineArray(xs0, ys0, xs1, ys1);		
		ys0 = 0;
		ys1 = 0;
	}
	if (yb0 && yb1) {
		line = LineArray(xb0, yb0, xb1, yb1);		
		yb0 = 0;
		yb1 = 0;
	}
	CombinedLine = IIf( IsNull( line ), CombinedLine, line );
	line = Null;
}
Plot( CombinedLine, "", colorYellow, styleDashed,Null,Null,0,1,5 );

What I expected to get is the lines between every pair of buy and sell, but what I got is one continuous line connecting every single buy and sell. Any ideas to fix it?
line

Besides, is there a way to get a larger up or down arrows? Since I didn't find a way to set the size of arrows.

Thanks a lot.

A humble request, post working code. Also, if you have borrowed code, do cite the source.

  1. Now the problem here is that you have chosen not to disclose the critical part, that is the Buy and Short themselves.

  2. There are system that are "Always IN Trade", which you buy-> sell and short from there.
    In that case, there is nothing wrong with the output of continuous lines.

Why am I saying that? Because these variables are used without their definitions known

if (Short[i]) { . . .
if (shortIn && Cover[i]) { . . .
  1. Another challenge is dealing with non-executable code, so if one wants to test, there is now way.
    If a couple of bits are missing, maybe one will take the pain, in your case its a lot of code missing.

  2. Let's assume your system is NOT always in the trade, then

if (ys0 && ys1)

May not be apt, with

IIf( IsNull( line )

Can the code guarantee that line variable is NULL in those segments?

Instead of using scalar variables ys0 && ys1, you could try shortIn but I guess you tried that and didn't work either., so

I'm truncating the post because I'm having too much guess work to do.
I'd rewrite those if() checks.

Use @Milosz code as example conditional-horizontal-lines

Milosz code is very apt and essentially similar.

This one is a mod of
http://www.amibroker.org/userkb/2007/04/20/plotting-trade-zigzag-lines/

There is nothing wrong with the original. Its very nice.
Its only the modified one than isn't articulated well.
But to use it in my system and make it working needs to assume a lot which didn't make sense at that time.

Edit
image

1 Like

Thanks all, finally I make it work.

can I get the code to the picture posted please.

@zenchanhk Your're welcome :slight_smile:
I think the code that calculates the Line Array was all fine, probably the Short and Buy signals themselves mixed up.

If you're Buy / Short code is correct, and your're not firing a Short before the Buy is exited (Sell = True), then
even one pair of check is ok.

_SECTION_BEGIN("Plot Trade Lines");

myf = Status("firstvisiblebar");
myl = Status("lastvisiblebar");

InTrade = False;
x0 = y0 = x1 = y1 = 0;
CombinedLine = Null;	line = Null;

for(i=myf; i<myl; i++)
{
	if (Short[i] || Buy[i]) {
		x0 = i;
		y0 = C[i];
		InTrade = True;
	}
	if (InTrade && ( Cover[i] || Sell[i] )) {
		x1 = i;
		y1 = C[i];
		InTrade = False;
	}	
	if ( y0 && y1 ) {
		line = LineArray(x0, y0, x1, y1);		
		y0 = 0;
		y1 = 0;
	}	
	CombinedLine = IIf( IsNull( line ), CombinedLine, line );
	line = Null;
}
Plot( CombinedLine, "", colorYellow, styleDashed, Null, Null, 0, 1, 1);

_SECTION_END();

image

3 Likes

image

Hi @travick , I used your above code and got the above error message. please advise further.

Right click, edit formula and paste whole code that you are using else won't know.

If you want to check yourself, assuming enough data is there,
Your other code should have Plot() for candles/bars and the Buy Sell Short Cover variables defined.

If they have valid signals, the trade line will be Plot.

1 Like