Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 945 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rating: 1
Page 1 of 25 1 2 3 24 25
Lapsa's very own thread #483948
08/17/21 13:08
08/17/21 13:08
Joined: Aug 2021
Posts: 237
L
Lapsa Offline OP
Member
Lapsa  Offline OP
Member
L

Joined: Aug 2021
Posts: 237
I want to document my experiments somewhere.
This seems like a decent place. I like forums.
Feedback is welcome.

------------------------

Cycles

My latest obsession is cycles. Been thinking a lot about what would be good ways to utilize Hilbert's Transform Dominant Cycle.

So today wrote a small script to compare how fixed length SMA looks compared to dc_period long SMA to see if I spot anything meaningful.


Code
#include <profile.c>

function run()
{
	set(PLOTNOW);
	BarPeriod = 1;
	LookBack = 100;
	StartDate = 20210702;
	EndDate = 20210816;

	vars seriesClose = series(priceClose());

	var sma23 = SMA(seriesClose,23);
	var period = HTDcPeriod(seriesClose);

	plot("SMA 23", sma23, MAIN, RED);
	plot("ASMA", adaptiveSma, MAIN, GREEN);
}


[Linked Image]


Conclusions are kind of obvious:
- lags less when dominant cycle is shorter than fixed one
- lags more when longer

Picked length 23 cause it looked like kind of average of what dominant cycle tends to spin around.

So I thought:
"Ok... but how that visually compares to The Shapeshifter of MA`s aka KAMA"?


Code
	var kama = KAMA(seriesClose, 23);
	var veryAdaptiveKama = KAMA(seriesClose, period);
	plot("KAMA 23", kama, MAIN, SILVER);
	plot("VAKAMA", veryAdaptiveKama, MAIN, GREY);


[Linked Image]


The effect is same although SMA behaves much differently than KAMA.

So really... No huge revelations here.

Lets smash in some trade signals!!!111eleven


Code
#include <profile.c>

function run()
{
	set(PLOTNOW);
	BarPeriod = 1;
	LookBack = 100;
	StartDate = 20210702;
	EndDate = 20210816;

	vars seriesClose = series(priceClose());
	var period = HTDcPeriod(seriesClose);

	var sma22 = SMA(seriesClose,22);
	var adaptiveSma = SMA(seriesClose,period);
	var kama22 = KAMA(seriesClose, 22);
	var veryAdaptiveKama = KAMA(seriesClose, period);
	
	if (	  
	  price() > veryAdaptiveKama &&
	  veryAdaptiveKama > kama22 &&
	  veryAdaptiveKama > adaptiveSma &&
	  adaptiveSma > sma22 &&
	  rising(series(sma22))
	) {
		exitShort();
		if (ProfitOpen >= 0) enterLong();
	}
	
	if (	  
	  price() < veryAdaptiveKama &&
	  veryAdaptiveKama < kama22 &&
	  veryAdaptiveKama < adaptiveSma &&
	  adaptiveSma < sma22 &&
	  falling(series(sma22))
	) {
		exitLong();
		if (ProfitOpen >= 0) enterShort();
	}
	
	plot("SMA 22", sma22, MAIN, RED);
	plot("ASMA", adaptiveSma, MAIN, GREEN);
	plot("KAMA 22", kama22, MAIN, SILVER);
	plot("VAKAMA", veryAdaptiveKama, MAIN, GREY);	
	plot("Profit Open", ProfitOpen, NEW, RED);
	plot("HT Dc Period", period, NEW, BLACK);
}


[Linked Image]

Quote

Monte Carlo Analysis... Median AR 2509%
Win 9100$ MI 6082$ DD 810$ Capital 2774$
Trades 2485 Win 42.7% Avg +36.6p Bars 67
AR 2631% PF 1.82 SR 0.00 UI 0% R2 1.00


For such randomness - PF of 1.82 ain't total disaster.

Last edited by Lapsa; 08/17/21 20:45.
Re: Lapsa's very own thread [Re: Lapsa] #483949
08/17/21 20:10
08/17/21 20:10
Joined: Aug 2021
Posts: 237
L
Lapsa Offline OP
Member
Lapsa  Offline OP
Member
L

Joined: Aug 2021
Posts: 237
Ehler's "Modified Simple Moving Averages"


Code
#include <profile.c>

var ModifiedSMA(var* Data, var* FractionalCoefficients)
{
	var sum=0., coefficientSum=0.;
	int i;
	int period=sizeof(FractionalCoefficients);
	
	for(i=0; i<period; i++) {
		sum += (Data[i] * FractionalCoefficients[i]);
		coefficientSum += FractionalCoefficients[i];
	}

	return sum / coefficientSum;
}

