How to write this in AFL

#1
Dear all,

I am a newbie in AFL. I am just wondering how to write AFL to identify the second signal.

For example, the price breaks EMA 10 at 10:00 (this is the first buy signal and i suppose not to enter), then the price breaks again EMA 10 at 10:30 (this time i suppose to enter the buy signal)

Thank you!!
Jack
 

mastermind007

Well-Known Member
#2
Code that will place shape 2 at second cross. There is no point in hard coding time. 1st or 2nd cross may happen at any time.

Code:
bi = BarIndex();
dc = Day() != Ref(Day(), -1);
X = EMA(C, 10);
XX = Cross(C, X);

dcBar = Nz(ValueWhen(dc, bi, 1));
xxBarRecent = Nz(ValueWhen(XX, bi, 1));
xxBarPrior = Nz(ValueWhen(XX, bi, 2));

XXBuy = xxBarRecent AND (xxBarRecent > xxBarPrior) AND (xxBarPrior > dcBar);

Plot(X, "EMA10", colorRed, styleLine);
Plot(C, "CLOSE", colorBlack, styleCandle);
PlotShapes(ExRem(XX, dc) * shapeDigit1, colorRed);
PlotShapes(ExRem(XXBuy, dc) * shapeDigit2, colorBlack);
 
#3
i want daily eod of 21ema moving average line is shown in every time in live charts like 5m,15m,30m and all time frame i need this as a horizondal line will u pls anyone give this
 

mastermind007

Well-Known Member
#4
i want daily eod of 21ema moving average line is shown in every time in live charts like 5m,15m,30m and all time frame i need this as a horizondal line will u pls anyone give this
ananth

this will do that for you.
Code:
Plot(SelectedValue(EMA(TimeFrameGetPrice("C", inDaily, -1), 21)), "EMA 21", colorBlack, styleDashed);
 

pratapvb

Well-Known Member
#6
is this correct one its vary on many timeframe pls rechek it
tfs = inDaily ;

timeframeset(tfs) ;

e = ema(c, 21) ;

timeframrestore() ;

ee = timeframeexpand(e, tfs, expandfirst) ;

plot(ee, "", colorblue) ;
 

mastermind007

Well-Known Member
#8
tfs = inDaily ;

timeframeset(tfs) ;

e = ema(c, 21) ;

timeframrestore() ;

ee = timeframeexpand(e, tfs, expandfirst) ;

plot(ee, "", colorblue) ;
Yup,

I had forgot timeframeexpand

Code:
Plot(SelectedValue(TimeFrameExpand(EMA(TimeFrameGetPrice("C", inDaily, -1), 21), inDaily)), "EMA 21", colorBlack, styleDashed);
 
#10
Happy New Year to you and your family .

I am slowly learning coding now and able to write small afl for simple cross overs. I have some issue while I add some condition. Can you help me here below?

Buy Conditions:

1. close price should be above 20 Ema.
2. Buy signal to be generated only if price is also above 100 Ema and 20 Ema is also above 100 Ema.
3. Buy to be 15 points above the high of the buy candles. ( I mean the buy will trigger in subsequent candles only when the high of the buy candle is broken.
4. While Ema 20 and price is above 100 ema, only buy above 20 ema and exit or sell the longs below 20 ema. No fresh shorts. Fresh shorts are only on the below sell conditions.
5. Exits ( Sell the Longs) will be when the price closes below 20 Ema and the low need to be broken in the subsequent candle. No further short trades.
6. While the price and Ema 20 stays above 100 ema fresh buy entry can be taken once again when price passes above 20 Ema and the high is broken with few points.

Sell Condition.

1. close price should be below 20 Ema.
2. Short signal to be generated only if price is also below 100 Ema and 20 Ema is also below 100 Ema.
3. Short to be 15 points below the low of the short candle. ( I mean the sell will trigger in subsequent candles only when the low of the short candle is broken.
4. While Ema 20 and price is below 100 ema, only short below 20 ema and cover above the 20 ema. No fresh longs. Longs are only on the above buy conditions.
5. Cover (Exit) will be when the price closes above 20 Ema and the high need to be broken in the subsequent candle.
6. While the price and Ema 20 stays below 100 ema fresh short entry can be taken once again when price goes below 20 Ema and the low is broken with few points.

I just wanted to back test and forward test this and see the result and take for live trades then based on the results.

Idea here is that I wanted to assume that as long as the price and the 20 ema are above 100 ema, only longs need to be entered and exit the longs. Viceversa for shorts.

Buy price is few points above the high of the buy candle.
Sell Price is few points below the low of the sell candle.

Shortprice is few points below the low of the short candle.
Cover price is few points above the high of the cover candle.

No trades when price is between 20 ema and 100 ema. Even if price closes above 20 ema but if 20 ema is lower than 00 ema no longs and vice versa for short trades.

Sorry to take your time.
 

Similar threads