Gamestudio Links
Zorro Links
Newest Posts
Removing outliers from files
by JeyKey II. 11/29/26 10:01
Experiences with cTrader
by JeyKey II. 11/27/26 09:04
make a complaint
by EW_bytes. 11/26/26 23:00
FXCM => AVA Trade Plugin???
by Martin_HH. 11/25/26 21:59
No connection to MT4
by Martin_HH. 11/23/26 06:38
MinMax function on History t6 file
by LucasJoshua. 11/21/26 14:28
What are you working on?
by rayp. 11/20/26 18:10
AUM Magazine
Latest Screens
Dorifto samurai
Shadow 2
Rocker`s Revenge
Stug 3 Stormartillery
Who's Online Now
2 registered members (JeyKey II, alibaba), 8,028 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EvanTJ, rht, MaliceTheMaker, Haitao_Ren, JimSimons
19250 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Removing outliers from files #489780
11/04/26 10:30
11/04/26 10:30
Joined: Jul 2000
Posts: 28,149
Frankfurt
jcl Offline OP

Chief Engineer
jcl  Offline OP

Chief Engineer

Joined: Jul 2000
Posts: 28,149
Frankfurt
Some brokers, for instance IBKR, have sometimes heavy outliers in historical prices of exotic assets. Here's a script for removing outliers, created by GPT-6.

Code
// Zorro lite-C, by ChatGPT-6. Save in Strategy, select it,  click [Test].
// Each OHLC field is compared with the same field of the previous,
// already corrected candle. The oldest candle is the unchanged seed.
// Factor > 1: allowed interval is [Previous/Factor, Previous*Factor].
// Assumes positive prices. Timestamps, fVal, and fVol are preserved.
// The original is backed up with appended "bk".

// +1 = upward outlier, -1 = downward outlier, 0 = within bounds.
int outlierSide(var Price, var Previous, var Factor)
{
	if(Price > Previous*Factor) return 1;
	if(Price < Previous/Factor) return -1;
	return 0;
}

// Returns the number of repaired fields, or -1 on failure.
int removeT6Outliers(string FileName, var Factor)
{
	if(!(Factor > 1)) {
		printf("\nFactor must be greater than 1.");
		return -1;
	}

	if(strlen(FileName)+3 > 1024) {
		printf("\nFile path too long.");
		return -1;
	}
	
	char Backup[1024];
	strx(Backup,1024,FileName,".t6","bk.t6");

	// T6 has seven dataset fields: timestamp plus six floats.
	int N = dataLoad(1,FileName,7);
	if(N < 2) {
		printf("\nCannot read T6 candles from %s",FileName);
		return -1;
	}
	T6* Candles = (T6*)dataStr(1,0,0);
	size_t Bytes = (size_t)N*sizeof(T6);
	if(file_length(FileName) != Bytes) {
		printf("\nInvalid T6 file size.");
		return -1;
	}

	// Reject an incorrectly ordered file before modifying anything on disk.
	int i;
	for(i = 0; i < N-1; i++) {
		if(Candles[i].time < Candles[i+1].time) {
			printf("\nT6 timestamps must be in descending order.");
			return -1;
		}
	}

	int Fixed = 0;
	// Newest is index 0; the previous candle is at i+1.
	for(i = N-2; i >= 0; i--) {
		T6* Current = Candles+i;
		T6* Previous = Candles+i+1;
		if(!(Previous->fOpen > 0 && Previous->fClose > 0
			&& Previous->fHigh > 0 && Previous->fLow > 0)) {
			printf("\nNonpositive reference price at record %i; file unchanged.",i+1);
			return -1;
		}
		var Distance = abs((var)Previous->fOpen-Previous->fClose);
		int Side = outlierSide(Current->fOpen,Previous->fOpen,Factor);
		if(Side) {
			Current->fOpen = Previous->fOpen+Side*Distance;
			Fixed++;
		}
		Side = outlierSide(Current->fClose,Previous->fClose,Factor);
		if(Side) {
			Current->fClose = Previous->fClose+Side*Distance;
			Fixed++;
		}

		// Clip outlying wicks to the repaired candle body. Also ensure
		// that high and low enclose open and close after their repair.
		var Top = max(Current->fOpen,Current->fClose);
		var Bottom = min(Current->fOpen,Current->fClose);
		if(outlierSide(Current->fHigh,Previous->fHigh,Factor)
			|| Current->fHigh < Top) {
			Current->fHigh = Top;
			Fixed++;
		}
		if(outlierSide(Current->fLow,Previous->fLow,Factor)
			|| Current->fLow > Bottom) {
			Current->fLow = Bottom;
			Fixed++;
		}
	}

	// Make the backup from the untouched input, not the corrected array.
	if(!file_copy(Backup,FileName) || file_length(Backup) != Bytes) {
		printf("\nBackup failed; input not overwritten.");
		return -1;
	}
	if(!file_write(FileName,(string)Candles,Bytes)
		|| file_length(FileName) != Bytes) {
		printf("\nOutput write failed. Original preserved in %s.",Backup);
		return -1;
	}
	printf("\n%i fields repaired in %i candles.\nOutput: %s\nOriginal: %s",
		Fixed,N,FileName,Backup);
	return Fixed;
}

void main()
{
	var Factor = 1.03; // Adjust the maximum permitted price ratio here.
	string FileName = file_select("History","T6 files\0*.t6\0\0");
	if(FileName && FileName[0])
		removeT6Outliers(FileName,Factor);
}

Re: Removing outliers from files [Re: jcl] #489804
Yesterday at 17:57
Yesterday at 17:57
Joined: Nov 2026
Posts: 2
nothing
E
EvanTJ Offline
Guest
EvanTJ  Offline
Guest
E

Joined: Nov 2026
Posts: 2
nothing
Ugliest piece of code i ever saw. Where did chatgpt6 learn this code from?

Last edited by EvanTJ; Yesterday at 17:57.
Re: Removing outliers from files [Re: jcl] #489805
Yesterday at 18:19
Yesterday at 18:19
Joined: Nov 2026
Posts: 2
nothing
E
EvanTJ Offline
Guest
EvanTJ  Offline
Guest
E

Joined: Nov 2026
Posts: 2
nothing
Let me qualify before somebody judges me. This line if(!(Factor > 1)) is hilarious because negative of (Factor > 1) is Factor <= 1. So it's funny AI wrote Factor <= 1 that way.

Other line: strlen(FileName)+3 > 1024. is hilarious too. Because it's not necessary anyway. it could be only strlen(FileName) > MAX_PATH or strlen(FileName) > PATHSIZE.

Also filtering out the out of order bars is not responsibility of the algo. It should instead set a panic flag and stop modifying data until data makes sense (in order again) and throw a warning that some out of order data has been met. You could also supply STRICT_ORDERING to make it fix them instead of just quitting it's job which is unrelated to reordering the file.

Another issue is the silly pointer mathematics. Why all of a sudden it did:
Code
                T6* Current = Candles+i;
		T6* Previous = Candles+i+1;

It literally just did:
Code
if(Candles[i].time < Candles[i+1].time)
few lines above. All of a sudden array indexing is worse than pointer mathematics.

And finally a note on the algorithms. Removing outliers is bad in this case. The better option is taking the averages of two adjacent points and replacing the outlier so data coherency is maintained.

Theres more to say but I think this is enough.

JCL, this is an obvious slip-up on your part. You trusted AI blindly on this one.

Edit: I looked at the algorithm again and it's not removing outlier. it's mulplying them by distance between high and low of the candle ahead of it. But why is doing this:
Code
        if(Price > Previous*Factor) return 1;
	if(Price < Previous/Factor) return -1;
. Previous/Factor is throwing me off. Is AI smarter than me on this one or is it a bug? The rest of the algo looks just as confusing to me. At any rate, taking average of two adjacent points would have did better in backtests.

Last edited by EvanTJ; Yesterday at 18:36.
Re: Removing outliers from files [Re: EvanTJ] #489806
24 minutes ago
24 minutes ago
Joined: Dec 2002
Posts: 2,014
Switzerland Zürich
JeyKey II Online
Expert
JeyKey II  Online
Expert

Joined: Dec 2002
Posts: 2,014
Switzerland Zürich
I’m curious to see the response.
If there even is one. smile


[Jeder ist sich selbst am Nächsten]

Moderated by  Petra 

Gamestudio download | 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