Simple Coding Help - No Promise.

You had done your Line based on y=a+bx, through the use of Linear Regression formula. But you had forgot to calculate the Slope of y= a+bx;

I think you need to recalculate the b(which is the slope) from the selected point to point n bars backward from there again.

You cannot simply take any value from the Linear Regression formula to substitute in.
The question of the slope I managed to resolve. And it was quite simple, actually. I didnt know exactly how did the slope function worked, so I ploted it in a different pane. It was like an Stocastic or MACD. When it was above the 0 line, the linear regression line was upwards, and vice-versa. So I just made the condition:

slope = linregslope( close , period);
Condition1 = slope > 0;

The wrong signals stopped appearing. :clap:

Tried my first back test and it looks promising:

1 - optimize: from 03/01/2012 to 03/06/2012;
2 - applied the best result: from 03/06/2012 to 28/04/2014;



It is not about the slop! He has problems to plot ApplyStop. The slope is OK as you can see in upper video (color changes as soon as direction (rising/falling) of the line changes). But again, his signal plots are wrong.
I saw you explaining the plot lines in another topic:

http://www.traderji.com/amibroker/89508-how-plot-applystop-function.html

I didnt read it all yet, because I was focusing in the system working correctly first, but I'm sure to read it. If I still have any doubt after that, than I'll ask you, Mr. Trash. :) :thumb:



And about the question of the:

1 - stop loss in the same bar as the entry bar (if touched);
2 - stop gain only after the entry bar.

Mr Gorzynski said it could be done with a loop function. I searched for something that would me teach that, and found it!!! I'm reading it right now, and it is VERY GOOD material. For anyone who wants to read it too, here it is:

http://www.amibrokerforum.com/index.php?topic=50.0

There is a pdf version, much better, in the amibroker yahoo group. I recomend the pdf version.

Thanks guys!!! Really!!!!:thumb:
 

trash

Well-Known Member
The question of the slope I managed to resolve. And it was quite simple, actually. I didnt know exactly how did the slope function worked, so I ploted it in a different pane. It was like an Stocastic or MACD. When it was above the 0 line, the linear regression line was upwards, and vice-versa. So I just made the condition:

slope = linregslope( close , period);
Condition1 = slope > 0;

The wrong signals stopped appearing. :clap:
That's the same as you do in the code that you posted. It's OK.


Code:
/ momozo code http://www.traderji.com/amibroker/90119-simple-coding-help-no-promise-155.html#post963041
// changes by trash

// input Ticksize in 'Information' window of the symbol

tsdecplaces = 2; // input number of decimal places of TickSize or write formula to calculate it automatically
div = 10^tsdecplaces; // -> 10^(number of decimal places of TickSize)
TickSize = TickSize / div; //http://codefortraders.com/phpBB3/viewtopic.php?f=38&t=715&sid=75afa014b01bc0f4fcda1ff7142083f6

tradedelay = 0; 

Periods = Optimize( "MAPeriods", 26, 2, 60, 2 );
Stop_Mult = Optimize( "StopMult", 10, 10, 40, 1 );
per = Param( "LinPeriod", 61, 5, 100, 2);
ArrayMA = ParamField( "Price field", 3 );
Color = ParamColor( "Color", colorCycle );
Style = ParamStyle( "Style", styleLine | styleNoLabel );

ProjectedMA = pricearray = Ref( WMA( ArrayMA, periods - 1 ) , -1 );
CenterLine = WMA( ArrayMA, Periods );

if ( tradedelay > 0 )
{
    pricearray = pricearray2 = Open;
    immediatestop = 1;
}
else
{
    pricearray2 = Close;
    immediatestop = 0;
}

SetOption( "ActivateStopsImmediately", immediatestop );
SetOption( "ReverseSignalForcesExit", 0 );
//SetOption( "EveryBarNullCheck", 1 );
SetPositionSize( 1, spsShares );

//================================================== ================================================== ==============

//This is the Linear Regression Line.
//if it is Upward, I want to Buy in the touch of the MA.
//if it is Downward, I want to short in the touch of the MA.
x = Cum(1);

aaa = LinRegIntercept( C, per );
bbb = LinRegSlope( C, per );
Slope = bbb;

// just related to plotting regression line,  start ::::::::::::::::::::::::::::
// references future Quotes start, because of lastvalue
lastx = LastValue( x );
// references future Quotes end

selv = SelectedValue( x );

