Simple Coding Help - No Promise.

trash

Well-Known Member
Your guess is wrong. You just need a list containing tickers and their belonging to Sector, Industry.

So you can create any format file depending on the format of your symbol list

$FORMAT Ticker,FullName,SectorName,IndustryName
$SKIPLINES 1
$SEPARATOR ,
$AUTOADD 1
$NOQUOTES 1
$OVERWRITE 1
$CLEANSECTORS 1
$SORTSECTORS 1

or

$FORMAT Ticker,FullName,SectorName,IndustryName,Watchlist, Group
$SKIPLINES 1
$SEPARATOR ,
$AUTOADD 1
$NOQUOTES 1
$OVERWRITE 1
$CLEANSECTORS 1
$SORTSECTORS 1

or

...

So a list could look like

Symbol,Name,Sector,Industry
ABCD,ABCDname,Financial,Credit Services
CDEF,CDEFname,Basic Materials,Oil & Gas Pipelines
and so on

according to first format file of this post

or

Symbol,Name,Sector,Industry,Watchlist,Group
ABCD,ABCDname,Financial,Credit Services,0,1
CDEF,CDEFname,Basic Materials,Oil & Gas Pipelines,2,1
and so on

according to second sample format of this post

Important line of each symbol list format is

$NOQUOTES 1

meaning that your csv or txt file has no quotation data like OHLC so no quotes has to be set to true -> 1.
 

trash

Well-Known Member
But I got 450 tickers in my Ami how do I sort them into industry specific and price-range?
Export your tickers to file then add sector and industry information yourself if you got no ready list for your country on the internet.

Here is how to export ticker with name to csv

Code:
// Usage:
// 1. open AFL in Analysis,
// 2. choose Apply to:
// 3. click Explore
// 4. either go to File - Export HMTL/CSV or copy&paste the exploration result list to Excel directly

Filter = Status( "lastbarinrange" );

SetOption( "NoDefaultColumns", True );

AddTextColumn( Name(), "Ticker", 1, colorLightGrey, colorDarkGrey );
AddTextColumn( FullName(), "Ticker Name", 1, colorLightGrey, colorDarkGrey, 300 );
After adding sector and industry info re-import the list via format example given above.
 
Hello there, i wonder if someone from here can help provide AFL lines for a cutloss code that i can incorporate on my BuySell AFL.

Below is the logic.

1. If the previous signal (yesterday) is Buy, and today's OPEN was lower than previous OPEN and the today's CLOSE is lower than today's open.... it must send a CutLoss alert email. It may look like this: Alertif(....."EMAIL", "Alert: cutloss, "....).

Thanks. :)
 

amitrandive

Well-Known Member
Hi All

Below exploration for ORB works only for 15 min,30 min or Hourly basis.

How can I make it work for 5 min or 10 min?:confused:

Code:
_SECTION_BEGIN("Intraday ORB Exploration");
 
SetChartOptions( 0, chartShowArrows | chartShowDates );
//("ORB");
 
BT = ParamTime ("Open Breakout Time", "09:15");
afterbreakout0 = Cross(TimeNum(),BT);
afterbreakout1 = TimeNum()>=BT;
NewDay = Day()!= Ref(Day(), -1);
highestoftheday = HighestSince(newday,H,1);
Lowestoftheday =LowestSince(newday,L,1);
ORBH = ValueWhen(afterbreakout0,highestoftheday,1);
ORBL = ValueWhen(afterbreakout0,lowestoftheday,1);
ORBM = (ORBH + ORBL)/2;
Plot(ORBH,"",colorGreen,styleDots);
Plot(ORBL,"",colorRed,styleDots);
Plot(ORBM, "", colorBlack, styleDots);
Plot ( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
ORBHG=H-ORBH;
ORBLG=ORBl-L;
Filter = ORBH OR ORBL;
AddColumn( ORBH, "C>ORBH", 1.2, colorDefault, IIf(Close>ORBH, colorGreen, IIf(Close>ORBM, colorSeaGreen, colorDefault)));
AddColumn( ORBL, "C<ORBL", 1.2, colorDefault, IIf(Close<ORBL, colorRed, IIf(Close<ORBM, colorOrange, colorDefault)));
AddColumn( ORBHG, "ORB High Breakout Gain", 1.2, colorDefault, IIf(ORBHG>0, colorGreen, colorDefault));
AddColumn( ORBLG, "ORB Low Breakout Gain", 1.2, colorDefault, IIf (ORBLG>0, colorRed, colorDefault));
 
I have no way to check the code. Check it and revert. If you are not getting Buy/Short signals on continuous candles then it should work otherwise it may not.

Code:
_SECTION_BEGIN("simple reversal");

// to plot price

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );


//buy and short conditions

Cond1 = Ref ( Low, -1 ) < Ref ( Low, -2 );
Cond2 = Ref ( Close, -1 ) > Ref ( Close, -2 );
Cond3 = Ref ( Close, -1 ) > Ref ( Open , -1);
Cond4 = Ref ( High , -1 ) < Ref ( High , -2 );
Cond5 = Ref (RSI (14), -2) < 30;
Cond6 = Ref (RSI (14) , -1 ) > 30 ;
Cond7 = Ref (Low, -1 ) < Ref (LLV (Low, 8) , -1 );
Cond8 = High > Ref ( High, -1 );


Cond9 = Ref ( High, -1) > Ref (High, -2 );
Cond10 = Ref ( Close, -1 ) < Ref ( Close, -2 );
Cond11 = Ref ( Close, -1 ) < Ref ( Open , -1);
Cond12 = Ref ( Low , -1 ) > Ref ( Low, -2);
Cond13 = Ref ( RSI (14) , -2 ) > 70 ;
Cond14 = Ref ( RSI (14) , -1 ) < 70 ;
Cond15 = Low < Ref (Low, -1) ;
Cond16 = Ref (High, -1 ) > Ref ( HHV (High ,8) , -1 );
Cond17 = TimeNum() < 143100 ;//signals after 2.30 r ignored
Cond18 = TimeNum() > 093100 ; // signals bfor 9.30 r ignored


Buy = Cond1 AND Cond2 AND Cond3 AND Cond4 AND Cond5 AND Cond6 AND Cond7 AND Cond8 AND Cond17 AND Cond18 ;
Short = Cond9 AND Cond10 AND Cond11 AND Cond12 AND Cond13 AND Cond14 AND Cond15 AND Cond16 AND Cond17 AND Cond18 ;

[COLOR="Blue"]Cap		= Param("Capital", 50000, 1000, 100000, 1000);
risk	= Param("Risk", 0.1, 0.1, 100, 0.1);

BuyPrice	= ValueWhen(Buy, Close); [COLOR="red"]This shd b prv bar high instead of close[/COLOR]
ShortPrice	= ValueWhen(Short, Close);[COLOR="red"]this shd be prv bar low instead of close[/COLOR]
SL		= ValueWhen(Buy OR Short, IIf(Buy, Ref(Low, -1), Ref(High, -1)));
LongPoints		= BuyPrice - SL;
ShortPoints	= SL - ShortPrice;

PointValue = 1;
Shares	= Cap*risk/100/SL;
SetPositionSize(shares, spsShares);

Sell	= Cross(Close, BuyPrice + LongPoints) OR Cross(SL, Close);[COLOR="red"]same way chngd cls to high/low resp as sell/cover shd b executed as soon as the price hits not on candle close[/COLOR]
Cover	= Cross(ShortPrice[U] +[/U] ShortPoints, Close) OR Cross(Close, SL);[COLOR="red"]this shd be minus[/COLOR]
[/COLOR]
R = Ref (High ,-1 ) - Ref ( Low , -1);

if(Status("action") == actionExplore);

Filter = Buy | Short;

SetOption ("NoDefaultColumns", True );
AddTextColumn(Name(), "SYMBOL");
AddColumn(DateTime(), "DATE", formatDateTime);
AddColumn(IIf(Buy, 66, 83), "TRIGGER", formatChar, colorWhite, IIf(Buy, colorGreen, colorRed));
AddColumn(IIf(Buy, Ref ( High , -1), Ref ( Low, -1 )), "TRIGGER PRICE", 1.2);
AddColumn(IIf(Buy, Ref(Low ,-1), Ref(High , -1)) , "Stop Loss", 1.2);
AddColumn(IIf(Buy, Ref (High , -1) + R , Ref (Low , -1) - R ), "Target", 1.2);
AddColumn(IIf(Buy, 420/R ,420/R ),"shares",1.2 );

//ApplyStop(stopTypeLoss, stopModePercent, 1, 1);
//ApplyStop(stopTypeProfit , stopModePercent, 1, 1);


//to get alert via speakers and in alert output window with the text put in aphostrophies

AlertIf( Buy, "SOUND C:\\WINDOWS\\Media\\Raga.wav", "REVBUY", 1 );
AlertIf( Short, "SOUND C:\\Windows\\Media\\Raga.wav", "REVSELL", 3 );