function run()
{
	set(PLOTNOW);
	BarPeriod = 1;
	LookBack = 100;
	StartDate = 20210702;
	EndDate = 20210816;

	vars seriesClose = series(priceClose());

	var sma = SMA(seriesClose, 4);
	var coefficients[] = {.5,1,1,1,.5};	
	var modifiedSMA = ModifiedSMA(seriesClose, coefficients);
	
	var weirdCoefficients[] = {.2,1,1.6,1,.2};
	var weirdSMA = ModifiedSMA(seriesClose, weirdCoefficients);
	
	var weirderCoefficients[] = {.1,1,1,1.7,.2};
	var weirderSMA = ModifiedSMA(seriesClose, weirderCoefficients);

	var shouldBeFastestCoefficients[] = {2,.2,1.2,.2,.4};
	var shouldBeFastest = ModifiedSMA(
		seriesClose, shouldBeFastestCoefficients
	);
	
	plot("SMA 4", sma, MAIN, RED);
	plot(".5 1 1 1 .5", modifiedSMA, MAIN, BLUE);
	plot(".2 1 1.6 1 .2", weirdSMA, MAIN, GREEN);
	plot(".1 1 1 1.7 .2", weirderSMA, MAIN, MAGENTA);
	plot("2 .2 1.2 .2 .4", shouldBeFastest, MAIN, CYAN);
}


[Linked Image]

Last edited by Lapsa; 08/17/21 20:45.
Re: Lapsa's very own thread [Re: Lapsa] #483950
08/17/21 20:44
08/17/21 20:44
Joined: Aug 2021
Posts: 237
L
Lapsa Offline OP
Member
Lapsa  Offline OP
Member
L

Joined: Aug 2021
Posts: 237
EMA Ribbon

So I've been reading that book.
Yesterday it got me wondering - how exactly EMAs with varying alphas look like.


Code
#include <profile.c>

function run()
{
	set(PLOTNOW);
	BarPeriod = 1;
	LookBack = 100;
	StartDate = 20210702;
	EndDate = 20210816;

	vars closes = series(priceClose());

	var ema1 = EMA(closes,.1);
	var ema2 = EMA(closes,.2);
	var ema3 = EMA(closes,.3);
	var ema4 = EMA(closes,.4);
	var ema5 = EMA(closes,.5);
	var ema6 = EMA(closes,.6);
	var ema7 = EMA(closes,.7);
	var ema8 = EMA(closes,.8);
	var ema9 = EMA(closes,.9);
	var ema10 = EMA(closes,.15);
	
	plot(".1", ema1, MAIN, RED);
	plot(".2", ema2, MAIN, GREEN);
	plot(".3", ema3, MAIN, BLUE);
	plot(".4", ema4, MAIN, ORANGE);
	plot(".5", ema5, MAIN, BLACK);
	plot(".6", ema6, MAIN, MAGENTA);
	plot(".7", ema7, MAIN, OLIVE);
	plot(".8", ema8, MAIN, PURPLE);
	plot(".9", ema9, MAIN, LIGHTBLUE);
	plot(".15", ema10, MAIN, MAROON);
}


[Linked Image]


Looks beautiful.

Can't help but see that graph in 3D.
Reminds me of Sietches from Dune.

Ohkay... Is there an application for bunch of EMAs?

One of the most obvious "Market inefficiencies" for an algo trader to exploit is automation itself.

Got me wondering how would ribbon look like with more commonly used time periods.


Code
#include <profile.c>

function run()
{
	set(PLOTNOW);
	BarPeriod = 1;
	LookBack = 1500;
	StartDate = 20210703;
	EndDate = 20210816;

	vars closes = series(priceClose());

	var ema2 = EMA(closes,2);
	var ema5 = EMA(closes,5);
	var ema15 = EMA(closes,15);
	var ema30 = EMA(closes,30);
	var ema45 = EMA(closes,45);	
	var ema60 = EMA(closes,60);
	var ema120 = EMA(closes,120);	
	var ema180 = EMA(closes,180);
	var ema240 = EMA(closes,240);
	var ema1440 = EMA(closes,1440);	
	
	plot("2", ema2, MAIN, SILVER + TRANSP);
	plot("5", ema5, MAIN, CYAN + TRANSP);
	plot("15", ema15, MAIN, GREEN + TRANSP);
	plot("30", ema30, MAIN, MAGENTA);
	plot("45", ema45, MAIN, BLUE);
	plot("60", ema60, MAIN, DARKBLUE);
	plot("120", ema120, MAIN, PURPLE);
	plot("180", ema180, MAIN, MAROON);
	plot("240", ema240, MAIN, RED);
	plot("1440", ema1440, MAIN, BLACK);
}


[Linked Image]


Frikin sea waves!

Lets smash in some trade signals!!!111eleven


Code
#include <profile.c>