daa = SelectedValue( ValueWhen( x, aaa, 1 ) );
dbb = SelectedValue( ValueWhen( x, bbb, 1 ) );

xx = IIf( x > selv - per AND x <= selv, x - ( selv - per ), Null );
yy = daa + dbb * xx;

dhh = abs( H - yy );
dll = abs( L - yy );
dtt = Max( dhh, dll );

wd = SelectedValue( HHV( dtt, per ) );
// just related to plotting regression line, end ::::::::::::::::::::::::::::


//================================================== ================================================== ==============
//Now for the buy and short signals.
//First the conditions. I want all 3 to be true at the same time to activate.
tn = TimeNum();
timecond = tn >= 093000 AND tn <= 170000;
exitcond = tn >= 171500;

BuyCondSlope = slope > 0;
BuyCondMATouch = ( Ref( Low , -1 ) > Ref( projectedMA , -1 ) ) AND ( Low < ProjectedMA );
BuyCondTimeLimit = timecond;

ShortCondSlope = slope < 0;
ShortCondMATouch = ( Ref( High , -1 ) < Ref( ProjectedMA , -1 ) ) AND ( High > ProjectedMA );
ShortCondTimeLimit = timecond;

Buycond = BuyCondSlope AND BuyCondMATouch AND BuyCondTimeLimit;
Shortcond = ShortCondSlope AND ShortCondMATouch AND ShortCondTimeLimit;

Buy = Ref( Buycond, -tradedelay );
Short = Ref( Shortcond, -tradedelay );

//Want to close any open position if its past 17h15m:
Sell = exitcond;
Cover = exitcond;

BuyPrice = pricearray;
ShortPrice = pricearray;

// for regular Sell/Cover conditions
SellPrice = CoverPrice = pricearray2; 

//Now I will activate the stop loss and take profit:
StopLoss_Gain = Stop_Mult * ( TickSize * div );

ApplyStop( stopTypeLoss, stopModePoint, StopLoss_Gain, exitatstop = 1, volatile = False, rentrydelay = 0 );
ApplyStop( stopTypeProfit, stopModePoint, StopLoss_Gain, exitatstop = 1, volatile = False, rentrydelay = 0 );


// Plotting :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
if( Status( "action" ) == actionIndicator )
{
	SetChartOptions(0, chartShowDates | chartShowArrows | chartWrapTitle );
	GraphXSpace = 5;

	Plot( C, "", colorGrey50, stylebar );
	Plot( CenterLine , "\nMédia" , colorWhite);
	Plot( ProjectedMA, "ProjectedMA" , colorAqua);

	Colorchange = IIf( SelectedValue( slope ) > 0, colorGreen, colorRed );
	//Plot( yy, "LinReg", colorChange, stylenorescale, 0, 0, 0, 0, 5 );
	Plot( bbb, "Slope", colorRed , styleOwnScale );
	Plot( aaa, "LinRegIntercept", colorOrange );
	//Plot(yy + wd, "Upper Boundary", colorRed, 4 );
	//Plot(yy - wd, "Lower Boundary", colorBrightGreen, 4 );

	// Plotshapes of signals here ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

	Equity( 1, 0 ); // evaluate stops, all quotes
/*
.
.
.
.
.
.
*/
}

I saw you explaining the plot lines in another topic:

http://www.traderji.com/amibroker/89508-how-plot-applystop-function.html

I didnt read it all yet, because I was focusing in the system working correctly first, but I'm sure to read it. If I still have any doubt after that, than I'll ask you, Mr. Trash. :) :thumb:
See here http://www.traderji.com/amibroker/90119-simple-coding-help-no-promise-153.html#post961993
 
Last edited:

pratapvb

Well-Known Member
CellClinic

I was able to modify the AFL to include CLOSE.I tried to have the line parameters to be changeable, but when I did that , the lines started retracing.I think you wanted a non retracing option.We need an expert to guide us on that.

Current modified code.


Code:
//ESSENTIAL TRADER TOOLS//

_SECTION_BEGIN("Price");
SetChartBkGradientFill( ParamColor("BgTop",colorBlack),ParamColor("BgBottom",colorBlack),ParamColor("Titleblock",colorLightGrey));
SetChartOptions(0,chartShowDates|chartShowArrows|chartLogarithmic|chartWrapTitle);
GraphXSpace = 5;
Plot(C,"",colorWhite,styleCandle);
_SECTION_END();

