Automated charting and trade-framing in Amibroker

I am a bit new to amibroker coding…

if i have a file which has following 4 columns:

Ticker Entry price Stop price Profit price

I would like code a batch job, which when executed, draws the lines in the corresponding ticker chart as per the above 3 price levels… ( red horizontal line for stop price, green for entry, and purple for profit exit ) - these tickers can be in a separate watch list or somewhere…

If it is possible to do this in Amibroker- if so, how to do this?

Thank you,
Satish

You wouldn’t necessarily need a batch job to do it.

One option is to get the chart code to loop through the file to find the ticker it needs and then plot the values. Each time you move to a new ticker it will loop through the file again to get the values it needs. However, that’s not very efficient, and if the file is big, there will be lag.

The alternative, therefore, is to loop through the file once the first time and store all the values it needs in static variables. Then you can check each time if the static variable has a value, and if it does, no need to access the file again.

If you use the ticker as part of the static variable names, there will be one for each ticker.

Some combination of the following will get you there:
https://www.amibroker.com/guide/afl/fgets.html
https://www.amibroker.com/guide/afl/plot.html
https://www.amibroker.com/guide/afl/staticvarset.html
https://www.amibroker.com/guide/afl/staticvarget.html
https://www.amibroker.com/guide/afl/isnull.html

Thank you…i am assuming i can get the chart code from plot.html file you referred.

Also, the file wouldnt be too big - it is an output of exploration. I am thinking i can output exploration output to a csv file and use that as input for the program.

Once this works - i will have to figure out how to combine both exploration and trade-framing charts together…

Thank You,
Satish

In Amibroker the chart code and Explore code can all be in the same AFL file. You can therefore allow them to both reference the same strategy logic (eg Buy/Sell signals).

To avoid conflicts or to selectively run them depending on the environment, you can use:

if (Status("ActionEx") == actionExplore)
{
	// Your explore code here
}

if (Status("ActionEx") == actionIndicator)
{
	// your chart code here
}

https://www.amibroker.com/guide/afl/status.html

Tried your suggestions - used hard code values to first have core logic (plot) work - i am stuck at few blockers - pls help…

how to make the width of the line fatter? styleThick isnt good enough; width =5 or will even higher number make a difference?

how to make the line extend beyond current bar - xshift=10 isnt extending the line

how do i draw a vertical line for a particular bar (say last bar or yesterday’s bar)

But the big question is how do i link this code to a ticker?
Right now i am executing this code after going to a specific ticker chart and overlaying it - instead, i want all my specific tickers
in a newly created watchlist say-WD0715 and have this code executed on those ticker charts?

Here is my code:

function drawLine(txt,coloor,level)
{
x0=1;
x1=5;
y0=y1=level;
Line = LineArray( x0, y0, x1, y1, 3 );
Plot(Line, txt, coloor, style = styleLine|styleThick, xshift=10, width=5 );
}

function drawVLine()
{
x0=x1=BarCount;
y0=150;
y1=200;
Line = LineArray( x0, y0, x1, y1, 3 );
Plot(Line, “Date”, colorBlue, style = styleLine|styleThick, width=5 );
}

EntryPrice =210.20;
InitialStop=208;
TargetPrice=214.44;

drawLine(“Entry”,colorCustom9,EntryPrice);
drawLine(“I-Stop”,colorRed,InitialStop);
drawLine(“PE”,colorCustom12,TargetPrice);

drawVLine();

drawVLine isnt working as expected.

The code is incorrect. You should NOT use Plot() for single lines. Plot is for CHARTS, not for straight lines. To draw a straight line from point A to B you should use GfxLineTo() function

See also GfxSetCoordsMode() function that allows you to change the coordinate system from pixels to bars/prices.

Thank you - that worked- My other questions still need response:

how do i draw a vertical line for a particular bar (say last bar or yesterday’s bar)
(drawVLine function code isnt working)

how do i delete a specific indicator before drawing this? If this isnt possible, how do i delete the existing lines and then draw these?

But the big question is how do i link this code to a ticker?
Right now i am executing this code after going to a specific ticker chart and overlaying it - instead, i want all my specific tickers
in a newly created watchlist say-WD0715 and have this code executed on those ticker charts?

Here is my code:

function drawLine(coloor,level)
{
GfxSelectSolidBrush( coloor );
GfxSelectPen( coloor );
GfxMoveTo( 0, level );
GfxLineTo( BarCount, level );
strr = NumToStr(level,format=3.2);
GfxSetTextColor( coloor );
GfxTextOut( strr, BarCount, level );
}

function drawVLine()
{
GfxSelectSolidBrush( colorBlue );
GfxSelectPen( colorBlue );
GfxMoveTo( BarCount, 0 );
GfxLineTo( BarCount, 300 );
}

GfxSetOverlayMode( 0 );
GfxSetCoordsMode( 1 );

EntryPrice =210.20;
InitialStop=208;
TargetPrice=214.44;

drawLine(colorCustom9,EntryPrice);
drawLine(colorRed,InitialStop);
drawLine(colorCustom12,TargetPrice);

drawVLine();

Moderator comment: You must use [code] tags for code !!!

All the answers are already provided. You need to carefully read the user’s guide references pointed to you in my previous reply. READ and UNDERSTAND, not just quickly scan. READING is a thought process when you ABSORB the knowledge. Each sentence in my reply is important. Each link is important.
Nowadays people SCAN texts without doing “understanding” part.

1 Like

I 100% agree and i did spend almost 2 hours understanding and coding based on your inputs and got the output i desired (attached ) - what i am trying to do is automate that - so i can run it every day reading the data and ticker symbol from a file, without opening the chart and overlaying it - if you can guide me in that direction, that would be helpful.
So I think my above 3 questions are still valid and i did read your each sentence and applied it:

"The code is incorrect. (I fixed it) You should NOT use Plot() for single lines. Plot is for CHARTS, not for straight lines. (I changed the code to use low-level GFxfunctions) To draw a straight line from point A to B you should use GfxLineTo()3 function

See also GfxSetCoordsMode()1 function that allows you to change the coordinate system from pixels to bars/prices.
(I used this as well)

Copying the code again:

function drawLine(coloor,level)
{
	GfxSelectSolidBrush( coloor ); 
	GfxSelectPen( coloor, width=5 ); 
	GfxMoveTo( 0, level ); 
    GfxLineTo( BarCount, level ); 
    strr = NumToStr(level,format=3.2);
    GfxSelectFont("Tahoma",12, 700 );
    GfxSetTextColor( coloor );
    GfxTextOut( strr, BarCount, level );
}

function drawVLine()
{
	GfxSelectSolidBrush( colorBlue ); 
	GfxSelectPen( colorBlue ); 
	GfxMoveTo( 0, 0 ); 
    GfxLineTo( BarCount, 300 ); 
}

GfxSetOverlayMode( 0 ); 
GfxSetCoordsMode( 1 ); 

EntryPrice =210.20;
InitialStop=208;
TargetPrice=214.44;

drawLine(colorCustom9,EntryPrice);
drawLine(colorRed,InitialStop);
drawLine(colorCustom12,TargetPrice);

drawVLine();

Just to be clear the 3 questions were:

  1. I am able to draw horizontal lines, but not a vertical line on yesterday’s bar- i used the drawVLine function
GfxMoveTo( BarCount, 0 ); 
GfxLineTo( BarCount, 300 ); 
  1. how do i delete a specific indicator before drawing this? If this isn’t possible, how do i delete the existing lines that were drawn using this code in the past and then draw these lines new?

  2. How do i automate executing this code for several tickers by reading the price levels data and ticker symbol data from a file? (without going thru each chart and overlaying this as a custom indicator manually)

Any help? Any comments?

Satish,

Here is code I use to highlight the bar X bars back. This is extracted so now it creates its’ own chart, but is easily modified to work in your own chart.

/* BarsBack
Code to Highlight a bar X bars back
*/

barsback = Param("Bars Back", 10, 1, 100, 1, 0);

Chartstyle = styleHistogram + styleOwnScale + styleNoLabel;
bi = BarIndex(); 

barcolor = IIf( bi == (LastValue( bi) - barsback), coloryellow, colorblack); 

Plot( 100, "", barcolor, styleHistogram + styleOwnScale, 0, 100);

The above code automatically “colors” the non selected bars, so they essentially disappear without the need to delete anyting.

I am not sure that I understand your second point. If you apply your code to a chart, then just cycle through your symbols.

Hope this Helps.
Snoopy