function run()
{
	set(NFA|PRELOAD|PLOTNOW);
	BarPeriod = 1;
	LookBack = 2000;
	StartDate = 20210703;
	EndDate = 20210716;
	BarMode = BR_FLAT;	
	History = "*.t6";
	Amount = 8;
	Capital = 600;
	// MaxLong = 5;
	// MaxShort = 5;
	// Stop = ATR(100) * 10;

	vars closes = series(priceClose());

	vars ema2 = series(KAMA(closes,2));
	vars ema5 = series(EMA(closes,5));
	vars ema15 = series(EMA(closes,14));
	vars ema30 = series(EMA(closes,27));
	vars ema45 = series(EMA(closes,44));	
	vars ema60 = series(EMA(closes,60));
	vars ema120 = series(EMA(closes,120));	
	vars ema180 = series(EMA(closes,181));
	vars ema240 = series(EMA(closes,235));
	vars ema1440 = series(EMA(closes,1433));	
	
	if (
		rising(ema2)
		&& rising(ema5)
		&& rising(ema15)
		&& rising(ema30)
		&& rising(ema45)
		&& rising(ema60)
		&& rising(ema120)
		&& rising(ema180)
		&& rising(ema240)
		&& rising(ema1440)
		&& ema2[0] > ema15[0]
		&& ema15[0] > ema30[0]
		&& ema30[0] > ema45[0]
		&& ema45[0] > ema60[0]
		&& ema120[0] > ema180[0]
		&& ema180[0] > ema240[0]
		&& ema240[0] < ema1440[0]		
	) enterLong(Amount);
	
	if (
		falling(ema2)
		&& falling(ema5)
		&& falling(ema15)
		&& falling(ema30)
		&& falling(ema45)
		&& falling(ema60)
		&& falling(ema120)
		&& falling(ema180)
		&& falling(ema240)
		&& falling(ema1440)
		&& ema2[0] < ema15[0]
		&& ema15[0] < ema30[0]
		&& ema30[0] < ema45[0]
		&& ema45[0] < ema60[0]
		&& ema120[0] < ema180[0]
		&& ema180[0] < ema240[0]
		&& ema240[0] > ema1440[0]
	) enterLong(Amount);
	
	if (crossUnder(ema5, ema45))
		exitShort();
	
	if (crossOver(ema5, ema45))
		exitLong();
}


[Linked Image]

Quote

Monte Carlo Analysis... Median AR 2070%
Win 10650$ MI 23156$ DD 2952$ Capital 666$
Trades 532 Win 62.6% Avg +25.0p Bars 59
CAGR 163492235397780660000000000000000000.00% PF 2.41 SR 0.00 UI 0% R2 1.00
Chart...


Finetuned af.
Likes to randomly pop up Margin Calls.


Re: Lapsa's very own thread [Re: Lapsa] #483960
08/18/21 10:58
08/18/21 10:58
Joined: Aug 2021
Posts: 237
L
Lapsa Offline OP
Member
Lapsa  Offline OP
Member
L

Joined: Aug 2021
Posts: 237
Ehler's "Modified Least-Squares Quadratics"


Code
#include <profile.c>

var ModifiedSMA(var* Data, var* FractionalCoefficients)
{
	var sum=0., coefficientSum=0.;
	int i;
	int period=sizeof(FractionalCoefficients);
	
	for(i=0; i<period; i++) {
		sum += (Data[i] * FractionalCoefficients[i]);
		coefficientSum += FractionalCoefficients[i];
	}

	return sum / coefficientSum;
}

function run()
{
	set(PLOTNOW);
	BarPeriod = 1;
	LookBack = 100;
	StartDate = 20210702;
	EndDate = 20210816;

	vars seriesClose = series(priceClose());

	var sma = SMA(seriesClose, 5);
	var c1[] = {7,24,34,24,7};
	var c2[] = {1,6,12,14,12,6,1};
	var c3[] = {-1,28,78,108,118,108,78,28,-1};
	var c4[] = {-11,18,88,138,168,178,168,138,88,18,-11};

	var m1 = ModifiedSMA(seriesClose, c1);
	var m2 = ModifiedSMA(seriesClose, c2);
	var m3 = ModifiedSMA(seriesClose, c3);
	var m4 = ModifiedSMA(seriesClose, c4);

	// [7 24 34 24 7] / 96
        // [1 6 12 14 12 6 1] / 52
        // [−1 28 78 108 118 108 78 28 −1] / 544
        // [−11 18 88 138 168 178 168 138 88 18 −11] / 980
	
	plot("SMA 5", sma, MAIN, RED);
	plot("c1", m1, MAIN, BLUE);
	plot("c2", m2, MAIN, GREEN);
	plot("c3", m3, MAIN, MAGENTA);
	plot("c4", m4, MAIN, CYAN);
}


[Linked Image]

