Indicator coding

Posted By: danatrader

Indicator coding - 03/16/20 19:32

hi alltogether,

still I am a noob, so excuse...

I try to calculate a simple indicator.

Workshop 4a is already nice, but a bit for a beginner.

May someone assist?

Indicator formula is rather primitive, it is called "Psychology Line".

PSL=UpMovementsinthelastPeriodsn/Periodsn×100

I only got some Sierra Chart code frown

Code

float Count = 0;
		if (sc.GetBarHasClosedStatus(sc.Index) == BHCS_BAR_HAS_CLOSED) {
			for (int ictr = 1; ictr <= (perioda); ictr++)
			{
				if (sc.Close[sc.Index] > sc.Close[sc.Index-ictr])
				{
					Count++;
				}
			}

			PSL[sc.Index] = (Count / (perioda)) *100.0f;
		}
		else {
			PSL[sc.Index] = PSL[sc.Index-1];
		}

Posted By: jcl

Re: Indicator coding - 03/17/20 09:02

The code would be very similar for Zorro. Only make sure that Count is an int, not a float, and find out what sc.GetBarHasClosedStatus(sc.Index) is doing.

On Financial Hacker, Petra has written a few articles about indicator coding.
Posted By: danatrader

Re: Indicator coding - 03/18/20 07:32

sc.Index is an array called once per bar.

if (sc.GetBarHasClosedStatus(sc.Index) == BHCS_BAR_HAS_CLOSED) is just making sure the actual bar is closed, since SC offers many different bars.

More simple pine script from tradingview:

xPSY = sum(close > close[1],Length) / Length * 100


Actually the indicator is so simple, but really great for trend determination, so if someone could help out would be awesome (I don't expect JCL to do so, since I assume he has his hands full with things to do).

But thank you a lot.
Posted By: Petra

Re: Indicator coding - 03/18/20 08:58

I am not 100% sure but believe the pinescript is not the same as the sierra code. The sierra code sums up by differences to the current close but the pinescript sums up by differences of subsequent closes.

In Zorro: SumUp(series(priceClose()),Length) / Length * 100;

Posted By: danatrader

Re: Indicator coding - 03/18/20 12:42

The SC code is from a really great futures / SC devleoper from Czech.
Sadly he won't share all, (which I understand fully).

So, the code from Sierra I assume is better (since I veryfied it graphically in the past).

In Zorro: SumUp(series(priceClose()),Length) / Length * 100; -> is the pince script version?
I will giv it a try, actually I was already working with SumUp, but didn't get it fully.

Thank you Petra.


BTW, this indicator allows to do really nice things, since most indicators may be piped through.
And it is almost realtime and really "cheap" in calculation costs.
Posted By: danatrader

Re: Indicator coding - 05/30/20 21:41

Psychological Line is an indicator developed by Ken Muranaka

The indicator’svalues may range from 0 to 100. It is a simple indicator which shows the numberof increasing/decreasing prices over a specified period; thereby is a means for deter-mining the overbought/oversold price level.
The standard calculation of theindicator is as follows:

PSL = n / 12 * 100

where n is the number of days that the price is closed higher than the previous period.

n and the number of comparison days (i.e. 12) in the above calculation are subject to change.

https://www.questia.com/magazine/1G1-63024940/opinion-oscillator
Posted By: Grat

Re: Indicator coding - 06/05/20 09:29

not nice, but working

Code
#define _SUM_PLUS  0         // register for AssetInt
#define _SUM_MINUS 1         // register for AssetInt

//---------------- indicator -----------------------------

double idxPL(){
    int sumUP = AssetInt[_SUM_PLUS];
    int sumDN = AssetInt[_SUM_MINUS];
    var pl=0;
	if (priceClose() > priceClose(1)){
        sumUP++;
        sumDN=0;
        pl=(sumUP/12.0)*100;
    }
	
	if (priceClose() < priceClose(1)){
        sumUP=0;
        sumDN++;
        pl=-(sumDN/12.0)*100;
    }
	//watch("\n",sumDN,sumUP,pl);
    AssetInt[_SUM_PLUS] = sumUP;
    AssetInt[_SUM_MINUS]= sumDN;
	
    return pl;
		
}


function run()
{

	set(PLOTNOW+LOGFILE);
    //set(STEPWISE);
	StartDate=2020;
	BarPeriod = 60;
	LookBack = 1;
	
	PlotScale = 8;
	PlotWidth = 1600;
	PlotHeight1 = 600;
	PlotHeight2 = 120;
    assetList("Assets");
    if (is(INITRUN)){
        assetHistory(Asset,1);
    }

	vars aPL = series(idxPL());
	plot("PLine",aPL,NEW,BLUE);
		 		  
}
Posted By: danatrader

Re: Indicator coding - 06/06/20 05:50

It goes into the direction, thank you, I will continue working on it.
Thats a good start I have right here.

Thank you both Grat and Petra.
Posted By: danatrader

Re: Indicator coding - 06/06/20 07:08

Did receive another snippet of code from the original developer... the thing is, of course it should be smoothed, but somehow calculations don't fit, it should move btween 0 and 100.
i will work on it, and crack it, I now have some approaches availabel, thanks to all again (sadly I am still fighting myself into it).

Seems like having winning ideas is so easy, coding so difficult laugh

// definition
Length1.Name = "Psychological area period";
Length1.SetInt(30);
Length1.SetIntLimits(1, MAX_STUDY_LENGTH);


//body
int &perioda = sc.GetPersistentInt(1);
perioda = Length1.GetInt();






//example output
//2: ATR Simple
if (CalculationMethod.GetIndex() == 2) {
sc.ATR(sc.BaseDataIn, outputID, Lengthfirst.GetInt(), MOVAVGTYPE_SIMPLE);
}




//PSL
for (int ictr = 1; ictr <= (perioda); ictr++)
{
if (outputID[sc.Index] > outputID[sc.Index - ictr])
{
Count = Count + 1.0f;
}
}

PSL[sc.Index] = (Count / (perioda)) *100.0f;
Posted By: Grat

Re: Indicator coding - 06/06/20 11:21

This indicator / original is not good for FX. Becouse calculate only the LONG. If is a downtrend, show zero. ( from head ). For this I have a modification (-100;100)
Posted By: Grat

Re: Indicator coding - 06/06/20 11:33

Depend on the theory:

Psychological Line is an indicator developed by Ken Muranaka.
The indicator’s values may range from 0 to 100. It is a simple indicator which shows the number of increasing/decreasing prices over a specified period; thereby is a means for deter- mining the overbought/oversold price level. The standard calculation of the indicator is as follows:

PI= n/12 ∗100

where n is the number of days(period) that the price is closed higher than the previous period.
n and the number of comparison days (i.e. 12) in the above calculation are subject to change.

[Linked Image]

Attached picture Snímek obrazovky 2020-06-06 v 16.25.54.png
Posted By: danatrader

Re: Indicator coding - 06/06/20 11:35

Yes, I was originally thinking that for the Sierra Chart developer too, but a nice effect can be achieved by just switching the length of the comparision period with indicator period.
Then it would be somewhat upside down.

I will try your way, actually it needs to be smoothed, anyway, I will work on it a bit.

it is a nice sample for me to learn more coding and the behaviour of Zorro.

Just I don't get, how I would let the sumDN / sumUP shift, since comparing the complete periods is not so good.
The periods compared should be created by HTDcPeriod / DominantPeriod.
Posted By: Grat

Re: Indicator coding - 06/06/20 11:38

Yes, for this I not finish, me code is only draft. Now I work on finish version.
Posted By: danatrader

Re: Indicator coding - 06/06/20 11:53

I promise you, it is so simple logic, but you can pipe anything through and it is really powerful.

E.g. preprocess Data with only candles matching a given ATR condition.

NumWhiteBlack(ATR(24)/3, 0, 1) or the moving average of it.
And so on...

Just my plot looks different from yours, I assume you did already change the code postet before.
Posted By: danatrader

Re: Indicator coding - 06/06/20 19:04

Grat,

I think there is something not as it should be, look at the plot below.
I did try already to change your code, eventually I will get it done, just if you say yours is not nice, mine will be horrible.... laugh

double idxPL(){
int sumUP = AssetInt[_SUM_PLUS];
int sumDN = AssetInt[_SUM_MINUS];
var pl=0;
if (priceClose() > priceClose(1)){
sumUP++;
sumDN=0;
pl=(sumUP/12.0)*100;
}

if (priceClose() < priceClose(1)){
sumUP=0;
sumDN++;
pl=-(sumDN/12.0)*100;
}
//watch("\n",sumDN,sumUP,pl);
AssetInt[_SUM_PLUS] = sumUP;
AssetInt[_SUM_MINUS]= sumDN;

return pl;

}


I think it goes too sharp down, as far I understand, the code above returns either the minus count or the plus count.
But in case to make your idea work with the down count, ther must be something like sumUP-sumDN if sumUP is bigger than sumDN, and the other way round sumDown - sumUP if sumDN is bigger than sumUP.

Of course only, if the opposite bar / close occurs.




Attached picture PSL-Plot.png
Posted By: danatrader

Re: Indicator coding - 06/07/20 05:45

How would I make it to oscillate between 100 and 0.
For me it is about also about understanding the tool (Zorro functions).

The version of Petra is simple, just it goes beyond 100 as max. value.
So I would just use the Max function?
Posted By: Grat

Re: Indicator coding - 06/07/20 06:50

theory:

Quote
where n is the number of days(period) that the price is closed higher than the previous period.


but

zorro fce sumUp - not calculate how many is close Price higher the price older, but sumOfprice if is highers

Code
var vUp, vDn;
var ProfitFactor(var* Data,int Length)
{
	vUp = 0.,vDn = 0.;
	for(int i=1; i<Length; i++) {
		if(Data[i-1] > Data[i])
			vUp += Data[i-1]-Data[i];
		else
			vDn += Data[i]-Data[i-1];
	}
	if(vDn == 0.) 
		return vUp == 0.? 1. : 10.;
	return Clamp(vUp/vDn,0.,9.999);
}

var SumUp(var* Data,int Length)
{
	ProfitFactor(Data,Length);
	return vUp;
}
Posted By: danatrader

Re: Indicator coding - 06/07/20 06:56

That is I assume it would need before the SumUp this

if (priceClose() > priceClose(1)){

still I really need the original version.

Can we get there together?
I can then show you why.

BTW, can you crack the EUR/CAD laugh
Posted By: danatrader

Re: Indicator coding - 06/07/20 07:28

But even using the code from Petra, I don't get it oscillating between 0 and 100.
What would I need to do to get it / any line oscillating between 0 / 100?
Posted By: Grat

Re: Indicator coding - 06/07/20 07:35

I don't know, how looking the original idx. Like this?

Attached picture Snímek obrazovky 2020-06-07 v 12.34.25.png
Posted By: danatrader

Re: Indicator coding - 06/07/20 08:51

You mean original in Sierra Chart, or original Petra or original your code implementation?
Posted By: Grat

Re: Indicator coding - 06/07/20 09:18

Original Sierra chart
Posted By: Grat

Re: Indicator coding - 06/07/20 17:55

try this

Attached File
xcor4_PL.c  (11 downloads)
Posted By: Grat

Re: Indicator coding - 06/12/20 10:09

I found also this:

NumRiseFall(vars Data, int TimePeriod): var

Middle Blue line

Attached picture Snímek obrazovky 2020-06-12 v 15.16.31.png
Posted By: danatrader

Re: Indicator coding - 06/12/20 16:54

Thank you Grat, I can learn a lot from you laugh
© 2024 lite-C Forums