Simple Coding Help - No Promise.

Nehal_s143

Well-Known Member
hi, i backtested 2 yrs data.i m sorry but most timeframes scenarios met with huge drawdowns . accuracy not crossing 40%. main prob is sideways not identified properly. Supertrend indicator by marketcalls.in gives better results.

or
there should be another way to identify sideways, so that we can avoid using this method in sideways period.
I would appreciate if any seniors express their views.
thanks,
regards
Simple systems like cross over will also work in trending market, main thing is how it will perform in range bound market, if system works in range bound market then its next to holy grail
 

vijkris

Learner and Follower
Hello friends,

I need help on stoploss code.

i tried applystop function but not getting results.

I need stoploss to be triggered if candle close below buy price for long and above short price.

can seniors plz guide me with afl or info link for the same.

Thanks,
Sudeep:)
hi,
applystop will work only if ur stop is at a distance from buyprice, u can trail it though.
if u want to exit when price close below buyprice, u can try below logic, but the biggest disadvantage is there is no room for movement of price and stop may hit immediately after entry.
I think u need to put better exit criteria.
just for eg.
Buy = Cross(C,MA(C,10);
BuyPrice = ValueWhen(Buy,H);// this is the buyprice
Sell = "ur target " OR Cross(BuyPrice,C);

regards
 

vijkris

Learner and Follower
i need expert advisor for the following afl

SetChartOptions(0,chartShowArrows|chartShowDates);
_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() );
//#include_once <popup.afl>

_SECTION_BEGIN("SuperTrend Ver 2 with Sound and Popup Alert");

SetBarsRequired(100000,0);

GraphXSpace = 15;

SetChartOptions(0,chartShowArrows|chartShowDates);

SetChartBkColor(ParamColor("bkcolor",ColorRGB(0,0, 0)));

GfxSetBkMode(0);

GfxSetOverlayMode(1);

SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));

Plot(C,"\nPrice",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);



//SetTradeDelays(1,1,1,1);

SetPositionSize(100,spsShares);





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



Factor=Param("Factor",3,1,10,0.1);

Pd=Param("ATR Periods",7,1,150,1);


Up=(H+L)/2+(Factor*ATR(Pd));

Dn=(H+L)/2-(Factor*ATR(Pd));

iATR=ATR(Pd);

TrendUp=TrendDown=Null;

trend[0]=1;

changeOfTrend=0;

flag=flagh=0;



for (i = 1; i <BarCount-1; i++) {

TrendUp = Null;

TrendDown = Null;



trend=1;





if (Close>Up[i-1]) {

trend=1;

if (trend[i-1] == -1) changeOfTrend = 1;



}

else if (Close<Dn[i-1]) {

trend=-1;

if (trend[i-1] == 1) changeOfTrend = 1;

}

else if (trend[i-1]==1) {

trend=1;

changeOfTrend = 0;

}

else if (trend[i-1]==-1) {

trend=-1;

changeOfTrend = 0;

}



if (trend<0 && trend[i-1]>0) {

flag=1;

}

else {

flag=0;

}



if (trend>0 && trend[i-1]<0) {

flagh=1;

}

else {

flagh=0;

}



if (trend>0 && Dn<Dn[i-1]){

Dn=Dn[i-1];

}



if (trend<0 && Up>Up[i-1])

{ Up=Up[i-1];

}



if (flag==1)

{ Up=(H+L)/2+(Factor*iATR);;

}

if (flagh==1)

{ Dn=(H+L)/2-(Factor*iATR);;

}

if (trend==1) {

TrendUp=Dn;

if (changeOfTrend == 1) {

TrendUp[i-1] = TrendDown[i-1];

changeOfTrend = 0;

}

}

else if (trend==-1) {

TrendDown=Up;

if (changeOfTrend == 1) {

TrendDown[i-1] = TrendUp[i-1];

changeOfTrend = 0;

}

}

}



Plot(TrendUp,"Trend",colorGreen);

Plot(TrendDown,"Down",colorRed);



Buy = trend==1;

Sell=trend==-1;



Buy=ExRem(Buy,Sell);

Sell=ExRem(Sell,Buy);

Short=Sell;

Cover=Buy;





BuyPrice=ValueWhen(Buy,C);

SellPrice=ValueWhen(Sell,C);