Last edited by Lapsa; 08/18/21 10:58.
Re: Lapsa's very own thread [Re: Lapsa] #483962
08/18/21 11:26
08/18/21 11:26
Joined: Aug 2021
Posts: 237
L
Lapsa Offline OP
Member
Lapsa  Offline OP
Member
L

Joined: Aug 2021
Posts: 237
2 Pole Butterworth filter

Quote

The Butterworth filter is a type of signal processing filter designed to have a frequency response as flat as possible in the passband. It is also referred to as a maximally flat magnitude filter.

Properties of the Butterworth filter are:
- monotonic amplitude response in both passband and stopband
- Quick roll-off around the cutoff frequency, which improves with increasing order
- Considerable overshoot and ringing in step response, which worsens with increasing order
- Slightly non-linear phase response
- Group delay largely frequency-dependent



Code
#include <profile.c>

var Butterworth2Pole(var* Data, int Period)
{
	var a = exp(-1.414*PI/Period);
	var b = 2*a*cos(1.414*1.25*180/Period);
	var c2 = b;
	var c3 = -a*a;
	var c1 = 1-c2-c3;
	
	var* Filt = series(*Data,3);
	// syntax error? TODO: check what happened w/ SETSERIES
	//	SETSERIES(Data,0);
	series(Data[0],0); // is that an equivalent?
	return Filt[0] = c1*Data[0] + c2*Filt[1] + c3*Filt[2];
}

function run()
{
	set(PLOTNOW);
	BarPeriod = 1;
	LookBack = 100;
	StartDate = 20210702;
	EndDate = 20210816;

	vars seriesClose = series(priceClose());

	var sma = SMA(seriesClose, 5);
	var butt = Butterworth2Pole(seriesClose, 5);
	var butt3Pole = Butterworth(seriesClose, 5);
		
	plot("SMA 5", sma, MAIN, SILVER);
	plot("2 Pole Butt", butt, MAIN, RED);
	plot("3 Pole Butt", butt3Pole, MAIN, BLUE);
}


[Linked Image]
[Linked Image]

Last edited by Lapsa; 08/18/21 11:59.
Re: Lapsa's very own thread [Re: Lapsa] #483964
08/18/21 18:26
08/18/21 18:26
Joined: Aug 2021
Posts: 237
L
Lapsa Offline OP
Member
Lapsa  Offline OP
Member
L

Joined: Aug 2021
Posts: 237
Ehler's Decycler Oscillator (fixed)

Quote

Trigonometric functions (Sine, Cosine etc) expect angles in degrees (0..360), while in C and in most other languages angles are in radians (0..2*PI). Log is the logarithm base e as in C.


Seemingly best use:
- set long periods
- 0 crossover for uptrend
- 0 crossunder for downtrend


Code
#include <profile.c>

var rad(var degrees) { return degrees*PI/180; }
var rcos(var degrees) {	return cos(rad(degrees)); }
var rsin(var degrees) {	return sin(rad(degrees)); }

var DecyclerOscillator(var* Close, var HPPeriod1, var HPPeriod2) // 30 60
{	
	var alpha1;
	var alpha2;
	var* hp1 = series(0,3);
	var* hp2 = series(0,3);
	
	alpha1 = 
		(rcos(.707*360/HPPeriod1)+
		rsin(.707*360/HPPeriod1)-1)
		/rcos(.707*360/HPPeriod1);

	alpha2 = 
		(rcos(.707*360/HPPeriod2)+
		rsin(.707*360/HPPeriod2)-1)/
		rcos(.707*360/HPPeriod2);
	
	hp1[0] = (1-alpha1/2)*(1-alpha1/2)*
		(Close[0]-2*Close[1]+Close[2])+
		2*(1-alpha1)*hp1[1]-(1-alpha1)*
		(1-alpha1)*hp1[2];
	
	hp2[0] = (1-alpha2/2)*(1-alpha2/2)*
		(Close[0]-2*Close[1]+Close[2])+
		2*(1-alpha2)*hp2[1]-(1-alpha2)*
		(1-alpha2)*hp2[2];
	 
	return hp2[0] - hp1[0];
}

function run()
{
	set(PLOTNOW);
	BarPeriod = 1;
	LookBack = 100;
	StartDate = 20210702;
	EndDate = 20210816;

	vars closes = series(priceClose());
	
	var osc = DecyclerOscillator(closes, 30, 60);
	var zma = ZMA(closes, 30);

	plot("ZMA", zma, MAIN, BLACK);
	plot("Decycler OSC", osc, NEW, BLACK);
}


[Linked Image]

Refactored:

Code
var DecyclerOscillator(var* Close, var HPPeriod1, var HPPeriod2) // 30 60
{
	return HighPass2(Close, HPPeriod2) - HighPass2(Close, HPPeriod1);
}


