# Simple Coding Help - No Promise.

#### JAYAKUMAR1975

##### Member
AMIBROKER HELP REQUIRED :-
1 ) NIFTY BUY GENERATE after 2 LOTS BUY ORDER PLACED AFTER 20 POINTS PROFIT ONE LOT SELLING @ 20 POINTS BALANCE QTY AUTOMATICALY CHANGE THE COST TO COST IT IS POSSIBLE SAME SELL SIDE VICE VERSA ( AMIBROKER AUTO TRADE )
Thanks and regards

#### vishp127

##### Member
Linear Regression and Trend Line with Std Dev Afl

Hi I am not sure if you are after this, this Afl has slope calculation in it..

Code:
``````_SECTION_BEGIN("Trend Lines");
p1 = Param("TL 1 Periods", 20, 5, 50, 1);
p2 = Param("TL 2 Periods", 5, 3, 25, 1);
TL1 = LinearReg(C, p1);
TL2 = EMA(TL1, p2);
Col1 = IIf(TL1 > TL2, ParamColor("TL Up Colour", colorBlue), ParamColor("TL Dn Colour", colorRed));
Plot(TL1, "TriggerLine 1", Col1, styleLine|styleThick|styleNoLabel);
Plot(TL2, "TriggerLine 2", Col1, styleLine|styleThick|styleNoLabel);
_SECTION_END();

_SECTION_BEGIN("Linear Regression Channel");
// CyberMan's Linear Regression Channel.. http://www.wisestocktrader.com/indicators/2690-price-with-regression-and-trend
// Linear Regression Line with 2 Standard Deviation Channels Plotted Above and Below
// The original was written by Patrick Hargus, with critical hints from Marcin Gorzynski, Amibroker.com Technical Support
// Wysiwyg coded the angle in degrees part
// I modified the original Linear Regression code so that the line will change color based on the degree of the Linear Regression slope.
// I combine this with my trading system.
// I will only take long positions if the Linear Regression line is green and the entry price is below the LR line.
// I will only take short positions if the Linear Regression line is red and the entry price is above the LR line.
// It is useful for filtering out lower probability trades.

//=====================Start Chart Configuration======================================

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 )) ));
SetChartBkGradientFill(ParamColor("Top", colorTeal), ParamColor("Bottom", colorLightGrey), ParamColor("Title", colorTeal));
SetChartBkColor(colorTeal);

Plot( C, "Close", colorWhite, styleCandle, Zorder = 1);

//=====================End Chart Configuration========================================

//=====================Start of Linear Regression Code===================================

P = ParamField("Price field",-1);

Length = 3;

Daysback = Param("Period for Liner Regression Line",Length,1,240,1);
shift = Param("Look back period",0,0,240,1);

//===================== Math Formula =============================================

x = Cum(1);
lastx = LastValue( x ) - shift;
aa = LastValue( Ref(LinRegIntercept( p, Daysback), -shift) );
bb = LastValue( Ref(LinRegSlope( p, Daysback ), -shift) );
y = aa + bb * ( x - (Lastx - DaysBack +1 ) );

//=====================Plot the Linear Regression Line ===================================

LRColor = ParamColor("LR Color", colorCycle );

LRStyle = ParamStyle("LR Style");

// A slope higher than 0.05 radians will turn green, less than -0.05 will turn red and anything in between will be white.
Angle = Param("Angle", 0.05, 0, 1.5, 0.01);

LRLine = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );

Pi = 3.14159265 * atan(1); // Pi
SlopeAngle = atan(bb)*(180/Pi);

LineUp = SlopeAngle > Angle;
LineDn = SlopeAngle < - Angle;

if(LineUp)
{
Plot(LRLine, "Lin. Reg. Line Up", IIf(LineUp, colorBrightGreen, colorWhite), LRStyle);
}
else
{
Plot(LRLine, "Lin. Reg. Line Down", IIf(LineDn, colorDarkRed, colorWhite), LRStyle);
}

Trend = IIf(LRLine > Ref(LRLine,-1),colorGreen,colorRed);//Changes LR line to green if sloping up and red if sloping down.

Plot( LRLine , "LinReg", Trend, LRSTYLE );

