How to clear the values of an array?

Questions about MultiCharts and user contributed studies.
evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

How to clear the values of an array?

Postby evdl » 17 May 2013

After taking a position I'm tracking some values (max, high, low etc.) in an array. These values are used to determine the moment when the trailingstop must come in action.

After the position is closed. How do I clear the values in the array (I assume this is in memory somewhere). So it will only use values to fill the array during a specific position.

Any reserved word for this?

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: How to clear the values of an array?

Postby bowlesj3 » 17 May 2013

From the help,
Dynamic arrays (arrays with an unlimited number of elements) are one-dimensional, and are initialized at declaration as having only one element. Declared dynamic arrays can be resized using Array_SetMaxIndex.
I have not read about a reserve word nor have I done this but I am thinking you could reduce the SetMaxIndex down to 1 or maybe even zero the reset it up to the size you want (two statements). The Wiki index is below on the command.

https://www.multicharts.com/trading-sof ... etMaxIndex

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Re: How to clear the values of an array?

Postby evdl » 18 May 2013

That could work, will give this a try.

Thanks you.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: How to clear the values of an array?

Postby bowlesj3 » 18 May 2013

Sounds good. Let us know how it works.

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Re: How to clear the values of an array?

Postby evdl » 22 May 2013

This way I managed to clear the values of an array:

With taking a position the dynamic array is set to add one index per the close of the bar of the chart timeframe.

Code: Select all

Vars:
Setmaxindex(false),
PositionOpen(false),
Macd_count(0),
MACD_daily_diff_intraday(0),
Highest_macd(0);

Array:
Macd_diff_array[] (0); // to store the macd diff per bar, but you can use anything

If (MP <> 0) then begin
setmaxindex = true; // used to change the setmaxindex
PositionOpen = true; // used for only count when a position is open
end
Else begin
Setmaxindex = false;
PositionOpen = false;
end;

If (barstatus(1)=2) and positionOpen then begin

If close <> close[1] then begin
Macd_count = macd_count + 1;
end;

// expand the array by one
array_setmaxindex(Macd_diff_array, macd_count);

If Setmaxindex then begin
// post data to array
Macd_diff_array[macd_count] = MACD_daily_diff_intraday;

// calculate the highest macd diff value
Highest_macd = HighestArray(macd_diff_array, macd_count);

end;
end;

If Setmaxindex = false then begin
// decline the array to zero
array_setmaxindex(Macd_diff_array, 0);
end;
Thanks for directing me in the right direction bowlesj3.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: How to clear the values of an array?

Postby bowlesj3 » 22 May 2013

No Problem. Thanks for confirming the idea. Its good to know.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: How to clear the values of an array?

Postby Henry MultiСharts » 23 May 2013

array_setvalrange(mas, 1, array_getmaxindex(mas), 0);
Assigns zero value to elements of mas array with index from 1 to mas array size.


Return to “MultiCharts”