Last edited by Lapsa; 08/21/21 23:34.
Re: Lapsa's very own thread [Re: Lapsa] #483965
08/18/21 19:25
08/18/21 19:25
Joined: Aug 2021
Posts: 237
L
Lapsa Offline OP
Member
Lapsa  Offline OP
Member
L

Joined: Aug 2021
Posts: 237
Quote

One of the amazing characteristics of a band-pass filter is that if the
center period of the filter is tuned to a static sine wave whose period is
the same as the center period of the filter, then there is absolutely no lag
in the output.

Re: Lapsa's very own thread [Re: Lapsa] #483967
08/19/21 20:24
08/19/21 20:24
Joined: Aug 2021
Posts: 237
L
Lapsa Offline OP
Member
Lapsa  Offline OP
Member
L

Joined: Aug 2021
Posts: 237
Ehler's "Adaptive Stochastic Indicator"

I'm proud and happy I pulled this one through.

Well... Sort of.

Running Tests multiple times produces different results.
Not sure why. Yay!

Something memory, variables, pointers whatnot related.
`MaxPwr` looks like potential troublemaker.

Interestingly, adaptive CCI source code has this:

Code
//Find Maximum Power Level for Normalization
MaxPwr = .991*MaxPwr[1];


Also - overall computation seems notoriously slow.
Like 3 seconds for a single day.
Something I've been doing wrong.

When it works - results look legit.

Haven't finished the book.
Not sure if this one is supposed to be equivalent of Zorro's bundled StochEhlers indicator.
They do look similar although Zorro's one got some of that buttery smoothing.

All in all - fun but unusable.

TODO:
try to identify the issue by replacing snippets with in-built stuff (e.g. center of gravity)


Code
#include <profile.c>

var rad(var degrees) { return degrees*PI/180; }
var rcos(var degrees) { return cos(rad(degrees)); }
var rsin(var degrees) { return sin(rad(degrees)); }

var AdaptiveStochastic(vars Close)
{
	var AvgLength = 3;
	var M;
	var N;
	var X;
	var Y;
	var alpha1;
	var* HP = series(0,3);
	var a1;
	var b1;
	var c1;
	var c2;
	var c3;
	var* Filt = series(0,3);
	var Lag;
	var count;
	var Sx;
	var Sy;
	var Sxx;
	var Syy;
	var Sxy;
	var Period;
	var Sp;
	var Spx;
	var* MaxPwr = series(0,3);
	var DominantCycle;
	
	var* Corr = series(0, 48);
	var* CosinePart = series(0, 48);
	var* SinePart = series(0, 48);
	var* SqSum = series(0, 48);
	var R[48][2];
	var Pwr[48];	
	
	//Highpass filter cyclic components whose periods are shorter than 48 bars
	alpha1 = (rcos(.707*360 / 48) + rsin(.707*360 / 48) - 1) / rcos(.707*360 / 48);
	
	HP[0] = (1 - alpha1 / 2)*(1 - alpha1 / 2)*
		(Close[0] - 2*Close[1] + Close[2]) + 
		2*(1 - alpha1)*HP[1] - 
		(1 - alpha1) * (1 - alpha1)*HP[2];
	
	
	//Smooth with a Super Smoother Filter from equation 3-3
	a1 = exp(-1.414*3.14159 / 10);
	b1 = 2*a1*rcos(1.414*180 / 10);
	c2 = b1;
	c3 = -a1*a1;
	c1 = 1 - c2 - c3;
	Filt[0] = c1*(HP[0] + HP[1]) / 2 + c2*Filt[1] + c3*Filt[2];
		
	//Pearson correlation for each value of lag
	for (Lag = 0; Lag < 48; Lag++) {		
		//Set the averaging length as M
		M = AvgLength;
		if (AvgLength == 0) M = Lag;
		Sx = 0;
		Sy = 0;
		Sxx = 0;
		Syy = 0;
		Sxy = 0;
		
		for (count = 0; count < M - 1; count++) {
			X = Filt[count];
			Y = Filt[Lag + count];
			Sx = Sx + X;
			Sy = Sy + Y;
			Sxx = Sxx + X*X;
			Sxy = Sxy + X*Y;
			Syy = Syy + Y*Y;
		}
		
		if ( (M*Sxx - Sx*Sx)*(M*Syy - Sy*Sy) > 0 ) {
			Corr[Lag] = (M*Sxy - Sx*Sy)/sqrt((M*Sxx-Sx*Sx)*(M*Syy-Sy*Sy));
		}
	}
	
	for (Period = 0; Period < 48; Period++) {
		CosinePart[Period] = 0;
		SinePart[Period] = 0;
		
		for(N = 3; N < 48; N++) {
			CosinePart[Period] = CosinePart[Period] + Corr[N]*rcos(360*N / Period);
			SinePart[Period] = SinePart[Period] + Corr[N]*rsin(360*N / Period);
		}
		SqSum[Period] = CosinePart[Period]*CosinePart[Period] +
		SinePart[Period]*SinePart[Period];
	}
	
	for (Period = 0; Period < 48; Period++) {
		R[Period][2] = R[Period][1];
		R[Period][1] = .2*SqSum[Period]*SqSum[Period] +.8*R[Period][2];
	}
	
	// Find Maximum Power Level for Normalization
	MaxPwr[0] = .995*MaxPwr[1];
	for (Period = 10; Period < 48; Period++) {
		if (R[Period][1] > MaxPwr[0]) MaxPwr[0] = R[Period][1];
	}
	
	for (Period = 3; Period < 48; Period++) {
		Pwr[Period] = R[Period][1] / MaxPwr[0];
	}
	
	//Compute the dominant cycle using the CG of the spectrum
	Spx = 0;
	Sp = 0;
	for(Period = 10; Period < 48; Period++) {
		if (Pwr[Period] >= .5) {
			Spx = Spx + Period*Pwr[Period];
			Sp = Sp + Pwr[Period];
		}
	}
	
	if (Sp != 0) DominantCycle = Spx / Sp;
	if (DominantCycle < 10) DominantCycle = 10;
	if (DominantCycle > 48) DominantCycle = 48;
	
	//Stochastic Computation starts here
	var* HighestC = series(0, 3);
	var* LowestC = series(0, 3);
	var* Stoc = series(0, 3);
	var* SmoothNum = series(0, 3);
	var* SmoothDenom = series(0, 3);
	var* AdaptiveStochastic = series(0, 3);
	
	HighestC[0] = Filt[0];
	LowestC[0] = Filt[0];
	
	for (count = 0; count < DominantCycle - 1; count++ ) {
		if (Filt[count] > HighestC[0]) HighestC[0] = Filt[count];
		if (Filt[count] < LowestC[0]) LowestC[0] = Filt[count];	
	}
	
	Stoc[0] = (Filt[0] - LowestC[0]) / (HighestC[0] - LowestC[0]);
	return AdaptiveStochastic[0] = 
		c1*(Stoc[0] + Stoc[1]) / 2 + c2*AdaptiveStochastic[1] + c3*AdaptiveStochastic[2];
}

