How to Plot conditionally on PINE v5? - pine-script-v5

How do I plot x and x2 only when the condition is sell/buy? Can't seem to figure that out on my own.
Here's the script
// SIGNAL ENTRY
sell= ema1 > ema2 and close < low[1] and open > close and open[1] > close[1] and close - open < close[1] - open[1] and ta.crossunder(jma, w_overbought)
buy= ema1 < ema2 and close > high[1] and open < close and open[1] < close[1] and close - open > close[1] - open[1] and ta.crossover(jma, w_oversold)
//PLOT ON CHART
plotshape(buy,title="Buy",text='Buy',color=color.green,textcolor=color.white, style=shape.labelup,location=location.belowbar,size=size.tiny)
plotshape(sell, title="Sell",text='Sell',color=color.red,textcolor=color.white, style=shape.labeldown,location=location.abovebar,size=size.tiny)
p1 = plot(x, title='ATR Short Stop Loss', color=colshort, trackprice=pline ? true : false, transp=20, style=plot.style_circles)
p2 = plot(x2, title='ATR Long Stop Loss', color=collong, trackprice=pline ? true : false, transp=20, style=plot.style_circles)

Related

How to define a peicewise function in a function file - Matlab

I made a funnction file and defined a peicewise function inside it using conditionals and a for loop. I tried calling the function in a seperate m.file but the variables 't' and 'v' aren't showing in the workspace, rather it is just outputting a vector with the t values called 'ans.
I tried putting the exact code (without the function definition) into a regular m file and it worked just fine showing both variables t and v
#function file
function [t, v] = VPieceWise(t_start, t_end);
t = t_start:0.01:t_end;
for i = 1:length(t);
if (t(i) >= 0) && (t(i) <= 10);
v(i) = 11.*(t(i).^2) - (5.*t(i));
elseif (t(i) >= 10) && (t(i) <= 20);
v(i) = 1100 - 5.*t(i);
elseif (t(i) >= 20) && (t(i) <= 30);
v(i) = 50.*t(i) + 2*((t(i)-20).^2.5);
elseif (t(i) >= 30) && (t(i) <= 100);
v(i) = 1520.*exp(-0.1.*(t(i)-30));
elseif (t(i) >= -100) && (t(i) <= 0);
v(i) = 0;
end
end
end
#m file
clear all; clc; close all
t_start = input('enter the start time');
t_end = input('enter the end time');
VPieceWise(t_start,t_end)
plot(t,v)
Since your function has two outputs, you should also assign them when calling the function. If you do not do that, only the first output will be put in the ans variable.
So call your function as follows:
clear all; clc; close all
t_start = input('enter the start time');
t_end = input('enter the end time');
[t,v] = VPieceWise(t_start,t_end);
plot(t,v)

An array for multiple LHS assignment cannot contain LEX_TS_STRING error

New to programing in matlab. I am currently trying to make a MATLAB program that will find the critical values of a multi-variable function and tell me whether each are a minimum, maximum, or saddle point. Unfortunately I always get the error : An array for multiple LHS assignment cannot contain LEX_TS_STRING
Any help will be very appreciated.
here's the code:
function [c,d] = critcalpoints(f)
syms x y
f(x,y)=x^3-3*x^2+5*x*y-7*y^2;
gradf = jacobian(f(x,y));
hessmatf = jacobian(gradf,[x,y]);
[xcr,ycr]=solve(gradf(1),gradf(2));
H1=subs(hessmatf,[x,y],[xcr(1),ycr(1)]);
H2=subs(hessmatf,[x,y],[xcr(2),ycr(2)]);
eig(H1);
eig(H2);
c = double(eig(H1));
d = double(eig(H2));
if (c(1) > 0 && d(1) > 0) || (c(2) > 0 && d(2) > 0)
print([xcr,ycr],' is a minimum')
elseif (c(1) < 0 && d(1) < 0) || (c(2) < 0 && d(2) < 0)
print( [xcr, ycr], ' is a maximum')
elseif (c(1) < 0 && d(1) > 0) || (c(1) > 0 && d(1) < 0)
print( [xcr, ycr], ' is a saddle point')
elseif (c(2) < 0 && d(2) > 0) || (c(2) > 0 && d(2) < 0)
print( [xcr, ycr], ' is a saddle point')
elseif (c(1)==0 || d(1)==0)
print( [xcr, ycr], ' is degenerate')
elseif (c(2)==0 || d(2)==0)
print( [xcr, ycr], ' is degenerate')
end
Cannot reproduce your error. The code can work but you need to change print to something else as print doesn't do what you think it does. The print function prints a figure that is open to file. Change it so that you display xcr and ycr first then display the right condition to satisfy after. Use disp instead:
syms x y
f(x,y)=x^3-3*x^2+5*x*y-7*y^2;
gradf = jacobian(f(x,y));
hessmatf = jacobian(gradf,[x,y]);
[xcr,ycr]=solve(gradf(1),gradf(2));
H1=subs(hessmatf,[x,y],[xcr(1),ycr(1)]);
H2=subs(hessmatf,[x,y],[xcr(2),ycr(2)]);
eig(H1);
eig(H2);
c = double(eig(H1));
d = double(eig(H2));
disp([xcr, ycr]); % Display the solutions first
if (c(1) > 0 && d(1) > 0) || (c(2) > 0 && d(2) > 0)
disp('is a minimum')
elseif (c(1) < 0 && d(1) < 0) || (c(2) < 0 && d(2) < 0)
disp('is a maximum')
elseif (c(1) < 0 && d(1) > 0) || (c(1) > 0 && d(1) < 0)
disp('is a saddle point')
elseif (c(2) < 0 && d(2) > 0) || (c(2) > 0 && d(2) < 0)
disp('is a saddle point')
elseif (c(1)==0 || d(1)==0)
disp('is degenerate')
elseif (c(2)==0 || d(2)==0)
disp('is degenerate')
end
I get:
[ 0, 0]
[ 59/42, 295/588]
is a maximum

