Best Algorithm to See If A price Is Rising Or Descending

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 06:07

Whats the best algorithm to find if a price is rising or descending. Im using average atm, is there a better more accurate way then averages, please let me know...

I found a script which uses loop parse to calc the average, but it doesnt work with arrays ...

A good script to see if a price is rising, would be great, ive included the basic dataset im using & the average script im using atm ...

Note the script doesnt work...

Thanks!

Code: Select all



a=
(
33,019.00
33,020.00
33,021.00
33,022.00
33,023.00
35,020.00
33,024.00
33,025.00
33,026.00
35,020.00
33,027.00
35,000.00
35,010.00
35,020.00
35,030.00
33,020.00
35,040.00
35,030.00
35,020.00
33,020.00
35,010.00
35,000.00
34,990.00
34,980.00
33,020.00
35,110.00
35,120.00
35,130.00
33,020.00
35,140.00
35,150.00
)

x++

if x:=10

loop,10
MyVar:=Clipboard
          
StringSplit, NewVarArray, a, `n ; Separates a string into an pseudo-array

loop, parse, NewVarArray, `n
{
	if A_LoopField <>
	{
		sum += A_LoopField
		count += 1
	}
}
msgbox % sum / count




User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 06:12

I appreciate the potential importance of the question. In seriousness, aren't you asking an economics question rather than a technical question?
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 06:25

Hi, technically yes, but ppl with better programming skills then me would have a better idea to program this then me, hence posting in here ...

Ive tried out min max, & finding the average, i just need some examples of some scripts to find the rise or fall of a price, im not too good with algorithms hence asking ...

Thanks.,
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 07:15

Code: Select all

str =
(
33,019.00
33,020.00
33,021.00
33,022.00
33,023.00
35,020.00
)
price := StrSplit(Trim(str, "`n"), "`n"), old := price[price.Count() - 1], new := price[price.Count()]
MsgBox, 64, Status
 , % "Old:  " old "`nNew: " new "`n`nDirection: " direction := new > old ? "Up" : new < old ? "Down" : "Same"
If you are actually looking for a trend formula, then I think you have the wrong forum.
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 07:19

The problem is the price spikes quite a bit,

As an example ... it jumps from 33,000 to 34,000 then back down to 33,000, 33,100, 33,200 then jumps to 33,900, 34,000, then back down to 33,000, 32,999, 33,100, 33,200

Or jumps downwards then back up again, ie 33,000 to 32,000 then back up to 33,000, 33,000, 33,200 etc.

Which is why im looking for something which finds the average, to see if the price is rising or falling "overall", over a range of time.

So basically is the price rising "overall" or on average, over a range of 5 minutes, because of all the spikes in the price ...
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 07:24

If you have a dataset with numbers and the first number is 33,019.00 and you want to count how many numbers in the set are higher than that you could use something like this

Code: Select all

