struct method not working

Created at 16 Mar 2019, 02:53
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
A.

a.fernandez.martinez

Joined 02.03.2019

struct method not working
16 Mar 2019, 02:53


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.


@a.fernandez.martinez
Replies

a.fernandez.martinez
16 Mar 2019, 03:29

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...


@a.fernandez.martinez

a.fernandez.martinez
17 Mar 2019, 00:02

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


@a.fernandez.martinez