Plotting Trade-Lines

A useful application is to plot straight lines between entry- and exit-signals, giving you the ability to view at a glance the location and magnitude of profits and losses of your trading system. The LineArray() function enables you to draw straight lines from one event or condition to another. In the chart below, which shows a reversal trading system, note how the lines begin and end at the exact trade prices, green being long and red being short. This gives you a quick impression of the profitability and location of individual trades:

Chart with Trade-Lines

Other applications would be plotting of custom ZigZag lines, price channels, trendlines, breakouts, etc.

There are two afl versions listed below, since I believe many of us use the first method, I decided to show them both for educational purposes.

The first one shows how you should NOT plot LineArrays. This method calls Plot() repeatedly and plots each LineArray segment as it is calculated. This is very resource consuming (executes slow) and may trigger a Warning 502 when you display a lot of data. Do not use this version.

The second version shows how Tomasz (Thanks TJ!) combined the individual LineArray segments inside the loop and then plots them with a single Plot() statement outside the loop. This code executes much faster and will never trigger Warning 502. The technique is simple but shows a clever way to combine array segments. Study it :-) it will come in handy one day!

// This version is only listed to show you how it should NOT be programmed
// Dummy system to generate some signals
Buy CrossMACD(), Signal() );
BuyPrice Open;     // Substitute your own prices
Sell CrossSignal(), MACD() );
SellPrice Close;     // Substitute your own prices
PlotShapesIIfBuyshapeSmallUpTriangleshapeNone ), colorBrightGreen0BuyPrice);
PlotShapesIIfSellshapeSmallDownTriangleshapeNone ), colorRed0SellPrice);
PlotC""1128 );
// Plot the Trade Lines
Sig Buy OR Sell;
y0 0;
y1 C[0];
TPrice C;
FirstVisibleBar Status"FirstVisibleBar" );
Lastvisiblebar Status"LastVisibleBar" );

for ( Firstvisiblebar<= Lastvisiblebar AND BarCountb++ )
{
    if ( Buy[b] )
    {
        Co colorRed;
        TPrice[b] = BuyPrice[b];
    }

    if ( Sell[b] )
    {
        Co colorBrightGreen;
        TPrice[b] = SellPrice[b];
    }

    if ( Sig[b] )
    {
        x0 y0;
        x1 y1;
        y0 b;
        y1 TPrice[b];
        PlotLineArrayx0x1y0y1 ), ""Co);
    }
}
// Improved version
// Dummy system to generate some signals
Buy CrossMACD(), Signal() );
BuyPrice O// Substitute your own prices
Sell CrossSignal(), MACD() );
SellPrice C// Substitute your own prices
PlotShapesIIfBuyshapeUpTriangleshapeNone ), colorBrightGreen0BuyPrice);
PlotShapesIIfSellshapeDownTriangleshapeNone ), colorRed0SellPrice);
PlotC""1128 );

// Plot the Trade Lines
Sig Buy OR Sell;
y0 0;
y1 C[0];
FirstVisibleBar Status"FirstVisibleBar" );
Lastvisiblebar Status"LastVisibleBar" );
CombinedColor colorWhite;
CombinedLine Null;

for ( Firstvisiblebar<= Lastvisiblebar AND BarCountb++ )
{

    if ( Buy[b] )
    {
        Co colorRed;
        TPrice[b] = BuyPrice[b];
    }
    else if ( Sell[b] )
    {
        Co colorBrightGreen;
        TPrice[b] = SellPrice[b];
    }

    if ( Sig[b] )

    {

        x0 y0;

        x1 y1;

        y0 b;

        y1 TPrice[b];

        La LineArrayx0x1y0y1 );

        CombinedLine IIfIsNullla ), CombinedLinela );
        CombinedColor IIfIsNullLa ), CombinedColorCo );
    }
}

PlotCombinedLine""CombinedColor );

Edited by Al Venosa

1 Star2 Stars3 Stars4 Stars5 Stars (14 votes, average: 4.50 out of 5)
Loading...Loading...

Comments are closed.