Simple Coding Help - No Promise.

I guess you'll have to avoid 30min TF and higher.

I guess you hv added one more cond like there cn be some diff in open = low and open = high. ex: open = 420 and low = 419 .

right? but the "close " in coverprice shows the value as 3.30 close and not as 3.20 close.

The confusion reg the tf of scanning still persist.
 
PHP:
_SECTION_BEGIN("abhig");
EMA1		= EMA(Close, 1000);
EMA2		= EMA(Close, 2000);
threshP	= Param("Min Profit trail", 1.005, 0.001, 2, 0.001);
tslfactor	= Param("Tsl Factor", 0.8, 0.01, 1, 0.01); 
slfactor =Param ("SL factor", 0.998,0.90,0.001,0);
Buy=Short=Sell=Cover=0;
Plot(EMA1, "EMA1", colorGreen, styleLine);
Plot(EMA2, "EMA2", colorRed, styleLine);

Buy		= Cross(EMA1, EMA2);
Short	= Cross(EMA2, EMA1);

BuyPrice	= ValueWhen(Buy, Close);
ShortPrice	= ValueWhen(Short, Close);

tLongProfit	= (BuyPrice * (threshP));
tShortProfit	= (ShortPrice * (2-threshP));

maxLong	= HighestSince(Buy, High);
maxShort	= LowestSince(Short, Low);

LossLong = LowestSince (Buy, Low);
LossShort = HighestSince (Short, High);

Longstoploss= (BuyPrice* slfactor);
Shortstoploss= (ShortPrice * (2-slfactor));


tslLong		= tLongProfit + abs(maxLong - tLongProfit) * tslfactor;
tslShort		= tShortProfit - abs(maxShort - tShortProfit) * tslfactor;

Sell	= Short OR IIf (losslong<longstoploss, Cross (Close,losslong),0) OR IIf(maxLong>tLongProfit, Cross(tslLong, Close),0);
Cover	= Buy OR IIf (lossShort>shortstoploss, Cross (lossshort,Close),0) OR IIf(maxShort<tShortProfit, Cross(Close, tslShort), 0);

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



_SECTION_BEGIN("Magified Market Price");

FS=Param("Font Size",30,11,100,1);
Hor=Param("Horizontal Position",940,1,1200,1);
Ver=Param("Vertical Position",12,1,830,1); 

YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);

GfxSetBkMode( 1 ); 

GfxSelectFont("Times New Roman", FS, 700, True ); 
-GfxSetTextColor( colorLime ); 
GfxTextOut(""+C, Hor+200 , Ver );

/*
GfxSelectFont("Times New Roman", 11, 700, True ); 
GfxSetTextColor(colorYellow ); 
GfxTextOut(""+DD+" ("+xx+"%)", Hor+200 , Ver+65 );
*/
_SECTION_END();







PlotShapes( IIf(Buy, shapeUpArrow, shapeNone ), colorBrightGreen, layer = 0,yposition = Low, offset = -10); 
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorPink, layer = 0, yposition = High, offset = -10); 
PlotShapes( IIf(Cover, shapeStar,shapeNone),  colorBrightGreen, layer = 0,yposition = Low, offset = -20);
PlotShapes( IIf(Sell,shapeStar,shapeNone),  colorPink, layer = 0, yposition = High, offset = 20); 


_SECTION_END();

_SECTION_BEGIN("Price1");
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() ); 
_SECTION_END();
Abhig I tried to add the stop loss code and have slowed down the EMAs for better readability.. Doesn't seem to work. Can I have your help again on this?
 
Last edited:

amitrandive

Well-Known Member
You can ask amitrandive or google it. I saw the code on amibroker site once but do not remember how I got there or the link.
Here is the code, but be warned .It looks to the future as it is based on Zig-zag function.