function run()
{
	set(PLOTNOW);
	BarPeriod = 1;
	LookBack = 500;
	StartDate = 20210809;
	EndDate = 20210810;

	vars closes = series(priceClose(), 48);
	var astoch = AdaptiveStochastic(closes);
	
	var original = StochEhlers(closes, 48, 10, 48);
	
	plot("ASTOCH", astoch, NEW, RED);
        plot("Zorro`s EStoch", original, NEW, BLUE);
}


[Linked Image]
[Linked Image]

Last edited by Lapsa; 08/19/21 20:49.
Re: Lapsa's very own thread [Re: Lapsa] #483972
08/20/21 17:25
08/20/21 17:25
Joined: Aug 2021
Posts: 237
L
Lapsa Offline OP
Member
Lapsa  Offline OP
Member
L

Joined: Aug 2021
Posts: 237
Ehler's "Even Better Sinewave"

Looks usable.



Code
#include <profile.c>

var rad(var degrees) { return degrees*PI/180; }
var rcos(var degrees) {	return cos(rad(degrees)); }
var rsin(var degrees) {	return sin(rad(degrees)); }

var EvenBetterSinewave(vars Close, var Duration)
{
	var alpha1;
	var* HP = series(1,2);
	var a1;
	var b1;
	var c1;
	var c2;
	var c3;
	var* Filt = series(1,3);
	var count;
	var Wave;
	var Pwr;
	
	// HighPass filter cyclic components whose periods are shorter than Duration input
	alpha1 = (1 - rsin(360 / Duration)) / rcos(360 / Duration);
	HP[0] = .5*(1 + alpha1)*(Close[0] - Close[1]) + alpha1*HP[1];
	
	// Smooth with a Super Smoother Filter from equation 3-3
	a1 = exp(-1.414*3.14159 / Duration);
	b1 = 2*a1*rcos(1.414*180 / Duration);
	c2 = b1;
	c3 = -a1*a1;
	c1 = 1 - c2 - c3;
	Filt[0] = c1*(HP[0] + HP[1]) / 2 + c2*Filt[1] + c3*Filt[2];
	
	// 3 Bar average of Wave amplitude and power
	Wave = (Filt[0] + Filt[1] + Filt[2]) / 3;
	Pwr = (Filt[0]*Filt[0] + Filt[1]*Filt[1] + Filt[2]*Filt[2]) / 3;
	
	// Normalize the Average Wave to Square Root of the Average Power
	return Wave / sqrt(Pwr);
}

