Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,10 @@
|
|||
(lib 'tree) ;; queues operations
|
||||
|
||||
|
||||
(define (make-sma p)
|
||||
(define Q (queue (gensym)))
|
||||
(lambda (item)
|
||||
(q-push Q item)
|
||||
(when (> (queue-length Q) p) (q-pop Q))
|
||||
(// (for/sum ((x (queue->list Q))) x) (queue-length Q))))
|
||||
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Type FuncType As Function(As Double) As Double
|
||||
|
||||
' These 'shared' variables are available to all functions defined below
|
||||
Dim Shared p As UInteger
|
||||
Dim Shared list() As Double
|
||||
|
||||
Function sma(n As Double) As Double
|
||||
Redim Preserve list(0 To UBound(list) + 1)
|
||||
list(UBound(list)) = n
|
||||
Dim start As Integer = 0
|
||||
Dim length As Integer = UBound(list) + 1
|
||||
If length > p Then
|
||||
start = UBound(list) - p + 1
|
||||
length = p
|
||||
End If
|
||||
Dim sum As Double = 0.0
|
||||
For i As Integer = start To UBound(list)
|
||||
sum += list(i)
|
||||
Next
|
||||
Return sum / length
|
||||
End Function
|
||||
|
||||
Function initSma(period As Uinteger) As FuncType
|
||||
p = period
|
||||
Erase list '' ensure the array is empty on each initialization
|
||||
Return @sma
|
||||
End Function
|
||||
|
||||
Dim As FuncType ma = initSma(3)
|
||||
Print "Period = "; p
|
||||
Print
|
||||
For i As Integer = 0 To 9
|
||||
Print "Add"; i; " => moving average ="; ma(i)
|
||||
Next
|
||||
Print
|
||||
ma = initSma(5)
|
||||
Print "Period = "; p
|
||||
Print
|
||||
For i As Integer = 9 To 0 Step -1
|
||||
Print "Add"; i; " => moving average ="; ma(i)
|
||||
Next
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
define simple_moving_average(a::array,s::integer)::decimal => {
|
||||
#a->size == 0 ? return 0.00
|
||||
#s == 0 ? return 0.00
|
||||
#a->size == 1 ? return decimal(#a->first)
|
||||
#s == 1 ? return decimal(#a->last)
|
||||
local(na = array)
|
||||
if(#a->size <= #s) => {
|
||||
#na = #a
|
||||
else
|
||||
local(ar = #a->ascopy)
|
||||
#ar->reverse
|
||||
loop(#s) => { #na->insert(#ar->get(loop_count)) }
|
||||
}
|
||||
#s > #na->size ? #s = #na->size
|
||||
return (with e in #na sum #e) / decimal(#s)
|
||||
}
|
||||
// tests:
|
||||
'SMA 3 on array(1,2,3,4,5,5,4,3,2,1): '
|
||||
simple_moving_average(array(1,2,3,4,5,5,4,3,2,1),3)
|
||||
|
||||
'\rSMA 5 on array(1,2,3,4,5,5,4,3,2,1): '
|
||||
simple_moving_average(array(1,2,3,4,5,5,4,3,2,1),5)
|
||||
|
||||
'\r\rFurther example: \r'
|
||||
local(mynumbers = array, sma_num = 5)
|
||||
loop(10) => {^
|
||||
#mynumbers->insert(integer_random(1,100))
|
||||
#mynumbers->size + ' numbers: ' + #mynumbers
|
||||
' SMA3 is: ' + simple_moving_average(#mynumbers,3)
|
||||
', SMA5 is: ' + simple_moving_average(#mynumbers,5)
|
||||
'\r'
|
||||
^}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
import queues
|
||||
|
||||
proc simplemovingaverage(period: int): auto =
|
||||
assert period > 0
|
||||
|
||||
var
|
||||
summ, n = 0.0
|
||||
values = initQueue[float]()
|
||||
for i in 1..period:
|
||||
values.add(0)
|
||||
|
||||
proc sma(x: float): float =
|
||||
values.add(x)
|
||||
summ += x - values.dequeue()
|
||||
n = min(n+1, float(period))
|
||||
result = summ / n
|
||||
|
||||
return sma
|
||||
|
||||
var sma = simplemovingaverage(3)
|
||||
for i in 1..5: echo sma(float(i))
|
||||
for i in countdown(5,1): echo sma(float(i))
|
||||
|
||||
echo ""
|
||||
|
||||
var sma2 = simplemovingaverage(5)
|
||||
for i in 1..5: echo sma2(float(i))
|
||||
for i in countdown(5,1): echo sma2(float(i))
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
import: parallel
|
||||
|
||||
: createSMA(period)
|
||||
| ch |
|
||||
Channel new [ ] over send drop ->ch
|
||||
#[ ch receive + left(period) dup avg swap ch send drop ] ;
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
: test
|
||||
| sma3 sma5 l |
|
||||
3 createSMA -> sma3
|
||||
5 createSMA -> sma5
|
||||
[ 1, 2, 3, 4, 5, 5, 4, 3, 2, 1 ] ->l
|
||||
"SMA3" .cr l apply( #[ sma3 perform . ] ) printcr
|
||||
"SMA5" .cr l apply( #[ sma5 perform . ] ) ;
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
sequence sma = {} -- {{period,history,circnxt}} (private to sma.e)
|
||||
integer sma_free = 0
|
||||
|
||||
global function new_sma(integer period)
|
||||
integer res
|
||||
if sma_free then
|
||||
res = sma_free
|
||||
sma_free = sma[sma_free]
|
||||
sma[res] = {period,{},0}
|
||||
else
|
||||
sma = append(sma,{period,{},0})
|
||||
res = length(sma)
|
||||
end if
|
||||
return res
|
||||
end function
|
||||
|
||||
global procedure add_sma(integer sidx, atom val)
|
||||
integer period, circnxt
|
||||
sequence history
|
||||
{period,history,circnxt} = sma[sidx]
|
||||
sma[sidx][2] = 0 -- (kill refcount)
|
||||
if length(history)<period then
|
||||
history = append(history,val)
|
||||
else
|
||||
circnxt += 1
|
||||
if circnxt>period then
|
||||
circnxt = 1
|
||||
end if
|
||||
sma[sidx][3] = circnxt
|
||||
history[circnxt] = val
|
||||
end if
|
||||
sma[sidx][2] = history
|
||||
end procedure
|
||||
|
||||
global function get_sma_average(integer sidx)
|
||||
sequence history = sma[sidx][2]
|
||||
integer l = length(history)
|
||||
if l=0 then return 0 end if
|
||||
return sum(history)/l
|
||||
end function
|
||||
|
||||
global function moving_average(integer sidx, atom val)
|
||||
add_sma(sidx,val)
|
||||
return get_sma_average(sidx)
|
||||
end function
|
||||
|
||||
global procedure free_sma(integer sidx)
|
||||
sma[sidx] = sma_free
|
||||
sma_free = sidx
|
||||
end procedure
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
include sma.e
|
||||
|
||||
constant sma3 = new_sma(3)
|
||||
constant sma5 = new_sma(5)
|
||||
constant s = {1,2,3,4,5,5,4,3,2,1}
|
||||
integer si
|
||||
|
||||
for i=1 to length(s) do
|
||||
si = s[i]
|
||||
printf(1,"%2g: sma3=%8g, sma5=%8g\n",{si,moving_average(sma3,si),moving_average(sma5,si)})
|
||||
end for
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
load "stdlib.ring"
|
||||
decimals(8)
|
||||
maxperiod = 20
|
||||
nums = newlist(maxperiod,maxperiod)
|
||||
accum = list(maxperiod)
|
||||
index = list(maxperiod)
|
||||
window = list(maxperiod)
|
||||
for i = 1 to maxperiod
|
||||
index[i] = 1
|
||||
accum[i] = 0
|
||||
window[i] = 0
|
||||
next
|
||||
for i = 1 to maxperiod
|
||||
for j = 1 to maxperiod
|
||||
nums[i][j] = 0
|
||||
next
|
||||
next
|
||||
for n = 1 to 5
|
||||
see "number = " + n + " sma3 = " + left((string(sma(n,3)) + " "),9) + " sma5 = " + sma(n,5) + nl
|
||||
next
|
||||
for n = 5 to 1 step -1
|
||||
see "number = " + n + " sma3 = " + left((string(sma(n,3)) + " "),9) + " sma5 = " + sma(n,5) + nl
|
||||
next
|
||||
see nl
|
||||
|
||||
func sma number, period
|
||||
accum[period] += number - nums[period][index[period]]
|
||||
nums[period][index[period]] = number
|
||||
index[period]= (index[period] + 1) % period + 1
|
||||
if window[period]<period window[period] += 1 ok
|
||||
return (accum[period] / window[period])
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
load "stdlib.ring"
|
||||
decimals(8)
|
||||
maxperiod = 20
|
||||
nums = newlist(maxperiod,maxperiod)
|
||||
accum = list(maxperiod)
|
||||
index = list(maxperiod)
|
||||
window = list(maxperiod)
|
||||
for i = 1 to maxperiod
|
||||
index[i] = 1
|
||||
accum[i] = 0
|
||||
window[i] = 0
|
||||
next
|
||||
for i = 1 to maxperiod
|
||||
for j = 1 to maxperiod
|
||||
nums[i][j] = 0
|
||||
next
|
||||
next
|
||||
for n = 1 to 5
|
||||
see "number = " + n + " sma3 = " + left((string(sma(n,3)) + " "),9) + " sma5 = " + sma(n,5) + nl
|
||||
next
|
||||
for n = 5 to 1 step -1
|
||||
see "number = " + n + " sma3 = " + left((string(sma(n,3)) + " "),9) + " sma5 = " + sma(n,5) + nl
|
||||
next
|
||||
see nl
|
||||
|
||||
func sma number, period
|
||||
accum[period] += number - nums[period][index[period]]
|
||||
nums[period][index[period]] = number
|
||||
index[period]= (index[period] + 1) % period + 1
|
||||
if window[period]<period window[period] += 1 ok
|
||||
return (accum[period] / window[period])
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
### RING: Function Moving Average. Bert Mariani 2016-06-22
|
||||
|
||||
###------------------------------
|
||||
### Data array of Google prices
|
||||
|
||||
aGOOGPrices = ["658","675","670","664","664","663","663","662","675","693","689","675",
|
||||
"636","633","632","607","607","617","617","581","593","570","574","571","575","596",
|
||||
"596","601","583","635","587","574","552","531","536","502","488","482","490","503",
|
||||
"507","521","534","525","534","559","552","554","555","555","552","579","580","577",
|
||||
"575","562","560","559","558","569","573","577","574","559","552","553","560","569",
|
||||
"582","579","593","598","593","598","593","586","602","591","594","595","603","614",
|
||||
"620","625","635","627","632","631","620","626","616","606","602","659","683","671",
|
||||
"670","659","673","679"]
|
||||
|
||||
###-------------------------------------------------------------
|
||||
### CALL the Function: MovingAverage arrayOfPrices timePeriod
|
||||
|
||||
aGOOGMvgAvg = MovingAverage( aGOOGPrices, 10 )
|
||||
|
||||
aGOOGMvgAvg = MovingAverage( aGOOGPrices, 30 )
|
||||
|
||||
###-------------------------------------------------------------
|
||||
### FUNCTION: MovingAverage
|
||||
|
||||
Func MovingAverage arrayPrices, timePeriod
|
||||
|
||||
arrayMvgAvg = [] ### Output Results to this array
|
||||
z = len(arrayPrices) ### array data length
|
||||
sumPrices = 0
|
||||
|
||||
###--------------------------------
|
||||
### First MAvg Sum 1 to timePeriod
|
||||
###--------------------------------
|
||||
|
||||
for i = 1 to timePeriod
|
||||
sumPrices = sumPrices + arrayPrices[i]
|
||||
mvgAvg = sumPrices / i
|
||||
Add( arrayMvgAvg, mvgAvg)
|
||||
next
|
||||
|
||||
###-----------------------------------------------
|
||||
### Second MAvg Sum timePeriod +1 to End of Data
|
||||
###-----------------------------------------------
|
||||
|
||||
for i = timePeriod + 1 to z
|
||||
sumPrices = sumPrices - arrayPrices[i-timePeriod] + arrayPrices[i]
|
||||
mvgAvg = sumPrices / timePeriod
|
||||
Add (arrayMvgAvg, mvgAvg
|
||||
next
|
||||
|
||||
return arrayMvgAvg
|
||||
|
||||
###-------------------------------------------------------------
|
||||
OUTPUT Google Prices moving average using timePeriod = 10
|
||||
|
||||
Index 88 CurPrice 631 Sum 17735 MvgAvg 591.17
|
||||
Index 89 CurPrice 620 Sum 17797 MvgAvg 593.23
|
||||
Index 90 CurPrice 626 Sum 17854 MvgAvg 595.13
|
||||
Index 91 CurPrice 616 Sum 17897 MvgAvg 596.57
|
||||
Index 92 CurPrice 606 Sum 17926 MvgAvg 597.53
|
||||
Index 93 CurPrice 602 Sum 17954 MvgAvg 598.47
|
||||
Index 94 CurPrice 659 Sum 18054 MvgAvg 601.80
|
||||
Index 95 CurPrice 683 Sum 18185 MvgAvg 606.17
|
||||
Index 96 CurPrice 671 Sum 18303 MvgAvg 610.10
|
||||
Index 97 CurPrice 670 Sum 18413 MvgAvg 613.77
|
||||
Index 98 CurPrice 659 Sum 18503 MvgAvg 616.77
|
||||
Index 99 CurPrice 673 Sum 18594 MvgAvg 619.80
|
||||
Index 100 CurPrice 679 Sum 18694 MvgAvg 623.13
|
||||
###-------------------------------------------------------------
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
func simple_moving_average(period) {
|
||||
|
||||
var list = []
|
||||
var sum = 0
|
||||
|
||||
func (number) {
|
||||
list.append(number)
|
||||
sum += number
|
||||
if (list.len > period) {
|
||||
sum -= list.shift
|
||||
}
|
||||
(sum / list.length)
|
||||
}
|
||||
}
|
||||
|
||||
var ma3 = simple_moving_average(3);
|
||||
var ma5 = simple_moving_average(5);
|
||||
|
||||
[1 ..^ 6, 6 ^.. 1].map{@|_} -> each {|num|
|
||||
printf("Next number = %d, SMA_3 = %.3f, SMA_5 = %.1f\n",
|
||||
num, ma3.call(num), ma5.call(num))
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
class sma_generator(period, list=[], sum=0) {
|
||||
|
||||
method SMA(number) {
|
||||
list.append(number)
|
||||
sum += number
|
||||
if (list.len > period) {
|
||||
sum -= list.shift
|
||||
}
|
||||
(sum / list.len)
|
||||
}
|
||||
}
|
||||
|
||||
var ma3 = sma_generator(3)
|
||||
var ma5 = sma_generator(5)
|
||||
|
||||
for num in [@|(1..5), @|(1..5 -> flip)] {
|
||||
printf("Next number = %d, SMA_3 = %.3f, SMA_5 = %.1f\n",
|
||||
num, ma3.SMA(num), ma5.SMA(num))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue