Simple Coding Help - No Promise.

trash

Well-Known Member
dear coders - a small request!

in the below code for correlation can we add exploration which gives correlation values against the base ticker by exploring a watchlist?

Code:
_SECTION_BEGIN("Historical Correlaion");
symbol = ParamStr("Correlation ticker", GetBaseIndex() );
fc = Foreign( symbol, "C" );
Plot( Correlation(C,fc,60), _SECTION_NAME() + "(" + symbol + ")", ParamColor( "color", colorCycle ), ParamStyle("Style")  );
_SECTION_END();
Just go to here http://www.amibroker.org/userkb/2007/04/24/creating-a-correlation-table/ and replace ticker1 stuff
And in correlation function you change C array to Close of baseindex. Just compare the codes.

Not tested but should work. Set "Apply to" to "Current".

Code:
WLNum = Param( "WatchList Number", 0, 0, 64, 1 );
CorrPd = Param( "Correlation Period", 8, 1, 30, 1 );

list = CategoryGetSymbols( categoryWatchlist, WLNum );

SetOption( "NoDefaultColumns", True );
Filter = Status( "LastBarInRange" );

Ticker1 = GetBaseIndex();
Ticker1Close = Foreign( Ticker1, "C" );

SetSortColumns( 1 );
AddTextColumn(Ticker1, "Correlation", 1.0 );

for ( Col = 0; ( Ticker2 = StrExtract( List, Col ) ) != ""; Col++ )
{
    Var2 = Foreign( Ticker2, "C" );
    Corr = Correlation( Ticker1Close, Var2, CorrPd );
    Color = IIf( Corr > 0, colorBrightGreen, IIf( Corr < 0, colorRed, colorWhite ) );
    Color = IIf( Ticker1 == Ticker2, 1, Color );
    AddColumn( Nz(Corr), Ticker2, 1.3, 1, Color );
}
 
Last edited:

manishchan

Well-Known Member
Hi All,

I need AFL to plot 1 min, 5 min and 15 min in the same chart (not diff pane). And probably option to switch the TF in param.. like instead of 1, 5, 15.. I want to plot 5, 15, 30.. etc.

Thanks for the help.
 

Nehal_s143

Well-Known Member
check if this is what you are aiming for . . .


Code:
// touch 
touchExitShort = H > NWf2f;
touchExitLong  = L < NWf2f;

Buy1 = Cover1 = jf2f > NWf2f;
//Sell1 = touchExitLong;
Short1 = Sell1 = jf2f<NWf2f;
//Cover1 = touchExitShort;

Buy1=ExRem(Buy1,Sell1);
Short1=ExRem(Short1,Cover1);
Sell1=ExRem(Sell1,Buy1);
Cover1=ExRem(Cover1,Short1);

LONG = Flip(Buy1,Short1);

PlotShapes(Buy1* shapeUpArrow,colorBlue, 0,L, Offset=-45); 
PlotShapes(Short1* shapeDownArrow,colorRed, 0,H, Offset=-45);
PlotShapes(Sell1*shapeHollowDownArrow,colorPink,0,L,-56);
PlotShapes(Cover1*shapeHollowUpArrow,colorSkyblue,0,H,-56);


Buy2 = LONG AND NOT(Buy1) AND C > NWB;
Sell2 = touchExitLong;
Short2 = NOT(LONG) AND NOT Short1 AND C < NWT;
Cover2 = touchExitShort;

Buy2=ExRem(Buy2,Sell2);
Short2=ExRem(Short2,Cover2);
Sell2=ExRem(Sell2,Buy2);
Cover2=ExRem(Cover2,Short2);

PlotShapes(Buy2* shapeUpArrow,colorGreen, 0,L, Offset=-51); 
PlotShapes(Short2* shapeDownArrow,colorOrange, 0,H, Offset=-51);
PlotShapes(Sell2*shapeHollowDownArrow,colorPink,0,L,-61);
PlotShapes(Cover2*shapeHollowUpArrow,colorSkyblue,0,H,-61);