ShortPrice=ValueWhen(Short,C);

CoverPrice=ValueWhen(Cover,C);


hi, pls be specific abt ur doubts...
I am sure this code is incomplete, no commands for buy/sell arrow, popup etc all are missing. pls download full code properly.
 
need help in editing this afl
its working but it shows wrong buy signal at start of new day need to avoid that wrong signal plz help

Code:
SetPositionSize( 2000, spsShares ); // 2000 shares by default 
_SECTION_BEGIN("levels");

DayH = TimeFrameGetPrice("H", inDaily, -1);		// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1);		//				low
DayC = TimeFrameGetPrice("C", inDaily, -1);		//				close
DayO = TimeFrameGetPrice("O", inDaily,-1);			// current day open

if ( True )
{
R = DayH - DayL;      
S3 = DayC-(R * 1.1/4);
R4 = (R * 1.1/2)+DayC;
R3 = (R * 1.1/4)+DayC;
S4 = DayC- (R * 1.1/2);
}

Plot(R4, "",colorGreen,styleStaircase);
Plot(S3, "",colorGreen,styleStaircase);
Plot(S4, "",colorRed,styleStaircase);
Plot(R3, "",colorRed,styleStaircase);

//----
Title = Name()
;
Filter=1;
AddColumn(C,"cmp",1.2);
AddColumn(R3,"R3",1.2);
AddColumn(R4,"R4",1.2);
AddColumn(S3,"S3",1.2);
AddColumn(S4,"S4",1.2);


_SECTION_END();

Buy1 = Cross (C, R4);
Buy2 = Cross (C, S3);
Sell1 = Cross (R3 ,C);
Sell2 = Cross (S4 ,C);

Buy= Buy1 OR Buy2;
Sell= sell1 OR sell2;



 PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-10);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-20);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-15); 
 PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=10);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=20);                      
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-15);
 


SetChartOptions(0,chartShowArrows|chartShowDates);
_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", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
looks like problem is having two buy sell condition which cross over with yesterday buy sell condition and showing wrong buy sell signal at start of newday, help me to solve it plz
 
Last edited:

vijkris

Learner and Follower
need help in editing this afl
its working but it shows wrong buy signal at start of new day need to avoid that wrong signal plz help

Code:
SetPositionSize( 2000, spsShares ); // 2000 shares by default 
_SECTION_BEGIN("levels");

DayH = TimeFrameGetPrice("H", inDaily, -1);		// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1);		//				low
DayC = TimeFrameGetPrice("C", inDaily, -1);		//				close
DayO = TimeFrameGetPrice("O", inDaily,-1);			// current day open

if ( True )
{
R = DayH - DayL;      
S3 = DayC-(R * 1.1/4);
R4 = (R * 1.1/2)+DayC;
R3 = (R * 1.1/4)+DayC;
S4 = DayC- (R * 1.1/2);
}

Plot(R4, "",colorGreen,styleStaircase);
Plot(S3, "",colorGreen,styleStaircase);
Plot(S4, "",colorRed,styleStaircase);
Plot(R3, "",colorRed,styleStaircase);

//----
Title = Name()
;
Filter=1;
AddColumn(C,"cmp",1.2);
AddColumn(R3,"R3",1.2);
AddColumn(R4,"R4",1.2);
AddColumn(S3,"S3",1.2);
AddColumn(S4,"S4",1.2);


_SECTION_END();

Buy1 = Cross (C, R4);
Buy2 = Cross (C, S3);
Sell1 = Cross (R3 ,C);
Sell2 = Cross (S4 ,C);

Buy= Buy1 OR Buy2;
Sell= sell1 OR sell2;



 PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-10);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-20);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-15); 
 PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=10);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=20);                      
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-15);
 


SetChartOptions(0,chartShowArrows|chartShowDates);
_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", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
looks like problem is having two buy sell condition which cross over with yesterday buy sell condition and showing wrong buy sell signal at start of newday, help me to solve it plz
hi, the code looks ok. it is showing correct signals, i.e opening bar signal is not considering yesterday's value.
regards
 
no its should show buy signal only when it cross greenline, why its showing buy signal in middle of greenline and redline ??

this problem occurs only at start of first candle bar in newday.
plz help
 