Code:
_SECTION_BEGIN("ZigZag");
P = Param("ZigZag_Percent",0.1,0.01,2,0.01);
ZigH  = Zig(H,P);		ZigL = Zig(L,P);
ZigHi = ZigH > Ref(ZigH,-1) AND ZigH > Ref(ZigH,1);		
ZigLo = ZigL < Ref(ZigL,-1) AND ZigL < Ref(ZigL,1);
j=0;	k=0;	PH[0] = PL[0] = 0;
for( i = 1; i < BarCount-1; i++ ) { 
 if (ZigLo[i])	{
	if (PL[k] > ZigL[i])  	PlotText("LL",i, L[i]-5, colorGreen);
	else						PlotText("HL",i, L[i]-5, colorGreen);
	k = k + 1;		PL[k] = ZigL[i]; }
 if (ZigHi[i]) {
	if (PH[j] < ZigH[i])  	PlotText("HH", i, H[i]+5, colorRed);
	else						PlotText("LH", i, H[i]+5, colorRed);
	j = j + 1;
	PH[j] = ZigH[i]; }
}
_SECTION_END();

_SECTION_BEGIN("Price");
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() ); 
_SECTION_END();
Incidentally , this code is present this thread

http://www.traderji.com/amibroker/90119-simple-coding-help-no-promise-23.html#post885515
 
Last edited:
@abhig

Your help has raised the hopes in being able to auto trade from tomorrow. I added the stop loss code, a simple IIf function where buyprice * slfactor is used to find SL level. But it doesn't work, and most peculiarly, the TSL levels are now disturbed, exiting too late leading to lower profits or loss. Changing the Slfactor affects the TSL exit. I just went through your previous posts re. SL/TSL but couldn't find a solution.

You must help here as well :)
 
I wasn't talking about the code, but 'quote'. Just quote the last post every time so it gets easy to track.

Cross function works in a mysterious way. The correct way is, Cross(Up Vector, Down Vector) which is how it works. You used Cross (Close,losslong) in the stop loss code, it will work when the Close crosses losslong from down towards upside not from upside to downside which is wrong. A stop loss is hit in long position when price moves from upside of the stop loss to downside of it, not the other way round. Also I don't think you need to use the IIF statement for stop loss. It should work for itself.

Code:
_SECTION_BEGIN("Price");
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() ); 
_SECTION_END();  


_SECTION_BEGIN("abhig");
EMA1			= EMA(Close, 5);
EMA2			= EMA(Close, 20);
threshP		= Param("Min Profit trail", 1.005, 0.001, 2, 0.001);
tslfactor		= Param("Tsl Factor", 0.8, 0.01, 1, 0.01); 
slfactor		= Param("SL factor", 0.998, 0.80, 1, .001);

Plot(EMA1, "EMA1", colorGreen, styleLine);
Plot(EMA2, "EMA2", colorRed, styleLine);

Buy			= Cross(EMA1, EMA2);
Short		= Cross(EMA2, EMA1);

BuyPrice		= ValueWhen(Buy, Close);
ShortPrice		= ValueWhen(Short, Close);

// threshold profit
tLongProfit	= (BuyPrice * (threshP));
tShortProfit	= (ShortPrice * (2-threshP));

// highest/lowest since last buy signal
maxLong	= HighestSince(Buy, High);
lossLong	= LowestSince (Buy, Low);

// highest/lowest since last short signal
maxShort		= LowestSince(Short, Low);
lossShort		= HighestSince (Short, High);

// Original SL, I guess
Longstoploss		= (BuyPrice* slfactor);
Shortstoploss		= (ShortPrice * (2-slfactor));

// Trailing SL
tslLong	= tLongProfit + abs(maxLong - tLongProfit) * tslfactor;
tslShort	= tShortProfit - abs(maxShort - tShortProfit) * tslfactor;

