Help in writing AFL with target and Stoploss

#1
Hi,

I am very new to AFL. I just want to code a simple afl with moving average.

for example
Buy = close > MA ( close, 10)
Short = close < MA ( close, 10)
Sell = Buy
Cover = Sell

Now here I want to add SAR (stop and reverse) for 25 point and profit target 50 point.

Can I be please advised, how it can be achieved.

Thanks in advance,
Jitesh
 

johnnypareek

Well-Known Member
#2
Hi,

I am very new to AFL. I just want to code a simple afl with moving average.

for example
Buy = close > MA ( close, 10)
Short = close < MA ( close, 10)
Sell = Buy
Cover = Sell

Now here I want to add SAR (stop and reverse) for 25 point and profit target 50 point.

Can I be please advised, how it can be achieved.




Thanks in advance,
Jitesh
Hi,

Go to setting, select the tab "stop". Their u can put sl, traling nsl and profit tgt in points or in %

johnny
 

asnavale

Well-Known Member
#3
Hi,

I am very new to AFL. I just want to code a simple afl with moving average.

for example
Buy = close > MA ( close, 10)
Short = close < MA ( close, 10)
Sell = Buy
Cover = Sell

Now here I want to add SAR (stop and reverse) for 25 point and profit target 50 point.

Can I be please advised, how it can be achieved.

Thanks in advance,
Jitesh
Hi Jitesh,

Check your code.

SELL = BUY

What does it mean?

-Anant
 
#5
Hi,

I need a AFL for the below condition..
Buy :- if close is above 13 EMA daily, and above weekly pivot point

Sell : - if close is below 13 EMA daily

Short : - if close is below 13 EMA daily, and below weekly PP

cover : - if close is above 13 EMA daily

one more condition, if candle time frame is changed, then I dont want EMA to be changed. I mean if I am seeing hourly candle, even in that case I want EMA to be plotted on daily candle.

Is it possible in AFL?

Thanks in advance.

Jitesh
 
#6
HI,

any one tell me can i add sound alert in amibroker when my price reach to our target price.

For eg. I purchase Bank nifty @ 11500 and my target is 11550 and stoploss is 11475.

I want set sound alert if price reach to 11550 and secound sound alert if price reach to my stop loss price 11475.

I not use any indicator for decided price target or stop loss its simple use price chart only and 50 points target and 25 poits stop loss for every

call.

if any one know about these pls tell me.

Thanks
Kishor.
 
#7
Help in writing AFL with Stoploss

Hi friends,

I wrote the below AFL. This AFL is working ok for the simple buy and sell as per the defined condition. But I am facing issue in adding the below criteria.

if price is reacing to certain value then stoploss should be buy price, similary for Short Trade.

