# How do I change a value inside a Currentstate variable array?

## Recommended Posts

Hi, I'm having a lot of trouble updating the values inside a public double[] CurrentState variable. The code looks something like this:

public double[] m_dLAvgAtrPerBarList;

CurrentState.m_dLAvgAtrPerBarList = new double[20] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};

CurrentState.m_dLAvgAtrPerTradeList = new double[20] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; CurrentState.m_dLTradeCountList = new double[20] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

CurrentState.m_dLAtrPerBarSumList[CurrentState.m_iPatternIndex] = CurrentState.m_dLAtrPerBarSumList[CurrentState.m_iPatternIndex] + m_dAtrPerBar; CurrentState.m_dLAtrPerBarAvgList[CurrentState.m_iPatternIndex] = Math.Round(CurrentState.m_dLAtrPerBarSumList[CurrentState.m_iPatternIndex] / CurrentState.m_dLTradeCountList[CurrentState.m_iPatternIndex], 0);

The point of this is to update the Total Sum of the Trade profits in the ATRPerBarSumList Currentstate array. And then divide this sum by the number of trades to get the average profit I can expect for each trade using this system. The problem is, my code never updates any of the values inside the arrays. It remains at all zeroes or just 1, 2, 3, 4, 5... like in the first array I typed (i was just checking to see if it would contain a value other than zero). How do i update the cell values inside these current state arrays? It feels like im missing something really basic but i dont know what it is.

Edited by Thebattlefront
##### Share on other sites

• Thebattlefront changed the title to How do I change a value inside a Currentstate variable array?

By Update I mean, I'd like to add a value to the last cell value to get a running sum of the total amount of profit my system has made up until the current point. So, for trade 1 it has a total sum let's say P, then the next trade's profit will add E to it and therefore the code line should look soemthing like P = P + E; where P is the value of the cell inside the currentstate variable array and E is just some double constant that im adding to P.

##### Share on other sites

Please send me the whole paintbar - export it and attach it to an email to support email....

##### Share on other sites

yessir mike*

Edited by Thebattlefront
##### Share on other sites

I haven't looked yet but here is a detail that may explain what you're seeing:

MT's paintbars are a "state machine". There is a "state", and the new candle changes the "state". The thing is, this state-changing for the new candle may not be called only once per candle. It is for "past" candles, but for the new, latest candle, the "state" is restored to the condition right BEFORE the new candle - then the state-changing code is called for the new, latest candle. Hope this makes sense?

##### Share on other sites

Ok, I just realized something... I should mention it in help file.

A struct that has arrays in it is a mutable struct. Which means that if you just do struct1=struct2, the arrays are not copied. They are there by reference, so both structs would be pointing to the same array.

Let me figure out how to change your code the most elegantly so that this doesn't happen (and that the efficiency does not suffer).

##### Share on other sites

Thanks Mike. Yeah i just want these arrays to store the data from the trades. Much like using individual currentstate variables and labelling them each CurrentState.m_dS1AtrPerBar, CurrentState.m_dS2AtrPerBar, CurrentState.m_dS3AtrPerBar, etc. but using just a single array for all to make it cleaner.

Can i ask , what did you change? My results still look like this:

Edit: The idea here is that each letter represents a different candlestick pattern. And what im doing is tracking the profit / number of trades / winrate of each candlestick, for each stock on my watchlist. That's what all the zeroes and the 1, 2, 3, 4, 5 numbers are in each column. They -should- be tracking the profit, tradecount, etc but i just get a bunch of zeroes.

Edited by Thebattlefront
##### Share on other sites

Do you have any idea how I can change the value of a cell in a CurrentState array, by referencing itself and adding another value to it? Or do i have to switch back to individual currentstate variables and abandon the idea of keeping the data stored in a single array altogether?

I remember before you showed me how to use the StateFIFOQueue to add data to a set, and then perform operations on that set (like getting the average) to get the important values i can use. So i know there are alternatives to using just a bunch of individual currentstate variables to hold the values. But I just don't know what those alternatives are.

Edited by Thebattlefront
##### Share on other sites

3 hours ago, Thebattlefront said:

Thanks Mike. Yeah i just want these arrays to store the data from the trades. Much like using individual currentstate variables and labelling them each CurrentState.m_dS1AtrPerBar, CurrentState.m_dS2AtrPerBar, CurrentState.m_dS3AtrPerBar, etc. but using just a single array for all to make it cleaner.

Can i ask , what did you change? My results still look like this:

