System based on TEMA slope

sr114

Well-Known Member
#1
System based on slope of tema
suitable for intraday (tf - 1 min and above)

1. Green up arrow - fresh buy signal, golden hollow down arrow - sell signal for the buy.

2. Red down arrow - fresh short signal , aqua hollow up arrow - cover signal for the short.

3. stop loss - default is 0.5% of the signal price , set it according to the price of the script

4. profit taker = default is 0.5%, set it up as the risk appetite

5. the stop loss is shaped as star and profit exit is shaped as hollow arrows and are plotted on the charts

6. the system starts from 0915 hrs and is totally exited or all position covered at 1510 hrs
(better to start from 0916 hrs to 1509 hrs to be on the safe side and for utilizing the MIS option of all the brokers)

_SECTION_BEGIN("Background_Setting");
SetChartBkGradientFill( ParamColor("BgTop", colorBlack),
ParamColor("BgBottom", colorDarkGrey),ParamColor("TitleBack",colorBlack)) ;
SetChartBkColor(ParamColor("Outer Panel",colorPaleBlue));
SetChartOptions(0,chartShowArrows|chartShowDates);
_SECTION_END();

_SECTION_BEGIN("Candle color");
upbar= H > Ref(H,-1) AND L > Ref(L,-1);
dnbar= H < Ref(H,-1) AND L < Ref(L,-1);