//======================Plot 1st SD Channel ========================================

SDP = Param("Standard Deviation", 1.5, 0, 6, 0.1);
SD = SDP/2;

width = LastValue( Ref(SD*StDev(p, Daysback),-shift) ); //Set width of inside chanels here.
SDU = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width , Null ) ;
SDL = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width , Null ) ;

SDColor = ParamColor("SD Color", colorCycle );
SDStyle = ParamStyle("SD Style");

Plot( SDU , "Upper Lin Reg", colorWhite,SDStyle ); //Inside Regression Lines
Plot( SDL , "Lower Lin Reg", colorWhite,SDStyle ); //Inside Regression Lines

//=====================  Plot 2d SD Channel ==========================================

SDP2 = Param("2d Standard Deviation", 2.0, 0, 6, 0.1);
SD2 = SDP2/2;

width2 = LastValue( Ref(SD2*StDev(p, Daysback),-shift) ); //Set width of outside chanels here.
SDU2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width2 , Null ) ;
SDL2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width2 , Null ) ;

SDColor2 = ParamColor("2 SD Color", colorCycle );
SDStyle2 = ParamStyle("2 SD Style");

Plot( SDU2 , "Upper Lin Reg", colorWhite,SDStyle2 ); //OutSide Regression Lines
Plot( SDL2 , "Lower Lin Reg", colorWhite,SDStyle2 ); //OutSide Regression Lines

//====================== End Indicator Code ==========================================``````

hi dear my friends amitrandive
will you plz add scan to this afl for below condition
sell if slopes is above 80<slope<100
i am not sure is it correct or not .imean scan for buy an sell to this condition according the slope positions

Last edited:

#### Blue_Ridge

##### New Member
Hi Happy_Singh,

Can you code rollover thing for futures?

Like say I am doing swing trading, and currently long 1 lot of Nifty Dec Future.

Then 1 days before expiry, code should exit Nifty Dec Future(Sell Nifty Dec 1 lot) and start Nifty Jan Long position(Buy Nifty Jan 1 lot) immediately.

And vice versa for the shorts.

It will be great help if you can come up with the solution.

Thanks.

#### SR_TA

##### Active Member
Is there an AFL which shows the visual pivots? or filter sideways pivots?
pratap's ema cross over pivots.

Code:
``````_SECTION_BEGIN("Visual Pivots");
// Author: Pratap Balakrishnan
// email id: [email][email protected][/email]
//

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 ) ) ));

SetChartBkColor( ParamColor("BKGrnd", ColorRGB( 190, 190, 190 ) ) );