//Previous Days HI LO CL//

DayH = TimeFrameGetPrice("H", inDaily, -1);     DayHI = LastValue (DayH,1);// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1);     DayLI = LastValue (DayL,1);    // yesterdays low
DayC = TimeFrameGetPrice("C", inDaily, -1);       DayCI = LastValue (DayC,1);    // yesterdays close
DayO = TimeFrameGetPrice("O", inDaily);            // current day open
DayH2= TimeFrameGetPrice("H", inDaily, -2);  DayH2I = LastValue (DayH2,1); // Two days before high
DayL2= TimeFrameGetPrice("L", inDaily, -2);  DayL2I = LastValue (DayL2,1);  // Two days before low
DayC2= TimeFrameGetPrice("C", inDaily, -2);  DayC2I = LastValue (DayC2,1);  // Two days before close
DayH3= TimeFrameGetPrice("H", inDaily, -3);  DayH3I = LastValue (DayH3,1);  // Three days before high
DayL3= TimeFrameGetPrice("L", inDaily, -3);  DayL3I = LastValue (DayL3,1);  // Three days before low
DayC3= TimeFrameGetPrice("C", inDaily, -3);  DayC3I = LastValue (DayC3,1);  // Three days before close
numbars = LastValue(Cum(Status("barvisible")));
hts  = -33.5;