dynamic parameter based on character of amplitude wav file matlab

i have implemented noise gate to make noise(that's not include speech) in speech wav file to become silence, but there are five parameters(sended by main program) that i must fill the parameters by my self. i have question, how can my program give suggest parameters every i input the speech wav file with different character of noise?
what i'm thinking: can i use the average of speech wav file amplitudes to get suggest parameters?
function n = noisegate( x, Fs, AT, RT, holdtime, ltrhold, utrhold)
% ------- Parameter Input ---------
% x - Input file
% Fs - sample frequency
% AT - Attack time
% RT - Release time
% ltrhold - lower threshold
% utrhold - upper threshold
att = round(AT*Fs); %calculate attack time in samples
rel = round(RT*Fs); %calculate release time in samples
ht = round(holdtime*Fs); %calculate hold time in samples
g = zeros(size(x)); %allocate an array for gain
%initialize low and high threshold counters
ltcnt = 0;
utcnt = 0;
%Calculate RMS Peak
%time average for peak measurement
tav = 0.2;
TAV =1 - exp(-2.2/(Fs*tav));
xrms(1) = 0;
for n = 2 : length(x)
xrms(n) = (1-TAV)*xrms(n-1) + TAV*x(n)^2;
if (xrms(n) <= ltrhold || (xrms(n) < utrhold && ltcnt > 0))
ltcnt = ltcnt + 1;
utcnt = 0;
if (ltcnt > ht), %hold time of low threshold exceeded
if(ltcnt > (ht + rel)) %hold and release time exceeded
%set gain to zero to completely fade out the sound
g(n) = 0;
else
%fade out the sound here
g(n) = 1 - (ltcnt - ht)/rel;
end
elseif (ltcnt < ht & (ltcnt == n))
g(n) = 0;
else
g(n) = 1;
end
elseif (xrms(n) > utrhold || (xrms(n) > ltrhold && utcnt > 0))
ltcnt = 0;
utcnt = utcnt + 1;
if(utcnt > ht)
%maximum attack and hold time exceeded
if(utcnt > (ht + att))
%set gain to 1
g(n) = 1;
else
%applay gain smoothening
g(n) = (utcnt - ht)/att;
end
else
g(n) = 0;
end
end
end
n = x.*g;
end

How to chose random lines that hit a rectangular in MatLab

I am creating random starting and ending points. I want to plot those who cross/intersect with a rectangular which is placed at the origin. I found out that my code misses some lines, as shown in the figure. After that, I want to count were the tracks hit the rectangle. For expample the track came from top side and exited from right side etc.
My code is
function [hot, cold, h] = MuonTracks(tracks)%#eml
% NOTE: no variables larger than 1x1 are initialized
width = 1e-4;
height = 2e-4;
% constant used for Laplacian noise distribution
bL = 15 / sqrt(2);
% Loop through all tracks
X = [];
bhit= [];
hot = 0;
ii = 0;
TopBottom= 0;
LeftRight= 0;
TopRight= 0;
TopLeft= 0;
LeftBottom= 0;
RightBottom= 0;
ihit= 0;
while ii <= tracks
ii = ii + 1;
% Note that I've inlined (== copy-pasted) the original laprnd()
% function call. This was necessary to work around limitations
% in loops in Matlab, and prevent the nececessity of those HUGE
% variables.
%
% Of course, you can still easily generalize all of this:
% the new data
u = rand-0.5;
Ystart = 15;
Xstart = 80*rand-40;
Xend = Xstart - bL*sign(u)*log(1-2*abs(u));
%Xend=laprnd(tracks,1,Xstart,15);
b = (Ystart*Xend)/(Xend-Xstart);
% the test
if ((b < height && b > 0)) ||...
(Xend < width/2 && Xend > -width/2)
hot = hot+1;
% growing an array is perfectly fine when the chances of it
% happening are so slim
X = [X [Xstart; Xend]]; %#ok
bhit=b;
end
end
% This is trivial to do here, and prevents an 'else'
cold = tracks - hot;
% Now plot the chosen ones
h = figure;
hold all
%Y = bsxfun(#times, 15, ones(size(X)));
if (size(X)==0)
%Disp('No hits were found');
msgbox('No tracks were found','Result','warn');
elseif (size(X)>1)
Y = bsxfun(#times, [15; 0], ones(size(X)));
plot(X, Y, 'r');
msgbox( [num2str(hot) ' tracks were found'],'Result','help',num2str(hot));
else
Y = bsxfun(#times, [15; 0], ones(size(X)));
plot(X, Y, 'r');
msgbox( [num2str(hot) ' track was found'],'Result','help',num2str(hot));
end
%X;
%Y;
%size(X,2)
while ihit<size(X,2)
ihit=ihit+1
%X(2,ihit)
if ((X(2,ihit)==-width && (bhit<=0 && bhit<=height))&&(bhit==0 && (X(2,ihit)>=-width && X(2,ihit)>=width)))
LeftBottom=LeftBottom+1;
elseif ((bhit==height && (X(2,ihit)>=-width && X(2,ihit)>=width)) && (X(2,ihit)==width && (bhit<=0 && bhit<=height)))
TopRight=TopRight+1;
elseif ((bhit==height && (X(2,ihit)>=-width && X(2,ihit)>=width)) && (bhit==0 && (X(2,ihit)>=-width && X(2,ihit)>=width)))
TopBottom=TopBottom+1;
elseif ((X(2,ihit)==-width && (bhit<=0 && bhit<=height)) && (X(2,ihit)==width && (bhit<=0 && bhit<=height)))
LeftRight=LeftRight+1;
elseif ((X(2,ihit)==-width && (bhit<=0 && bhit<=height)) && (bhit==height && (X(2,ihit)>=-width && X(2,ihit)>=width)))
TopLeft=TopLeft+1;
elseif ((X(2,ihit)==width && (bhit<=0 && bhit<=height)) && (bhit==0 && (X(2,ihit)>=-width && X(2,ihit)>=width)))
RightBottom=RightBottom+1;
else
display('sth is wrong');
end
X(2,ihit)
end
%X(1,:);
%Y(1,:);
LeftBottom
TopRight
TopBottom
LeftRight
TopLeft
RightBottom
%display('sdfghjk');
end
Any ideas would be more than welcome!
Here you have a function that is capable of intersecting whole groups of segments and returning the intersection points. I hope it helps.

Matlab - how to plot step function

I am having a hard time plotting a step function. The function involves is the Haar scaling function which is defind as:
ø(x) = 1 if 0 ≤ x < 1
ø(x) = 0 otherwise
I am supposed to plot the following function:
f(x) = 2ø(4x) + 2ø(4x - 1) + ø(4x - 2) - ø(4x - 3)
This is supposed to give me a plot where f = 2 on the interval 0 ≤ x < 0.5; f = 1 on the interval 0.5 ≤ x < 0.75; f = -1 on the interval 0.75 ≤ x < 1, and f = zero otherwise.
I tried the following code:
f = #(t) 2*(4*t > 0) + 2*(4*t > 1) + (4*t > 2) - (4*t > 3);
t = linspace(-2,2,100);
stairs(t,f(t))
However, this does not give me an accurate graph. So what am I doing wrong here? Any help will be greatly appreciated!
Your implementation of f only deals with half of your specification of phi.
f = #(t) 2*(4*t > 0) + 2*(4*t > 1) + (4*t > 2) - (4*t > 3);
In each of the terms applies the inequality 0 < X, rather than 0 <= X. Also nothing is done about the X < 1 inequality.
Rather than trying to make a custom version for each term, why not code up your formula directly?
phi = #(x) x >= 0 & x < 1;
f = #(x) 2*phi(4*x) + 2*phi(4*x-1) + phi(4*x - 2) - phi(4*x - 3);
it should be:
f = #(t) 2*(4*t > 0 & 4*t < 1) + 2*(4*t > 1 & 4*t < 2) + (4*t > 2 & 4*t < 3) - (4*t > 3);
Because every segment should be defined precisely with start and end values.