Edit: The idea here is that each letter represents a different candlestick pattern. And what im doing is tracking the profit / number of trades / winrate of each candlestick, for each stock on my watchlist. That's what all the zeroes and the 1, 2, 3, 4, 5 numbers are in each column. They -should- be tracking the profit, tradecount, etc but i just get a bunch of zeroes.

Look in the save and restore functions. Instead of copying just the structs, I specifically made it copy the arrays...

##### Share on other sites

3 hours ago, Thebattlefront said:

Do you have any idea how I can change the value of a cell in a CurrentState array, by referencing itself and adding another value to it? Or do i have to switch back to individual currentstate variables and abandon the idea of keeping the data stored in a single array altogether?

I remember before you showed me how to use the StateFIFOQueue to add data to a set, and then perform operations on that set (like getting the average) to get the important values i can use. So i know there are alternatives to using just a bunch of individual currentstate variables to hold the values. But I just don't know what those alternatives are.

Yes for what you want (I think) it is best just to use StateFIFOQueue(20) for each of the arrays. You still have to manually save and restore state for each like you're already doing for the one StateFIFOQueue that you have. And you can only store Doubles of course...

##### Share on other sites

Is it possible to reference a value in stateFIFOqueue array (e.g.Q.LAtrPerBar[5]) and then replace that value with Q.LAtrPerBar[5] + E; where E is a double? Is it possible to call an individual cell's value inside the array like that, and then add some value to itself? So the code line would look something like

Q.LAtrPerBarSum[5] = Q.LAtrPerBarSum[5] + E;

By the way just so it's clear, "AtrPerBar" is my "unit" for expressing profit. ATR = average true range. I use ATR instead of percentages because some stocks move faster than others and so the percentage swings can be much larger than others so it's harder to normalize a system around that. So instead i use ATR because most stocks have price swings that are relatively similar to their average true range value. Just so you aren't confused. So this codeline would be adding another data point of profit to the total "profit" of the candlestick pattern it is measuring. That's why i have to reference the cell value again and then add E. E is the latest profit to be added to the total sum. E is also expressed in units of ATR per candlestick bar, as the total sum is too.

Edited by Thebattlefront
##### Share on other sites

I meant to write QLAtrPerBarSum.[5] not Q.LAtrPerBarSum[5]. The period is too soon.

I also checked the medved help center, it doesn't appear like this operation would work. What do you mean by the save and restore functions? Are you referring to the currentstate and savestate functions? Or are you referring to something else?

Edit: I just want to clarify what exactly I'm looking for. I just want to be able to create an array of variables, up to 20, and be able to do basic operations on individual cells in that array - addition / subtraction / multiplication etc. And I want to save that array somewhere so when the scan runs the next candle, that information is not lost. Is there any way to do that. Or do you think I have to separate each cell in that array into it's own currentstate variable and then perform the operations that way. This is the way I've been doing it before, but it's really really ugly to look at the code for it. It's just these long lines of declaring these current state variables and then I have to repeat the codelines for the same function 20 times for each candlestick pattern in order to assign the values I need to all of the individual currentstate variables i've declared. It really looks terrible. That's why I'm trying to figure out an array solution. It would look so much nicer and cleaner to implement.

Edited by Thebattlefront
##### Share on other sites

Ok - you can reference the values in FIFOQueues by using indexing - but the indexing is backwards. That is, [0] is the last value you added. [1] is the one before last. [2] is 2 before last etc.

Assignment of those values - I didn't do before. I put that in, will send you email where to pick up latest exe so you can do queue[3]=2;

##### Share on other sites

Ok, sounds good. Thank you mike

##### Share on other sites

Mike, So i understand that queue[3] = 2 should work now, but is it also possible to do queue[3]= queue[3] + 2? So I can make a cumulative sum inside only one of its cells.

Also, can i do lets say,

queue[5] = queue[4] / queue[2] or

queue[5] = queue[4] * queue[2]

Is division and multiplication allowed like that? And also, can i do math operations like

queue[10] = Math.Round(queue[4] / queue[2], 2);

Edited by Thebattlefront
##### Share on other sites

On 12/22/2023 at 5:09 AM, Thebattlefront said:

Mike, So i understand that queue[3] = 2 should work now, but is it also possible to do queue[3]= queue[3] + 2? So I can make a cumulative sum inside only one of its cells.

Also, can i do lets say,

queue[5] = queue[4] / queue[2] or

queue[5] = queue[4] * queue[2]

Is division and multiplication allowed like that? And also, can i do math operations like

queue[10] = Math.Round(queue[4] / queue[2], 2);

Yes, all of those are possible.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.