[R] remembering the state of an action in R....
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sun Dec 11 18:57:31 CET 2022
Às 17:46 de 11/12/2022, akshay kulkarni escreveu:
> Dear Rui,
> Thanks for your reply....your reply covers the first part of my question. What about the second part? i.e remembering the state when the price q breaches Q? Will some thing like this work:
>
> f <- function(envir) {expr1; expr2; expr3; envir$j <- envir$j + 1L}
>
> e <- new.env()
> e$j <- 1L
> # do you need counter for something else?
> # if not delete these two lines
> counter <- list()
> counter[[e$j]] <- 1L
> # this seems to be all you need, not counter
> if ((q >= Q) && e$j == 1L) {f(e); C1 <- "the price Q has been breached!"}
>
> if(C1 != "the price Q has been breached!") { do something }
>
> ANy other method?
>
> Thanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
>
>
>
> ________________________________
> From: Rui Barradas <ruipbarradas using sapo.pt>
> Sent: Sunday, December 11, 2022 10:58 PM
> To: akshay kulkarni <akshay_e4 using hotmail.com>; R help Mailing list <r-help using r-project.org>
> Subject: Re: [R] remembering the state of an action in R....
>
> Às 17:11 de 11/12/2022, akshay kulkarni escreveu:
>> Dear members,
>> I am a stock trader and using R for my research. I am monitoring stock prices in real time. I have the following code:
>>
>>> if (sock price q, breaches a certain value Q) { expr1; expr2; expr3}
>>
>> THe point is, expr1,expr2,expr3 should execute only once, i.e when q breaches Q. I thought of something like this:
>>
>>> if( q >= Q ) { expr1; expr2; expr3;}
>>
>> But expressions keep repeating as long as q >= Q, NOT when q breaches Q for the first time. I did some research on myself and came up with this:
>>
>>> f <- function() {expr1; expr2; expr3; j <<- j + 1}
>>> j <- 1; counter[[j]] <- 1;
>>> if ((q >= Q) && length(counter) == 1) {f}
>>
>> I just want your help to know whether it is logically right or not. ARe not there any other possibility other than using the superassignment operator?
>>
>> Also, any way how to remember when the real time price q, breaches a value Q, for the first time ( the price may come back to Q again afterwards) ?
>>
>> THanking you,
>> Yours sincerely,
>> AKSHAY M KULKARNI
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> Hello,
>
> Use environments. You will assign to a variable living in a special
> place, protected from the rest of the code, that you can access any time
> you want.
> Something like the following.
>
>
>
> f <- function(envir) {expr1; expr2; expr3; envir$j <- envir$j + 1L}
>
> e <- new.env()
> e$j <- 1L
> # do you need counter for something else?
> # if not delete these two lines
> counter <- list()
> counter[[e$j]] <- 1L
> # this seems to be all you need, not counter
> if ((q >= Q) && e$j == 1L) {f(e)}
>
>
>
> Hope this helps,
>
> Rui Barradas
>
>
Hello,
Envirnments are still the solution for this. You can hold another
variable in the environment passed to f.
f <- function(real_time_price, Q_breach = Q, envir) {
envir$counter <- envir$counter + 1L
envir$breached <- "the price Q has been breached"
x <- pi
x
}
q <- 10
Q <- 5
e <- new.env()
e$counter <- 0L
# this seems to be all you need, not counter
if ((q >= Q) && e$counter == 0L) {
print(f(q, Q, envir = e))
}
if(e$breached != "the price Q has been breached!") {
message("do something...")
}
Hope this helps,
Rui Barradas
More information about the R-help
mailing list