Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Multiple Time Frame for Spreads by Anil Lalwani for Amibroker (AFL)

Copy & Paste Friendly
/*
MultiTimeFrame for Price and Spread: 
////////////////////////////////
HOW TO USE THIS afl: 

	This is an afl for "function" (for multiTimeframe Price, multiTimeframe Spread, Exploration.) You need to call this function in another afl as follows (here #include is used to select path )

	1)For Price: (Let this afl be named as MyPrice. Double click MyPrice to display price in upper pane. Let the time frame be 1min. Double click MyPrice agian to display price in lower pane. Right click lower pane and in parameter dialogue box, access multiple time frames to say Daily.)
		#include "C:\Program Files\AmiBroker5.6\Formulas\Custom\Anil\1_PriceSpread\a_PriceSpreadExploreBackTestFunction.afl"
		MySpread(1,"Default","Default",0,0);

	2)For Spread: (Use this for spreads if you are selecting symbol2 from parameters box)
		#include "C:\Program Files\AmiBroker5.6\Formulas\Custom\Anil\1_PriceSpread\a_PriceSpreadExploreBackTestFunction.afl"
		MySpread(1,"Default","Default",0,0);

	3)For Spread: (Use this for spreads if you are not selecting symbol2 from parameters box)
		#include "C:\Program Files\AmiBroker5.6\Formulas\Custom\Anil\1_PriceSpread\a_PriceSpreadExploreBackTestFunction.afl"
		MySPREAD(0,"NIFTY-I","BANKNIFTY-I",1,0.27);

	4)For Ready-made Spreads: (ready-made functions are defined at the bottom of this afl)
		#include "C:\Program Files\AmiBroker5.6\Formulas\Custom\Anil\1_PriceSpread\a_PriceSpreadExploreBackTestFunction.afl"
		Smetal1();

	5)Exploration: Explore Symbols with abs %change of more than 3% on daily basis. You include path of this afl for in exploration.


////////////////////////////////
SPREAD CONCEPTS:

	Spread = Scale1*Price1-Scale2*Price2, 
	CurrencyValue =Leg1*Lot1*Price1-Leg2*Lot2*Price2

	ScaleMultiplier=ATR1/ATR2
	LotMultiplier=LotSize1/LotSize2
	LegMultiplier=SclaeRatio*LotMultiplierRatio

	For Simplification we make ratios as 1:x.
	ATR ratio = (1/ATR1):(1/ATR2) = 1:(ATR1/ATR2)
	LegRatio = (LotSize2*ATR2):(LotSize1*ATR1) = 1:(LotSize1/Scale1)/(LotSize2/Scale2)
	PointValueOfSpread(ie SpreadCurrencyValueChange/SpreadScaleValueSpread) = (Leg*LotSize)/Scale.

Explanation for LegMultiplier:
	Symbol1 and Symbol2 have different ATR and LotSize. To make a spread between them, we need to normalize both scale and currency value "simultaneously" of both Legs. For eg: 
	Symbol1=NIFTY  			ATR1=100, LotSize1=75;
	Symbol2=BankNIFTY  	ATR2=267, LotSize2=20;

	So ScaleRatio = 1:0.267 ie (Since ATRRatio =  ATR1:ATR2, ScaleRatio = 1/ATR1:1/ATR2)
	To make CurrencyValue of both legs equal ie 75, we need to multiply Lot2 by 3.75 ie (LotSize1/LotSize2). So LotMultiplierRatio (ie LegRatio before normalisation) = 1:3.75
	Now since ScaleRatio=1:0.267 and LotMultiplierRatio=1:3.75, LegRatio=1:1 (ie LegRatio after normalisation = ScaleRatio*LotMultiplierRatio = 0.267*3.75 )

	LegRatio is basically LotMultiplierRatio after taking into consideration the ScaleRatio. If we dont normalise scale and lotsize simultaneously, we dont get uniform PL per Spread point. eg. 

	Spread=Nifty-0.267*BankNifty; (Scale Ratio = 1:0.267)

Case1: LotMultiplier=1:3.75

	Scenario1:
	Nifty1=12000(CurrencyValueNifty1=1*75*12000=9L) 				BankNifty1 = 32000(CurrencyValueBankNifty1=3.75*20*32000=24L);			SpreadScaleValue1(Nifty1-0.267*BankNifty2)=3456(ie 12000-0.267*32000),		SpreadCurrencyValue1(CurerncyValueNifty1- CurrencyValueBanknifty1)= -15L (ie 9L-24L)							SpreadPointValue1(SpreadCurrencyValue1/SpreadScaleValue1)=-434(ie -15L/3456)
	Nifty2=12001(CurrencyValueNifty2=1*75*12001=9.00075L)		BankNifty2 = 32000(CurrencyValueBankNifty2=3.75*20*32000=24L); 			SpreadScaleValue2(Nifty2-0.267*BankNifty2)=3457(ie 12001-0.267*32000), 		SpreadCurrencyValue2(CurerncyValueNifty1- CurrencyValueBanknifty1)= -14.99925L (ie 9.00075L-24L)		SpreadPointValue2(SpreadCurrencyValue1/SpreadScaleValue1)=-433.8(ie -14.99925L/3457)
	SpreadScaleValueChange		=	SpreadScaleValue2		-	SpreadScaleValue1		=	3457-3456 = 1
	SpreadCurrencyValueChange=	SpreadCurrencyValue2	-	SpreadCurrencyValue1	=	-14.99925L-15L = 75
	PL/Point= (So when spread changed from 3156 to 3457, we got a profit of 75. ie 75 per 1point) = 75

	Scenario2:
	Case2:  ScaleRatio = 1:0.267, LegRatio=1:3.75
	Nifty1=12000(CurrencyValueNifty1=12000*75=9L) 				BankNifty1 = 32000(CurrencyValueBankNifty1=32000*20*3.75=24L); 			SpreadScaleValue1(Nifty1-0.267*BankNifty2)=3456(ie 12000-0.267*32000), 		SpreadCurrencyValue1(CurerncyValueNifty1- CurrencyValueBanknifty1)= -15L (ie 9L-24L) 						SpreadPointValue1(SpreadCurrencyValue1/SpreadScaleValue1)=-434(ie -15L/3456)
	Nifty2=12000(CurrencyValueNifty2=12000*75=9L)				BankNifty2 = 32001(CurrencyValueBankNifty2=32001*20*3.75=24.00075L);SpreadScaleValue2(Nifty2-0.267*BankNifty2)=3455.733(ie 12000-0.267*32001), 	SpreadCurrencyValue2(CurerncyValueNifty1- CurrencyValueBanknifty1)= -15.00075L (ie 9L-24.00075L) 		SpreadPointValue2(SpreadCurrencyValue1/SpreadScaleValue1)=-434.08(ie -15.00075L/3455.733)
	SpreadScaleValueChange		=	SpreadScaleValue2		-	SpreadScaleValue1		=	3455.73-3456 = -0.27
	SpreadCurrencyValueChange=	SpreadCurrencyValue2	-	SpreadCurrencyValue1	=	-15.00075L+15L = -75
	PL/Point= (So when spread changed from 3156 to 3455.733, we got a profit of -75. ie 75 per 0.27point ie 277.8 per 1point) = 277.8

	PL/Point(Scenario1) is not equal to PL/Point(Scenario2)

Case2: LegMultiplier=1:1

	Scenario1:
	Nifty1=12000(CurrencyValueNifty1=12000*75=9L) 				BankNifty1 = 32000(CurrencyValueBankNifty1=32000*20*1=6.4L);				SpreadScaleValue1(Nifty1-0.267*BankNifty2)=3456(ie 12000-0.267*32000),		SpreadCurrencyValue1(CurerncyValueNifty1- CurrencyValueBanknifty1)= 2.6L (ie 9L-6.4L)						SpreadPointValue1(SpreadCurrencyValue1/SpreadScaleValue1)=75.24(ie 2.6L/3456)
	Nifty2=12001(CurrencyValueNifty2=12001*75=9.00075L)		BankNifty2 = 32000(CurrencyValueBankNifty2=32000*20*1=6.4L); 			SpreadScaleValue2(Nifty2-0.267*BankNifty2)=3457(ie 12001-0.267*32000), 		SpreadCurrencyValue2(CurerncyValueNifty1- CurrencyValueBanknifty1)= 2.60075L (ie 9.00075L-6.4L)		SpreadPointValue2(SpreadCurrencyValue1/SpreadScaleValue1)=75.24(ie 2.60075L/3457)
	SpreadScaleValueChange		=	SpreadScaleValue2		-	SpreadScaleValue1		=	3457-3456 = 1
	SpreadCurrencyValueChange=	SpreadCurrencyValue2	-	SpreadCurrencyValue1	=	2.60075L-2.6L = 75
	PL/Point(Scenario1)= (So when spread changed from 3456 to 3457, we got a profit of 75. ie profit of 75 per 1point) = 75

	Scenario2:
	Case2:  ScaleRatio = 1:0.267, LegRatio=1:3.75
	Nifty1=12000(CurrencyValueNifty1=12000*75=9L) 				BankNifty1 = 32000(CurrencyValueBankNifty1=32000*20*1=6.4L); 			SpreadScaleValue1(Nifty1-0.267*BankNifty2)=3456(ie 12000-0.267*32000), 		SpreadCurrencyValue1(CurerncyValueNifty1- CurrencyValueBanknifty1)= 2.6L (ie 9L-6.4L) 						SpreadPointValue1(SpreadCurrencyValue1/SpreadScaleValue1)=75.24(ie 2.6L/3456)
	Nifty2=12000(CurrencyValueNifty2=12000*75=9L)				BankNifty2 = 32001(CurrencyValueBankNifty2=32001*20*1=6.4002L);		SpreadScaleValue2(Nifty2-0.267*BankNifty2)=3455.733(ie 12000-0.267*32001), 	SpreadCurrencyValue2(CurerncyValueNifty1- CurrencyValueBanknifty1)= 2.6002L (ie 9L-6.4002L) 				SpreadPointValue2(SpreadCurrencyValue1/SpreadScaleValue1)=434.24(ie 2.6002L/3455.733)
	SpreadScaleValueChange		=	SpreadScaleValue2		-	SpreadScaleValue1		=	3456-3455.73 = 0.27
	SpreadCurrencyValueChange=	SpreadCurrencyValue2	-	SpreadCurrencyValue1	=	2.6L-2.6002L = -20
	PL/Point(Scenario2)= (So when spread changed from 3456 to 3455.733, we got a loss of 20. ie 20 per 0.267point ie 75 per 1point) = 75

	PL/Point(Scenario1) is equal to PL/Point(Scenario2)


////////////////////////////////
CUSTOMIZING ScaleRatio ACCORDING TO LegRatio

Most of the time we take approximate values of LegRatio by round them off. To get rounded values of LegRatio, we tweak ScaleRatio as
ScaleRatio= 1:(Leg2/Leg1)*(LotSize2/LotSize1). We refer this ScaleRatio in this afl as Manual Scale Ratio. ScaleRatioAutomatic is the inverse of actual ATR ratio = (1/ATR1):(1/ATR2) = 1:(ATR1/ATR2)
So we calculate ScaleRatio twice, first ScaleRatioAutomatic to calculate exact LegRatio, then ScaleRatioManual to calculate approximate LegRatio.

*/


