Cast constant value from series - constants

I'm completely new to this forum and tradingview/pinescript. I am trying to script an indicator, and part of the script requires assigning a constant value to a variable from a series.
From a moving average of the volume (50day average volume), I want to set the constant variable 'reference_Vol' to the value of the moving average from yesterday's price bar only and no other days in the past before yesterday, but the variable gets overwritten after each older bar as 'volume' is a series, so effectively the variable is not constant but dynamically changes over the history of the price bars.
SMA_50d = sma(volume,50)
reference_Vol =SMA_50d[1]
I have tried setting the value as a literal (literal int(x)), messing around with the valuewhen() function, but I have just not enough scripting experience of what I am doing. I have a feeling there is an easy solution to this, and I could really use some help.
Thanks a bunch!

This should do what you intended
//#version=5
indicator("My Script", overlay=false)
var float reference_Vol = na
new_day = ta.change(dayofmonth)
SMA_50d = ta.sma(volume,50)
if new_day
reference_Vol := SMA_50d[1]
plot(reference_Vol, style=plot.style_linebr, color=new_day ? na : color.blue)

Related

LO Calc Basic - What is the right property name for the axis major/minor settings and how to set them correctly?

Using a libreoffice basic macro for charts, we can control the maximum value of an axis and turn the automatic mode on/off:
oAxis.AutoMax = False
oAxis.Max = 12345
But what are the right property names for
Major Interval
Major Auto
Major Time
which you can set manually
???
First, I created a chart with Insert Chart > Line > Points and Lines.
Modifying the Y axis with code was fairly straightforward.
For both axes, I went into formatting and specified Positioning > Interval Marks > Minor > Outer so that the minor ticks are shown.
oCharts = ThisComponent.getSheets().getByIndex(0).getCharts()
oEmbeddedObject = oCharts.getByIndex(0).getEmbeddedObject()
oDiagram = oEmbeddedObject.getDiagram()
oYAxis = oDiagram.getYAxis()
oYAxis.StepMain = 40.0
oYAxis.StepHelpCount = 3
Here is what the Y Axis properties looked like after running the code:
AutoStepMain (and the corresponding Major interval checkbox) started out as True, but setting the StepMain value via macro changed it to False.
With the X axis, things were more complex. For the scale, there was a choice of Type, and selecting Date seemed to be the only way to control step settings.
After that, instead of StepMain (which didn't seem to be relevant in this case), there is a complex structure called ExplicitTimeIncrement that specifies the type of increment (Days or Months) along with each value. I didn't dig too far into it, but it looks like it has all of the values you were asking about.
EDIT:
I tried the following code, but none of the values were changed, and the last line throws an error stating that the property is read-only (as also shown by MRI). So perhaps the values cannot be modified via the API.
sTimeIntervalMajor = CreateUnoStruct("com.sun.star.chart.TimeInterval")
sTimeIntervalMajor.Number = 4
sTimeIntervalMajor.TimeUnit = 0
sTimeIntervalMinor = CreateUnoStruct("com.sun.star.chart.TimeInterval")
sTimeIntervalMinor.Number = 1
sTimeIntervalMinor.TimeUnit = 0
sTimeIncrement = CreateUnoStruct("com.sun.star.chart.TimeIncrement")
sTimeIncrement.MajorTimeInterval = sTimeIntervalMajor
sTimeIncrement.MinorTimeInterval = sTimeIntervalMinor
sTimeIncrement.TimeResolution = 1
oXAxis = oDiagram.getXAxis()
oXAxis.ExplicitTimeIncrement.MajorTimeInterval = sTimeIntervalMajor
oXAxis.setPropertyValue("ExplicitTimeIncrement", sTimeIncrement)
oXAxis.ExplicitTimeIncrement = sTimeIncrement
It might also be worth posting at ask.libreoffice.org or forum.openoffice.org to see if anyone there can find a way to modify the values, with a link to this question.
Of course, the UNO API isn't the only possibility. You could write a script to unzip the .ods file and modify the XML code with a parsing library such as xml.etree or regular expressions.

How to create a variable in Matlab where certain subjects are coded as 1?

I want to create a variable called 'flag_artifact' where certain subjects from my dataset (for whom I know have bad quality images) are coded as e.g., 1. My dataset is stored in a table T with a certain number of rows and 'subject' is the 1st column in the table.
I managed to do it by creating a for loop. Surely there is a more efficient way to do this by perhaps directly creating the variable and using less lines of code? Could anyone give some advice?
Thank you very much! Here is what I have:
flag_artifact = {'T_300'}; %flagging subject number 300 for example
for i = 1:size(T,1)
if isequal(table2cell(T(i, 1)), flag_artifact)
T(i, 1) = {'1'};
end
end
However, when creating the variable flag_artifact = {'T_300'}, I would like it to include more than one subject. I tried using flag_artifact = {'T_300'}; {'T_301'}, as well as flag_artifact = {'T_300', 'T_301'} but it doesn't work because these subject identifiers do not get replaced with 1s.

Change figure name in every loop Matlab

I'm a beginner in Matlab but I must use it for my master thesis...
I must change the name of my figure and the name of the file I'll save for every loop I've tried that using the function "eval" as in the figure name is supposed to be written, as "Figure - Date which will change at each loops" but the way I used it is wrong. What could another function do this work? Or how do I have to change the setting of eval?
NAME={'Sept-Oct 2015','Nov 2015','Jan-Fe 2016','Fev 2016','Mars-Av 2016','Av-Mai 2016','Juin 2016','Juil 2016','Août 2016','Sept 2016','Oct 2016','Nov 2016','Déc 2016'};
for k=1:13
plot(time,data,'g');
eval(title('Figure -' NAME{1,k},'fontsize';14))
axis tight
eval(saveas(gcf,'Figure -' NAME{1,k},'eps'))
end
Thank you very much for the help.
Here is a fixed version of your code, with minimal change, if I got your intention correctly.
NAME={'Sept-Oct 2015','Nov 2015','Jan-Fe 2016','Fev 2016','Mars-Av 2016','Av-Mai 2016','Juin 2016','Juil 2016','Août 2016','Sept 2016','Oct 2016','Nov 2016','Déc 2016'};
for k = 1:13
plot(time,data,'g');
title(['Figure -' NAME{1,k}],'fontsize',14)
axis tight
saveas(gcf,['Figure -' NAME{1,k}],'eps')
end
However, right now you plot 13 times the same figure (with a different title), so I guess data and time should be indexed somehow. Also keep in mind that this changes the title of the figure (which is printed in it), not it's name (which appears at the top of the window/tab).

Calculations in table based on variable names in matlab

I am trying to find a better solution to calculation using data stored in table. I have a large table with many variables (100+) from which I select smaller sub-table with only two observations and their difference for smaller selection of variables. Thus, the resulting table looks for example similarly to this:
air bbs bri
_________ ________ _________
test1 12.451 0.549 3.6987
test2 10.2 0.47 3.99
diff 2.251 0.078999 -0.29132
Now, I need to multiply the ‘diff’ row with various coefficients that differ between variables. I can get the same result with the following code:
T(4,:) = array2table([T.air(3)*0.2*0.25, T.bbs(3)*0.1*0.25, T.bri(3)*0.7*0.6/2]);
However, I need more flexible solution since the selection of variables will differ between applications. I was thinking that better solution might be using either varfun or rowfun and speficic function that would assign correct coefficients/equations based on variable names:
T(4,:) = varfun(#func, T(3,:), 'InputVariables', {'air' 'bbs' 'bri'});
or
T(4,:) = rowfun(#func, T(3,:), 'OutputVariableNames', T.Properties.VariableNames);
However, the current solution I have is similarly inflexible as the basic calculation above:
function [air_out, bbs_out, bri_out] = func(air, bbs, bri)
air_out = air*0.2*0.25;
bbs_out = bbs*0.1*0.25;
bri_out = bri*0.7*0.6/2;
since I need to define every input/output variable. What I need is to assign in the function coefficients/equations for every variable and the ability of the function to apply it only to the variables that are present in the specific sub-table.
Any suggestions?

Debugging a for loop in matlab

I've been looking throught the documentation, but can't seem to find the bit I want.
I have a for loop and I would like to be able to view every value in the for loop.
for example here is a part of my code:
for d = 1 : nb
%for loop performs blade by blade averaging and produces a column vector
for cc = navg : length(atbmat);
atb2 = (sum(atbmat((cc-(navg-1):cc),d)))/navg;
atbvec2(:,cc) = atb2;
end
%assigns column vector 'atbvec2' to the correct column of the matrix 'atbmat2'
atbmat2(d,1:length(atbvec2)) = atbvec2;
end
I would like to view every value of atb2. I'm a python user(new to MATLAB) and would normally use a simple print statement to find this.
I'm sure there is a way to do it, but I can't quite find how.
Thankyou in advance.
you can use disp in Matlab to print to the screen but you might want to use sprintf first to format it nicely. However for debugging you're better off using a break point and then inspect the variable in the workspace browser graphically. To me, this is one of Matlab's best features.
Have a look at the "Examine Values" section of this article
The simplest way to view it everywhere is to change this line:
atb2 = (sum(atbmat((cc-(navg-1):cc),d)))/navg;
Into this, without semicolon:
atb2 = (sum(atbmat((cc-(navg-1):cc),d)))/navg
That being said, given the nature of your calculation, you could get the information you need as well by simply storing every value of abt2 and observing them afterwards. This may be done in atbmat2 already?
If you want to look at each value at the time it happens, consider setting a breakpoint or conditional breakpoint after the line where abt2 is assigned.