replace this last part of the code . . .



Happy :)
Thanks Happy Sir, out of 2 problems 1 problem got solved, 2nd problem still remains as its,

posting the unsolved problem again, I want to plot exit signal if price touch or cross Red line or even if price comes in range of Red line by 0.01%

below code is already plotting exit if price cross red line, need to include plot sell if price is near to red line by 0.01% in below example



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

_SECTION_BEGIN("SuperTrend Modi 15min");
tf=15;
tfrm=in1Minute*tf;
TimeFrameSet(tfrm);

kk = Optimize( "mult", Param( "mult", 2, 1, 6, 0.25 ), 1, 2, 0.25 );
Per = Optimize( "period", Param( "period", 10, 5, 50, 1 ), 5, 50, 1 );
sdfact = 2;
offset = 2;
tc = ParamList( "Channel Display", List = "No Channel|Channel|ChannelRT|Both
Channels", 1 );
ms = ParamToggle( "Trend", "Regular|Smoothed", 1 );

//x = Cum( 1 );
HaClose2 = ( O + H + L + C ) / 4;
HaOpen2 = AMA( Ref( HaClose2, -1 ), 0.5 );
HaHigh2 = Max( H, Max( HaClose2, HaOpen2 ) );
HaLow2 = Min( L, Min( HaClose2, HaOpen2 ) );

if ( ms == 0 )
{
nm = ( H - L );
J2 = ( O + H + L + C ) / 4;
}
else
{
nm = ( HaHigh2 - HaLow2 );
J2 = ( HaOpen2 + HaHigh2 + HaLow2 + HaClose2 ) / 4;
}

rfsctor = WMA( nm, Per );

revers = kk * rfsctor;
Trend = 1;
NW2[0] = 0;

for ( i = 1;i < BarCount;i++ )
{
if ( Trend[i-1] == 1 )
{
if ( J2[i] < NW2[i-1] )
{
Trend[i] = -1;
NW2[i] = J2[i] + Revers[i];
}
else
{
Trend[i] = 1;

if ( ( J2[i] - Revers[i] ) > NW2[i-1] )
{
NW2[i] = J2[i] - Revers[i];
}
else
{
NW2[i] = NW2[i-1];
}
}
}

if ( Trend[i-1] == -1 )
{
if ( J2[i] > NW2[i-1] )
{
Trend[i] = 1;
NW2[i] = J2[i] - Revers[i];
}
else
{
Trend[i] = -1;

if ( ( J2[i] + Revers[i] ) < NW2[i-1] )
{
NW2[i] = J2[i] + Revers[i];
}
else
{
NW2[i] = NW2[i-1];
}
}
}
}

cp = ( H + L ) / 2;

TimeFrameRestore();


Nwf2f = TimeFrameExpand( NW2, tfrm);
Jf2f = TimeFrameExpand( J2, tfrm);


mycNjtf=IIf(jf2f>NWf2f,colorBlue,
     IIf(jf2f<NWf2f,colorRed,31));

Plot( IIf( NWf2f < jf2f, NWf2f, Null ), "", ParamColor( "ColorTrailLong", colorBlue ), styleLine | styleLine );
Plot( IIf( NWf2f > jf2f, NWf2f, Null ), "", ParamColor( "ColorTrailShort", colorRed ), styleLine | styleLine );
 
_SECTION_END();

n = Param("Ratio 2 Adjust Band",2,0.1,8,0.1);
R = H-L;  eR = MA(R/n,20);

NWT=NWf2f - eR;
NWB= NWf2f + eR;

//Plot(NWT, "",colorPink, styleLine | styleLine);
//Plot(NWB, "",colorPink, styleLine | styleLine);

// touch 
touchExitShort = H > NWf2f;
touchExitLong  = L < NWf2f;

Buy1 = Cover1 = jf2f > NWf2f;
//Sell1 = touchExitLong;
Short1 = Sell1 = jf2f<NWf2f;
//Cover1 = touchExitShort;

Buy1=ExRem(Buy1,Sell1);
Short1=ExRem(Short1,Cover1);
Sell1=ExRem(Sell1,Buy1);
Cover1=ExRem(Cover1,Short1);

LONG = Flip(Buy1,Short1);

PlotShapes(Buy1* shapeUpArrow,colorBlue, 0,L, Offset=-45); 
PlotShapes(Short1* shapeDownArrow,colorRed, 0,H, Offset=-45);
PlotShapes(Sell1*shapeHollowDownArrow,colorPink,0,L,-56);
PlotShapes(Cover1*shapeHollowUpArrow,colorSkyblue,0,H,-56);


Buy2 = LONG AND NOT(Buy1) AND C > NWB;
Sell2 = touchExitLong;
Short2 = NOT(LONG) AND NOT Short1 AND C < NWT;
Cover2 = touchExitShort;

Buy2=ExRem(Buy2,Sell2);
Short2=ExRem(Short2,Cover2);
Sell2=ExRem(Sell2,Buy2);
Cover2=ExRem(Cover2,Short2);

PlotShapes(Buy2* shapeUpArrow,colorGreen, 0,L, Offset=-51); 
PlotShapes(Short2* shapeDownArrow,colorOrange, 0,H, Offset=-51);
PlotShapes(Sell2*shapeHollowDownArrow,colorPink,0,L,-61);
PlotShapes(Cover2*shapeHollowUpArrow,colorSkyblue,0,H,-61);
 

trash

Well-Known Member
Just go to here http://www.amibroker.org/userkb/2007/04/24/creating-a-correlation-table/ and replace ticker1 stuff
And in correlation function you change C array to Close of baseindex. Just compare the codes.

Not tested but should work. Set "Apply to" to "Current".

Code:
WLNum = Param( "WatchList Number", 0, 0, 64, 1 );
CorrPd = Param( "Correlation Period", 8, 1, 30, 1 );

list = CategoryGetSymbols( categoryWatchlist, WLNum );

SetOption( "NoDefaultColumns", True );
Filter = Status( "LastBarInRange" );

Ticker1 = GetBaseIndex();
Ticker1Close = Foreign( Ticker1, "C" );

SetSortColumns( 1 );
AddTextColumn(Ticker1, "Correlation", 1.0 );

for ( Col = 0; ( Ticker2 = StrExtract( List, Col ) ) != ""; Col++ )
{
    Var2 = Foreign( Ticker2, "C" );
    Corr = Correlation( Ticker1Close, Var2, CorrPd );
    Color = IIf( Corr > 0, colorBrightGreen, IIf( Corr < 0, colorRed, colorWhite ) );
    Color = IIf( Ticker1 == Ticker2, 1, Color );
    AddColumn( Nz(Corr), Ticker2, 1.3, 1, Color );
}
A short follow up. Actually the following one is better than above one for this task since you compare to just one symbol. You can use any "Apply to" setting here.

Code:
CorrPd = Param( "Correlation Period", 8, 1, 30, 1 );

Ticker1Close = Foreign( GetBaseIndex(), "C" );
Corr = Correlation( C, Ticker1Close, CorrPd );
Color = IIf( Corr > 0, colorBrightGreen, IIf( Corr < 0, colorRed, colorWhite ) );

Filter = Status( "LastBarInRange" );

SetSortColumns( -2 );
SetOption( "NoDefaultColumns", True );
AddTextColumn(Name(), "Ticker", 1.0 );
AddColumn( Nz( Corr ), "Correlation", 1.3, 1, Color, 100 );
 

xsis

Active Member
@trash - thks!
i did try the earlier code which gives a complete table. though its pretty good and one stop point kind of a thing, i believe your 2nd code makes sense as i want correlation figs against nifty only! bt got confused where to put "nifty" in ur mentioned code.

A short follow up. Actually the following one is better than above one for this task since you compare to just one symbol. You can use any "Apply to" setting here.

Code:
CorrPd = Param( "Correlation Period", 8, 1, 30, 1 );

Ticker1Close = Foreign( GetBaseIndex(), "C" );
Corr = Correlation( C, Ticker1Close, CorrPd );
Color = IIf( Corr > 0, colorBrightGreen, IIf( Corr < 0, colorRed, colorWhite ) );

Filter = Status( "LastBarInRange" );

SetSortColumns( -2 );
SetOption( "NoDefaultColumns", True );
AddTextColumn(Name(), "Ticker", 1.0 );
AddColumn( Nz( Corr ), "Correlation", 1.3, 1, Color, 100 );
 

trash

Well-Known Member
@trash - thks!
i did try the earlier code which gives a complete table. though its pretty good and one stop point kind of a thing, i believe your 2nd code makes sense as i want correlation figs against nifty only! bt got confused where to put "nifty" in ur mentioned code.
Well, you said that you want to use GetBaseIndex(). It retrieves base relative-strength index for given security as defined in Symbol->Categories.

But if you want to use just Nifty you can alternatively use for example

Code:
CorrPd = Param( "Correlation Period", 8, 1, 30, 1 );

Ticker1Close = Foreign( "NIFTY", "C" );// set your preferred foreign symbol here
Corr = Correlation( C, Ticker1Close, CorrPd );
Color = IIf( Corr > 0, colorBrightGreen, IIf( Corr < 0, colorRed, colorWhite ) );

Filter = Status( "LastBarInRange" );

SetSortColumns( -2 );
SetOption( "NoDefaultColumns", True );
AddTextColumn(Name(), "Ticker", 1.0 );
AddColumn( Nz( Corr ), "Correlation", 1.3, 1, Color, 100 );
I don't know what you use as symbol for Nifty so just insert the correct symbol yourself as seen in the code.
 

xsis

Active Member
:thanx: trash...you relieved me of this pain of doing it manually thru the base afl formula! thks a ton!

Well, you said that you want to use GetBaseIndex(). It retrieves base relative-strength index for given security as defined in Symbol->Categories.

But if you want to use just Nifty you can alternatively use for example

Code:
CorrPd = Param( "Correlation Period", 8, 1, 30, 1 );

Ticker1Close = Foreign( "NIFTY", "C" );// set your preferred foreign symbol here
Corr = Correlation( C, Ticker1Close, CorrPd );
Color = IIf( Corr > 0, colorBrightGreen, IIf( Corr < 0, colorRed, colorWhite ) );

Filter = Status( "LastBarInRange" );

SetSortColumns( -2 );
SetOption( "NoDefaultColumns", True );
AddTextColumn(Name(), "Ticker", 1.0 );
AddColumn( Nz( Corr ), "Correlation", 1.3, 1, Color, 100 );
I don't know what you use as symbol for Nifty so just insert the correct symbol yourself as seen in the code.
 

amitrandive

Well-Known Member
Hi All,

I need AFL to plot 1 min, 5 min and 15 min in the same chart (not diff pane). And probably option to switch the TF in param.. like instead of 1, 5, 15.. I want to plot 5, 15, 30.. etc.

Thanks for the help.
Manish
For a start , this afl will plot price chart in any number of minutes.

Code:
_SECTION_BEGIN("multiple timeframe");
n=Param("TM",1,1,600,1);
n_open = TimeFrameGetPrice("O",n*in1Minute); 
n_high = TimeFrameGetPrice("H",n*in1Minute); 
n_low = TimeFrameGetPrice("L",n*in1Minute); 
n_close = TimeFrameGetPrice("C",n*in1Minute); 
 
n_Open = TimeFrameCompress( n_Open, n*in1Minute );
n_High = TimeFrameCompress( n_High, n*in1Minute );
n_Low = TimeFrameCompress( n_Low, n*in1Minute );
n_Close = TimeFrameCompress( n_Close, n*in1Minute ); 
Color=IIf(C>O,colorGreen,colorRed);
PlotOHLC(n_open,n_high,n_low,n_close,"n",color,styleCandle);
_SECTION_END();
 
Last edited:

Similar threads