TimeFrameDaily=ParamToggle("TimeFrameDaily","NO|YES",0);
Period=Param("ATRPeriod",3,1,500,1); 
ParamStr("NOTE:" ,"At a time, only one Signal should be made ON");
/////////////////////////////////////////////
_SECTION_BEGIN("PriceSpread");
function MySpread(PriceSpreadFlag, Symbol1Name,Symbol2Name,Scale1,Scale2) //PriceSpreadFlag =1 for Price, and PriceSpreadFlag = 2 for spread
{
/////////////////////////////////////////////
/////////////////////////////////////////////
/*
_SECTION_BEGIN("PriceFrames");
{
	PriceFrame=ParamToggle("Display Price","OFF|ON",IIf(PriceSpreadFlag==1,1,0));
	sPeriod= ParamList( "Time Frame", "nMinute|1 min|15 min|Hourly|Daily", 0);
	if(sPeriod=="nMinute") {nPeriod=Param("n",1,1,500,1)*in1Minute;}
	if (sPeriod== "1 min")			{nPeriod=in1Minute;}		
	else if (sPeriod== "15 min")	{nPeriod=in15Minute;}
	else if (sPeriod== "Hourly")	{nPeriod=inHourly;}
	else if (sPeriod== "Daily")		{nPeriod=inDaily;}
	DateComp = TimeFrameCompress( DateTime(), nPeriod, compressOpen );  // compressOpen works better with dates
	TimeFrameSet(nPeriod);
	SetChartOptions( 0, chartShowDates|chartShowArrows |chartWrapTitle );
	_N( Title = StrFormat( "Interval:" + Interval() + " DateComp: " + DateTimeToStr( SelectedValue(DateComp) ) + "/ DateTime(): " + DateTimeToStr( SelectedValue(DateTime()) ) + " Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );

	if(PriceFrame) 
	{
	TypeofPrice  = ParamList("PriceStyle","Candle|Heiken Ashi|Bar",0);
	switch(TypeofPrice)
	{
   		case "Candle":
			Plot(C, "Close", IIf(C>O,colorBrightGreen,colorOrange), styleNoTitle | styleCandle| GetPriceStyle() ); 
			break;
		case "Heiken Ashi":
			//HaClose = (O + H + L + C)/4; HaOpen = AMA( Ref( HaClose, -1 ), 0.5); HaHigh = Max( H, Max( HaClose, HaOpen ) ); HaLow = Min( L, Min( HaClose, HaOpen ) ); 
			HaClose[0] = (Open[0]+High[0]+Low[0]+Close[0]) / 4; HaOpen[0]  = (HaClose[0] + Open[0]) / 2; HaHigh[0]  = Max( High[0], Max( HaClose[0], HaOpen[0] ) );HaLow[0]   = Min( Low[0], Min( HaClose[0], HaOpen[0] ) );
			for (i=1; i<BarCount; i++) {	HaClose[i] = (Open[i]+High[i]+Low[i]+Close[i]) / 4; 	Haopen[i]  = (HaClose[i-1] + HaOpen[i-1]) / 2; HaHigh[i]  = Max( High[i], Max( HaClose[i], HaOpen[i] ) );	Halow[i]   = Min( Low[i], Min( HaClose[i], HaOpen[i] ) );} // Here is the problem when using Arrays: Haopen always uses its own previous value	
			PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", IIf(HaClose >= HaOpen,colorBrightGreen,colorOrange), styleCandle );
			break;
	   case "Bar":
			PlotOHLC( Open,  High,  Low,  Close, "", IIf(C>Ref(C,-1),colorBrightGreen,colorOrange), styleBar|styleThick);
			break;
  	}
	}
	TimeFrameRestore();
}
_SECTION_END();
*/
/////////////////////////////////////////////
/////////////////////////////////////////////
_SECTION_BEGIN("Spread");
{
	DisplayCash2Future = ParamToggle("Display Cash2Future","OFF|ON",0);
	DisplaySymbol1=ParamToggle("Display Symbol1","OFF|ON",PriceSpreadFlag);
	DisplaySymbol2=ParamToggle("Display Symbol2","OFF|ON",0);
	DisplaySpread = ParamToggle("Display Spread (Symbol1 - Symbol2) ","OFF|ON",IIf(PriceSpreadFlag==0,1,0));
		
	SetChartOptions(0,chartShowArrows|chartShowDates);
	_N( Symbol1a= Name()); 
	/*_N( Symbol1a= ParamList("Symbol1", 
	"ACC-I|ADANIENT-I|ADANIPORTS-I|ADANIPOWER-I|AMARAJABAT-I|AMBUJACEM-I|ANDHRABANK-I|APOLLOHOSP-I|APOLLOTYRE-I|ARVIND-I|ASHOKLEY-I|ASIANPAINT-I|AUROPHARMA-I|AXISBANK-I|BAJAJ-AUTO-I|BAJAJFINSV-I|BAJFINANCE-I|BALKRISIND-I|BALRAMCHIN-I|BANKBARODA-I|BANKINDIA-I|BANKNIFTY-I|BATAINDIA-I|BEL-I|BERGEPAINT-I|BHARATFIN-I|BHARATFORG-I|BHARTIARTL-I|BHEL-I|BIOCON-I|BOSCHLTD-I|BPCL-I|BRITANNIA-I|CADILAHC-I|CANBK-I|CAPF-I|CASTROLIND-I|CENTURYTEX-I|CESC-I|CHOLAFIN-I|CIPLA-I|COALINDIA-I|COLPAL-I|CONCOR-I|CUMMINSIND-I|DABUR-I|DALMIABHA-I|DHFL-I|DISHTV-I|DIVISLAB-I|DLF-I|DRREDDY-I|EICHERMOT-I|ENGINERSIN-I|EQUITAS-I|ESCORTS-I|EXIDEIND-I|FEDERALBNK-I|GAIL-I|GLENMARK-I|GMRINFRA-I|GODREJCP-I|GRANULES-I|GRASIM-I|HAVELLS-I|HCC-I|HCLTECH-I|HDFC-I|HDFCBANK-I|HEROMOTOCO-I|HEXAWARE-I|HINDALCO-I|HINDPETRO-I|HINDUNILVR-I|HINDZINC-I|IBULHSGFIN-I|ICICIBANK-I|ICICIPRULI-I|IDBI-I|IDEA-I|IDFCBANK-I|IGL-I|INDIGO-I|INDUSINDBK-I|INFRATEL-I|INFY-I|IOC-I|ITC-I|JINDALSTEL-I|JPASSOCIAT-I|JSWSTEEL-I|JUBLFOOD-I|JUSTDIAL-I|KAJARIACER-I|KOTAKBANK-I|KPIT-I|L&TFH-I|LICHSGFIN-I|LT-I|LUPIN-I|M&M-I|M&MFIN-I|MANAPPURAM-I|MARICO-I|MARUTI-I|MCDOWELL-N-I|MCX-I|MFSL-I|MGL-I|MINDTREE-I|MOTHERSUMI-I|MRF-I|MUTHOOTFIN-I|NATIONALUM-I|NBCC-I|NCC-I|NESTLEIND-I|NIFTY-I|NIFTYIT-I|NIFTYMID50-I|NIITTECH-I|NMDC-I|NTPC-I|OFSS-I|OIL-I|ONGC-I|PAGEIND-I|PEL-I|PETRONET-I|PFC-I|PIDILITIND-I|PNB-I|POWERGRID-I|PTC-I|PVR-I|RAMCOCEM-I|RAYMOND-I|RBLBANK-I|RCOM-I|RECLTD-I|RELCAPITAL-I|RELIANCE-I|RELINFRA-I|RNAVAL-I|SAIL-I|SBIN-I|SHREECEM-I|SIEMENS-I|SREINFRA-I|SRF-I|SRTRANSFIN-I|STAR-I|SUNPHARMA-I|SUNTV-I|TATACHEM-I|TATAELXSI-I|TATAGLOBAL-I|TATAMOTORS-I|TATAMTRDVR-I|TATAPOWER-I|TATASTEEL-I|TCS-I|TECHM-I|TITAN-I|TORNTPHARM-I|TORNTPOWER-I|TVSMOTOR-I|UBL-I|UJJIVAN-I|ULTRACEMCO-I|UNIONBANK-I|UPL-I|VEDL-I|VOLTAS-I|WIPRO-I|YESBANK-I|ZEEL-I	",140));*/
	Symbol1=WriteIf(Symbol1Name=="Default",Symbol1a, Symbol1Name); 	
	SetForeign( Symbol1 ); 	
		O1_i = O; H1_i = H; L1_i = L; C1_i = C; PrevC1_i= Ref(C,-1); pC1_i=ROC(C,1); TR1_i = ATR(1); ATR1_i = ATR(Period); 
		TimeFrameSet(inDaily); 
			O1_d=O; H1_d=H; L1_d=L; C1_d=C;PrevC1_d=Ref(C,-1); pC1_d=ROC(C,1); TR1_d =ATR(1); ATR1_d =ATR(Period); pTR1_d=ATR(1)/ATR(Period); 
		TimeFrameRestore();
		O1=IIf(TimeFrameDaily,O1_d,O1_i); H1=IIf(TimeFrameDaily,H1_d,H1_i); L1=IIf(TimeFrameDaily,L1_d,L1_i); C1=IIf(TimeFrameDaily,C1_d,C1_i); PrevC1=IIf(TimeFrameDaily,PrevC1_d,PrevC1_i); pC1=IIf(TimeFrameDaily,pC1_d,pC1_i); TR1=IIf(TimeFrameDaily,TR1_d,TR1_i); ATR1=IIf(TimeFrameDaily,ATR1_d,ATR1_i);
	RestorePriceArrays(); 

	SetForeign( StrReplace(Symbol1,"-I","") ); 	
		O_i = O; H_i = H; L_i = L; C_i = C; PrevC_i= Ref(C,-1); pC_i=ROC(C,1); TR_i = ATR(1); ATR_i = ATR(Period); 
		TimeFrameSet(inDaily); 
			O_d=O; H_d=H; L_d=L; C_d=C;PrevC_d=Ref(C,-1); pC_d=ROC(C,1); TR_d =ATR(1); ATR_d =ATR(Period); pTR_d=ATR(1)/ATR(Period); 
		TimeFrameRestore();
		cO=IIf(TimeFrameDaily,O_d,O_i); cH=IIf(TimeFrameDaily,H_d,H_i);cL=IIf(TimeFrameDaily,L_d,L_i);cC=IIf(TimeFrameDaily,C_d,C_i); cPrevC=IIf(TimeFrameDaily,PrevC_d,PrevC_i); cpC=IIf(TimeFrameDaily,pC_d,pC_i); cTR=IIf(TimeFrameDaily,TR_d,TR_i); cATR=IIf(TimeFrameDaily,ATR_d,ATR_i);
	RestorePriceArrays(); 

	_N( Symbol2a= ParamList("Symbol2", 
	"ACC-I|ADANIENT-I|ADANIPORTS-I|ADANIPOWER-I|AMARAJABAT-I|AMBUJACEM-I|ANDHRABANK-I|APOLLOHOSP-I|APOLLOTYRE-I|ARVIND-I|ASHOKLEY-I|ASIANPAINT-I|AUROPHARMA-I|AXISBANK-I|BAJAJ-AUTO-I|BAJAJFINSV-I|BAJFINANCE-I|BALKRISIND-I|BALRAMCHIN-I|BANKBARODA-I|BANKINDIA-I|BANKNIFTY-I|BATAINDIA-I|BEL-I|BERGEPAINT-I|BHARATFIN-I|BHARATFORG-I|BHARTIARTL-I|BHEL-I|BIOCON-I|BOSCHLTD-I|BPCL-I|BRITANNIA-I|CADILAHC-I|CANBK-I|CAPF-I|CASTROLIND-I|CENTURYTEX-I|CESC-I|CHOLAFIN-I|CIPLA-I|COALINDIA-I|COLPAL-I|CONCOR-I|CUMMINSIND-I|DABUR-I|DALMIABHA-I|DHFL-I|DISHTV-I|DIVISLAB-I|DLF-I|DRREDDY-I|EICHERMOT-I|ENGINERSIN-I|EQUITAS-I|ESCORTS-I|EXIDEIND-I|FEDERALBNK-I|GAIL-I|GLENMARK-I|GMRINFRA-I|GODREJCP-I|GRANULES-I|GRASIM-I|HAVELLS-I|HCC-I|HCLTECH-I|HDFC-I|HDFCBANK-I|HEROMOTOCO-I|HEXAWARE-I|HINDALCO-I|HINDPETRO-I|HINDUNILVR-I|HINDZINC-I|IBULHSGFIN-I|ICICIBANK-I|ICICIPRULI-I|IDBI-I|IDEA-I|IDFCBANK-I|IGL-I|INDIGO-I|INDUSINDBK-I|INFRATEL-I|INFY-I|IOC-I|ITC-I|JINDALSTEL-I|JPASSOCIAT-I|JSWSTEEL-I|JUBLFOOD-I|JUSTDIAL-I|KAJARIACER-I|KOTAKBANK-I|KPIT-I|L&TFH-I|LICHSGFIN-I|LT-I|LUPIN-I|M&M-I|M&MFIN-I|MANAPPURAM-I|MARICO-I|MARUTI-I|MCDOWELL-N-I|MCX-I|MFSL-I|MGL-I|MINDTREE-I|MOTHERSUMI-I|MRF-I|MUTHOOTFIN-I|NATIONALUM-I|NBCC-I|NCC-I|NESTLEIND-I|NIFTY-I|NIFTYIT-I|NIFTYMID50-I|NIITTECH-I|NMDC-I|NTPC-I|OFSS-I|OIL-I|ONGC-I|PAGEIND-I|PEL-I|PETRONET-I|PFC-I|PIDILITIND-I|PNB-I|POWERGRID-I|PTC-I|PVR-I|RAMCOCEM-I|RAYMOND-I|RBLBANK-I|RCOM-I|RECLTD-I|RELCAPITAL-I|RELIANCE-I|RELINFRA-I|RNAVAL-I|SAIL-I|SBIN-I|SHREECEM-I|SIEMENS-I|SREINFRA-I|SRF-I|SRTRANSFIN-I|STAR-I|SUNPHARMA-I|SUNTV-I|TATACHEM-I|TATAELXSI-I|TATAGLOBAL-I|TATAMOTORS-I|TATAMTRDVR-I|TATAPOWER-I|TATASTEEL-I|TCS-I|TECHM-I|TITAN-I|TORNTPHARM-I|TORNTPOWER-I|TVSMOTOR-I|UBL-I|UJJIVAN-I|ULTRACEMCO-I|UNIONBANK-I|UPL-I|VEDL-I|VOLTAS-I|WIPRO-I|YESBANK-I|ZEEL-I	",21));
	Symbol2=WriteIf(Symbol2Name=="Default",Symbol2a,Symbol2Name);
	SetForeign( Symbol2 ); 	
		O2_i = O; H2_i = H; L2_i = L; C2_i = C;	PrevC2_i= Ref(C,-1); pC2_i=ROC(C,1); TR2_i = ATR(1); ATR2_i = ATR(Period); 
		TimeFrameSet(inDaily); 
			O2_d=O; H2_d=H; L2_d=L; C2_d=C; PrevC2_d=Ref(C,-1); pC2_d=ROC(C,1); TR2_d =ATR(1); ATR2_d=ATR(Period); pTR2_d=ATR(1)/ATR(Period);
		O2=IIf(TimeFrameDaily,O2_d,O2_i); H2=IIf(TimeFrameDaily,H2_d,H2_i); L2=IIf(TimeFrameDaily,L2_d,L2_i); C2=IIf(TimeFrameDaily,C2_d,C2_i); PrevC2=IIf(TimeFrameDaily,PrevC2_d,PrevC2_i); pC2=IIf(TimeFrameDaily,pC2_d,pC2_i); TR2=IIf(TimeFrameDaily,TR2_d,TR2_i); ATR2=IIf(TimeFrameDaily,ATR2_d,ATR2_i);
		TimeFrameRestore();
	RestorePriceArrays();

	cfO1=(cO-O1); cfH1=(cH-H1); cfL1=(cL-L1); cfC1=(cC-C1); cfPrevC1=(cPrevC-PrevC1); //sTR=Max(sH,sL)-Min(sH,sL);
	//Display Price
	if(DisplayCash2Future){	PlotOHLC(cfO1,cfH1,cfL1,cfC1,"",colorWhite,styleLine,0,0,0,0,2);}		

	AutoScale1=1; AutoScale2=EMA(ATR1/ATR2,Period); ManualScale1a=Param("ScaleRatioManualSymbol1",1,1,1,1); ManualScale2a=Param("ScaleRatioManualSymbol2",1,-10000,10000,0.001); ManualScale1=IIf(Scale1==0,ManualScale1a,Scale1); ManualScale2=IIf(Scale2==0,ManualScale2a,Scale2);
	sO=(O1*ManualScale1-O2*ManualScale2); sH=(H1*ManualScale1-H2*ManualScale2); sL=(L1*ManualScale1-L2*ManualScale2); sC=(C1*ManualScale1-C2*ManualScale2); sPrevC1=(PrevC1*ManualScale1-PrevC2*ManualScale2); sTR=Max(sH,sL)-Min(sH,sL);
	//Display Price
	if(DisplaySymbol1){	PlotOHLC(O1,H1,L1,C1,"",colorDefault,styleCandle);}		
	if(DisplaySymbol2){	PlotOHLC(O2,H2,L2,C2,"",colorDefault,styleCandle|styleDashed);}
	if(DisplaySpread)	{	PlotOHLC( sO,sH,sL,sC, "Spread", IIf(ParamToggle("StyleSpread","Candle|Line",0),colorWhite,IIf( sC > sO, colorSkyblue/*ParamColor("ColorSymbol3Up",colorSkyblue)*/, colorCustom12/*ParamColor("ColorSymbol3Dn",colorCustom12)*/)),IIf(ParamToggle("StyleSpread","Candle|Line",0),styleLine|styleThick|styleOwnScale,styleCandle|styleDashed)); }
	//if(ParamToggle("Display AutoScale","OFF|ON",0)){Plot(AutoScale2,"\nAutoScale2" ,colorWhite,styleOwnScale);}
}	
_SECTION_END();
///////////////////////////////////////////////
///////////////////////////////////////////////
_SECTION_BEGIN("Background Information");
{
	//_SECTION_BEGIN("Title_ATR");
	Font=Param("Font Title",9,8,20,1); GfxSelectFont("Arial Bold", Font, 00, False,False,10*0 ); GfxSetTextColor(colorWhite); GfxSetBkMode(1); GfxSetOverlayMode(1); GfxSetTextAlign( 0 );
	//Title1a=StrFormat(Interval(2) + " "+Date()+ "; ");
	Title1=StrFormat("Current: 	TR(ATR)=%0.2f (%0.2f(%0.2f%%)); O=%0.2f, H=%0.2f, L=%0.2f, PrevC=%0.2f, C=%0.2f(%0.2f%%)",		IIf(DisplaySpread,sTR,IIf(DisplaySymbol2,TR2,TR1)),														IIf(DisplaySpread,0,IIf(DisplaySymbol2,ATR2,ATR1)), 												IIf(DisplaySpread,0,IIf(DisplaySymbol2,ATR2*100/C2,ATR1*100/C1)), 																								IIf(DisplaySpread,sO,IIf(DisplaySymbol2,O2,O1)), 														IIf(DisplaySpread,sH,IIf(DisplaySymbol2,H2,H1)),														IIf(DisplaySpread,sL,IIf(DisplaySymbol2,L2,L1)),															IIf(DisplaySpread,0,IIf(DisplaySymbol2,PrevC2,PrevC1)),												IIf(DisplaySpread,sC,IIf(DisplaySymbol2,C2,C1)),														IIf(DisplaySpread,0,IIf(DisplaySymbol2,pC2,pC1)));
	Title2=StrFormat("Daily : 		TR(ATR)=%0.2f (%0.2f(%0.2f%%)); O=%0.2f, H=%0.2f, L=%0.2f, PrevC=%0.2f, C=%0.2f(%0.2f%%)",		IIf(DisplaySpread,LastValue(sTR),IIf(DisplaySymbol2,LastValue(TR2_d),LastValue(TR1_d))),	IIf(DisplaySpread,0,IIf(DisplaySymbol2,LastValue(ATR2_d),LastValue(ATR1_d))), 			IIf(DisplaySpread,0,IIf(DisplaySymbol2,LastValue(ATR2_d)*100/LastValue(C2_d),LastValue(ATR1_d)*100/LastValue(C1_d))), 					IIf(DisplaySpread,LastValue(sO),IIf(DisplaySymbol2,LastValue(O2_d),LastValue(O1_d))),	IIf(DisplaySpread,LastValue(sH),IIf(DisplaySymbol2,LastValue(H2_d),LastValue(H1_d))),	IIf(DisplaySpread,LastValue(sL),IIf(DisplaySymbol2,LastValue(L2_d),LastValue(L1_d))),		IIf(DisplaySpread,0,IIf(DisplaySymbol2,LastValue(PrevC2_d),LastValue(PrevC1_d))),			IIf(DisplaySpread,LastValue(sC),IIf(DisplaySymbol2,LastValue(C2_d),LastValue(C1_d))),	IIf(DisplaySpread,0,IIf(DisplaySymbol2,LastValue(pC2_d),LastValue(pC1_d)))); 
	Title3 = StrFormat("Formulae: LegRatio = 1 : (Scale2/Scale1)*(LotSize1/LotSize2); Scale Ratio = 1: (Leg2/Leg1)*(LotSize2/LotSize1); 	PointValue = (Leg*LotSize)/Scale = ValueChange/ScaleChange");
	Title4 = StrFormat("AutoScaleRatio= %1.0f : %0.3f; ATR Ratio=%1.2f : %1.2f, InverseATR Ratio = 1:%1.2f; ManualScaleRatio = %1.0f : %0.3f; LegRatio= %1.0f : %1.3f * (LotSize1/LotSize2)", AutoScale1,AutoScale2,ATR1_d,ATR2_d,ATR1_d/ATR2_d, ManualScale1,ManualScale2,1,ManualScale2/ManualScale1);
	Title5 = StrFormat("%1.0f*" + Symbol1 + " - %.3f*" + Symbol2 + "= %0.2f" + ";   LastValue = "+ LastValue(sC),ManualScale1,ManualScale2,sC);
	GfxTextOut(Title1, 0*Font, (1*1.5)*Font); 
	GfxTextOut(Title2, 0*Font, (2*1.5)*Font);
	GfxTextOut(Title3, 0*Font, (3*1.5)*Font); 
	GfxTextOut(Title4, 0*Font, (4*1.5)*Font);
	GfxTextOut(Title5, 0*Font, (5*1.5)*Font);
	//_SECTION_END();

	//_SECTION_BEGIN("BkgroundSymbol");
	{
	GfxSetBkMode(1); GfxSetOverlayMode(1); GfxSetTextAlign( 6 ); GfxSetTextColor( ParamColor("ColorOfBkgroundSymbol",colorWhite) );
	GfxSelectFont("Tahoma", 0.65*Status("pxheight")/25+0.65*Status("pxwidth")/25);
	GfxTextOut( WriteIf(DisplaySpread,Symbol1 +" - "+ManualScale2+"*" + Symbol2 + " = " + (round((LastValue(sC)/0.01))*0.01),WriteIf(DisplaySymbol2,Symbol2,Symbol1)), Status("pxwidth")/2, Status("pxheight")/2 );
	GfxSelectFont("Tahoma", 0.35*Status("pxheight")/25+0.35*Status("pxwidth")/25 );
	GfxTextOut( StrFormat("%0.2f(%0.2f%%) / %0.2f(%0.2f%%)",IIf(DisplaySpread,sTR,IIf(DisplaySymbol2,LastValue(TR2_d),LastValue(TR1_d))),IIf(DisplaySpread,0,IIf(DisplaySymbol2,LastValue(pC2_d),LastValue(pC1_d))),IIf(DisplaySpread,0,IIf(DisplaySymbol2,LastValue(ATR2_d),LastValue(ATR1_d))),IIf(DisplaySpread,0,IIf(DisplaySymbol2,LastValue(ATR2_d)*100/LastValue(C2_d),LastValue(ATR1_d)*100/LastValue(C1_d)))), Status("pxwidth")/2, Status("pxheight")/1.5);
	}

	//_SECTION_END();

	//_SECTION_BEGIN("Mark EOD");
	StartTime = 093000; EndTime = 152900; {Plot( Cross( TimeNum(), EndTime ), "",colorWhite , styleHistogram| styleOwnScale | styleDashed | styleThick, 0, 1 );}
	//_SECTION_END();

	//_SECTION_BEGIN("Time Left");
	{
	RequestTimedRefresh( 1 );
	TimeFrame = Interval();
		Time = Now( 4 );
		Seconds = int( Time % 100 );
		Minutes = int( Time / 100 % 100 );
		Hours = int( Time / 10000 % 100 );
		SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds );
		SecNumber = SecondNum;
	Newperiod = SecNumber % TimeFrame == 0;
	SecsLeft = SecNumber - int( SecNumber / TimeFrame ) * TimeFrame;
	SecsToGo = TimeFrame - SecsLeft;
	if ( NewPeriod )
	{
		GfxSelectSolidBrush( colorYellow);
		GfxSelectPen( colorYellow, 2 );
		Say( "New period" );
		Plot( 1, "", colorDarkGreen, styleArea | styleOwnScale, 0, 1 );	
	}
	//Rectangle
	x= Status("pxwidth")/1.5; y=Status("pxheight")/10; FontTimer=1.5*Font; /*GfxSelectSolidBrush( colorBlack ); GfxSelectPen( colorBlack, 2 ); GfxRoundRect( x+3*FontTimer, y+1.5*FontTimer, x-3*FontTimer, y-1.5*FontTimer, 0, 0 );*/ GfxSetBkMode(1); GfxSelectFont( "Arial", FontTimer, 000, False ); GfxSetTextColor( colorBrightGreen );
	GfxTextOut( ""+SecsToGo+" / "+NumToStr( TimeFrame, 1.0 ), x, y );
	/*Title
	Title = "\n" +
        "Current Time: 		" + Now( 2 ) + "\n" +
        "Chart Interval: 		" + NumToStr( TimeFrame, 1.0 ) + " Seconds\n" +
        "Second Number: 	" + NumToStr( SecNumber, 1.0, False ) + "\n" +
        "Seconds Left: 		" + NumToStr( SecsLeft, 1.0, False ) + "\n" +
        "Seconds To Go: 	" + NumToStr( SecsToGo, 1.0, False );*/
	}
	//_SECTION_END(); 
}
_SECTION_END();
///////////////////////////////////////////////
///////////////////////////////////////////////
/*_SECTION_BEGIN("Table");    //amibroker doesnt support function within function, hence this section is commented out.
{
	RowReference = Param("RowReference",3,0,15,1); ColReference = Param("ColReference",0,0,15,1);
	CellWidth = Param("CellWidth ",100,50,500,1); CellHeight = CellWidth/3; //Param("CellHeight ",50,40,500,1);
	Rows=7;Col=4;

	//Draw Blank Table
	GfxSelectPen( colorBrightGreen );
	for( r = 0; r < Rows && r < BarCount; r++ )	{GfxMoveTo( 0, (r+RowReference) * CellHeight );	GfxLineTo( Col* CellWidth, (r+RowReference) * CellHeight );	}
	GfxMoveTo( 0, (r+RowReference) * CellHeight);
	GfxLineTo( Col * CellWidth, (r+RowReference) * CellHeight );
	//Fill Table
	GfxSelectFont( "Tahoma", CellHeight/2 );  GfxSetBkMode(1); GfxSetTextColor(colorWhite);
	function PrintInCell( string, row, Col ) {	GfxDrawText( string, Col * CellWidth, row * CellHeight, (Col + 1 ) * CellWidth, (row + 1 ) * CellHeight, 0 );}

	PrintInCell( "Details", RowReference+0, ColReference+0 );
	PrintInCell( Symbol1, RowReference+0, ColReference+1 );
	PrintInCell( Symbol2, RowReference+0, ColReference+2 );
	PrintInCell( "Spread",RowReference+0, ColReference+3 );

	PrintInCell( "Open", 	RowReference+1, ColReference +0 );				PrintInCell( StrFormat("%g", O1), 	RowReference +1, ColReference +1 );		PrintInCell( StrFormat("%g", O2), 	RowReference +1, ColReference +2 ); 												PrintInCell( StrFormat("%g", sO), 	RowReference +1, ColReference +3 );
	PrintInCell( "High", 	RowReference +2, ColReference +0 );			PrintInCell( StrFormat("%g", H1), 	RowReference +2, ColReference +1 );		PrintInCell( StrFormat("%g", H2), 	RowReference +2, ColReference +2 ); 												PrintInCell( StrFormat("%g", sH), 	RowReference +2, ColReference +3 );
	PrintInCell( "Low", 	RowReference +3, ColReference +0 );			PrintInCell( StrFormat("%g", L1), 	RowReference +3, ColReference +1 );		PrintInCell( StrFormat("%g", L2), 	RowReference +3, ColReference +2 ); 												PrintInCell( StrFormat("%g", sL), 	RowReference +3, ColReference +3 );
	PrintInCell( "Close", RowReference +4, ColReference +0 );		 	PrintInCell( StrFormat("%g", C1), 	RowReference +4, ColReference +1 );		PrintInCell( StrFormat("%g", C2), 	RowReference +4, ColReference +2 ); 												PrintInCell( StrFormat("%g", sC), 	RowReference +4, ColReference +3 );
	PrintInCell( "TR", 		RowReference +5, ColReference +0 );			PrintInCell( StrFormat("%g", TR1), 	RowReference +5, ColReference +1 );		PrintInCell( StrFormat("%g", TR2), 	RowReference +5, ColReference +2 ); 												PrintInCell( StrFormat("%g"+":"+"%g", 1,Prec(TR2/TR1,2)), RowReference +5, ColReference +3 );
	PrintInCell( "ATR", 	RowReference+6 ,	ColReference +0  );			PrintInCell( StrFormat("%g", ATR1),RowReference +6, ColReference +1 );		PrintInCell( StrFormat("%g", ATR2),RowReference +6, ColReference +2 ); 											PrintInCell( StrFormat("%g"+":"+"%g", 1,Prec(1/AutoATRRatio,2)), RowReference +6, ColReference +3 );
	PrintInCell( "ScaleRatio", RowReference+7, ColReference +0 );	PrintInCell( "Auto", RowReference+7, ColReference +1 );										PrintInCell( StrFormat("%g", IIf(ScaleRatioManual,0,1)),	RowReference +7, ColReference +2 );			PrintInCell( StrFormat("%g"+":"+"%g", 1,AutoScale2), RowReference +7, ColReference +3 );
}																							 							PrintInCell( "Manual", RowReference+8, ColReference +1 );									PrintInCell( StrFormat("%g", IIf(ScaleRatioManual,1,0)),	RowReference +8, ColReference +2 );			PrintInCell( StrFormat("%g"+":"+"%g", 1,ManualScale2/ManualScale1), RowReference +8, ColReference +3 );
_SECTION_END();*/
/////////////////////////////////////////////
/////////////////////////////////////////////
/*_SECTION_BEGIN("Spread"); //Another way to write Spread function and assign a name to spread through AddToComposite 
	function Spread(Symbol1,Symbol2,Scale1,Scale2) //nested function gives error, so write it separately.
	{
	SetForeign( Symbol1 ); O1 = O; H1 = H; L1 = L; C1 = C; PrevC1 = Ref(C,-1); V1 = V;RestorePriceArrays();
	SetForeign( Symbol2 ); O2 = O; H2 = H; L2 = L; C2 = C; PrevC2 = Ref(C,-1); V2 = V;RestorePriceArrays();
	sO=O1*Scale1-O2*Scale2; sH=H1*Scale1-H2*Scale2; sL=L1*Scale1-L2*Scale2; sCc=C1*Scale1-C2*Scale2; sPrevC=PrevC1*Scale1-PrevC2*Scale2;
	//AddToComposite( sCc, ''~ticker'', "'C", flags = atcFlagDefaults ) ;
	PlotOHLC( sO,sH,sL,sCc, "(" + Name()+" - "+Symbol2 + ") spread", colorDefault, styleCandle );
	}
	//Spread("BankNifty-I","HDFCBank-I",1,12.5);

_SECTION_END();*/
/////////////////////////////////////////////
/////////////////////////////////////////////

	return sC;
}

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

NIFTY=75; 
BANKNIFTY=20; 
ACC=400;
AMBUJACEM=2500;
APOLLOTYRE=3000;
ASIANPAINT=600;
AUROPHARMA=1000;
AXISBANK=1200;
BAJAJAUTO=250;
BAJAJFINSV=125;
BAJFINANCE=250;
BERGEPAINT=2200;
BHARATFIN=500;
BHARTIARTL=1851;
BIOCON=1800;
BRITANNIA=200;
BPCL=1800;
CANBK=2000;
CENTURYTEX=600;
CHOLAFIN=2500;
CIPLA=1000;
DABUR=1250;
EICHERMOT=25;
ENGINERSIND=4600;
EQUITAS=4000;
GAIL=2667;
HCLTECH=700;
HDFC=500;
HDFCBANK=250;
HEROMOTOCO=200;
HINDALCO=3500;
HINDPETRO=2100;
HINDZINC=3200;
HINDUNILVR=300;
ICICIBANK=1375;
IDEA=28000;
INDIANB=2000;
INDUSINDBK=400;
INFY=1200;
IOC=3500;
JINDALSTEL=3200;
JSWSTEEL=2000;
KOTAKBANK=400;
LICHSGFIN=1100;
LUPIN=700;
MARUTI=75;
MFSL=1200;
MM=1000;
MMFIN=1250;
MRF=10;
NBCC=8500;
NTPC=4800;
NIITTECH=750;
OIL=3399;
ONGC=3750;
PETRONET=3000;
PFC=6200;
PIDILITIND=500;
POWERGRID=4000;
RAMCOCEM=800;
RAYMOND=800;
RECLTD=6000;
SAIL=12000;
SHREECEM=50;
SRTRANSFIN=600;
STAR=1200;
SUNPHARMA=1100;
TATAMOTORS=3000;
TATAMTRDVR=6000;
TATAPOWER=9000;
TATASTEEL=1061;
TCS=250;
TORNTPHARMA=500;
TORNTPOWER=3000;
TVSMOTOR=1100;
UJJIVAN=1600;
ULTRACEM=200;
UPL=600;
VEDL=3000;
YESBANK=1750;

