Page 1 of 1

Storing a value

Posted: Nov 25 2015
by monick
This may be very simple and I think it should be, but it seems to be causing a little problem. I want to do the following to store a value

If time_s <= checktime then begin;
checkprice = currentbid of data2;
end;


if time_s > checktime then begin;
checkprice = checkprice;
end;

Will this accomplish what I am trying to do, which is set checkprice to the last bid of data2 at the checktime for the rest of the day after the crossover in time.

Re: Storing a value

Posted: Nov 25 2015
by TJ
This may be very simple and I think it should be, but it seems to be causing a little problem. I want to do the following to store a value
If time_s <= checktime then begin;
checkprice = currentbid of data2;
end;
if time_s > checktime then begin;
checkprice = checkprice;
end;
Will this accomplish what I am trying to do, which is set checkprice to the last bid of data2 at the checktime for the rest of the day after the crossover in time.
You need to be more descriptive on what you want to do.
Can you give some examples?
What is wrong with the code above?

Re: Storing a value

Posted: Nov 25 2015
by TJ
ps.
see post #1 & 2
[FAQ] How to Post Codes (... so that people can read)
viewtopic.php?f=16&t=11713

Re: Storing a value

Posted: Nov 25 2015
by tony
Are you trying to "hold" a value, not "store" a value. In other words do you want a value to hold a constant once something happens and only change that constant to when another "something" happens? If so, I don't believe how you wrote your signal that is going to work. You need to identify a moment something goes from true to false or vice versa and then a value will be held.

Re: Storing a value

Posted: Nov 25 2015
by monick
Tony,
Yes, that is what i want to do. Hold the value constant after the time has crossed over. Not exactly sure how you are suggesting to do it. Could you please give a simple example?
Thanks

Re: Storing a value

Posted: Nov 25 2015
by tony
Tony,
Yes, that is what i want to do. Hold the value constant after the time has crossed over. Not exactly sure how you are suggesting to do it. Could you please give a simple example?
Thanks
Can you explain what exactly you want to hold and when to hold it and I or someone can offer suggestions on code. It looks like you want to hold a bid value but I'm not fully sure. Please elaborate and someone will help out (or try). Thanks.

Re: Storing a value

Posted: Nov 25 2015
by monick
input: checktime(120000)
var: checkprice(0)

if currenttime_s <= checktime then begin;
checkprice = currentbid of data2;
end;

if currenttime_s > checktime then begin;
checkprice = checkprice;
end;

I just want the value checkprice to remain constant after 120000 at the last value of the bid of data2 at 120000

Re: Storing a value

Posted: Nov 25 2015
by tony
input: checktime(120000)
var: checkprice(0)

if currenttime_s <= checktime then begin;
checkprice = currentbid of data2;
end;

if currenttime_s > checktime then begin;
checkprice = checkprice;
end;

I just want the value checkprice to remain constant after 120000 at the last value of the bid of data2 at 120000
I didn't test this at all but you can try and see if it works.

Code: Select all


var:

var1(0),
checkprice(0);

var1 = currenttime_s;

IF var1 = 120000 AND ( var1 - 1 ) = 119999

Then checkprice = currentbid of data2;

Re: Storing a value

Posted: Nov 25 2015
by tony
Just realized the above may and may not work. Essentially the script needs to calculate at 120000 exactly for it to work, otherwise it won't work. You'd need to have recalclastbar() to force a calculation which may not be ideal.

Re: Storing a value

Posted: Nov 25 2015
by monick
Well I don't need to be more exact than a minute so I could just use currentime = 1200 and also I do use recalclastbarafter(1) but I guess I don't see how this is any different than my original code.

Bottom-line, if there's a var that is calculated and then the conditions are no longer true for it to be calculated, will it just return the last calculated value from then on??? Or will it return
0?

Re: Storing a value

Posted: Nov 26 2015
by tony
Well I don't need to be more exact than a minute so I could just use currentime = 1200 and also I do use recalclastbarafter(1) but I guess I don't see how this is any different than my original code.

Bottom-line, if there's a var that is calculated and then the conditions are no longer true for it to be calculated, will it just return the last calculated value from then on??? Or will it return
0?
The reason this should work (you'll need to test in playback mode) is there are conditions being set for the value of the variable checkprice. Once all (in this case 2) conditions are met, then checkprice is calculated and the value is held. When conditions are not met then the variable is not calculated and therefore the value remains constant. Once conditions are met again the variable is recalculated and the variable is once again held to this new value.

In the code you were trying, checkprice is always calculated and thus always updated.

If you can use minute resolution then you may want to use "time" which is the time of the current bar if one of your data series are in minute resolution rather than check time_s

There are other ways to do this, I just thought of one quickly but haven't tested. The key is identifying conditions that are only true at the time you want your variable to calculate and hold.

Re: Storing a value

Posted: Nov 26 2015
by arjfca
Not sure to understand your question but when you work with variable that have to be kept between bars calculation, you need to declare them as intrabarpersist


This way, data stored in these variable, will be kept and reuse anywhere and anytime in the process.

Code: Select all

Var:
Intrabarpersist Anyvariable_1 (False),
Intrabarpersist AstringVariable (""),
Intrabarpersit AtimeVariable (0),
CalculatedPrice (0);
This way, the first 3 variables will be kept after a new bar had been created while the 4' one (CalculculatedPrice) will be reset

Martin

Re: Storing a value

Posted: Nov 26 2015
by bensat
No comment ... hope this helps.

Code: Select all

input: checktime(1659);

var: double checkprice(0);


if time <= checktime then
begin
checkprice = currentbid of data1;
end
else
checkprice = checkprice[1];

Plot1(checkprice);
As you can see, if time takes over 459pm the price stays as last price in condition.

Image