function run()
{
	set(PLOTNOW);
	BarPeriod = 1;
	LookBack = 100;
	StartDate = 20210702;
	EndDate = 20210816;

	vars close = series(priceClose());
		
	var evenBetterSinewave = EvenBetterSinewave(close, 10);
	
	plot("EBSW", evenBetterSinewave, NEW, BLACK);
}


[Linked Image]

Refucktored version:

Code
#include <profile.c>

var EvenBetterSinewave(vars Close, var Duration)
{
	var Wave;
	var Pwr;
		
	var* HP = series(HighPass1(Close, Duration), 2);
	if (HP[0] == 0) HP[0] = 1;
	
	var* Filt = series(Smooth(HP, Duration), 3);	
	Wave = SMA(Filt, 3);
	Pwr = (Filt[0]*Filt[0] + Filt[1]*Filt[1] + Filt[2]*Filt[2]) / 3;
	
	return Wave / sqrt(Pwr);
}

function run()
{
	set(PLOTNOW);
	BarPeriod = 1;
	LookBack = 100;
	StartDate = 20210702;
	EndDate = 20210816;

	vars close = series(priceClose());
		
	var evenBetterSinewave = EvenBetterSinewave(close, 10);
	
	plot("EBSW", evenBetterSinewave, NEW, BLACK);
}


Comparison with [CC] EBSW indicator on TradingView:

[Linked Image]


Interestingly - that one allows setting HighPass length separately from SuperSmoothFilter length.

Like this:

Code
var EvenBetterSinewave(vars Close, var HPLength, SSFLength)
{
	var Wave;
	var Pwr;
		
	var* HP = series(HighPass1(Close, HPLength), 2);
	if (HP[0] == 0) HP[0] = 1;
	
	var* Filt = series(Smooth(HP, SSFLength), 3);	
	Wave = SMA(Filt, 3);
	Pwr = (Filt[0]*Filt[0] + Filt[1]*Filt[1] + Filt[2]*Filt[2]) / 3;
	
	return Wave / sqrt(Pwr);
}


Lets smash in some trade signals!!!111eleven


Code
#include <profile.c>

var EvenBetterSinewave(vars Close, var Duration)
{
	var Wave;
	var Pwr;
		
	var* HP = series(HighPass1(Close, Duration), 2);
	if (HP[0] == 0) HP[0] = 1;
	
	var* Filt = series(Smooth(HP, Duration), 3);	
	Wave = SMA(Filt, 3);
	Pwr = (Filt[0]*Filt[0] + Filt[1]*Filt[1] + Filt[2]*Filt[2]) / 3;
	
	return Wave / sqrt(Pwr);
}

function run()
{
	set(PLOTNOW|PARAMETERS);
	BarPeriod = 1;
	LookBack = 250;
	StartDate = 20210702;
	EndDate = 20210816;

	vars close = series(priceClose());

	var* ebsw = series(EvenBetterSinewave(close, 225));
	
	if (crossOver(ebsw, -.99)) enterLong();	
	if (crossUnder(ebsw, .99)) enterShort();

	plot("EBSW", ebsw, NEW, BLACK);
}


[Linked Image]

Quote

Monte Carlo Analysis... Median AR 805%
Win 732$ MI 489$ DD 144$ Capital 448$
Trades 245 Win 38.8% Avg +29.9p Bars 181
AR 1311% PF 1.44 SR 0.00 UI 0% R2 1.00


Gotta fit that curve!

Last edited by Lapsa; 08/22/21 21:50.
Re: Lapsa's very own thread [Re: Lapsa] #483973
08/21/21 13:46
08/21/21 13:46
Joined: Aug 2021
Posts: 237
L
Lapsa Offline OP
Member
Lapsa  Offline OP
Member
L

Joined: Aug 2021
Posts: 237
Ehler's "Band-Pass Filter"


Code
#include <profile.c>

var rad(var degrees) { return degrees*PI/180; }
var rcos(var degrees) {	return cos(rad(degrees)); }
var rsin(var degrees) {	return sin(rad(degrees)); }

var BPSignal;
var BPTrigger;