greaterCount = 0
firstValue :=
a=
(
33,019.00
33,020.00
33,021.00
33,022.00
33,023.00
35,020.00
33,014.00
32,025.00
33,026.00
35,020.00
31,027.00
35,000.00
35,010.00
35,020.00
32,030.00
33,020.00
35,040.00
35,030.00
35,020.00
33,020.00
35,010.00
35,000.00
34,990.00
34,980.00
33,020.00
35,110.00
35,120.00
35,130.00
33,020.00
35,140.00
35,150.00
)
for i, v in array := StrSplit(a, "`n") {
	if (a_index = 1) {
		firstValue := v
		continue
	}
	(v >= firstValue) ? greaterCount++
}
x := greaterCount
y := array.length()
(x >= y // 2) ? result := x " of " y " are higher possibly trending up" : result := x " of " y " are higher possibly trending down"
MsgBox, % result
ExitApp
If you want to see a real recent trend the above code is of little value (depending on time frame) because you must the position of the numbers just like you would if you were a human looking at a chart. If you had 30 numbers you would give more weight to the more recent number.

It all really depends what you are doing, time frames, data needed etc. Most trading tools come built with all these formulas already.
User avatar
Chunjee
Posts: 1499
Joined: 18 Apr 2014, 19:05
Contact:

Re: Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 07:28

Dunno how much data your script is going to have access to but if only a day's worth or so; you could check if the average of the first half is bigger than the second half.

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

prices =
(
35,030.00
35,020.00
33,020.00
35,010.00
35,000.00
34,990.00
34,980.00
33,020.00
35,110.00
35,120.00
35,130.00
33,020.00
35,140.00
35,150.00
)

array := strSplit(prices, "`n")
; split the array in half
chunkedArray := A.chunk(array, array.count()/2)
firstHalfAverage := A.mean(chunkedArray[1])
secondHalfAverage := A.mean(chunkedArray[2])

if (firstHalfAverage > secondHalfAverage) {
	msgbox, % "The price average is down"
}
if (firstHalfAverage < secondHalfAverage) {
	msgbox, % "The price average is up"
}
if (firstHalfAverage == secondHalfAverage) {
	msgbox, % "The price is flat"
}
For a more robust BTC thingamajig; maybe google "self governing btc bot"
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 07:29

Also a mean computation here:

Code: Select all

str =
(
33,019.00
33,020.00
33,021.00
33,022.00
33,023.00
35,020.00
)
price := StrSplit(Trim(StrReplace(str, ","), "`n"), "`n"), mean := 0
For k, v in price
 mean += new := v, old := k < price.Count() ? v : old
mean /= price.Count()
MsgBox, 64, Status
 , % "Mean: " mean "`n"
   . "Old:    " old  "`n"
   . "New:   "  new  "`n`n"
   . "Direction: " direction := new > old ? "Up" : new < old ? "Down" : "Same"
bigA can maybe do the same in one line.
Last edited by mikeyww on 11 Jun 2021, 07:33, edited 1 time in total.
User avatar
boiler
Posts: 17392
Joined: 21 Dec 2014, 02:44

Re: Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 07:31

Taking all the data into account in determining the slope of the line through the data points:

Code: Select all

a=
(
33,019.00
33,020.00
33,021.00
33,022.00
33,023.00
35,020.00
33,024.00
33,025.00
33,026.00
35,020.00
33,027.00
35,000.00
35,010.00
35,020.00
35,030.00
33,020.00
35,040.00
35,030.00
35,020.00
33,020.00
35,010.00
35,000.00
34,990.00
34,980.00
33,020.00
35,110.00
35,120.00
35,130.00
33,020.00
35,140.00
35,150.00
)

Num := StrSplit(StrReplace(a, ","), "`n")
Xmean := (Num.MaxIndex() + 1) / 2
Ymean := Mean(Num)
SlopeNumerator := 0
for X, Y in Num
	SlopeNumerator += (X - Xmean) * (Y - Ymean)
SlopeDenominator := 0
for X, Y in Num
	SlopeDenominator += (X - Xmean)**2
Slope := SlopeNumerator / SlopeDenominator
if (Slope = 0)
	MsgBox, Neither increasing nor decreasing
else
	MsgBox, % Slope > 0 ? "Increasing" : "Decreasing"
return

Sum(n) {
	for k, v in n
		s += v
	return s
}

Mean(n) {
	return Sum(n) / n.MaxIndex()
}


roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 07:34

Awesome, thanks guys, im checking out your examples, much appreciated!
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 07:55

Thanks mikey, from what i understand your using a basic step ie is 13 higher then 12 to check if the price is rising,

Could you change the script so it uses the mean to see if the price is rising ...

So if the overall mean is closer to the highest price ie if the overall mean is 34,400 & the highest price is 35,000 its rising ... or for example the overall mean is 33,200 & the highest price is 35,000 its descending ...

Thanks, much appreciated!
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 08:31

Boiler, thanks!, your script works brilliantly, it seems to use the average from what i understand, to check the direction. Many thanks!
User avatar
boiler
Posts: 17392
Joined: 21 Dec 2014, 02:44

Re: Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 08:45

roonyrooxcess wrote:
11 Jun 2021, 08:31
Boiler, thanks!, your script works brilliantly, it seems to use the average from what i understand, to check the direction. Many thanks!
The average is just one component of the equation for finding the slope of the best fit line using the least square method.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 09:51

I wasn't really hoping that I had an answer for assessing the trend! The other approaches presented here for doing that are better.
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Best Algorithm to See If A price Is Rising Or Descending

11 Jun 2021, 12:45

I totally missed AHKStudents post, which is also a great approach, counts the number of highs vs lows. Thanks, very handy!

Also Chunjees suggestion is great, but ill be adding historical data later on to detect the overall trend, i just need to predict the current price direction.

Thanks guys, saved me huge amounts of time, much appreciated!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 173 guests