*Trading System Backtesting*

singhboy

Active Member
#1
Hello Traders, I am trying to make a Trading system for NIFTY. It has simple conditions but impressive results. I have written Half part of the System AFL. Need your help to complete AFL and run backtesting. The AFL is as follows




SetChartOptions(0,chartShowArrows|chartShowDates,colorRed);


Plot(C,"Price",IIf(C>O,colorBrightGreen,colorBrown),styleCandle);

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L,C, SelectedValue( ROC( C, 1 )) ));

_SECTION_END();



Plot(EMA(Close,200),"Fast",colorCustom11,style=styleThick|styleNoLabel);



_SECTION_BEGIN("15 mint MACD");
TimeFrameSet( in15Minute=15*60);
m = MACD(12, 26 ); // MACD from 15 minute data
s= Signal(12,26,9);
TimeFrameRestore();

m1 = TimeFrameExpand( m, in15Minute=15*60 );
s1 = TimeFrameExpand(s,in15Minute=15*60) ;


MM1= m1>s1 ;
MM2= m1<s1;

_SECTION_END();
Cond2= EMA(C,200);
Cond1= C ;


B1 = (Cond1>Cond2) ;
S1 = (Cond1<Cond2);
B2 = (MACD()>Signal()) AND MM1;
S2= (MACD()<Signal()) AND MM2;

EMAUP = Flip(B1,S1);
MACDUP = Flip(B2,S2);

BSig = EMAUP AND MACDUP;
SSig = (NOT EMAUP) AND (NOT MACDUP);

BSig = ExRem(Bsig,Ssig);
SSig = ExRem(Ssig,Bsig);



EMADOWN= Flip (S1,B1) ;
MACDDOWN = Flip (S2,B2);
Asig = EMADOWN AND MACDDOWN ;
SSSig = (NOT EMADOWN) AND (NOT MACDDOWN );
Asig = ExRem (Asig,sssig) ;
sssig = ExRem(sssig,Asig);



LongBookProfit = H> BSig+50 ;
ShortBookProfit= L<ASig-50 ;

Sell1= (C< Cond2 AND MACD()<Signal()) OR C<Bsig-25 ;
Buy1= ( C>Cond2 AND MACD() > Signal() ) OR C>Asig+25 ;

Buy = Bsig ;
Sell= Sell1 OR LongBookProfit;
Short= Asig;
Cover= Buy1 OR ShortBookProfit;

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







PlotShapes(IIf(BSig, shapeSquare, shapeNone),colorBlue, 0, L, Offset=-20);
PlotShapes(IIf(BSig, shapeSquare, shapeNone),colorBlue, 0,L, Offset=-30);
PlotShapes(IIf(BSig, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-25);
PlotShapes(IIf(ASig, shapeSquare, shapeNone),colorRed, 0, H, Offset=20);
PlotShapes(IIf(ASig, shapeSquare, shapeNone),colorOrange, 0,H, Offset=30);
PlotShapes(IIf(ASig, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-25);


_SECTION_END();


Conditions are Buy when price cross above 200 EMA, and macd is above signal line on both 5 mint and 15 mint time frame. Opposite for shorts.

Stop loss, for longs, either 25 points down the buy price, or if macd on 5 mint crosses down and price cross below 200 EMA, whichever happens 1st.
Opposite for shorts.

Profit booking for Longs, 1st LOT book at 50 points profit and book 2nd at 100 points profit.


Now there needs many modifications in my AFL.
1.) I think book profit conditions are wrong LongBookProfit = H> BSig+50
How to add 50 points to Buy price? similar with 25 pts SL condition.

2.) Suppose 200 EMA is at 5200, price cross up 5200 but macd is below
signal line on 5 and 15 mint charts. After few time price is at 5210 and
macd is above signal line on 5 mint but below Signal line on 15 mint.
Then price at 5235 and MACD is above signal line on both 5 and 15 mint
charts. So now a buy signal will appear at 5235. But I want that Buy
should trigger only within 20 pts range from 200 ema, i.e between
5200-5220. If all the conditions are fullfilled at 5235, then the system
should wait for 5220. If it reverse back to 5220 and all the conditions
for going long are still valid then BUY should trigger (note we will see
LOW of candle to see whether it comes to 20 points range from 200
EMA, we will not wait for close). And if price doesnt reverse back, it
should ignore the Buy Signal. Opposite for shorts.

3.) In the above AFL profit booking conditions are for 1 Lot only. I dont Know
How to write profit booking conditions for 2nd LOT.

4.) Suppose we are long and our SL is hit ( either 25 Pts SL or macd down on
5 mint and price<200 EMA). We will trade long again when long conditions
are met again.

5.) For trailing SL, when we book 1st Lot at 50 Pts profit and price goes 70
Pts in our favor, we will revise SL to 10 Pts below 1st Tgt ( for Longs).

6.) We will hold only half Qty for next day if price closes in our favor but
doesnt hit even 1st Tgt ( 50 pts profit). We will exit 1 Lot at close price
and will hold 2nd for next day. And on next day If hit Tgt, we will exit
100% qty i.e only 1 LOT at 1st Tgt.


So pls help me to complete this AFL and also backtesting it. BIG THANKS in Advance :thanx:
 

Similar threads