var BandPassFilter(var* Close, var Period, var Bandwidth)
{
	var alpha2;
	var* HP = series(0, 3);
	var gamma1;
	var alpha1;
	var beta1;
	var* BP = series(0, 3);
	var* Peak = series(0, 2);
	var* Signal = series(0, 2);
	var* Trigger = series(0, 2);
	
	alpha2 = (
		rcos(.25*Bandwidth*360 / Period) + 
		rsin(.25*Bandwidth*360 / Period) - 1
	  )  / rcos(.25*Bandwidth*360 / Period);

	HP[0] = (1 + alpha2 / 2)*(Close[0] - Close[1]) + 
		(1- alpha2)*HP[1];
		
	beta1 = rcos(360 / Period);
	
	gamma1 = 1 / rcos(360*Bandwidth / Period);
	alpha1 = gamma1 - sqrt(gamma1*gamma1 - 1);
	
	BP[0] = .5*(1 - alpha1)*(HP[0] - HP[2]) + 
		beta1*(1 + alpha1)*BP[1] - alpha1*BP[2];

	Peak[0] = .991*Peak[1];
	
	if (abs(BP[0]) > Peak[0]) Peak[0] = abs(BP[0]);
	
	if (Peak[0] != 0) Signal[0] = BP[0] / Peak[0];
	
	alpha2 = (rcos(1.5*Bandwidth*360 / Period) + 
		rsin(1.5*Bandwidth*360 / Period) - 1) / 
		rcos(1.5*Bandwidth*360 / Period);

	Trigger[0] = 
		(1 + alpha2 / 2)*(Signal[0] - Signal[1]) +
		(1- alpha2)*Trigger[1];
		
	BPSignal = Signal[0];
	BPTrigger = Trigger[0];
}

function run()
{
	set(PLOTNOW);
	BarPeriod = 1;
	LookBack = 100;
	StartDate = 20210702;
	EndDate = 20210816;

	vars closes = series(priceClose());
	BandPassFilter(closes, 20, .3);
	
	plot("Signal", BPSignal, NEW, BLACK);
	plot("Trigger", BPTrigger, END, RED);
}


[Linked Image]

Lets smash in some trade signals!!!111eleven

Code
#include <profile.c>

var rad(var degrees) { return degrees*PI/180; }
var rcos(var degrees) {	return cos(rad(degrees)); }
var rsin(var degrees) {	return sin(rad(degrees)); }

var BPSignal;
var BPTrigger;

var BandPassFilter(var* Close, var Period, var Bandwidth)
{
	var alpha2;
	var* HP = series(0, 3);
	var gamma1;
	var alpha1;
	var beta1;
	var* BP = series(0, 3);
	var* Peak = series(0, 2);
	var* Signal = series(0, 2);
	var* Trigger = series(0, 2);
	
	alpha2 = (
			rcos(.25*Bandwidth*360 / Period) + 
			rsin(.25*Bandwidth*360 / Period) - 1
		) / rcos(.25*Bandwidth*360 / Period);

	HP[0] = (1 + alpha2 / 2)*(Close[0] - Close[1]) + 
		(1- alpha2)*HP[1];
		
	beta1 = rcos(360 / Period);
	
	gamma1 = 1 / rcos(360*Bandwidth / Period);
	alpha1 = gamma1 - sqrt(gamma1*gamma1 - 1);
	
	BP[0] = .5*(1 - alpha1)*(HP[0] - HP[2]) + 
		beta1*(1 + alpha1)*BP[1] - alpha1*BP[2];

	Peak[0] = .991*Peak[1];
	
	if (abs(BP[0]) > Peak[0]) Peak[0] = abs(BP[0]);
	
	if (Peak[0] != 0) Signal[0] = BP[0] / Peak[0];
	
	alpha2 = (rcos(1.5*Bandwidth*360 / Period) + 
		rsin(1.5*Bandwidth*360 / Period) - 1) / 
		rcos(1.5*Bandwidth*360 / Period);

	Trigger[0] = 
		(1 + alpha2 / 2)*(Signal[0] - Signal[1]) +
		(1- alpha2)*Trigger[1];
		
	BPSignal = Signal[0];
	BPTrigger = Trigger[0];
}

function run()
{
	set(PLOTNOW);
	BarPeriod = 1;
	LookBack = 300;
	StartDate = 20210810;
	EndDate = 20210816;
	//Capital = 500;

	vars closes = series(priceClose());
	BandPassFilter(closes, 23, .05821);
	
	var* signalSeries = series(BPSignal);
	var* triggerSeries = series(BPTrigger);	

	MaxLong = 1;
	MaxShort = 1;
	
	if (
		BPTrigger < BPSignal
		//&& BPSignal > -0.3
	) {
		if (ProfitOpen >= 0) enterLong();
		else exitShort();
	}
	if (
		BPTrigger > BPSignal
		//&& BPSignal < 0.3
	) {
		if (ProfitOpen >= 0)	enterShort();
		else exitLong();
	}
	
	plot("Signal", BPSignal, NEW, BLACK);
	plot("Trigger", BPTrigger, END, RED);
}


[Linked Image]

Quote

Monte Carlo Analysis... Median AR 4054%
Win 318$ MI 1481$ DD 134$ Capital 409$
Trades 424 Win 50.9% Avg +7.5p Bars 13
AR 4350% PF 1.22 SR 0.00 UI 0% R2 1.00


High signal count.

Only Period of 23 works seemingly well in combination with that weird bandwidth of .05821.

Fails to perform month before.


Last edited by Lapsa; 08/21/21 14:32.
Page 1 of 25 1 2 3 24 25

Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1