struct method not working

16 Mar 2019, 02:53struct method not working#1
a.fernandez.martinezposts: 22since: 02 Mar 2019

Hi,

 

First of all, I only tested this on backtesting since we are saturday.

 

I created a List<TickData> named tickRecord, which stores the following struct :

 

public struct TickData
        {
            public double Variation;

            public void SetVariation(double _newPrice, double _oldPrice)
            {
                if (_newPrice > _oldPrice)
                    Variation = _newPrice - _oldPrice;
                else if (_newPrice < _oldPrice)
                    Variation = -(_oldPrice - _newPrice);
                else
                    Variation = 0;
            }
        }

 

When I try to set the Variation property through a for loop like this :

 for (int i = tickRecord.Count - 1; i >= 0; i--)
            {
                if (i = 0)
                    // Do nothing

                else
                    tickRecord[i].SetVariation(tickRecord[i].Bid, tickRecord[i - 1].Bid);
            }

 

It doesn't work, Variation stays the same :

for i = 1 : 1,12879 - 1,12879 = 0

for i = 2 : 1,12879 - 1,12879 = 0

for i = 3 : 1,1288 - 1,12879 = 0

for i = 4 : 1,12879 - 1,1288 = 0

for i = 5 : 1,1288 - 1,12879 = 0

etc...

 

From my research I'd say, tickRecord[i].SetVariation(); is the problem.

But I can't find anywhere how to make it work or why does it do that.

16 Mar 2019, 03:29#2
a.fernandez.martinezposts: 22since: 02 Mar 2019

I tried to do it outside of the loop like this :

 

bool isFirstTick = true;

TickData currentData;
currentData.Time = Server.Time;
currentData.Ask = Symbol.Ask;
currentData.Bid = Symbol.Bid;
currentData.Variation = 0;
            if (isFirstTick)
            {
                currentData.SetVariation(0, 0);
                isFirstTick = false;
            }
            else
                currentData.SetVariation(currentData.Bid, tickRecord[tickRecord.Count - 1].Bid);

tickRecord.Add(currentData);

 

It still didn't work, same results...

17 Mar 2019, 00:02#3
a.fernandez.martinezposts: 22since: 02 Mar 2019

I solved the problem by making a class instead of a struct