SetBarFillColor(IIf(upbar,ParamColor("Candle UP Color", colorGreen),IIf(dnbar,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
Plot(C,"",IIf(upbar,ParamColor("UP Color", colorDarkGreen),IIf(dnbar,ParamColor("Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);

//grid_day = IIf(Day()!=Ref(Day(),-1),1,0);
grid_day = Day()!=Ref(Day(),-1);
Plot(grid_day,"",colorGrey50,styleHistogram|styledots|styleNoLabel|styleOwnScale);
_SECTION_END();

_SECTION_BEGIN("Breakout Setting");
beginTradeTime= ParamTime( "No trade B4 ", "091500" );
endTradeTime= Param("End Time",151000,151000,233000,10);

EndDay = ParamTime( "No trade after ", "15:09:00" );
EndDay2 = ParamTime( "close position ", "15:09:00" );

tn = TimeNum();
dn = DateNum();
Exit = Ref( tn < EndDay2, -1 ) AND ( tn >= EndDay2 OR dn < Ref( dn, 1 ) );

Buy = Sell = Short = Cover = 0;

fact=U=Param("Main Curve Factor ",100,10,2000,1);
//fact2=P=Param("Support Curve Factor..0-100",10,0,100,1);

//ST=(P*U/100);
//tMONITOR= TEMA(C,10);
tslow=TEMA(TEMA(Close,10),10);

Slptslow = (1000000*((tslow- Ref(tslow, -1))/tslow));
//slptmonitor= (1000000*((tmonitor- Ref(tmonitor, -1))/tmonitor));

//Plot (tmonitor, "", colorGold, ParamStyle("Style"));
//Plot (tslow,"", colorWhite,ParamStyle(" Slope Style"));

Buy = Sell = Short = Cover = 0;

b1= slptslow > fact AND Tn < EndDay AND Tn > beginTradeTime;
s1= slptslow <-fact AND Tn < EndDay AND Tn > beginTradeTime;

Buy=b1;
Short=s1;

Buysetup=Flip(b1,s1);
Shortsetup=Flip(s1,b1);

Sell=Shortsetup OR exit;
Cover=Buysetup OR exit;

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

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

BuyPrice1=Max(Ref(C,-1),O)+0.5;// 0.5 - for better fill
ShortPrice1=Min(Ref(C,-1),O)+0.5;

sellPrice1=Min(Ref(C,-1),O)+0.5;
CoverPrice1=Max(Ref(C,-1),O)+0.5;

BuyPrice = ValueWhen(Buy,BuyPrice1); bp=NumToStr(BuyPrice,1.2);
ShortPrice = ValueWhen(Short,shortPrice1); shp=NumToStr(ShortPrice,1.2);
SellPrice = ValueWhen(Sell,sellPrice1);
CoverPrice = ValueWhen(Cover,coverPrice1);

PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), 43, layer = 0,yposition = Low, offset = -20);
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, layer = 0, yposition = High, offset = -20); //shapeSmallCircle

//PlotShapes( IIf(Cover, shapeHollowUpArrow,shapeNone), colorAqua, layer = 0,yposition = Low, offset = 30);
//PlotShapes( IIf(Sell,shapeHollowDownArrow,shapeNone), colorPink, layer = 0, yposition = High, offset = -30);

StopLevel = Param( "Stop Loss (%)", 0.5, 0.1, 10, 0.1 );
TgtLevel = Param( "Profit target (%)", 0.5, 0.1, 10, 0.1 );

ApplyStop( stopTypeLoss, stopModePercent, StopLevel, True );
ApplyStop( stopTypeProfit, stopModePercent, TgtLevel, True );

Equity( 1, 0 );

sellcond1=Sell==1; covercond1=Cover==1;
sellcond2=Sell==2; covercond2=Cover==2;
sellcond3=Sell==3; covercond3=Cover==3;


//PlotShapes(IIf(sellcond1,shapeSmallDownTriangle,sh apeNone),colorOrange,0,H,-25);
PlotShapes(IIf(sellcond3,shapeHollowDownArrow,shapeNone),colorOrange,0,H,-15);
PlotShapes(IIf(sellcond2,shapeStar,shapeNone),colorOrange,0,H,-15);

//PlotShapes(IIf(covercond1,shapeSmallUpTriangle,sha peNone),colorLime,0,L,-25);
PlotShapes(IIf(covercond3,shapeHollowUpArrow,shapeNone),colorAqua,0,L,-25);
PlotShapes(IIf(covercond2,shapeStar,shapeNone),colorAqua,0,L,-25);

WriteIf( Sell == 1, "Regular exit",WriteIf( Sell == 3, "Profit Tgt",WriteIf( Sell == 2, "Max stop", "" ) ));
WriteIf( Cover == 1, "Regular exit",WriteIf( Cover == 3, "Profit Tgt",WriteIf( Cover == 2, "Max stop", "" ) ));

GraphXSpace=10;

_SECTION_END();

_SECTION_BEGIN("Profit calc");

PositionOpen = IIf(BarsSince(Buy==1) < BarsSince(Short==1),1,-1); // using 1 for Long and -1 for short
BarsOpen = IIf(PositionOpen==1,BarsSince(Buy==1),IIf(PositionOpen==-1,BarsSince(Short==1),0));
EntryPrice = Ref(Close,-BarsOpen);
MaxOpenProfit = IIf(BarsOpen==0,0,IIf(PositionOpen==1, HHV(H,BarsOpen) - EntryPrice, IIf(PositionOpen==-1, EntryPrice - LLV(L,BarsOpen),0)));
OpenProfit = IIf(BarsOpen==0,0,IIf(PositionOpen==1, C - EntryPrice, IIf(PositionOpen==-1, EntryPrice - C,0)));

Long = Flip(Buy,Short);
shrt = Flip(Short,Buy);
_SECTION_END();

_SECTION_BEGIN("hi lo daily ");
Today = LastValue(Day());
HiDay = TimeFrameGetPrice("H", inDaily);
LoDay = TimeFrameGetPrice("L", inDaily);

Xh = HHV(H,BarsSince(Buy));
xl = LLV(L,BarsSince(Short));
_SECTION_END();

_SECTION_BEGIN("msg brd");

{SetChartOptions(0,chartShowArrows|chartShowDates) ;
GfxSelectFont( "Tahoma", 10, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite);
GfxSelectSolidBrush( colorDarkGrey );
pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 240;
x = 15;
x2 = 240;
y = pxHeight;

GfxSelectPen( colorLightBlue, 1);
GfxRoundRect( x, y - 195, x2, y , 7, 7 ) ;

GfxTextOut( "TS V 1.0 ", 25, y-185);
GfxTextOut("----------------------------------------", 25, y-165);
GfxTextOut( Name(), 25, y-150);
GfxTextOut( Date(), 25, y-130);
GfxTextOut("Close : " + NumToStr((C),1.2) , 25, y-110);
GfxTextOut("Hi : " + NumToStr(Hiday,1.2), 25, y-90);
GfxTextOut("Lo : " + NumToStr(Loday,1.2), 135, y-90);
GfxTextOut( ("" + WriteIf (Long AND NOT Buy, "Hi frm Sig : "+NumToStr((xh),1.2) + "","")), 25, y-70);
GfxTextOut( ("" + WriteIf (Shrt AND NOT Sell, "Lo frm Sig : "+NumToStr((xl),1.2)+ "","")), 25, y-70);
GfxTextOut("----------------------------------------", 25, y-55);

GfxTextOut( ("" + WriteIf (Long AND NOT Buy, "Long at : "+(BuyPrice) + "","")), 25, y-40);
GfxTextOut( ("" + WriteIf (Shrt AND NOT Sell, "Short at : "+(ShortPrice) + "","")), 25, y-40);

GfxTextOut("P/L : " + NumToStr((OpenProfit ),1.2) , 25, y-20);
GfxTextOut("Max P/L : " + NumToStr((MaxOpenProfit ),1.2) , 120, y-20);

}
_SECTION_END();
screen shots [ bank nifty - 3 min]



want the feed back from u guys - anything and also if there is some improvement pls do it and post the code

Concept of Sandip1952 , thanx for the concept- but the credit lies with the author or not?

rgds
 
Last edited:

sr114

Well-Known Member
#3
hi sr114

this was sandip1952's concept . however u shud have mentioned his name .

he deserves the credit for the idea . and for the author - any credit for him/her??
added that credit and thanx - but what abut the author? can he claim the credit?

so can i post it now - or i remove the post?

why i posted it - may be this afl needs some refinement. may be - so to get the feedback

so can i do this?
 
Last edited:

xsis

Active Member
#4
its shows mny syntax errors.
cud u pls fix them up as i cant test the system.

also, in past i have used similar systems where my exit (profit taking and stoploss) were ATR driven and not fixed as 0.5%. cud u use ATR instead? or shud i ask why fixed as 0.5%?
 

sr114

Well-Known Member
#5
corrected codes [ some blank spaces r there= removed all]

Code:
_SECTION_BEGIN("Background_Setting");
 SetChartBkGradientFill( ParamColor("BgTop", colorBlack),
 ParamColor("BgBottom", colorDarkGrey),ParamColor("TitleBack",colorBlack)) ; 
 SetChartBkColor(ParamColor("Outer Panel",colorPaleBlue));
 SetChartOptions(0,chartShowArrows|chartShowDates);
 _SECTION_END();

 _SECTION_BEGIN("Candle color");
 upbar= H > Ref(H,-1) AND L > Ref(L,-1);
 dnbar= H < Ref(H,-1) AND L < Ref(L,-1);

 SetBarFillColor(IIf(upbar,ParamColor("Candle UP Color", colorGreen),IIf(dnbar,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
 Plot(C,"",IIf(upbar,ParamColor("UP Color", colorDarkGreen),IIf(dnbar,ParamColor("Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);

 //grid_day = IIf(Day()!=Ref(Day(),-1),1,0); 
 grid_day = Day()!=Ref(Day(),-1); 
 Plot(grid_day,"",colorGrey50,styleHistogram|styleDots|styleNoLabel|styleOwnScale);
 _SECTION_END();

 _SECTION_BEGIN("Breakout Setting");
 beginTradeTime= ParamTime( "No trade B4 ", "091500" );
 endTradeTime= Param("End Time",151000,151000,233000,10);

 EndDay = ParamTime( "No trade after ", "15:09:00" );
 EndDay2 = ParamTime( "close position ", "15:09:00" );

 tn = TimeNum();
 dn = DateNum();
 Exit = Ref( tn < EndDay2, -1 ) AND ( tn >= EndDay2 OR dn < Ref( dn, 1 ) );

 Buy = Sell = Short = Cover = 0; 

 fact=U=Param("Main Curve Factor ",100,10,2000,1);
 //fact2=P=Param("Support Curve Factor..0-100",10,0,100,1);

 //ST=(P*U/100);
 //tMONITOR= TEMA(C,10); 
 tslow=TEMA(TEMA(Close,10),10); 

 Slptslow = (1000000*((tslow- Ref(tslow, -1))/tslow));
 //slptmonitor= (1000000*((tmonitor- Ref(tmonitor, -1))/tmonitor));

 //Plot (tmonitor, "", colorGold, ParamStyle("Style"));
 //Plot (tslow,"", colorWhite,ParamStyle(" Slope Style"));

 Buy = Sell = Short = Cover = 0; 

 b1= slptslow > fact AND Tn < EndDay AND Tn > beginTradeTime; 
 s1= slptslow <-fact AND Tn < EndDay AND Tn > beginTradeTime; 

 Buy=b1; 
 Short=s1;

 Buysetup=Flip(b1,s1);
 Shortsetup=Flip(s1,b1);

 Sell=Shortsetup OR exit; 
 Cover=Buysetup OR exit; 

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

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

 BuyPrice1=Max(Ref(C,-1),O)+0.5;// 0.5 - for better fill
 ShortPrice1=Min(Ref(C,-1),O)+0.5;

 sellPrice1=Min(Ref(C,-1),O)+0.5;
 CoverPrice1=Max(Ref(C,-1),O)+0.5;

 BuyPrice = ValueWhen(Buy,BuyPrice1); bp=NumToStr(BuyPrice,1.2);
 ShortPrice = ValueWhen(Short,shortPrice1); shp=NumToStr(ShortPrice,1.2);
 SellPrice = ValueWhen(Sell,sellPrice1);
 CoverPrice = ValueWhen(Cover,coverPrice1);

 PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), 43, layer = 0,yposition = Low, offset = -20);
 PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, layer = 0, yposition = High, offset = -20); //shapeSmallCircle

 //PlotShapes( IIf(Cover, shapeHollowUpArrow,shapeNone), colorAqua, layer = 0,yposition = Low, offset = 30);
 //PlotShapes( IIf(Sell,shapeHollowDownArrow,shapeNone), colorPink, layer = 0, yposition = High, offset = -30); 

 StopLevel = Param( "Stop Loss (%)", 0.5, 0.1, 10, 0.1 );
 TgtLevel = Param( "Profit target (%)", 0.5, 0.1, 10, 0.1 );

 ApplyStop( stopTypeLoss, stopModePercent, StopLevel, True );
 ApplyStop( stopTypeProfit, stopModePercent, TgtLevel, True );

 Equity( 1, 0 ); 

 sellcond1=Sell==1; covercond1=Cover==1; 
 sellcond2=Sell==2; covercond2=Cover==2; 
 sellcond3=Sell==3; covercond3=Cover==3; 


 //PlotShapes(IIf(sellcond1,shapeSmallDownTriangle,sh apeNone),colorOrange,0,H,-25);
 PlotShapes(IIf(sellcond3,shapeHollowDownArrow,shapeNone),colorOrange,0,H,-15);
 PlotShapes(IIf(sellcond2,shapeStar,shapeNone),colorOrange,0,H,-15);

 //PlotShapes(IIf(covercond1,shapeSmallUpTriangle,sha peNone),colorLime,0,L,-25);
 PlotShapes(IIf(covercond3,shapeHollowUpArrow,shapeNone),colorAqua,0,L,-25);
 PlotShapes(IIf(covercond2,shapeStar,shapeNone),colorAqua,0,L,-25);

 WriteIf( Sell == 1, "Regular exit",WriteIf( Sell == 3, "Profit Tgt",WriteIf( Sell == 2, "Max stop", "" ) ));
 WriteIf( Cover == 1, "Regular exit",WriteIf( Cover == 3, "Profit Tgt",WriteIf( Cover == 2, "Max stop", "" ) )); 

 GraphXSpace=10;

 _SECTION_END();

 _SECTION_BEGIN("Profit calc");

 PositionOpen = IIf(BarsSince(Buy==1) < BarsSince(Short==1),1,-1); // using 1 for Long and -1 for short
 BarsOpen = IIf(PositionOpen==1,BarsSince(Buy==1),IIf(PositionOpen==-1,BarsSince(Short==1),0));
 EntryPrice = Ref(Close,-BarsOpen);
 MaxOpenProfit = IIf(BarsOpen==0,0,IIf(PositionOpen==1, HHV(H,BarsOpen) - EntryPrice, IIf(PositionOpen==-1, EntryPrice - LLV(L,BarsOpen),0)));
 OpenProfit = IIf(BarsOpen==0,0,IIf(PositionOpen==1, C - EntryPrice, IIf(PositionOpen==-1, EntryPrice - C,0)));

 Long = Flip(Buy,Short);
 shrt = Flip(Short,Buy);
 _SECTION_END();

 _SECTION_BEGIN("hi lo daily ");
 Today = LastValue(Day());
 HiDay = TimeFrameGetPrice("H", inDaily);
 LoDay = TimeFrameGetPrice("L", inDaily);

 Xh = HHV(H,BarsSince(Buy)); 
 xl = LLV(L,BarsSince(Short)); 
 _SECTION_END();

 _SECTION_BEGIN("msg brd");

 {SetChartOptions(0,chartShowArrows|chartShowDates) ;
 GfxSelectFont( "Tahoma", 10, 100 );
 GfxSetBkMode( 1 );
 GfxSetTextColor( colorWhite);
 GfxSelectSolidBrush( colorDarkGrey );
 pxHeight = Status( "pxchartheight" ) ;
 xx = Status( "pxchartwidth");
 Left = 1100;
 width = 240;
 x = 15;
 x2 = 240; 
 y = pxHeight;

 GfxSelectPen( colorLightBlue, 1);
 GfxRoundRect( x, y - 195, x2, y , 7, 7 ) ;

 GfxTextOut( "TS V 1.0 ", 25, y-185);
 GfxTextOut("----------------------------------------", 25, y-165);
 GfxTextOut( Name(), 25, y-150);
 GfxTextOut( Date(), 25, y-130);
 GfxTextOut("Close : " + NumToStr((C),1.2) , 25, y-110);
 GfxTextOut("Hi : " + NumToStr(Hiday,1.2), 25, y-90);
 GfxTextOut("Lo : " + NumToStr(Loday,1.2), 135, y-90);
 GfxTextOut( ("" + WriteIf (Long AND NOT Buy, "Hi frm Sig : "+NumToStr((xh),1.2) + "","")), 25, y-70);
 GfxTextOut( ("" + WriteIf (Shrt AND NOT Sell, "Lo frm Sig : "+NumToStr((xl),1.2)+ "","")), 25, y-70);
 GfxTextOut("----------------------------------------", 25, y-55);

 GfxTextOut( ("" + WriteIf (Long AND NOT Buy, "Long at : "+(BuyPrice) + "","")), 25, y-40);
 GfxTextOut( ("" + WriteIf (Shrt AND NOT Sell, "Short at : "+(ShortPrice) + "","")), 25, y-40);

 GfxTextOut("P/L : " + NumToStr((OpenProfit ),1.2) , 25, y-20);
 GfxTextOut("Max P/L : " + NumToStr((MaxOpenProfit ),1.2) , 120, y-20);

 }
 _SECTION_END();
 

sr114

Well-Known Member
#6
its shows mny syntax errors.
cud u pls fix them up as i cant test the system.

also, in past i have used similar systems where my exit (profit taking and stoploss) were ATR driven and not fixed as 0.5%. cud u use ATR instead? or shud i ask why fixed as 0.5%?
stoploss - fixed as 0.5% - means u know from the trade how much u r going to loose in that trade if the price reverses abruptly. so u can set it as per ur risk bearing capacity.

for the profit target - suggest some better alternative to capture most of the profit the trade will generate.

rgds
 

sr114

Well-Known Member
#8
hi sr114

this was sandip1952's concept . however u shud have mentioned his name .

he deserves the credit for the idea .
STalwar

the concept of slope of any MA means the rate of change of the MA. this concept is around 5-6 years and some work was done in this respect. search in amibroker group in yahoo.com and u will find it.
 
#9
I do not see how this system can work because to create a buy or short signal you use: tslow=TEMA(TEMA(Close,10),10);

so the earliest this signal can be valid is at the close price of the bar. Yet the code uses the open price +/- 0.5 or yesterdays close price as the entry price. It is just not possible. Earliest you can enter is at the open of the next bar. Try to backtest that and you see what I mean.
 

XRAY27

Well-Known Member
#10
I do not see how this system can work because to create a buy or short signal you use: tslow=TEMA(TEMA(Close,10),10);

so the earliest this signal can be valid is at the close price of the bar. Yet the code uses the open price +/- 0.5 or yesterdays close price as the entry price. It is just not possible. Earliest you can enter is at the open of the next bar. Try to backtest that and you see what I mean.
yes i agree with you