//function P() { return MySpread(1,"Default","Default",0,0);} 
function S() { return MySpread(2,"Nifty-I","BankNifty-I",1,0.267);} 		

//INDEX
function Sindex1() {return MySpread(0,"NIFTY-I","BANKNIFTY-I",1,round(((1/NIFTY)/(1/BANKNIFTY))/0.01)*0.01); 	}
function Sindex2() {return MySpread(2,"NIFTY-I","RELIANCE-I",1,5); }
function Sindex3() {return MySpread(0,"HDFCBANK-I","BANKNIFTY-I",1,round(((1/HDFCBANK)/(1/BANKNIFTY))/0.01)*0.01); }
function Sindex4() {return MySpread(0,"KOTAKBANK-I","BANKNIFTY-I",1,round(((1/KOTAKBANK)/(1/BANKNIFTY))/0.01)*0.01); }	
function Sindex5() {return MySpread(0,"INDUSINDBK-I","BANKNIFTY-I",1,round(((1/INDUSINDBK)/(1/BANKNIFTY))/0.01)*0.01); }	
function Sindex6() {return MySpread(0,"INDUSINDBK-I","BANKNIFTY-I",1,round(((1/INDUSINDBK)/(1/BANKNIFTY))/0.01)*0.01); }	

//BANK
function Sbank1() {return MySpread(0,"HDFC-I","HDFCBANK-I",1,2*round(((1/HDFC)/(1/HDFCBANK))/0.01)*0.01); }
function Sbank2() {return MySpread(0,"KOTAKBANK-I","INDUSINDBK-I",1,round(((1/KOTAKBANK)/(1/INDUSINDBK))/0.01)*0.01); }
function Sbank3() {return MySpread(0,"ICICIBANK-I","HDFCBANK-I",1,round(((1/ICICIBANK)/(1/HDFCBANK))/0.01)*0.01); }
function Sbank4() {return MySpread(0,"ICICIBANK-I","AXISBANK-I",1,round(((1/ICICIBANK)/(1/AXISBANK))/0.01)*0.01); }
function Sbank5() {return MySpread(0,"AXISBANK-I","KOTAKBANK-I",1,round(((1/AXISBANK)/(1/KOTAKBANK))/0.01)*0.01); }
//FIN
function Sfin1() {return MySpread(0,"BAJFINANCE-I","BAJAJFINSV-I",1,round(((1/BAJFINANCE)/(1/BAJAJFINSV))/0.01)*0.01); }
function Sfin2() {return MySpread(0,"EQUITAS-I","UJJIVAN-I",1,round(((1/EQUITAS)/(1/UJJIVAN))/0.01)*0.01); }
function Sfin3() {return MySpread(0,"M&MFIN-I","MFSL-I",1,round(((1/MMFIN)/(1/MFSL))/0.01)*0.01); }
function Sfin4() {return MySpread(0,"M&MFIN-I","LICHSGFIN-I",1,round(((1/MMFIN)/(1/LICHSGFIN))/0.01)*0.01); }
function Sfin5() {return MySpread(0,"M&MFIN-I","SRTRANSFIN-I",1,round(((1/MMFIN)/(1/SRTRANSFIN))/0.01)*0.01); }
//AUTO
function Sauto1() {return MySpread(0,"APOLLOTYRE-I","MRF-I",1,round(((1/APOLLOTYRE)/(1/MRF))/0.01)*0.01); 	}
function Sauto2() {return MySpread(0,"TATAMTRDVR-I","TATAMOTORS-I",1,round(((1/TATAMTRDVR)/(1/TATAMOTORS))/0.01)*0.01); 	}
function Sauto3() {return MySpread(0,"BAJAJ-AUTO-I","HEROMOTOCO-I",1,round(((1/BAJAJAUTO)/(1/HEROMOTOCO))/0.01)*0.01); 	}			
function Sauto4() {return MySpread(0,"M&M-I","HEROMOTOCO-I",1,round(((1/MM)/(1/HEROMOTOCO))/0.01)*0.01); 	}
function Sauto5() {return MySpread(0,"TVSMOTOR-I","M&M-I",1,round(((1/TVSMOTOR)/(1/MM))/0.01)*0.01); 	}	
function Sauto6() {return MySpread(0,"MARUTI-I","EICHERMOT-I",1,round(((1/MARUTI)/(1/EICHERMOT))/0.01)*0.01); 	}
//CEM
function Scem1() {return MySpread(0,"ACC-I","RAMCOCEM-I",1,round(((1/ACC)/(1/RAMCOCEM))/0.01)*0.01); }
function Scem2() {return MySpread(0,"AMBUJACEM-I","RAMCOCEM-I",1,round(((1/AMBUJACEM)/(1/RAMCOCEM))/0.01)*0.01); }
function Scem3() {return MySpread(0,"RAMCOCEM-I","ULTRACEM-I",1,round(((1/RAMCOCEM)/(1/ULTRACEM))/0.01)*0.01); }
function Scem4() {return MySpread(0,"SHREECEM-I","ULTRACEM-I",1,round(((1/SHREECEM)/(1/ULTRACEM))/0.01)*0.01); }
function Scem5() {return MySpread(0,"NBCC-I","ENGINERSIND-I",1,round(((1/NBCC)/(1/ENGINERSIND))/0.01)*0.01); }
//ENERGY
function Soil1() {return MySpread(0,"PETRONET-I","GAIL-I",1,round(((1/PETRONET)/(1/GAIL))/0.01)*0.01); }
function Soil2() {return MySpread(0,"HINDPETRO-I","BPCL-I",1,round(((1/HINDPETRO)/(1/BPCL))/0.01)*0.01); }
function Soil3() {return MySpread(0,"ONGC-I","OIL-I",1,round(((1/ONGC)/(1/OIL))/0.01)*0.01); }
//function Soil4() {return MySpread(0,"ONGC-I","IOC-I",1,round(((1/ONGC)/(1/IOC))/0.01)*0.01); }
function Spower1() {return MySpread(0,"PFC-I","RECLTD-I",1,round(((1/PFC)/(1/RECLTD))/0.01)*0.01); }	
function Spower2() {return MySpread(0,"NTPC-I","POWERGRID-I",1,round(((1/NTPC)/(1/POWERGRID))/0.01)*0.01); }
function Spower3() {return MySpread(0,"TATAPOWER-I","TORNTPOWER-I",1,round(((1/TATAPOWER)/(1/TORNTPOWER))/0.01)*0.01); }
function Smetal1() {return MySpread(0,"HINDALCO-I","VEDL-I",1,round(((1/HINDALCO)/(1/VEDL))/0.01)*0.01); }
function Smetal2() {return MySpread(0,"JSWSTEEL-I","TATASTEEL-I",1,round(((1/JSWSTEEL)/(1/TATASTEEL))/0.01)*0.01); }
function Smetal3() {return MySpread(0,"JSWSTEEL-I","JINDALSTEL-I",1,round(((1/HINDALCO)/(1/HINDZINC))/0.01)*0.01); }
function Smetal4() {return MySpread(0,"JINDALSTEL-I","TATASTEEL-I",1,round(((1/JINDALSTEL)/(1/TATASTEEL))/0.01)*0.01); }
function Smetal5() {return MySpread(0,"SAIL-I","TATASTEEL-I",1,round(((1/SAIL)/(1/TATASTEEL))/0.01)*0.01); }
function Smetal6() {return MySpread(0,"HINDZINC-I","VEDL-I",1,round(((1/HINDZINC)/(1/VEDL))/0.01)*0.01); }
//IT
function Sit1() {return MySpread(0,"NIITTECH-I","HCLTECH-I",1,round(((1/NIITTECH)/(1/HCLTECH))/0.01)*0.01); 	}
function Sit2() {return MySpread(0,"INFY-I","TCS-I",1,round(((1/INFY)/(1/TCS))/0.01)*0.01); 	}
//PHARMA
function Spharma1() {return MySpread(0,"AUROPHARMA-I","LUPIN-I",1,1/0.7*round(((1/AUROPHARMA)/(1/LUPIN))/0.01)*0.01); 	}
function Spharma2() {return MySpread(0,"SUNPHARMA-I","CIPLA-I",1,round(((1/SUNPHARMA)/(1/CIPLA))/0.01)*0.01); 	}
function Spharma3() {return MySpread(0,"BIOCON-I","LUPIN-I",1,round(((1/BIOCON)/(1/LUPIN))/0.01)*0.01); 	}
function Spharma4() {return MySpread(0,"STAR-I","LUPIN-I",1,1/1*round(((1/STAR)/(1/LUPIN))/0.01)*0.01); 	}
function Spharma5() {return MySpread(0,"STAR-I","TORNTPHARMA-I",1,round(((1/STAR)/(1/TORNTPHARMA))/0.01)*0.01); 	}
function Spharma6() {return MySpread(0,"STAR-I","BIOCON-I",1,1/0.75*round(((1/STAR)/(1/BIOCON))/0.01)*0.01); 	}
//OTHER
function Sother1() {return MySpread(0,"BERGEPAINT-I","ASIANPAINT-I",1,round(((1/BERGEPAINT)/(1/ASIANPAINT))/0.01)*0.01); }	
function Sother2() {return MySpread(0,"UPL-I","PIDILITIND-I",1,round(((1/UPL)/(1/PIDILITIND))/0.01)*0.01); }
function Sother3() {return MySpread(0,"RAYMOND-I","CENTURYTEX-I",1,round(((1/RAYMOND)/(1/CENTURYTEX))/0.01)*0.01); }
function Sother4() {return MySpread(0,"HINDUNILVR-I","BRITANNIA-I",1,round(((1/HINDUNILVR)/(1/BRITANNIA))/0.01)*0.01); }
function Sother5() {return MySpread(0,"DABUR-I","HINDUNILVR-I",1,round(((1/DABUR)/(1/HINDUNILVR))/0.01)*0.01); }
/*
/////////////////////////////////////////////
/////////////////////////////////////////////
_SECTION_END();
/////////////////////////////////////////////
/////////////////////////////////////////////
_SECTION_BEGIN("BackTest");
	function BackTest(BuyEntry,BuyExit,SellEntry,SellExit)
	{
	Buy = BuyEntry; Sell = BuyExit; Short=SellEntry; Cover=SellExit;
	Buy = ExRem(Buy,Sell); Sell = ExRem(Sell,Buy);
	BuyPrice=ValueWhen(Buy,C); SellPrice=ValueWhen(Sell,C); ShortPrice=ValueWhen(Short,C); CoverPrice=ValueWhen(Cover,C);

	PlotShapes( Buy * shapeUpTriangle, colorBrightGreen,0,L);
	PlotShapes( Sell* shapeSmallCircle, colorBrightGreen,0,H, 20);
	PlotShapes( Short * shapeDownTriangle, colorYellow,0,H);
	PlotShapes( Cover* shapeSmallCircle, colorYellow,0,L,-20);

	//Stploss on PrevCandle
	StopPercent=Param("StopPercent",5,0.25,5,0.05); StopPercent=Optimize("StopPercent",5,0.25,5,0.05);
	StpPerc=0.01*StopPercent*C;
	stopLevelLong = Ref( L, -1 );  stopLevelShort = Ref( H, -1 );  
	stopAmountLong = Min(StpPerc,BuyPrice - stopLevelLong); stopAmountShort = Min(StpPerc,stopLevelShort - ShortPrice);
	stopAmount = IIf(Flip(Buy,Sell),stopAmountLong ,stopAmountShort );
	ApplyStop(stopTypeLoss, stopModePoint, stopAmount , True);
	}
_SECTION_END();
/////////////////////////////////////////////
/////////////////////////////////////////////

*/
/////////////////////////////////////////////
/////////////////////////////////////////////
_SECTION_BEGIN("ATR Exploration");
{
	TimeFrameSet(inDaily); TR1=ATR(1); ATR1=ATR(Period); pC1_d=(ROC(C,1)); pTR1_lvd=LastValue(TR1)/LastValue(ATR1); trATR_lvd=StrFormat("%0.2f (%0.2f)",LastValue(TR1),LastValue(ATR1));TimeFrameRestore();
	//Filter =pTR1_lvd>1;
	Filter =abs(LastValue(pC1_d))>2.5;

	widthDefault=100; colorCondition1=IIf(LastValue(pC1_d)>0,colorBlue,colorRed); //colorCondition2=IIf(pTR1_lvd>1,colorBrightGreen,colorDefault);
	SetOption( "NoDefaultColumns", True );	
	AddTextColumn( Name(), "Ticker", 1, colorCondition1,colorDefault, 1.1*widthDefault);	
	AddColumn( DateTime(), "Date/Time", formatDateTime,colorDefault,colorDefault,0.1*widthDefault);

	//AddTextColumn( trATR_lvd, "TR(ATR)",1,colorDefault,colorDefault,widthDefault);
	//AddColumn(pTR1_lvd,"TR/ATR",1.2,colorDefault,colorCondition2,0.5*widthDefault);
	
	AddColumn(LastValue(pC1_d),"%Close",1.2,colorCondition1,colorDefault,0.75*widthDefault);
	AddTextColumn(NumToStr(C,1.2)+"("+NumToStr(H,1.2)+"-"+NumToStr(L,1.2)+")"+";   "+trATR_lvd,"LTP(H-L);  TR(ATR)",1.2,colorDefault,colorDefault,2*widthDefault);	
	SetSortColumns(-3);
	AddSummaryRows(16,1.0);
}
_SECTION_END();
///////////////////////////////////////////////
///////////////////////////////////////////////
Back