# Help me to create Amibroker AFL use with my formula

Discussion in 'AmiBroker' started by smarttrade, Jun 25, 2010.

Joined:
Jun 18, 2010
Messages:
111
39
Trophy Points:
28
Dear all,

I am using stochastic oscillator crossover system for trading. My parameter is Stsci value 39,1 and moving average 3(E).

Here I have created AFL :-

uptrend= (StochK(39,3)>StochD(39,1,3));
downtrend=(StochK(39,3)<StochD(39,1,3));
Sell = Cross(downtrend,uptrend);
PlotShapes( shapeDownArrow * Sell, colorRed,0,H,-12);

Above this code will show Buy & Sell Signal When Stscill Cross Above or Below Level. But I want buy and sell signal in below condition.

BUY CONDITION :- WHEN MARKET IS IN UPTREND [StochK(39,3) >StochD(39,1,3)] WE FIND WHICH CANDLE FIRST BREAK HIGH + 5 POINTS COMPARE WITH PRE.CANDLE HIGH THEN WANT SHOW HERE BUY SIGNAL.

SELL CONDITION :- WHEN MARKET IS IN DOWNTREND [StochK(39,3) <StochD(39,1,3)] WE FIND WHICH CANDLE FIRST BREAK LOW - 5 POINTS COMPARE WITH PRE.CANDLE LOW THEN WANT SHOW HERE SELL SIGNAL.

Here I attached Nifty Chart for Example.

Explanation :-

In this Above Chart, First 5 Candle is green . That means Sto.osci. (39,1) is Above The Moving Average (3) Line. But 4th Candle only (First) Breake Pre.Candle High + 5 Points.
So Here I want to get Buy Signal .

AND 6th & 7th Candle is Red. This means Sto.osci. (39,1) is Below The Moving Average (3) Line. But 7th Red Candle didnt Breake Pre.Candle Low. So Here is Continuing Our Long Trade.

8th and 9th Candle is Green and 9th Candle High also breake Pre.Candle High + 5. But Here we dont Get Buy Signal. Because Already we have Long Position. So we should wait only for Sell signal.

10th Candle is Red But 11th Candel didnt Breake 10th Candle Low. 12,15,18th also the same. [Compare the 17th red Candle low with 16th Candle low = 10 Points. But Pre.Candle 16 is Green] . So here also Contiuing our Long Position.

19th,20th,21st is green Candle and 21st candle is breake Pre.Candle High + 5. Here also we dont get Buy Signal . Because Already we have Long Position.

22nd and 23rd is red and 23rd Candle Breake Pre.Candle Low + 5 . So Here I want to get Sell signal.

24th Candle is Green But 25th Candle Didnt Breake High + 5 Points.

25th & 26th is Red Candle .------> Market closed.

This Day Total Trade : 1 Lot Long & 1 Lot Short.

Kindly Help Me to Create Buy & Sell Signal in My Condition.

2. ### nagarjuna13Member

Joined:
May 28, 2010
Messages:
65
62
Trophy Points:
18
Can you check this?

uptrend= IIf( StochK(39,3) > StochD(39,3,3), 1, 0 );
downtrend=IIf( StochK(39,3)<StochD(39,3,3), 1, 0 );
BuyV = uptrend AND Ref(uptrend, -1) AND (H > ( Ref(H, -1) + 5));
SellV = downtrend AND Ref(downtrend,-1) AND (L > ( Ref(L, -1) - 5));

Sell = SellV AND BuySB < SellSB ;

It took couple of hours to get the logic of the last 2 lines.

I tried back testing of this and I did not get much from it. Is there another indicator that needs to be clubbed with this? or Any more theory that you can provide?

thank you
nagarjuna

3. ### vkunisettyActive Member

Joined:
Mar 20, 2008
Messages:
81
113
Trophy Points:
33

Try the attached code

vidyasagar

Joined:
Jun 18, 2010
Messages:
111
39
Trophy Points:
28
Dear Nagarjuna,
I dont know How to say Thanks to you. Exactly I was Expecting this. Thanks, Thanks, Thanks a lot. :clap::clapping::thumb:

5. ### nagarjuna13Member

Joined:
May 28, 2010
Messages:
65
62
Trophy Points:
18
If possible, can you please respond to the following question? That will help me a lot.

Joined:
Jun 18, 2010
Messages:
111
39
Trophy Points:
28
Dear Nagarjuna,

I am using this method for Trade Nifty Future in Intraday. Time Frame : 15 Min. If I get buy signal I will take 2 Lots in Nifty Future. I will Close 1 lot at Every 25 Points+ and Another 1 lot I will hold till 3.29 p.m.

Suppose I booked Ist lot in 25 points+. After that If I get sell signal I will Close that 1 lot Long Position & I will go Short 2 lots Nifty @ Sell Signal. Here also same first I will book one lot @ 25 Points Profit and another 1 Lot I will hold till 3.29 p.m..

Stop Loss :

For Buy : In Uptrend, Which Candle high Breake Pre.Candle High (Here We will get Buy Signal) I will put SL @ That Pre.Candle Low Value - 3 Points.

