Plot standard deviation lines in Pine Script - charts

I have this pine script I'm using on tradingview to plot a line that is the average of the highs and lows of a given time period.
I'd like to plot the standard deviation on the chart of this value:
highLowAvg =(sum(maxValue,length_avg) + sum(minValue,length_avg)) / (length_avg*2)
Is there an easy way to do this? Below is the whole script.
//Fill Arrays with latest LOWS/HIGHS
//Get highest HIGH and lowest LOW values from the arrays.
//Do Average between X highest highs and X lowest lows and plot line (Length can be modified)
//If price LOW is above line, Up Trending (Green) (Source can be modified)
//If price HIGH is below line, Down Trending (Red) (Source can be modified)
//If neither above, slow down in trend / reversal might occur (White)
study("Low - High Simple Tracker",overlay=true)
////==================Inputs
length = input(8)
length_avg = input(8)
up_Trend_Condition = input(high)
down_Trend_Condition = input(low)
stdev = stdev(highLowAvg, length)
////==================Array setup and calculations
lowArray = array.new_float(0)
highArray = array.new_float(0)
//Fill Lows to an array
for i = 0 to length-1
array.push(highArray,high[i])
//Fill Highs to an array
for i = 0 to length-1
array.push(lowArray,low[i])
//Get the highest value from the high array
maxValue= array.max(highArray)
//Get the lowest value from the low array
minValue= array.min(lowArray)
//Average between highest and lowest (length can be modifed)
highLowAvg =(sum(maxValue,length_avg) + sum(minValue,length_avg)) / (length_avg*2)
////////==================Plotting
colorHL = down_Trend_Condition > highLowAvg ? color.green : up_Trend_Condition < highLowAvg ?
color.red : color.white
plot(highLowAvg, color =colorHL, style = plot.style_line, linewidth = 2)

Related

Extrapolate Animation Curve (Endless game balancing curve)

I need a curve editor to make balancing of endless game then I tried to use AnimationCurve.
I need to set a curve to a certain range ex. [0;1] and if I want a value over 1, the result of the Evaluation have to extrapolate the curve. I want to be able to compute Y from X and X from Y.
The problem is AnimationCurve have only 3 WrapMode (Clamp, PingPong, Loop).
How to extrapolate an AnimationCurve ?
Is there a better tool to make curve with extrapolation (post and pre curve) ?
For real extrapolation I think you'd have to implement your own system based on Bézier mathematics. Me at least am not aware of unity providing it out of the box.
A work around for it could be to just define values beyond the 0 to 1 range to cover the extents, animation curves do allow this, I don't think there are to many issues with that.
Another solution, to stay in 0 to 1 range still but achieve the same effect, would be to model the curve from 0 to 1 so that it would cover extreme values within that range and remap the time for curve evaluation given by the object to a 0 to 1 range.
E.g.:
// define range extents
float rangeMin = -5f, rangeMax = 5f;
var range = 10f;
// range could be calculated at runtime if necessary:
// [to] (higher value) - [from] (lower value) = [range]
// 5f - -5f = 10f
var timeRaw = 0; // variable provided value
var time01 = (timeRaw - rangeMin) / range;
// reult by timeRaw = 0: (0 - -5) / 10 = 0.5
// reult by timeRaw = 5: (5 - -5) / 10 = 1.0
// reult by timeRaw = -5: (-5 - -5) / 10 = 0.0
Combining both solutions allow you to cover even more extreme values.

offset in atr in tradingview - pine script