//to plot up arrow and down arrow in chart for buy and short signal


PlotShapes(Buy*shapeUpArrow,colorBrightGreen,0,L);
PlotShapes (Short*shapeDownArrow, colorRed, 0, H);
[COLOR="Blue"]PlotShapes(Cover*shapeHollowUpArrow,colorGreen,0,L);
PlotShapes (Sell*shapeHollowDownArrow, colorRed, 0, H);[/COLOR]
_SECTION_END();


Hi,

A simple thanks won't do fot the great help done. Yes, I don't get signals on continuous bars. so this is of great help.

Noted some changes to be done as per the system and hv written the same text in red color in your code and changed the code accordingly and hv given the final code here.

Now two problems. Sorry for bothering again and again.

1. It was showing excess cover/sell signals. So included the exrem function. Even after the exrem code, getting excess sell signal in case of SL hit cases. So I hv given the changed code here for your perusal and some chart examples for excess sell signals(not short)

http://i.imgur.com/TOxfPoH.png

2. While exploring, signals r correct but while backtesting, the window is blank. Don't know why? pls help.

The final code is below:

PHP:
_SECTION_BEGIN("simple reversal");

// to plot price

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 


//buy and short conditions

Cond1 = Ref ( Low, -1 ) < Ref ( Low, -2 );
Cond2 = Ref ( Close, -1 ) > Ref ( Close, -2 );
Cond3 = Ref ( Close, -1 ) > Ref ( Open , -1);
Cond4 = Ref ( High , -1 ) < Ref ( High , -2 );
Cond5 = Ref (RSI (14), -2) < 30;
Cond6 = Ref (RSI (14) , -1 ) > 30 ;
Cond7 = Ref (Low, -1 ) == Ref (LLV (Low, 8) , -1 );
Cond8 = High > Ref ( High, -1 );


Cond9 = Ref ( High, -1) > Ref (High, -2 );
Cond10 = Ref ( Close, -1 ) < Ref ( Close, -2 );
Cond11 = Ref ( Close, -1 ) < Ref ( Open , -1); 
Cond12 = Ref ( Low , -1 ) > Ref ( Low, -2);
Cond13 = Ref ( RSI (14) , -2 ) > 70 ;
Cond14 = Ref ( RSI (14) , -1 ) < 70 ;
Cond15 = Low < Ref (Low, -1) ;
Cond16 = Ref (High, -1 ) == Ref ( HHV (High ,8) , -1 );
Cond17 = TimeNum() < 143100  ;//signals after 2.30 r ignored
Cond18 = TimeNum() > 093100 ; // signals bfor 9.30 r ignored
Cond19 = Close > 100 ;// shares below 100 Rs r ignored

Buy = Cond1 AND Cond2 AND Cond3 AND Cond4 AND Cond5 AND Cond6 AND Cond7 AND Cond8 AND Cond17 AND Cond18 AND Cond19  ;
Short = Cond9 AND Cond10 AND Cond11 AND Cond12 AND Cond13 AND Cond14 AND Cond15 AND Cond16 AND Cond17 AND Cond18 AND Cond19 ; 

cap = Param("capital", 50000, 1000, 100000, 1000);
risk	= Param("Risk", 0.1, 0.1, 100, 0.1);

BuyPrice = ValueWhen(Buy, Ref (High ,-1));// with this getting correct buy price
ShortPrice = ValueWhen(Short, Ref (Low, -1));

SL		= ValueWhen(Buy OR Short, IIf(Buy, Ref(Low, -1), Ref(High, -1)));
LongPoints		= BuyPrice - SL;
ShortPoints	= SL - ShortPrice;

PointValue = 1;
Shares	= Cap*risk/100/SL;
SetPositionSize(shares, spsShares);

Sell	= Cross(High, BuyPrice + LongPoints) OR Cross(SL, Low);
Cover	= Cross(ShortPrice - ShortPoints, Low) OR Cross(High, SL);

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);
Short = ExRem(Short, Cover);
Cover = ExRem(Cover, Short);

R = Ref (High ,-1 ) - Ref ( Low , -1);

if(Status("action") == actionExplore);

Filter = Buy | Short;