Plot( C, "Close", ParamColor("Candle Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

ro = ParamField("Open", 0) ;
rh = ParamField("High", 1) ;
rl = ParamField("Low", 2) ;
rc = ParamField("Close", 3) ;

P = (rH+rL+rC)/3 ;
p1 = Param("Fast", 1, 1, 100, 1) ;
p2 = Param("Slow", 3, 1, 100, 1) ;
usebpvts = ParamToggle("Use Body Pvts", "No|Yes", 0) ;
isemapvts = ParamToggle("Pvt Type", "H-L|EMA", 1) ;

ismph = H-H ;
ismpl = L-L ;
bph = Max(rO,rC);
bpl = Min(rO,rC);

if (isemapvts)
{
EMA1 = EMA(P, p1) ;
EMA2 = EMA(P, p2) ;

upcross = Cross(EMA1, EMA2) ;
downcross = Cross(EMA2, EMA1) ;

//bph = Max(O,C);
//bpl = Min(O,C);
bi = BarIndex() ;

mphbar = HighestSinceBars(upcross, rH) ;
mplbar = LowestSinceBars(downcross, rL) ;

mphbi = ValueWhen(downcross, bi, 0) - ValueWhen(downcross, mphbar, 0) ;
ismph = mphbi == bi ;

mplbi = ValueWhen(upCross, bi, 0) - ValueWhen(upCross, mplbar, 0) ;
ismpl = mplbi == bi ;

ismph = IIf(downCross AND mphbar == 0, True, ismph) ;
ismpl = IIf(upcross AND mplbar == 0, True, ismpl) ;

bmphbar = HighestSinceBars(upcross, bph) ;
bmplbar = LowestSinceBars(downcross, bpl) ;

bmphbi = ValueWhen(downcross, bi, 0) - ValueWhen(downcross, bmphbar, 0) ;
isbmph = bmphbi == bi ;

bmplbi = ValueWhen(upCross, bi, 0) - ValueWhen(upCross, bmplbar, 0) ;
isbmpl = bmplbi == bi ;

isbmph = IIf(downCross AND bmphbar == 0, True, isbmph) ;
isbmpl = IIf(upcross AND bmplbar == 0, True, isbmpl) ;
}
else
{
isbmph = ismph = rH > Ref(rH, -1) AND rH > Ref(rH, 1) ;
isbmpl = ismpl = rL < Ref(rL, -1) AND rL < Ref(rL, 1) ;

}

showminor = ParamToggle("Show Minor Pvts", "No|Yes", 1) ;

issquare = ParamToggle("Pvt style", "Circle|Square", 0) ;
Clrmph = ParamColor("MPH Color", colorGreen) ;
Clrmpl = ParamColor("MPL Color", colorBrown) ;

pvtmarker = IIf(issquare, shapeSmallSquare, shapeSmallCircle) ;

if (showminor)
{
if (usebpvts)
{
PlotShapes(isbmph*pvtmarker , Clrmph , 0, rH, 12) ;
PlotShapes(isbmpl*pvtmarker , Clrmpl, 0, rL, -12) ;
}
else
{
PlotShapes(ismph*pvtmarker , Clrmph , 0, rH, 12) ;
PlotShapes(ismpl*pvtmarker , Clrmpl, 0, rL, -12) ;
}
}

showminorlvls = ParamToggle("Show Minor Levels", "No|Yes", 1) ;

mph = ValueWhen(ismph, rH) ;
mpl = ValueWhen(ismpl, rL) ;

if (showminorlvls)
{
x = LineArray(0, SelectedValue(mph), (BarCount-1), SelectedValue(mph));

Plot(x, "", colorGold, styleDashed);

x = LineArray(0, SelectedValue(mpl), (BarCount-1), SelectedValue(mpl));

Plot(x, "", colorGold, styleDashed);
}

showminortrend = ParamToggle("Show Minor Trend", "No|Yes", 0) ;
if (showminortrend)
{
// initialize minor levels
mph = ValueWhen(ismph, rH) ;
mpl = ValueWhen(ismpl, rL) ;
mbph = ValueWhen(isbmph, bph) ;
mbpl = ValueWhen(isbmpl, bpl) ;

// minor trend
mut = rH > mph OR (rC > mbph) OR (ismph AND rH > Ref(mph, -1));
mdt = rL < mpl OR (rC < mbpl) OR (ismpl AND rL < Ref(mpl, -1));
mdt = Flip(mdt, mut) ;
mut = Flip(mut, mdt) ;
//mutst = ExRem(mut, mdt) ;
//mdtst = ExRem(mdt, mut) ;

Clr = IIf(mut, colorBlue, colorRed) ;
PlotOHLC(rO,rH,rL,rC, "", Clr, styleCandle|styleNoLabel);
Plot(4, "", Clr, styleArea|styleOwnScale, 0, 100, 0, -1) ;

}

tffactor = Param("TF Factor", 3, 1, 100, 1)  ;
showhigher = ParamToggle("Show Higher", "No|Yes", 0) ;

if (showhigher)
{
tfs = tffactor *Interval() ;

TimeFrameSet(tfs) ;

if (isemapvts)
{
Ptf = (H+L+C)/3 ;
EMA1tf = EMA(Ptf, p1) ;
EMA2tf = EMA(Ptf, p2) ;

upcrosstf = Cross(EMA1tf, EMA2tf) ;
downcrosstf = Cross(EMA2tf, EMA1tf) ;
bphtf = Max(O,C) ;
bpltf = Min(O,C) ;

if (usebpvts)
{
Refhtf = bphtf ;
Refltf = bpltf ;
}
else
{
Refhtf = H ;
Refltf = L ;
}

mphbartf = HighestSinceBars(upcrosstf, Refhtf) ;
mplbartf = LowestSinceBars(downcrosstf, Refltf) ;

bitf = BarIndex() ;
mphbitf = ValueWhen(downcrosstf, bitf, 0) - ValueWhen(downcrosstf, mphbartf, 0) ;
ismphtf = mphbitf == bitf ;

mplbitf = ValueWhen(upCrosstf, bitf, 0) - ValueWhen(upCrosstf, mplbartf, 0) ;
ismpltf = mplbitf == bitf ;

ismphtf = IIf(downCrosstf AND mphbartf == 0, True, ismphtf) ;
ismpltf = IIf(upcrosstf AND mplbartf == 0, True, ismpltf) ;
}
else
{
ismphtf = H > Ref(H, -1) AND H > Ref(H, 1) ;
ismpltf = L < Ref(L, -1) AND L < Ref(L, 1) ;
}

if (showminortrend)
{
// initialize minor levels
mphtf = ValueWhen(ismphtf, H) ;
mpltf = ValueWhen(ismpltf, L) ;
mbphtf = ValueWhen(ismphtf, bphtf) ;
mbpltf = ValueWhen(ismpltf, bpltf) ;

// minor trend
muttf = H > mphtf OR (C > mbphtf) OR (ismphtf AND H > Ref(mphtf, -1));
mdttf = L < mpltf OR (C < mbpltf) OR (ismpltf AND L < Ref(mpltf, -1));
mdttf = Flip(mdttf, muttf) ;
muttf = Flip(muttf, mdttf) ;
//mutst = ExRem(mut, mdt) ;
//mdtst = ExRem(mdt, mut) ;

Clrtf = IIf(muttf, colorBlue, colorRed) ;
//	PlotOHLC(rO,rH,rL,rC, "", Clr, styleCandle|styleNoLabel);
Clrtfe = TimeFrameExpand(Clrtf, tfs, expandLast) ;
Plot(2, "", Clrtfe, styleArea|styleOwnScale, 0, 100) ;

}

ismphtfe = TimeFrameExpand(ismphtf, tfs, expandFirst) ;
ismpltfe = TimeFrameExpand(ismpltf, tfs, expandFirst) ;

He=TimeFrameExpand(H, tfs, expandFirst) ;
Le=TimeFrameExpand(L, tfs, expandFirst) ;

TimeFrameRestore() ;

ishtfmph = IIf(ismph == ismphtfe, 1, 0) ;
ishtfmpl = IIf(ismpl == ismpltfe, 1, 0) ;

PlotShapes(ismphtfe *shapeSmallSquare, Clrmph , 0, He, 12) ;
PlotShapes(ismpltfe *shapeSmallSquare, Clrmpl, 0, Le, -12) ;

showhtflvls = ParamToggle("Show HTF levels", "No|Yes", 1) ;
Clrhtflvl = colorPaleGreen ;

//Plot(2, "", IIf(ismph , colorBlue, IIf(ismpl, colorRed, colorGrey40)), styleArea|styleOwnScale, 0, 100) ;
//Plot(4, "", IIf(ishtfmph , colorBlue, IIf(ishtfmpl , colorRed, colorGrey40)), styleArea|styleOwnScale, 0, 100) ;
if (showhtflvls )
{
mphhtf = ValueWhen(ismphtfe , He, 1) ;
mplhtf = ValueWhen(ismpltfe , Le, 1) ;
x = LineArray(0, SelectedValue(mphhtf), (BarCount-1), SelectedValue(mphhtf));

Plot(x, "", Clrhtflvl , styleDashed);

x = LineArray(0, SelectedValue(mplhtf), (BarCount-1), SelectedValue(mplhtf));

Plot(x, "", Clrhtflvl , styleDashed);
}

//Plot(2, "", IIf(ismphtfe , colorRed, IIf(ismpltfe , colorBlue, colorGrey40)), styleArea|styleOwnScale, 0, 100) ;
//Plot(4, "", IIf(ismph, colorRed, IIf(ismpl , colorBlue, colorGrey40)), styleArea|styleOwnScale, 0, 100) ;
}
_SECTION_END();``````

#### navinbhatia100

##### New Member
Hello Dear,i need a afl,those will scan first 15 mints volume break out.
cond. 1-tell me the volume of first 15 mints(total volume at 9:30)
cond. 2-15 days avg. of first 15 mints volume.
cond. 3-today volume is 1.5 times of avg. volume.
i was write a afl about it but it is not working properly,can you please write it for me.

#### toughard

##### Well-Known Member
Hi,

Trying to obtain the value of today's highest volume bar's high

kindly help!!

#### kriya7

##### Member
I have an AFL for vwap as under
_SECTION_BEGIN("VWAP");
/*
The VWAP for a stock is calculated by adding the dollars traded for every
transaction in that stock ("price" x "number of
shares traded") and dividing the total shares traded. A VWAP is computed
from the Open of the market to the market Close, AND is
calculated by Volume weighting all transactions during this time period
*/

Bars_so_far_today = 1 + BarsSince( Day() != Ref(Day(), -1));
StartBar = ValueWhen(TimeNum() == 091500, BarIndex());
TodayVolume = Sum(V,Bars_so_far_today);
IIf (BarIndex() >= StartBar, VWAP = Sum (C * V, Bars_so_far_today ) /
TodayVolume,0);

Plot (VWAP,"VWAP",colorOrange, styleThick);

_SECTION_END();

I just need to add dotted lines of any colour for closing Vwap of previous 3 days. I will be grateful if anybody can add

#### shri_r3

##### Active Member
Hi
Today I tried to adjust stock split for jswsteel in amibroker 6 .
Earlier in 5.7 version it automatically detected ratio and split date but now in amibroker ver 6 it does not.
I tried to enter manually but with this shows gap up in chart with todays price also divided by same ratio.Tried for day before split, reversed ratio ect but cant get continous chart after split, there is gap down from split date.
Same problem when tried to adjust bonus for Enginersin scrip.
Can this done using some afl ?

Thank you

#### PoorBoy

##### New Member
Hi Happy_Singh - I use Amibroker but don't code well at all and it's been hard getting help. I would like to have a system that trades the ES S & P mini futures intraday, planning to use 1 minute bars. Here is what I plan:

1) I want to backtest trade entry only on the 2nd or 3rd touch of the same new high or low of day, obviously when a higher high or lower low wasn't also hit. In other words, each touch of that new level would have to be the highest or lowest to that point. So if the ES traded up to 2140.25, HOD, then pulled back a couple of ticks, then went back up to 2140.25, that would be the 2nd touch, and would trigger a trade entry. I would backtest 2nd touch only, 3rd touch only, etc. You can comment scenarios and leave the one active, and I will backtest each one separately. Sometimes the system will trade in the direction of the new HOD/new LOD, and sometimes it will fade it for a few ticks, depending on values of some indicators.

2) One of the indicators will look at 4 ETF tickers to determine underlying strength/trend day up or trend day down. Perhaps user-defined function such as MktLeaders would simplify the entry rules. So I need to determine if they are up ON THE DAY over the previous days' close. I would like this to look at all 4 to see how many of these are up, and also how many are down, on the day. For example, I will backtest entry long when 2 of the 4 are up on the day, 3 are up, or all 4. Conversely, if X number of these 4 ETFs are down, a sell signal is created. For now I will simply call out these tickers A, AA, B, and C. I did get some support (please see below) but feel that it is not correct, as this is an intraday trading system. I look to see if those 4 ETFs are up on the day over the previous days' close. So don't we need a '-1' at the end of the 2nd half of the code? No telling when/if I will get response using standard channels so any help would be appreciated! I'm not even totally sure how to integrate that last line of code into the entry rules. Thank you so much for any help you could give me here,

Poorboy

'you could check the condition for a list of symbols and sum up the values, e.g. like this:

cond1 = Foreign("symbol1", "C") > Foreign("symbol1", "O");
cond2 = Foreign("symbol2", "C") > Foreign("symbol2", "O");
cond3 = Foreign("symbol3", "C") > Foreign("symbol3", "O");
total = cond1 + cond2 + cond3;
condFilter = total == 3;///'

#### Singhraja

##### Active Member
Happy singh sir you shared inverted chart afl but there is no scaling on right side if you please rectify with scaling I need 10 point scale for nifty I will be thankful.