Below is the AFL
---------------------------------------
_SECTION_BEGIN("System for moving average");
SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( Close, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

// Check if it is time to close the postions.
FirstTradeTime = 090000;
LastTradeTime = 140000;
ExitAllPositionsTime = 152500;

H_1 = TimeFrameGetPrice( "H", inDaily, -1 );
L_1 = TimeFrameGetPrice( "L", inDaily, -1 );
C_1 = TimeFrameGetPrice( "C", inDaily, -1 );
O = TimeFrameGetPrice( "O", inDaily ); // current day's open

// To calculate the Pivot Levels

R = H_1 - L_1; // Range
r1 = 0;
PP = Null;

ulb = L_1 + (R * 1.618);
uls = L_1 + (R * 1.382);
mlb = L_1 + (R * 0.618);
mls = H_1 - (R * 0.618);
llb = H_1 - (R * 1.382);
lls = H_1 - (R * 1.618);

rl = mlb - mls ;
r4 = Null;
s4 = Null;

q1 = int ( 4350 / R);
SetPositionSize( q1, 4 ); // 100 shares by default

Buy = ((High > ulb AND Low < ulb) OR (High > mlb AND Low < mlb) OR (High > llb AND Low < llb)) AND TimeNum() <= 150000;
Short = ((Low < uls AND High > uls) OR (Low < mls AND High > mls) OR (Low < lls AND High > lls)) AND TimeNum() <= 150000;

//Sell = ((Low < uls AND High > uls) OR (Low < mls AND High > mls) OR (Low < lls AND High > lls)) OR TimeNum() >= ExitAllPositionsTime;
Cover = ((High > ulb AND Low < ulb) OR (High > mlb AND Low < mlb) OR (High > llb AND Low < llb)) OR TimeNum() >= ExitAllPositionsTime;

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

pbuyprice = BuyPrice;
BuyPrice = IIf(( High > ulb AND Low < ulb), ulb, IIf((High > mlb AND Low < mlb), mlb , IIf((High > llb AND Low < llb) , llb , pbuyprice)));
psellprice = SellPrice;
SellPrice = IIf(( High > uls AND Low < uls), uls, IIf((High > mls AND Low < mls), mls , IIf((High > lls AND Low < lls) , lls , psellprice)));
pcoverprice = CoverPrice;
CoverPrice = IIf(( High > ulb AND Low < ulb), ulb, IIf((High > mlb AND Low < mlb), mlb , IIf((High > llb AND Low < llb) , llb , pcoverprice)));
pshortprice = ShortPrice;
ShortPrice = IIf(( High > uls AND Low < uls), uls, IIf((High > mls AND Low < mls), mls , IIf((High > lls AND Low < lls) , lls , pshortprice)));

slamt = int (R * 0.23); // stoploss amount
slamt2 = 0 ;
slamt1 = BuyPrice + slamt ;

priceatbuy=0;
highsincebuy = 0;

for( i = 0; i < BarCount; i++ )
{
if( priceatbuy == 0 AND Buy[ i ] )
{
priceatbuy = BuyPrice[ i ];
}

if( priceatbuy > 0 )
{
highsincebuy = Max( High[ i ], highsincebuy );
}
slamt2 = IIf(highsincebuy > priceatbuy + slamt, 0 , slamt);
}
ApplyStop(0,2,slamt2,1) ;

//SellPrice = IIf((Buy AND High > slamt1), BuyPrice, psellprice);

//Ftval = int (BuyPrice + (R * 0.25)


//stamt = int (R *.23);
//ApplyStop(2,2,stamt,1);

_SECTION_END();
---------------------------------------------------------------

Experts please have a look, looking for your help.

with regards.
Jitesh
 
#8
I changed the AFL, But still not giving the result.. experts your kind advice is needed..
------
_SECTION_BEGIN("System");
SetChartOptions(0, chartShowArrows | chartShowDates);
Plot(Close, "Close", ParamColor("Color", colorBlack), styleNoTitle | ParamStyle("Style") | GetPriceStyle());

// Define Time Parameter.

FirstTradeTime = 090000;
LastTradeTime = 140000;
ExitAllPositionsTime = 152500;

H_1 = TimeFrameGetPrice("H", inDaily, -1);
L_1 = TimeFrameGetPrice("L", inDaily, -1);
C_1 = TimeFrameGetPrice("C", inDaily, -1);
O = TimeFrameGetPrice("O", inDaily); // current day's open

// To calculate the Pivot Levels

R = H_1 - L_1; // Range
r1 = 0;
PP = Null;

ulb = L_1 + (R * 1.618);
uls = L_1 + (R * 1.382);
mlb = L_1 + (R * 0.618);
mls = H_1 - (R * 0.618);
llb = H_1 - (R * 1.382);
lls = H_1 - (R * 1.618);

rl = mlb - mls;
r4 = Null;
s4 = Null;

q1 = int(4350 / R);
SetPositionSize(q1, 4); // Total position size keeping app. 1000 rs as stoploss

Buy = ((High > ulb AND Low < ulb) OR(High > mlb AND Low < mlb) OR(High > llb AND Low < llb)) AND TimeNum() <= 150000;
Short = ((Low < uls AND High > uls) OR(Low < mls AND High > mls) OR(Low < lls AND High > lls)) AND TimeNum() <= 150000;

Sell = ((Low < uls AND High > uls) OR(Low < mls AND High > mls) OR(Low < lls AND High > lls)) OR TimeNum() >= ExitAllPositionsTime;
Cover = ((High > ulb AND Low < ulb) OR(High > mlb AND Low < mlb) OR(High > llb AND Low < llb)) OR TimeNum() >= ExitAllPositionsTime;

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

pbuyprice = BuyPrice;
BuyPrice = IIf((High > ulb AND Low < ulb), ulb, IIf((High > mlb AND Low < mlb), mlb, IIf((High > llb AND Low < llb), llb, pbuyprice)));
psellprice = SellPrice;
SellPrice = IIf((High > uls AND Low < uls), uls, IIf((High > mls AND Low < mls), mls, IIf((High > lls AND Low < lls), lls, psellprice)));
pcoverprice = CoverPrice;
CoverPrice = IIf((High > ulb AND Low < ulb), ulb, IIf((High > mlb AND Low < mlb), mlb, IIf((High > llb AND Low < llb), llb, pcoverprice)));
pshortprice = ShortPrice;
ShortPrice = IIf((High > uls AND Low < uls), uls, IIf((High > mls AND Low < mls), mls, IIf((High > lls AND Low < lls), lls, pshortprice)));

slamt = int(R * 0.23); // stoploss amount
slamt1 = BuyPrice + slamt; // stoploss value in case of Buy
slamt2 = shortPrice - slamt; // stoploss value in case of Short

High1 = highestsince(Buy, High, 1); // Highest High since last buy
Low1 = Lowestsince(Short, Low, 1); // Lowest Low since last Low

slamtby = IIf((High1 >= slamt1), 0, slamt);
slamtst = IIFf(Low1 <= slamt2, 0, slamt);

slamtf = IIF(Buy, slamtby, iif(Short, slamtst, slamt));

ApplyStop(0, 2, slamtf, 1);


_SECTION_END();


-----
 

Similar threads