Simple Coding Help - No Promise.

Code:
RoundLotSize = 1;
MarginDeposit = 350;
TickSize = 0;

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("simple reversal");
//buy and short conditions

Cond1 = Ref ( Low, -1 ) < Ref ( Low, -2 );
Cond2 = Ref ( Close, -1 ) > Ref ( Close, -2 );
Cond3 = Ref ( Close, -1 ) > Ref ( Open , -1);
Cond4 = Ref ( High , -1 ) < Ref ( High , -2 );
Cond5 = Ref (RSI (14), -2) < 30;
Cond6 = Ref (RSI (14) , -1 ) > 30 ;
Cond7 = Ref (Low, -1 ) == Ref (LLV (Low, 8) , -1 );
Cond8 = High > Ref ( High, -1 );


Cond9 = Ref ( High, -1) > Ref (High, -2 );
Cond10 = Ref ( Close, -1 ) < Ref ( Close, -2 );
Cond11 = Ref ( Close, -1 ) < Ref ( Open , -1);
Cond12 = Ref ( Low , -1 ) > Ref ( Low, -2);
Cond13 = Ref ( RSI (14) , -2 ) > 70 ;
Cond14 = Ref ( RSI (14) , -1 ) < 70 ;
Cond15 = Low < Ref (Low, -1) ;
Cond16 = Ref (High, -1 ) == Ref ( HHV (High ,8) , -1 );
Cond17 = TimeNum() < 143100  ;//signals after 2.30 r ignored
Cond18 = TimeNum() > 093100 ; // signals bfor 9.30 r ignored
Cond19 = Close > 100 ;// shares below 100 Rs r ignored

Buy = Cond1 AND Cond2 AND Cond3 AND Cond4 AND Cond5 AND Cond6 AND Cond7 AND Cond8 AND Cond17 AND Cond18 AND Cond19;
Short = Cond9 AND Cond10 AND Cond11 AND Cond12 AND Cond13 AND Cond14 AND Cond15 AND Cond16 AND Cond17 AND Cond18 AND Cond19;

Cap		= Param("Capital", 50000, 1000, 100000, 1000);
risk	= Param("Risk", 0.1, 0.1, 100, 0.1);

BuyPrice = ValueWhen(Buy, (Ref (High ,-1)+0.05));// with this getting correct buy price
ShortPrice = ValueWhen(Short, (Ref (Low, -1)-0.05));

LongSL			= ValueWhen(Buy, Ref(Low, -1));
ShortSL		= ValueWhen(Short, Ref(High, -1));
LongPoints		= BuyPrice - LongSL;
ShortPoints	= ShortSL - ShortPrice;

PointValue = 1;
//Shares	= (Cap*risk)/LongSL;
Shares	=	IIf(Buy OR Short, IIf(Buy, (Cap*risk)/LongSL, (Cap*risk)/ShortSL), IIf(Nz(BarsSince(Buy))>Nz(BarsSince(Short)), (Cap*risk)/LongSL, (Cap*risk)/ShortSL));
shares	= floor(shares);

SetPositionSize(shares, spsShares);
Sell    = Cross(High, BuyPrice + LongPoints) OR Cross(LongSL, Low);
Cover    = Cross(ShortPrice - ShortPoints, Low) OR Cross(High, ShortSL);
SellPrice	= ValueWhen(Sell, LongSL);
CoverPrice	= ValueWhen(Cover, ShortSL);

//printf("$" + BarsSince(Buy) + "^" + BarsSince(Short) + "^" + LongSL + "^" + shares + "\n");
//printf("$" + BarsSince(Short) + "^" + BarsSince(Buy) + "^" + ShortSL + "^" + shares + "\n");

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

R = Ref (High ,-1 ) - Ref ( Low , -1);

if(Status("action") == actionExplore);

Filter = Buy | Short;

SetOption ("NoDefaultColumns", True );
AddTextColumn(Name(), "SYMBOL");
AddColumn(DateTime(), "DATE", formatDateTime);
AddColumn(IIf(Buy, 66, 83), "TRIGGER", formatChar, colorWhite, IIf(Buy, colorGreen, colorRed));
AddColumn(IIf(Buy, Ref ( High , -1), Ref ( Low, -1 )), "TRIGGER PRICE", 1.2);
AddColumn(IIf(Buy, Ref(Low ,-1), Ref(High , -1)) , "Stop Loss", 1.2);
AddColumn(IIf(Buy, Ref (High , -1) + R , Ref (Low , -1) - R ), "Target", 1.2);
//AddColumn(IIf(Buy, 420/R ,420/R ),"shares",1.2 );
AddColumn(shares,"shares",1.2 );

//to get alert via speakers and in alert output window with the text put in aphostrophies,working fine

AlertIf( Buy, "SOUND C:\\WINDOWS\\Media\\Raga.wav", "REVBUY", 1 );
AlertIf( Short, "SOUND C:\\Windows\\Media\\Raga.wav", "REVSELL", 3 );

//to get alert in email, need to correct this, not getting mail alert

AlertIf( Buy, "EMAIL", "A sample alert on "+FullName(), 1 );
AlertIf( Short, "EMAIL", "A sample alert on "+FullName(), 3 );


PlotShapes(shapeUpArrow*Buy,colorBrightGreen,0,L);
PlotShapes (shapeDownArrow*Short, colorRed, 0, H);
PlotShapes(Cover*shapeHollowUpArrow,colorGreen,0,L);
PlotShapes (Sell*shapeHollowDownArrow, colorRed, 0, H); 
_SECTION_END();
Hi,

Now with this code, excess signals are avoided. Getting clean buy,sell,cover,short signals. But still not getting any backtest results. Exploration working fine.
 

ocil

Well-Known Member
Hi Friends i am using below code and i need help. I am facing 2 issues

1) Its intraday system so it should stop calculating P/L at 3.20 PM in the display or message box.
2) If there is no call on the day it shows no call on display/message box but currently it show yesterday call and count P/L as per last call. Can anyone help?







SetChartOptions(0,chartShowArrows|chartShowDates|chartWrapTitle);
SetPositionSize(1,4);
#pragma nocache

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

_SECTION_BEGIN("TEMA");
function ZeroLagTEMA( array, period )
{
TMA1 = TEMA( array, period );
TMA2 = TEMA( TMA1, period );
Diff = TMA1 - TMA2;
return TMA1 + Diff ;
}

/////////////////////

HaClose = (O+H+L+C)/4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );

avp = Param("Up TEMA avg", 49, 1, 100 );
avpdn = Param("Dn TEMA avg", 60, 1, 100 );

// Velvoort is using not original, but modified Heikin-Ashi close
HaClose = ( HaClose + HaOpen + Max( H, HaOpen ) + Min( L, HaOpen ) )/4;

// up average
ZlHa = ZeroLagTEMA( HaClose, avp );
ZlCl = ZeroLagTEMA( ( H + L ) / 2, avp );
ZlDif = ZlCl - ZlHa;

keep1 = Hold( HaClose >= HaOpen, 2 );
keep2 = ZlDif >= 0;
keeping = keep1 OR keep2;
keepall = keeping OR ( Ref( keeping, -1 ) AND ( C > O ) OR C >= Ref( C, -1 ) );
keep3 = abs( C - O ) < ( H - L ) * 0.25 AND H >= Ref( L, -1 );
utr = keepall OR ( Ref( keepall, -1 ) AND keep3 );

// dn average
ZlHa = ZeroLagTEMA( HaClose, avpdn );
ZlCl = ZeroLagTEMA( ( H + L ) / 2, avpdn );
ZlDif = ZlCl - ZlHa;

keep1 = Hold( HaClose < HaOpen, 2 );
keep2 = ZlDif < 0;
keeping = keep1 OR keep2;
keepall = keeping OR ( Ref( keeping, -1 ) AND ( C < O ) OR C < Ref( C, -1 ) );
keep3 = abs( C - O ) < ( H - L ) * 0.25 AND L <= Ref( H, -1 );
dtr = keepall OR ( Ref( keepall, -1 ) AND keep3 );

upw = dtr == 0 AND Ref( dtr, -1 ) AND utr;
dnw = utr == 0 AND Ref( utr, -1 ) AND dtr;

/////////////////////////////////////////////////////////

CloseAtEnd = ParamToggle("Close Positions EOD", "Yes|No");
ForceCloseTradesAfter =151500;

NextBarOutsideRTH = ( Ref(TimeNum(), 1) > ForceCloseTradesAfter);

NextBarNotToday = (Ref(DateNum(), 1) > DateNum());
Buy= Cross (upw,dnw) AND TimeNum() < 150000 ;
Short=Cross(dnw,upw) AND TimeNum() < 150000 ;
Sell =IIf(CloseAtEnd==True, Short, Short OR NextBarOutsideRTH OR NextBarNotToday );

Cover=IIf(CloseAtEnd==True, Buy, Buy OR NextBarOutsideRTH OR NextBarNotToday );
Buy=ExRem(Buy,Short);

Long=Flip(Buy,Sell OR Cover);
Shrt=Flip(Sell,Buy OR Cover);
NOTrade= NOT (Long OR shrt);
BuyPrice=ValueWhen(Buy,C);
SellPrice=ValueWhen(Sell,C);
ShortPrice=ValueWhen(Short,C);
CoverPrice=ValueWhen(Cover,C);
_SECTION_END();

Sell=ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorBrightGreen, 0,Low,-15);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorYellow, 0,High,-15);

dist = 2.5*ATR(20);

for( i = 5; i < BarCount; i++ )
{
if( Buy ) PlotText( "Buy\n" + C[ i ], i, L[ i ]-dist, colorBrightGreen, colorBlack );

if( Short ) PlotText( "Sell\n" + C[ i ], i, H[ i ]+dist, colorYellow, colorBlack );

}

///////////////////////////////////////////////////////

for(i=BarCount-1;i>1;i--)
{
if(Buy == 1)
{
entry = C;
sig = "BUY";
sl = entry -50;
tar1 = entry + 85;
tar2 = entry + 130;
tar3 = entry + 78;
bars = i;
i = 0;
}
if(Short == 1)
{
sig = "Short";
entry = C;
sl = 50- entry;



bars = i;
i = 0;
}
}
Offset = 20;


printf("Last " + sig + " Signal came " + (BarCount-bars) + " bars ago");
printf("\n" + sig );
printf("\nCurrent P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2));

messageboard = 1;
if (messageboard == 1 )

{
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );

if ( sig =="BUY")
{
GfxSelectSolidBrush( colorDarkGreen ); // this is the box background color
}
else
{
GfxSelectSolidBrush( colorDarkRed ); // this is the box background color
}
pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 310;
x = 5;
x2 = 210;

y = pxHeight;

GfxSelectPen( colorGreen, 1); // broader color
GfxRoundRect( x+150, y-70, x, y , 49, 49 ) ;
GfxTextOut( (" "),27,y-20);
GfxTextOut( ("" + WriteIf(sig =="BUY",sig + " @ ",sig + " @") + " : " + entry), 15, y-60);
GfxTextOut( ("P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2)), 15, y-30);;
}
_SECTION_END();
 

Dawood

Active Member
Need help from traderji members. I need this "a" and "b" in parameter window. Minimum value is 10 and maximum is 500.

O1 = TimeFrameGetPrice( "O", inMonthly ,-12);
a=500;
b=250;

x=O1+a;
y=O1-a;
xx=O1+b;
yy=O1-b;

Thanks in advance
 
Fixed.

Code:
RoundLotSize = 1;
MarginDeposit = 350;
TickSize = 0;

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("simple reversal");
//buy and short conditions

Cond1 = Ref ( Low, -1 ) < Ref ( Low, -2 );
Cond2 = Ref ( Close, -1 ) > Ref ( Close, -2 );
Cond3 = Ref ( Close, -1 ) > Ref ( Open , -1);
Cond4 = Ref ( High , -1 ) < Ref ( High , -2 );
Cond5 = Ref (RSI (14), -2) < 30;
Cond6 = Ref (RSI (14) , -1 ) > 30 ;
Cond7 = Ref (Low, -1 ) == Ref (LLV (Low, 8) , -1 );
Cond8 = High > Ref ( High, -1 );


Cond9 = Ref ( High, -1) > Ref (High, -2 );
Cond10 = Ref ( Close, -1 ) < Ref ( Close, -2 );
Cond11 = Ref ( Close, -1 ) < Ref ( Open , -1);
Cond12 = Ref ( Low , -1 ) > Ref ( Low, -2);
Cond13 = Ref ( RSI (14) , -2 ) > 70 ;
Cond14 = Ref ( RSI (14) , -1 ) < 70 ;
Cond15 = Low < Ref (Low, -1) ;
Cond16 = Ref (High, -1 ) == Ref ( HHV (High ,8) , -1 );
Cond17 = TimeNum() < 143100  ;//signals after 2.30 r ignored
Cond18 = TimeNum() > 093100 ; // signals bfor 9.30 r ignored
Cond19 = Close > 100 ;// shares below 100 Rs r ignored

Buy = Cond1 AND Cond2 AND Cond3 AND Cond4 AND Cond5 AND Cond6 AND Cond7 AND Cond8 AND Cond17 AND Cond18 AND Cond19;
Short = Cond9 AND Cond10 AND Cond11 AND Cond12 AND Cond13 AND Cond14 AND Cond15 AND Cond16 AND Cond17 AND Cond18 AND Cond19;

Cap		= Param("Capital", 50000, 1000, 100000, 1000);
risk	= Param("Risk", 0.1, 0.1, 100, 0.1);

BuyPrice = ValueWhen(Buy, Ref (High ,-1));// with this getting correct buy price
ShortPrice = ValueWhen(Short, Ref (Low, -1));

LongSL			= ValueWhen(Buy, Ref(Low, -1));
ShortSL		= ValueWhen(Short, Ref(High, -1));
LongPoints		= BuyPrice - LongSL;
ShortPoints	= ShortSL - ShortPrice;

PointValue = 1;
//Shares	= (Cap*risk)/LongSL;
Shares	=	IIf(Buy OR Short, IIf(Buy, (Cap*risk)/LongSL, (Cap*risk)/ShortSL), IIf(Nz(BarsSince(Buy))>Nz(BarsSince(Short)), (Cap*risk)/LongSL, (Cap*risk)/ShortSL));
shares	= floor(shares);

SetPositionSize(shares, spsShares);
Sell    = Cross(High, BuyPrice + LongPoints) OR Cross(LongSL, Low);
Cover    = Cross(ShortPrice - ShortPoints, Low) OR Cross(High, ShortSL);
SellPrice	= ValueWhen(Sell, IIf(Cross(High, BuyPrice + LongPoints), (BuyPrice + LongPoints), LongSL));
CoverPrice	= ValueWhen(Cover, IIf(Cross(ShortPrice - ShortPoints, Low), (ShortPrice - ShortPoints), ShortSL));

//printf("$" + BarsSince(Buy) + "^" + BarsSince(Short) + "^" + LongSL + "^" + shares + "\n");
//printf("$" + BarsSince(Short) + "^" + BarsSince(Buy) + "^" + ShortSL + "^" + shares + "\n");

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

R = Ref (High ,-1 ) - Ref ( Low , -1);

if(Status("action") == actionExplore);

Filter = Buy | Short;

SetOption ("NoDefaultColumns", True );
AddTextColumn(Name(), "SYMBOL");
AddColumn(DateTime(), "DATE", formatDateTime);
AddColumn(IIf(Buy, 66, 83), "TRIGGER", formatChar, colorWhite, IIf(Buy, colorGreen, colorRed));
AddColumn(IIf(Buy, Ref ( High , -1), Ref ( Low, -1 )), "TRIGGER PRICE", 1.2);
AddColumn(IIf(Buy, Ref(Low ,-1), Ref(High , -1)) , "Stop Loss", 1.2);
AddColumn(IIf(Buy, Ref (High , -1) + R , Ref (Low , -1) - R ), "Target", 1.2);
//AddColumn(IIf(Buy, 420/R ,420/R ),"shares",1.2 );
AddColumn(shares,"shares",1.2 );

//to get alert via speakers and in alert output window with the text put in aphostrophies,working fine

AlertIf( Buy, "SOUND C:\\WINDOWS\\Media\\Raga.wav", "REVBUY", 1 );
AlertIf( Short, "SOUND C:\\Windows\\Media\\Raga.wav", "REVSELL", 3 );

//to get alert in email, need to correct this, not getting mail alert

AlertIf( Buy, "EMAIL", "A sample alert on "+FullName(), 1 );
AlertIf( Short, "EMAIL", "A sample alert on "+FullName(), 3 );


PlotShapes(shapeUpArrow*Buy,colorBrightGreen,0,L);
PlotShapes (shapeDownArrow*Short, colorRed, 0, H);
PlotShapes(Cover*shapeHollowUpArrow,colorGreen,0,L);
PlotShapes (Sell*shapeHollowDownArrow, colorRed, 0, H); 
_SECTION_END();
Hi,

Thank you very much for your support. :clapping:

While backtested, observed the follg:

1. It omits the trade which doesn't achieve tgt/hit sl during the same day before market close. In such case, the trade shd b closed at 3.15 pm.

2. It omits the trade which hits the tgt/sl in the same bar of entry.(tried by even checking the option "allow same bar entry/exit" in settings. But in vain)
Should we include set option (allow same bar exit) in afl?

3. Calculation of no. of shares is incorrect.

These 3 things need to be fixed.

Regards,
 
1. I didn't check it. Tell me the date, time, time frame and stock on which it happened.

2. I didn't check it. Tell me the date, time, time frame and stock on which it happened.

3. I checked it. It is correct. Maybe you need to increase your initial equity size more and set margin to maximum. I'll put the options in AFL in the next update. For instance

Code:
SYMBOL		DATE			TRIGGER	TRIGGER PRICE	Stop Loss	Target	shares
TATAMOTORS	14/03/14 1:56:00 PM	B		386.85	385.60		388.10	12.00

Shares = Cap * risk / Stop-loss = 50000 * 0.1 / 385.6 = 12.9 ~ 12 (Floor).
here is the chart for prob 1.




chart for prob2.







From the above charts, I learnt that exit is checked only from 2nd bar from the entry bar, assuming the entry bar is 0.

Thank you.

Regards,
 

Similar threads