For Sell : In downtrend, Which Candle Low Break Pre. Candle Low (Here We will get Sell Signal) I will Put SL @ That Pre.Candle High Value + 3 Points .

In which Candle We get Buy or Sell Signal, I will use SL That Pre.Candle H/L value +/- 3 Points. I dont Modify this SL for Each Candle H/L .

One More Condition:

I will use this concept only for Intraday not for delivery. So everyday we should close all the position before market end. Another Day Morning We should get Fresh Signal. So we should add here one more condition If we get buy or Sell signal, We must check here Pre.Candle and Current Candle Date also Should be the Same Date. This will help to Avoid Continuing Yesterday's Signal.

One More Request Can You give your code with Add of this above Condition with SL? and I am trying to write this same concept for Metastock Expert. Can you help to this also?

Last edited: Jun 30, 2010
nagarjuna13 likes this.

Joined:
Jun 18, 2010
Messages:
111
39
Trophy Points:
28
One More Query :-

I am using this stscillator value in Metastock :

%K Parameters Time Periods:39,Slowing:3
%D Parameters Time Periods:3.

But there Using Exponential Method. How Should this Exponential Method add in Amibroker Code.

8. ### nagarjuna13Member

Joined:
May 28, 2010
Messages:
65
62
Trophy Points:
18
are you looking out for this?

http://www.amibroker.com/guide/afl/afl_view.php?id=44

Thank you
nagarjuna

9. ### nagarjuna13Member

Joined:
May 28, 2010
Messages:
65
62
Trophy Points:
18
Thank you very much for taking time to explain this. I do not have live data flowing to my ami. So, I am not able to visualize this. I will work on this theoretically and let you know.

thank you
nagarjuna

Joined:
Jun 18, 2010
Messages:
111
39
Trophy Points:
28

No Mr.Nagarjuna . I ask Method of use EMA in Stsci. indicator.
Here I show example of Metastock Chart Option. Please see this Below Picture.
Here Method of moving average menu is available in Stsci. Properties Window.
I am using 39,3,3 & Selected EMA Method.
How should add this EMA option in Amibroker Stscill ? .

I got this below Example AFL in Amibroker website. Here Using Stsci. in EMA Method. Kindly see this .

***************************************************
/*StochK/StochD Crossover for A single ticker.

Enters long on stochastic indicator when EMA StochD line crosses the EMA StochK
line. Exit on reversing signals only.

Sequence: To run optimisation remove // in front of lines with optimization
instructions (the 3 lines in the praragraph below) and place // in front of line
with specific STK, STD and pds per ticker (3 lines in 2nd paragraph below). Set
range, click apply to current stock and run optimisation.
Insert the optimized figures in ( STK=, STD= and pds=) statement of 2nd
paragraph below.
Once completed replace // in front of optimization instruction lines (1st
paragraph below), remove // in front of line with specific STK, STD and pds (2nd
paragraph below), save file and click on scan, explore or backtest. These
formulas can also be used as indicator.

Each optimisation requires 729 optimisation steps.*/

/*1st paragraph.*/
STK=Optimize ("StK" , 14, 2, 18, 2); //*Remove // before STK for optimization,
replace for scan, exploration & backtesting.
STD=Optimize ("StD" ,14, 2, 18, 2); //*Remove // before STD for optimization,
replace for scan, exploration & backtesting.
pds = Optimize("pds", 9,2,18,2); //*Remove // before pds for optimization,
replace for scan, exploration & backtesting.

/*2nd paragraph.*/

//STK= 14; //*Place // in front of STK before optimization, remove for scan,
exploration & backtesting.*//
//STD= 14; //*Place // in front of STD before optimization, remove for scan,
exploration & backtesting.*//
//pds= 14; //*Place // in front of pds before optimization, remove for scan,
exploration & backtesting.*//

STK=LastValue(STK);
STD=LastValue(STD);
pds = LastValue(pds);

Sell= Cross (EMA (StochD (STD),pds),EMA( StochK (STK),pds));
Buy= Cross (EMA(StochK (STK),pds),EMA( StochD (STD),pds));
Cover = Cross (EMA(StochK (STK),pds),EMA( StochD (STD),pds));
Short = Cross (EMA (StochD (STD),pds),EMA( StochK (STK),pds));

Filter=Buy OR Sell OR Short OR Cover;
Cover=ExRem(Cover,Short);
NumColumns = 2;
Column0 = EMA( StochK (STK),pds);
Column1 = EMA (StochD (STD),pds);

Graph0 = EMA( StochK (STK),pds);
Graph1 = EMA (StochD (STD),pds);
Graph2 = (EMA( StochK (STK),pds) - EMA (StochD (STD),pds))*2;
Graph2Style = 2;
Graph2Color =1;

Title=Name()+" " +"StD% Cross StK%" + " "
+"%K="+WriteVal(Graph0,FORMAT=1.1)+", %D="+
WriteVal(Graph1,FORMAT=1.1)+
WriteIf(Graph0>Graph1," LONG position"," ")+
WriteIf(Graph1>Graph0," SHORT position","")+"; "+ " Delta" + "
"+WriteVal((Graph2/2),FORMAT=1.1)+"%";
*********************************************************

Can you help me to add this EMA Method ?