then its not yesderday value problem ?
problem in Buy2 = Cross (C, S3); ??
bcoz it should show buy2 signal only when it crosses bottom buysignal but its showing in middle of buy1 and sell2line why?
i just want formula for
show buy signal only when price touch certain price not like when it crosses certain price like that?
thanks sir for trying to help reply plz
 
Last edited:

vijkris

Learner and Follower
then its not yesderday value problem ?
problem in Buy2 = Cross (C, S3); ??
bcoz it should show buy2 signal only when it crosses bottom buysignal but its showing in middle of buy1 and sell2line why?
i just want formula for
show buy signal only when price touch certain price not like when it crosses certain price like that?
thanks sir for trying to help reply plz
hi, I have modified the code to ignore the first bar of the day. use parameter to adjust opening time. as a default I included 09:15 as opening time. for mcx change it to'"100000". pls check whether ur criteria is met or not.
regards.:thumb:

Code:
SetChartOptions(0,chartShowArrows|chartShowDates);
_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", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

SetPositionSize( 2000, spsShares ); // 2000 shares by default 
_SECTION_BEGIN("levels");

breakoutime = Param("open time",091500,091500,230000,1,0);// for nse 100000,
Filterdaych = ParamToggle("Filter day Change Connections", "No|Yes", 1) ;
dt = DateTime() ;
//mytimenum = DateTimeConvert(2,dt,TimeNum());
    //newt = mytimenum != 100000;

	dtnum = DateTimeConvert(2, dt,TimeNum()) ;
	newdt = dtnum != Ref(dtnum, -1);
    //mytimenum = DateTimeConvert(1,dt);
   // newt = mytimenum == 100000;

	Lastbar = H-H ;
	if (Filterdaych)
		Lastbar = dtnum != Ref(dtnum, 1) AND TimeNum()<=breakoutime; ;
strnday = "day";
afterbreakout0 = Cross(TimeNum(),breakoutime);
afterbreakout1 = TimeNum()>=breakoutime ;// TimeNum()>=100000;
NewDay = Day()!= Ref(Day(), -1);



DayH = TimeFrameGetPrice("H", inDaily, -1);		// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1);		//				low
DayC = TimeFrameGetPrice("C", inDaily, -1);		//				close
DayO = TimeFrameGetPrice("O", inDaily,-1);			// current day open

if ( True )
{
R = DayH - DayL;      
S3 = DayC-(R * 1.1/4);
R4 = (R * 1.1/2)+DayC;
R3 = (R * 1.1/4)+DayC;
S4 = DayC- (R * 1.1/2);
}

Plot(IIf(lastbar,Null,R3), "r3",colorRed,styleStaircase);
Plot(IIf(lastbar,Null,R4), "r4",colorGreen,styleStaircase);
Plot(IIf(lastbar,Null,S3), "s3",colorPaleGreen,styleStaircase);
Plot(IIf(lastbar,Null,S4), "s4",colorOrange,styleStaircase);


//----
//Title = Name()
;
Filter=1;
AddColumn(C,"cmp",1.2);
AddColumn(R3,"R3",1.2);
AddColumn(R4,"R4",1.2);
AddColumn(S3,"S3",1.2);
AddColumn(S4,"S4",1.2);


_SECTION_END();

Buy1 = Cross (C, R4);
Buy2 = Cross (C, S3) ;
Sell1 = Cross (R3 ,C);
Sell2 = Cross (S4 ,C);

Buy= ( Buy1 OR Buy2)AND TimeNum()>breakoutime;
Sell=  (sell1 OR sell2) AND TimeNum()>breakoutime;



 PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-10);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-20);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-15); 
 PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=10);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=20);                      
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-15);
 
thanks a lot
but it also ignore correct signals if comes in first bar ?
i used in amibroker only
but it still shows wrong buy signal at 9.15 start first bar
i know how to ignore first 5mins 9.15to9.20 signals but it also ingnores correct buy signal in that time
tn = TimeNum();
startTime = 092000; // start in HHMMSS format
endTime = 143000; // end in HHMMSS format
lastTime = 151500;
timeOK = tn >= startTime AND tn <= endTime;
Buy= (Buy1 OR Buy2) AND timeOK;
i use this formula to ignore first 5mins 9.15to9.20 signals but it also ingnores correct buy signal in that time
is there any other way plz??
 
Last edited:

Similar threads