Stochastic

Overview



A breakout occurs when a price crosses a prior maximum or minimum value of the series. Breakouts require a time period to specify. For example, you could ask for a 20 breakout, which means the point where the price crosses the point where it becomes the maximum value for the last 20 days (or more).
{% \%K = \frac{Close - Low}{High - Low} %}
where:

  • Close - the most recent close
  • High - the highest close in the recent period
  • Low - the lowest close in the recent period


where period is a predefined number of days, for example 14 days.
{% \%D = Moving \; Average \; of \; \%K %}

Calculation


The following sample code demonstrates calculating the stochastic oscillator using the $list api.



let data = [{price:100},{price:102},{price:103},{price:101},{price:103},{price:99},{price:100},{price:101},{price:100},];

let data2 = data.map((p,i,data)=>{
	if(i<14) return { ...p } 
	let window = $list(data).take(i-14,i).map(p=>p.price).items;
	let min = Math.min(...window);
	let max = Math.max(...window);
	return {
		...p,
		"%K" : (p.price - min)/(max - min)
	};
});