Pine script code: unable to get similar code syntax between code using sma and using ema - pine-script-v5

I'm struggling with the difference of behaviour between 'length' parameters required by ta.sma and ta.ema.
The following code run fine while I use ta.sma, but changing in ta.ema cannot compile with error:
"Cannot call 'ta.ema' with argument 'length'='maPeriods1'. An argument of 'series int' type was used but a 'simple int' is expected"
The pine script v5 manual tells that using 'var' like
var int maPeriods1=int(math.round(tMinutes(cycle)/minutesTF()/16))
I'm creating a simple integer for length, or not?
//#version=5
indicator("Some averages", overlay=true)
daysaWeek=input.int(8,"Days a week",options=[6,7,8])
minutesTF()=>
if timeframe.period=='D'
60*24
else if timeframe.period=='W'
60*24*daysaWeek
else if timeframe.period=='M'
60*24*30
else
str.tonumber(timeframe.period)
T="T"
Tp1="T+1"
Tp2="T+2"
Tp3="T+3"
Tp4="T+4"
Tp5="T+5"
Tp6="T+6"
Tp7="T+7"
Tm1="T-1"
Tm2="T-2"
Tm3="T-3"
Tm4="T-4"
minutesEachDay=1440
tMinutes(cycleName)=>
if cycleName==T
daysaWeek*minutesEachDay
else if cycleName==Tp1
16*minutesEachDay
else if cycleName==Tp2
32*minutesEachDay
else if cycleName==Tp3
64*minutesEachDay
else if cycleName==Tp4
128*minutesEachDay
else if cycleName==Tp5
256*minutesEachDay
else if cycleName==Tp6
512*minutesEachDay
else if cycleName==Tp7
1024*minutesEachDay
else if cycleName==Tm1
4*minutesEachDay
else if cycleName==Tm2
2*minutesEachDay
else if cycleName==Tm3
1*minutesEachDay
else if cycleName==Tm4
0.5*minutesEachDay
else
0 // error in parameter?
cycle = input.string(T, "Cycle", options=[Tm4,Tm3,Tm2,Tm1,T, Tp1,Tp2,Tp3,Tp4,Tp5,Tp6,Tp7])
var int maPeriods1=int(math.round(tMinutes(cycle)/minutesTF()/16))
// MA1 Plot
yMA1=ta.ema(close, maPeriods1)
plot(maPeriods1>0? yMA1:na, "MA/8", color.yellow, linewidth=1)

Related

How to store a return value in variable flutter

I need a little help.
I have this function in another file and i want to store the return value in a variable, because i dont want to repeat the same code again and again and I want to reuse it as many times I want.
here is the code in another file.
double dropDownIf(dropDownVal, finalVal, valParsed) {
if(dropDownVal == 'm'){
finalVal = valParsed;
} else if(dropDownVal == 'cm'){
finalVal = valParsed/100;
} else if(dropDownVal == 'mm'){
finalVal = valParsed/1000;
}
print('here is the updated value $finalVal');
return finalVal;
}
as you can see that it show the return value in debug console but it doesnt show the value in another page in which i am using this code.
here is the code on another page.
dropDownIf(dropDownValueL, finalLength, lengthParsed);
print(finalLength);
here in this page, the print function shows 0, I have declared the double finalLength = 0; in the beginning of the file. so the print shows 0 instead of the updated value.
the middle value in the dropDownIf function is the return value but it doesnt work.
You need to store the return value of the method in a variable, then use it:
double returnValue = dropDownIf(dropDownValueL, finalLength, lengthParsed);
print(returnValue);

ebpf unknown opcode comparing strings

I currently try to filter calls to a function by command. I try to do so with the following code where ##REPLACE_comm## is replaced by python by the command name. The double backslash are cause I am using bcc. The following code throws an error when loading:
if(1){
char filter[TASK_COMM_LEN] = "##REPLACE_comm##";
char command[TASK_COMM_LEN];
bpf_get_current_comm(&command, sizeof(command));
for(u16 i = 0; i<=TASK_COMM_LEN; i++){
if(command[i] == '\\0' && filter[i] == '\\0'){
break;
}
if(command[i] == filter[i]){
continue;
}
return 0;
}
}
The error is:
unknown opcode 70
HINT: The 'unknown opcode' can happen if you reference a global or static variable, or data in read-only section. For example, 'char *p = "hello"' will result in p referencing a read-only section, and 'char p[] = "hello"' will have "hello" stored on the stack.
I feel like I already made sure the variables are on the stack by allocating space and not just having a pointer but it doesnt work. What am I missing?

rephrase the if statement in Dart 2.0 using null

I have following statement in flutter. weight is the text from _weightController i.e. _weightController.text
int.parse(weight).toString().isNotEmpty && int.parse(weight) > 0
But in Dart 2.0 it is not working properly. For empty TextField, it is giving the error.
====== Exception caught by gesture ==============================
The following FormatException was thrown while handling a gesture:
Invalid number (at character 1)
The code block is like this.
if (int.parse(weight).toString().isNotEmpty && int.parse(weight) > 0)
return int.parse(weight) * multiplier;
else
print('Error');
As an alternative to the other answer, you can use the tryParse() method:
Like parse except that this function returns null where a similar call to parse would throw a FormatException, and the source must still not be null.
If you use this approach, you should check the return value for null:
String weight ="";
int number = int.tryParse(weight);
if (number !=null){
print(number );
}
else
print("error");
Don't forget to also check the variable for null with weight ? "" or with weight != null
Try this:
if (weight != null && weight.isNotEmpty) {
return int.tryParse(weight) * multiplier;
} else {
print("CoolTag: error");
return -1;
}

How to change value of module_param parameter in the device driver?

I wrote a simple program for taking a value through command line into my driver. I used module_param() for this and gave permission argument, i.e third arg of module_param(), as S_IWUSR.
This I guess would allow user to modify the value of that parameter once driver is loaded in the kernel. I tried to modify the value of that parameter by:
echo 1 > /sys/module/ghost/parameters/num
But this shows me Permission denied error every time I try to do this, even when I execute the command with sudo. I also tried changing permission in module_param() to 0770 but still was not able to change the parameter value. Is there a way to change the value of parameter passed while inserting the driver ? Why does the above command shows permission denied, even if I run as sudo ?
After the answer of #Ian Abott I am to change the value of the parameter. Now I tried to define a callback function to notify me any changes in the value of that parameter while my driver is loaded. Here is the code
#include"headers.h"
#include"declarations.h"
static int my_set(const char *val, const struct kernel_param *kp)
{
int n = 0, ret;
ret = kstrtoint(val,10,&n); // Kernel function to convert string to integer
if (ret !=0 || n > 10) {
return -EINVAL;
}
printk(KERN_ALERT "my-set function running\n");
return param_set_int(val,kp);
}
static const struct kernel_param_ops param_ops = {
.set = my_set,
.get = param_get_int,
};
module_param(num,int,0600);
static char *name = "hello";
module_param(name,charp,0770);
static int __init init_func(void)
{
int i;
module_param_cb(callBack, &param_ops, &num, 0770);
printk(KERN_INFO "Value of num is %d\n",num);
for( i=0; i<num; i++)
{
printk(KERN_INFO "%s\n", name);
}
return 0;
}
static void __exit exit_func(void)
{
printk(KERN_INFO "Value of num is %d\n",num);
printk(KERN_ALERT "Module removed successfully\n");
}
module_init(init_func);
module_exit(exit_func);
But it doesn't seem to work because my_set function never runs, even if I change the value. My doubt is
1) Is this correct way to implement callback function for the parameter?
2) What is significance of first argument to the function module_param_cb?

PostgreSQL clarification

I have written a function inside PostgreSQL which has the following code:
for (i = 0; i < 4; i++)
{
Datum dat_value = CStringGetDatum(inp->str[0][i]);
values[i] = datumCopy(dat_value,
stats->attrtype->typbyval,
stats->attrtype->typlen);
}
The input strings are {ALGERIA,ARGENTINA,BRAZIL,CANADA}. The code runs for ALGERIA,ARGENTINA but terminates abruptly for BRAZIL. When I investigated I found that inside datumCopy function, the statement after memcpy is not getting printed. I checked if palloc failed with (s == NULL) condition, but that seems to be not the reason. I think memcpy is failing. Any reason why? Thanks!
Datum
datumCopy(Datum value, bool typByVal, int typLen)
{
Datum res;
if (typByVal)
res = value;
else
{
Size realSize;
char *s;
if (DatumGetPointer(value) == NULL)
return PointerGetDatum(NULL);
realSize = datumGetSize(value, typByVal, typLen);
s = (char *) palloc(realSize);
printf ("Value : %s\n",DatumGetPointer(value));
memcpy(s, DatumGetPointer(value), realSize);
printf ("Not printing \n");
res = PointerGetDatum(s);
}
return res;
}
EDITED : Ok this is really wierd. When the input is one of {BRAZIL,PAKISTAN,FRANCE}, the code terminates abruptly. If I have other countries (I haven't tried extensively, but some countries), the code runs correctly.
EDITED 2 : Found the cause and rectified the issue. If we are passing C strings to datumCopy, we have to pass -2 for typLen parameter. I had been passing it incorrectly.
Thanks!
I have found the cause and rectified the issue.
If we are passing C strings to datumCopy, we have to pass -2 for typLen parameter. I had been passing it incorrectly.