YHL = ParamToggle("Yesterday HI LO CL ","Show|Hide",1);
if(YHL==1) {
  Plot(DayL,"YL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayH,"YH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayC,"YC",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  PlotText(" YH " ,     LastValue(BarIndex())-(numbars/Hts), DayHI, colorTurquoise);
  PlotText(" YL " ,     LastValue(BarIndex())-(numbars/Hts), DayLI, colorTurquoise);
PlotText(" YC " ,     LastValue(BarIndex())-(numbars/Hts), DayCI, colorTurquoise);
}

TDBHL = ParamToggle("2/3Days before HI LO CL","Show|Hide",1);
if(TDBHL==1) {
  Plot(DayL2,"2DBL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayH2,"2DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayC2,"2DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayL3,"3DBL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayH3,"3DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
 Plot(DayC3,"3DBC",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  PlotText(" 2DBH " ,     LastValue(BarIndex())-(numbars/Hts), DayH2I, colorTurquoise);
  PlotText(" 2DBL " ,     LastValue(BarIndex())-(numbars/Hts), DayL2I, colorTurquoise);
 PlotText(" 2DBC " ,     LastValue(BarIndex())-(numbars/Hts), DayC2I, colorTurquoise);
  PlotText(" 3DBH " ,     LastValue(BarIndex())-(numbars/Hts), DayH3I, colorTurquoise);
  PlotText(" 3DBL " ,     LastValue(BarIndex())-(numbars/Hts), DayL3I, colorTurquoise);
 PlotText(" 3DBC " ,     LastValue(BarIndex())-(numbars/Hts), DayC3I, colorTurquoise);
}

// Pivot Levels //
PP = (DayL + DayH + DayC)/3;  PPI = LastValue (PP,1);   // Pivot
R1  =  (PP * 2) - DayL;       R1I = LastValue (R1,1);   // Resistance 1
S1  =  (PP * 2)  - DayH;      S1I = LastValue (S1,1);   // Support 1
R2  =  PP + R1 - S1;          R2I = LastValue (R2,1);   // Resistance 2
S2  =  PP - R1 + S1;          S2I = LastValue (S2,1);   // Support 2
R3  =  PP + R2 - S1;          R3I = LastValue (R3,1);   // Resistance 3
S3  =  PP - R2 + S1;          S3I = LastValue (S3,1);   // Support 3

ppl = ParamToggle("Pivot Levels","Show|Hide",1);
if(ppl==1)  {
  Plot(PP, "PP",colorYellow,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(R1, "R1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(S1, "S1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(R2, "R2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(S2, "S2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(R3, "R3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(S3, "S3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);

  PlotText(" Pivot ",  LastValue(BarIndex())-(numbars/Hts), PPI, colorYellow);
  PlotText(" R1 " ,    LastValue(BarIndex())-(numbars/Hts), R1I, colorViolet);
  PlotText(" S1 " ,    LastValue(BarIndex())-(numbars/Hts), S1I, colorViolet);
  PlotText(" R2 " ,    LastValue(BarIndex())-(numbars/Hts), R2I, colorViolet);
  PlotText(" S2 " ,    LastValue(BarIndex())-(numbars/Hts), S2I, colorViolet);
  PlotText(" R3 " ,    LastValue(BarIndex())-(numbars/Hts), R3I, colorViolet);
  PlotText(" S3 " ,    LastValue(BarIndex())-(numbars/Hts), S3I, colorViolet);
}
// Camerilla Levels //

rg = (DayH - DayL);

H5=DayC+1.1*rg;     H5I = LastValue (H5,1);
H4=DayC+1.1*rg/2;   H4I = LastValue (H4,1);
H3=DayC+1.1*rg/4;   H3I = LastValue (H3,1);
H2=DayC+1.1*rg/6;   H2I = LastValue (H2,1);
H1=DayC+1.1*rg/12;  H1I = LastValue (H1,1);
L1=DayC-1.1*rg/12;  L1I = LastValue (L1,1);
L2=DayC-1.1*rg/6;   L2I = LastValue (L2,1);
L3=DayC-1.1*rg/4;   L3I = LastValue (L3,1);
L4=DayC-1.1*rg/2;   L4I = LastValue (L4,1);
L5=DayC-1.1*rg;     L5I = LastValue (L5,1);

pcl = ParamToggle("Camerilla Levels","Show|Hide",0);
if(pcl==1)  {
   Plot(H5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(H4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(H3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(H2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(H1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   PlotText(" H5 = " ,     LastValue(BarIndex())-(numbars/Hts), H5I  +0.05, colorRose);
   PlotText(" H4 = " ,     LastValue(BarIndex())-(numbars/Hts), H4I  +0.05, colorRose);
   PlotText(" H3 = " ,     LastValue(BarIndex())-(numbars/Hts), H3I  +0.05, colorRose);
   PlotText(" H2 = " ,     LastValue(BarIndex())-(numbars/Hts), H2I  +0.05, colorRose);
   PlotText(" H1 = " ,     LastValue(BarIndex())-(numbars/Hts), H1I  +0.05, colorRose);
   PlotText(" L1 = " ,     LastValue(BarIndex())-(numbars/Hts), L1I  +0.05, colorRose);
   PlotText(" L2 = " ,     LastValue(BarIndex())-(numbars/Hts), L2I  +0.05, colorRose);
   PlotText(" L3 = " ,     LastValue(BarIndex())-(numbars/Hts), L3I  +0.05, colorRose);
   PlotText(" L4 = " ,     LastValue(BarIndex())-(numbars/Hts), L4I  +0.05, colorRose);
   PlotText(" L5 = " ,     LastValue(BarIndex())-(numbars/Hts), L5I  +0.05, colorRose);
}

// Current Days Hi Lo //
THL = ParamToggle("Todays Hi Lo","Show|Hide",1);
if(THL==1)  {
  isRth = TimeNum() >= 084500 & TimeNum() <= 085959;
  isdRth = TimeNum() >= 084500 & TimeNum() <= 160000;
  aRthL = IIf(isRth, L, 1000000);
  aRthH = IIf(isdRth, H, Null);
  aRthLd = IIf(isdRth, L, 1000000);
  DayH = TimeFrameCompress( aRthH, inDaily, compressHigh );
  DayH = TimeFrameExpand( DayH, inDaily, expandFirst );
  DayL = TimeFrameCompress( aRthLd, inDaily, compressLow );
  DayL = TimeFrameExpand( DayL, inDaily, expandFirst );
  Bars = BarsSince(TimeNum() >= 94500 AND TimeNum() < 095959);//,BarIndex(),1); // AND DateNum()==LastValue(DateNum());
  x0 = BarCount-LastValue(Bars);
  x1 = BarCount-1;
  DayHline=LineArray(x0,LastValue(DayH),x1,LastValue (DayH),0);
  DayLline=LineArray(x0,LastValue(DayL),x1,LastValue (DayL),0);
  DayHlineI = LastValue (DayHline,1);
  DayLlineI = LastValue (DayLline,1);
  Plot(DayHline,"DayH",colorYellow,styleBar|styleNoRescale|styleNoTitle);
  Plot(DayLline,"DayL",colorYellow,styleBar|styleNoRescale|styleNoTitle);
  PlotText(" Day Hi " ,     LastValue(BarIndex())-(numbars/Hts), DayHlineI  +0.05, colorYellow);
  PlotText(" Day Lo " ,     LastValue(BarIndex())-(numbars/Hts), DayLlineI  +0.05, colorYellow);
 }
Go to post 511 of my vwap thread and follow the link to dailysr afl. See if that helps as base or meets requirements
 

amitrandive

Well-Known Member
Go to post 511 of my vwap thread and follow the link to dailysr afl. See if that helps as base or meets requirements
pratap Sir

I took the AFL from your said location

http://www.traderji.com/advanced-tr...ombining-trend-vwap-ranges-20.html#post874946

This is the image I get.


CellClinic's requirement is that the lines do not retrace and appear as independent lines as shown in the below sketch.
With the current AFL posted in this thread the image is as below


When I tried to change the lines to have flexible option of colour,style and thickness,I got lines that were retracing in the previous day.

Please advise.
 
pratap Sir

CellClinic's requirement is that the lines do not retrace and appear as independent lines as shown in the below sketch.

Please advise.
if xyz is the original level you plot . . .

convert it by using . . .

abc = iif(xyz!=ref(xyz,-1),null,xyz);

then

Plot(abc . . .



Happy :)
 

Nehal_s143

Well-Known Member
I'm not an expert just the beginning for AmiBroker.Maybe too simple for you, but I'm having trouble writing code.You want a small code example is not a system.

Example: I have two indicator example macd and rsi

I trade 15 minute

Buy rule: if 60 minutes macd long then 15 minutes rsi OR macd buy
if 60 minutesmacd short then 15 miutes rsi AND macd buy

Sell rule: if 60 minutes macd short then 15 miutes rsi OR macd short
if 60 minutes macd long then 15 miutes rsi AND macd short

How is the code?

This is not a system but I want to learn diffirent timeframe buy sell rules
Please see old post, this or similar to this has been already coded in some old post....
 

manishchan

Well-Known Member
Hi.. looks like right now this thread is flooded with requests. Kindly accommodate when possible.

I want to plot text (HH and LL) instead of circles in an AFL. Can someone help me change these codes of Shapes to Plot text.

PlotShapes(ismph*shapeSmallCircle, colorBlue, 0, H, H*.001);

PlotShapes(ismpl*shapeSmallCircle, colorGreen, 0, L, -L*.001);
 

Nehal_s143

Well-Known Member
Hi.. looks like right now this thread is flooded with requests. Kindly accommodate when possible.

I want to plot text (HH and LL) instead of circles in an AFL. Can someone help me change these codes of Shapes to Plot text.
try this

PlotText( "HH", ismph, colorBlue );
PlotText( "LL", ismpl, colorGreen );
 

amitrandive

Well-Known Member
if xyz is the original level you plot . . .

convert it by using . . .

abc = iif(xyz!=ref(xyz,-1),null,xyz);

then

Plot(abc . . .

Happy :)

Happy Sir

Got the plot correct line is snipped as required ,but the Text is not plotted
Please check this snippet of the code.Here the last line does not plot the text.See attached image(A)


Code:
DayH = TimeFrameGetPrice("H", inDaily, -1)
DayHI =IIf(DayH!=Ref(DayH,-1),Null,DayH);
PriceLineColor=ParamColor("PriceLineColor",ColorRGB(82,82,82));
numbars = LastValue(Cum(Status("barvisible")));
hts  = -33.5;
Plot(DayHI,"YH",PriceLineColor,ParamStyle("LineStyle",styleLine|styleDashed|styleNoTitle|styleNoLabel|styleThick,maskAll));
PlotText("YH",LastValue(BarIndex())-(numbars/Hts),DayHI,PriceLineColor);
In this version, the line properties are changeable and the text is plotted,but the lines retrace back to previous day.See attached image B
Code:
DayH = TimeFrameGetPrice("H", inDaily, -1)
DayHI = LastValue (DayH,1);
PriceLineColor=ParamColor("PriceLineColor",ColorRGB(82,82,82));
numbars = LastValue(Cum(Status("barvisible")));
hts  = -33.5;
Plot(DayH,"YH",PriceLineColor,ParamStyle("LineStyle",styleLine|styleDashed|styleNoTitle|styleNoLabel|styleThick,maskAll));
PlotText(" YH " ,     LastValue(BarIndex())-(numbars/Hts), DayHI, PriceLineColor);
Please advise,how to get both the things , the line snipped and the text plotted:confused:
 

Similar threads