SetOption ("NoDefaultColumns", True );
AddTextColumn(Name(), "SYMBOL");
AddColumn(DateTime(), "DATE", formatDateTime);
AddColumn(IIf(Buy, 66, 83), "TRIGGER", formatChar, colorWhite, IIf(Buy, colorGreen, colorRed));
AddColumn(IIf(Buy, Ref ( High , -1), Ref ( Low, -1 )), "TRIGGER PRICE", 1.2);
AddColumn(IIf(Buy, Ref(Low ,-1), Ref(High , -1)) , "Stop Loss", 1.2);
AddColumn(IIf(Buy, Ref (High , -1) + R , Ref (Low , -1) - R ), "Target", 1.2);
AddColumn(IIf(Buy, 420/R ,420/R ),"shares",1.2 );

//to get alert via speakers and in alert output window with the text put in aphostrophies,working fine

AlertIf( Buy, "SOUND C:\\WINDOWS\\Media\\Raga.wav", "REVBUY", 1 );
AlertIf( Short, "SOUND C:\\Windows\\Media\\Raga.wav", "REVSELL", 3 );

//to get alert in email, need to correct this, not getting mail alert

AlertIf( Buy, "EMAIL", "A sample alert on "+FullName(), 1 );
AlertIf( Short, "EMAIL", "A sample alert on "+FullName(), 3 );

//to plot up arrow and down arrow in chart for buy and short signal


PlotShapes(shapeUpArrow*Buy,colorBrightGreen,0,L);
PlotShapes (shapeDownArrow*Short, colorRed, 0, H);
PlotShapes(Cover*shapeHollowUpArrow,colorGreen,0,L);
PlotShapes (Sell*shapeHollowDownArrow, colorRed, 0, H);
_SECTION_END() ;
 
Last edited:
Hi All,

I would like to access (get handle of ) lines drawn on the chart in AFL code. Does anyone know how to write this code?
e.g. I have drawn support and resistance line and in my exploration code I want to check if price crossed the support or resistance line.. if yes then show it in exploration result.
Can someone help on how to write this piece of code?

Thanks in advance!!
 

pratapvb

Well-Known Member
Hi All,

I would like to access (get handle of ) lines drawn on the chart in AFL code. Does anyone know how to write this code?
e.g. I have drawn support and resistance line and in my exploration code I want to check if price crossed the support or resistance line.. if yes then show it in exploration result.
Can someone help on how to write this piece of code?

Thanks in advance!!
double click on the line and in params set the study id.....then use the Study function to access that line

for e.g.
sell = cross( study( "SU" ), close, GetChartID() );

for more detials please search study ID in help and check out "Using studies in AFL formulas" and the like
 

trash

Well-Known Member
double click on the line and in params set the study id.....then use the Study function to access that line

for e.g.
sell = cross( study( "SU" ), close, GetChartID() );

for more detials please search study ID in help and check out "Using studies in AFL formulas" and the like
Code line is not correct because there is a syntax error
This is correct sell = cross( study( "SU", GetChartID() ), close );

Secondly if he right-clicks a hand drawn line then it is not params but properties. If he wants to choose support then he has to set SU in Study ID drop down menu. But actually any 2 letter codes are possible but they have to be the same in StudyID code and in Properties window.

Thirdly since Analysis is the slave and chart is the master you have to set static variable in your chart code to receive the study in the analysis via staticvarget. In analysis code you can not use sell = cross( study( "SU", GetChartID() ), close ). It makes not sense.

So for example in your chart code you set

Code:
su = Study("SU", GetChartID() ); // support

nm         = Name(); 
uniqueID   = "12345";
uniqueName = nm + uniqueID; // static variable of support line depends on symbol and id
StaticVarSet(uniqueName + "SUMaster", su); // set static variable dependent on set unique name
then in analysis code you choose same symbol and unique ID as in chart.
Or you can choose a different symbol in analysis but set "AAPL" instead of Name() in analysis code since chart would show AAPL instead of INTC in analysis.

Code:
nm = Name(); // or "AAPL" string or ....
uniqueID = "12345"; // same one as in chart
uniqueName = nm + uniqueID;

su  = LastValue( Nz( StaticVarGet( uniqueName + "SUMaster" ) ) );

Close_ = Foreign( nm, "C" );
SUcond = Cross( su, Close_ ); // cross of price below suppport

Filter = Status( "LastBarInRange" );

AddTextcolumn( nm, "Chart Ticker", 1 );
AddTextcolumn( WriteIf( SUcond, "True", "False" ), "Support Line Cross DN", 1 );
Addcolumn( Close_, "Chart Price", 1.2 );
Addcolumn( su, "Support", 1.2 );
You should also take care of the timeframe in chart and analysis! For example if cross in chart happens in 1 minute time frame but in analysis periodicity is set to daily you will get problematic output there.
 
Last edited:

Similar threads