//Sell	= Short OR IIf (losslong<longstoploss, Cross (losslong, Close),0) OR IIf(maxLong>tLongProfit, Cross(tslLong, Close),0);
//Cover	= Buy OR IIf (lossShort>shortstoploss, Cross (Close, lossshort),0) OR IIf(maxShort<tShortProfit, Cross(Close, tslShort), 0);
Sell	= Short OR Cross (losslong, Close) OR IIf(maxLong>tLongProfit, Cross(tslLong, Close),0);
Cover	= Buy OR Cross (Close, lossshort) OR IIf(maxShort<tShortProfit, Cross(Close, tslShort), 0);


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

PlotShapes( IIf(Buy, shapeUpArrow, shapeNone ), colorBrightGreen, layer = 0,yposition = Low, offset = -10); 
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorPink, layer = 0, yposition = High, offset = -10); 
PlotShapes( IIf(Cover, shapeStar,shapeNone),  colorBrightGreen, layer = 0,yposition = Low, offset = -20);
PlotShapes( IIf(Sell,shapeStar,shapeNone),  colorPink, layer = 0, yposition = High, offset = 20); 
_SECTION_END();



_SECTION_BEGIN("Magified Market Price");
FS=Param("Font Size",30,11,100,1);
Hor=Param("Horizontal Position",940,1,1200,1);
Ver=Param("Vertical Position",12,1,830,1); 

YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);

GfxSetBkMode( 1 ); 

GfxSelectFont("Times New Roman", FS, 700, True ); 
-GfxSetTextColor( colorLime ); 
GfxTextOut(""+C, Hor+200 , Ver );

/*
GfxSelectFont("Times New Roman", 11, 700, True ); 
GfxSetTextColor(colorYellow ); 
GfxTextOut(""+DD+" ("+xx+"%)", Hor+200 , Ver+65 );
*/
_SECTION_END();
Congrats, abhig, most of the signals are in place! But curiously one or two doesn't obey the parameters. Could it be an indication of something still missing?

I attach a screenshot. The short should have exited at cursor position, given the SL factor value (see the param box ) of 0.998 for crude at 6200 approx. But it did n't and fetched some more loss.

And oh yes, I knew you were talking about the quote. It was me who wanted to give the code for explanation :)
 

Attachments

Here is the code, but be warned .It looks to the future as it is based on Zig-zag function.

Code:
_SECTION_BEGIN("ZigZag");
P = Param("ZigZag_Percent",0.1,0.01,2,0.01);
ZigH  = Zig(H,P);		ZigL = Zig(L,P);
ZigHi = ZigH > Ref(ZigH,-1) AND ZigH > Ref(ZigH,1);		
ZigLo = ZigL < Ref(ZigL,-1) AND ZigL < Ref(ZigL,1);
j=0;	k=0;	PH[0] = PL[0] = 0;
for( i = 1; i < BarCount-1; i++ ) { 
 if (ZigLo[i])	{
	if (PL[k] > ZigL[i])  	PlotText("LL",i, L[i]-5, colorGreen);
	else						PlotText("HL",i, L[i]-5, colorGreen);
	k = k + 1;		PL[k] = ZigL[i]; }
 if (ZigHi[i]) {
	if (PH[j] < ZigH[i])  	PlotText("HH", i, H[i]+5, colorRed);
	else						PlotText("LH", i, H[i]+5, colorRed);
	j = j + 1;
	PH[j] = ZigH[i]; }
}
_SECTION_END();

_SECTION_BEGIN("Price");
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() ); 
_SECTION_END();
Incidentally , this code is present this thread

http://www.traderji.com/amibroker/90119-simple-coding-help-no-promise-23.html#post885515

Thanks in amit for your kind help:thumb:
 
Hi, figured out that the code I gave you is unfortunately wrong. It doesn't calculate the proper number of shares for the trade which is why you do not get any results while back testing and some other error as well. I'll try to fix it, if possible, and get back to you.
Woww!! Eagerly awaiting!! Thank you so much for the efforts taken!!
 

Similar threads