Need to have offset in ATR function in pine script
Background: The indicator script below is based on the hypothesis that this period's range will be mostly within [last period high + atr(14)] and [last period low - atr(14)]. I want to sell the high call option and low put option and enjoy the premium at the end of the period (week, month).
I have created a pine script that will calculate this period range based on [last period high + atr(14)] and [last period low - atr(14)].
However, because atr(14) applies to current period as well, it plots the dots that change with the current price.
I need to have an atr(14) days till the last period and not considering this current period. Could you please advise how to achieve that.
//#version=3
study(title="High and Low Levels", shorttitle="HL Levels", overlay = true)
Width = input(2, minval=1)
SelectPeriod = input("W", defval="W", type=string)
LookBack = input(1, minval=1)
xHigh = high[LookBack]
xHigh := xHigh + (atr(14))
xLow = low[LookBack] - atr(14)
vS1 = xHigh
vR1 = xLow
plot(vS1, color=#ff0000, title="S1", style = circles, linewidth = Width)
plot(vR1, color=#009600, title="R1", style = circles, linewidth = Width)
Expected: the dots plotted should be plotted based on last period high + last period atr(14) and last period low - last period atr(14)
Actual: the dots plotted based on last week high + atr(14) till the current period and last week low - atr(14) till the current period. This is changing the dots based on the current price movement.
Maybe, I got it wrong, but I think what you want is to take the previous value of atr(14). So it looks like that:
xLow = low[LookBack] - atr(14)[1]
I think, you've got my idea.
This should help....
plot(vS1[1], color=#ff0000, title="S1", style = circles, linewidth = Width)
plot(vR1[1], color=#009600, title="R1", style = circles, linewidth = Width)

Calculate hue angle range for red colours

I'm trying to correctly calculate colour hue angle range. Given an input default hue say 120 and a threshold value of 20 the range is between 100 - 140 (yes, I know - complex math).
Now in the application, when filtering an image, I can check if a given pixel falls into that range:
let inputHue = 120
let threshold = 20
let minHue = inputHue - threshold // 100
let maxHue = inputHue + threshold // 140
if (pixelHue > minHue && pixelHue < maxHue) {
// do something
}
Now the problem is with red colours range where the most saturated red colour is at 0/360 on the colour wheel. Given an input hue of 10 the minHue is now -10 (with a threshold of 20) and maxHue is 30. Because of that negative value for minHue the condition fails:
let pixelHue = 355 // this falls into a valid red range I want to get
let minHue = -10
let maxHue = 30
if (pixelHue > minHue && pixelHue < maxHue) {
// do something
}
Does anyone know how to tackle this problem of a colour wheel? I'm trying to develop a general solution that would work for any given input hue (not only red colours).
Thanks in advance.
Hues should be considered modulo 360. Hence the range for red is 0-30 AND 350-360.
Now your value of 355 passes because (355>350 AND 355<30) OR (350>30 AND (355>350 OR 355<30)).
The critical bit here starts after the first OR. In normal arithmetic, you check against lower and upper bound, and you have to pass both tests. But in modular arithmetic, if the lower bound is higher than the upper bound, the range wraps around 0 and you only need to pass one of the two both tests.
To recap: (hue>min AND hue<max) OR (min>max AND (hue>min OR hue<max))
A possible approach: Imagine both hue values as points on a circle, and compare the "shorter" angular distance between these points with the threshold:
let diff = abs(pixelHue - inputHue)
if min(diff, 360 - diff) <= threshold {
// do something
}

How to find maximum pixel intensity of the two regions obtained after finding the threshold of the image

We are working on DIP project where we found out the threshold of the gray scale image. Now we have to * find the maximum intensity of the two regions that we got, one region whose pixels are less than the threshold and the other whose pixels are greater than it. *
PS. We are not converting the image into binary image after finding the threshold. We just have to separate pixels in two regions and find the maximum intensity in each region
PS: We are working on MATLAB
Actually it is pretty simple to do.
Here is a function to do so:
function [ lowThrMaxIntns, highThrMaxIntns ] = FindMaxIntnesity ( mInputImage, thrLevel )
% Pixels which are lower than the threshold level
mLowThrImage = zeros(size(mInputImage));
% Pixels which are higher than the threshold level
mHightThrImage = zeros(size(mInputImage));
mLowThrImage(mInputImage < thrLevel) = mInputImage(mInputImage < thrLevel);
mHightThrImage(mInputImage >= thrLevel) = mInputImage(mInputImage >= thrLevel);
[lowThrMaxIntns lowThrMaxIntnsIdx] = max(mLowThrImage(:));
[highThrMaxIntns highThrMaxIntnsIdx] = max(mHightThrImage(:));
end
The output are only the intensities of the pixels.
If you need the pixel location, use the Idx variables (Use ind2sub for the sub scripts form).

Changing the axis range dynamically doesnt refresh the lineplot but only the axis labels

I am trying to change the axes Min and Max properties to show only the plot points that lie in a particular range.
say If I have lineplot whose X values are ranging from 0 to 100, I want to display only the values that are greater than 50.However I noticed that the lineplot is drawn using all the position points.Only the x axis ticks got renamed such that they start from 50 and end at 100.
The following is the code I am using
var axes = m_plotCube.First<ILAxisCollection>();
if (axes != null)
{
ILAxis xAxis = axes.Where<ILAxis>(item => item.Label.Text == "X Axis").First();
xAxis.Min = 50;
xAxis.Max = 100;
xAxis.Configure();
}
Am I missing something ?
Use ILPlotCube.Limits instead:
var pc = ilPanel1.Scene.First<ILPlotCube>();
pc.Limits.Set(
new Vector3(50, pc.Limits.YMin, pc.Limits.ZMin),
new Vector3(100, pc.Limits.YMax, pc.Limits.ZMax));
BTW: the axes are accessed much easier: plotcube.Axes.XAxis ...