Storing a value

Questions about MultiCharts and user contributed studies.
monick
Posts: 19
Joined: 20 Nov 2015

Storing a value

Postby monick » 25 Nov 2015

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.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Storing a value

Postby TJ » 25 Nov 2015

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?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Storing a value

Postby TJ » 25 Nov 2015

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

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Storing a value

Postby tony » 25 Nov 2015

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.

monick
Posts: 19
Joined: 20 Nov 2015

Re: Storing a value

Postby monick » 25 Nov 2015

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

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Storing a value

Postby tony » 25 Nov 2015

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.

monick
Posts: 19
Joined: 20 Nov 2015

Re: Storing a value

Postby monick » 25 Nov 2015

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

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Storing a value

Postby tony » 25 Nov 2015

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;

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Storing a value

Postby tony » 25 Nov 2015

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.

monick
Posts: 19
Joined: 20 Nov 2015

Re: Storing a value

Postby monick » 25 Nov 2015

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?

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Storing a value

Postby tony » 26 Nov 2015

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.

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: Storing a value

Postby arjfca » 26 Nov 2015

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

User avatar
bensat
Posts: 331
Joined: 04 Oct 2014
Has thanked: 46 times
Been thanked: 104 times

Re: Storing a value

Postby bensat » 26 Nov 2015

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
Attachments
MC_Forum.png
(19.25 KiB) Downloaded 974 times


Return to “MultiCharts”