passing a lot of variables to a function

Created at 09 Dec 2017, 23:38
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!
lec0456's avatar

lec0456

Joined 14.11.2012

passing a lot of variables to a function
09 Dec 2017, 23:38


I would like a good way to pass a bunch of variables(like below) from the OnTick event to a function. Anyone have an idea how to do it.

double high0 = MarketSeries.High[t0];
            double high1 = MarketSeries.High[t1];
            double high2 = MarketSeries.High[t2];
            double high3 = MarketSeries.High[t3];

            double low0 = MarketSeries.Low[t0];
            double low1 = MarketSeries.Low[t1];
            double low2 = MarketSeries.Low[t2];
            double low3 = MarketSeries.Low[t3];

            double vol0 = MarketSeries.TickVolume[t0];
            double vol1 = MarketSeries.TickVolume[t1];
            double vol2 = MarketSeries.TickVolume[t2];

            double open0 = MarketSeries.Open[t0];
            double open1 = MarketSeries.Open[t1];
            double open2 = MarketSeries.Open[t2];
            double open3 = MarketSeries.Open[t3];

            double close0 = MarketSeries.Close[t0];
            double close1 = MarketSeries.Close[t1];
            double close2 = MarketSeries.Close[t2];
            double close3 = MarketSeries.Close[t3];


@lec0456
Replies

ap11
11 Dec 2017, 11:40

Hi lec0456,

You need Bar class with Open, High, Low, Close, Volume fields. You can pass collection of Bars to your method.

Also you can create an extention for MarketSeries to get Bar like this, MarketSeries.GetBar(t0):

public static class MarketSeriesGetBarExtention
{
    public static Bar GetBar(this MarketSeries series, int index)
    {
        var bar = new Bar()
        {
            Open = series.Open[index],
            High = series.High[index],
            Low = series.Low[index],
            Close = series.Close[index],
            TickVolume = series.TickVolume[index]
        };
        return bar;
    }
}

Kind Regards,
Andrey


@ap11

lec0456
12 Dec 2017, 23:09

I am answering my own question but its the best thing i did for me cBots so I figured I would share the solution.

3 steps: create a struct, create a function to poputlate the fields, declare the struct variable, call the function onTick or onBar.

Create a simple struct:

        public struct idata
        {     
            public int t0, t1, t2, t3; 
            public int LondonHour, NYHour, SydneyHour, TokyoHour;
            public int LondonHour1, NYHour1, SydneyHour1, TokyoHour1;             
                     
            public int DateDifference;
            public int LstPkWkly;
            
            public double pchigh0, pchigh1, pchigh2, pchigh3;
            public double pclow0, pclow1, pclow2, pclow3;
            public double pccenter0, pccenter1;
            
            public double pcWidth0, pcWidth1, pcWidth2, pcWidth3;
            public double pcWidth0D;
            //public double pcWidth0W;
            public double smaWidth;
            public double high0, high1, high2, high3;
            //public double high0H, high1H, high2H, high3H;
            public double high0D, high1D; 
            public double high0W, high1W;
            public double highSMA;
            
            public double low0, low1, low2, low3;
            //public double low0H, low1H, low2H, low3H;
            public double low0D, low1D;
            public double low0W, low1W;
            public double lowSMA;
            
            public double vol0, vol1, vol2;
            public double open0, open1, open2, open3;
            public double open0D;
            public double open0W;
            
            public double close0, close1, close2, close3;
            public double ocdiff0, ocdiff1, ocdiff2, ocdiff3;
            public double ocdiff1H, ocdiff2H;
            public double ocdiff0D, ocdiff1D;
            public double Uwick1, Uwick2, Uwick3;
            public double Lwick1, Lwick2, Lwick3;
            public double sto0, sto1,sto2, sto3;
            public double cyc0, cyc1, cyc2, cyc3;
            public double ma1t0, ma1t1, ma1t2, ma1t3;
            public double ma2t0, ma2t1, ma2t2, ma2t3;
            public double ma3t0, ma4t0;
            public double ma1_ma2, ma1_ma3, ma1_ma4, ma2_ma3, ma2_ma4, ma3_ma4;
            
            public double ssum24_0, ssum24_1, ssum24_2, ssum24_3;
            public double ssum96_0, ssum96_1, ssum96_2, ssum96_3;
            public double ma1slp0, ma2slp0, ma3slp0, ma4slp0;
            public double ma1slp1, ma2slp1, ma3slp1, ma4slp1;
            
            public double ma1pk0,ma2pk0,ma3pk0,ma4pk0;
            public double ma1pk1,ma2pk1,ma3pk1,ma4pk1;
            
            public double ma24x0,ma24x1;
            
            public double WklyHighDiff, WklyLowDiff;
        }

Then create a function to populate the fields:

idata GetIndicatorData()
        {
            idata indicatordata;
            int t0 = MarketSeries.Close.Count - 1;
            int t1 = t0 - 1;
            int t2 = t1 - 1;
            int t3 = t2 - 1;
            
            var weeklyindex = Weekly.OpenTime.GetIndexByTime(MarketSeries.OpenTime[t0]);
            var dailyindex = Daily.OpenTime.GetIndexByTime(MarketSeries.OpenTime[t0]);
            var hourlyindex = Hourly.OpenTime.GetIndexByTime(MarketSeries.OpenTime[t0]);
            var _15minindex = _15min.OpenTime.GetIndexByTime(MarketSeries.OpenTime[t0]);
  
            indicatordata.t0 = t0;
            indicatordata.t1 = t0 - 1;
            indicatordata.t2 = t1 - 1;
            indicatordata.t3 = t2 - 1;

            indicatordata.pchigh0 = pc.Upper[t0];
            indicatordata.pchigh1 = pc.Upper[t1];
            indicatordata.pchigh2 = pc.Upper[t2];
            indicatordata.pchigh3 = pc.Upper[t3];

            indicatordata.pclow0 = pc.Lower[t0];
            indicatordata.pclow1 = pc.Lower[t1];
            indicatordata.pclow2 = pc.Lower[t2];
            indicatordata.pclow3 = pc.Lower[t3];

            indicatordata.pccenter0 = pc.Center[t0];
            indicatordata.pccenter1 = pc.Center[t1];

            indicatordata.pcWidth0D = PipDiff(Daily.High[dailyindex], Daily.Low[dailyindex]);

            indicatordata.pcWidth0 = Math.Round(pcw.Result[t0], 2);
            indicatordata.pcWidth1 = Math.Round(pcw.Result[t1], 2);
            indicatordata.pcWidth2 = Math.Round(pcw.Result[t2], 2);
            indicatordata.pcWidth3 = Math.Round(pcw.Result[t3], 2);
            
            indicatordata.smaWidth=SMABandDist();

            indicatordata.high0 = MarketSeries.High[t0];
            indicatordata.high1 = MarketSeries.High[t1];
            indicatordata.high2 = MarketSeries.High[t2];
            indicatordata.high3 = MarketSeries.High[t3];
            
            indicatordata.high0D = Daily.High[dailyindex];
            indicatordata.high1D = Daily.High[dailyindex-1];
            indicatordata.high0W = Weekly.High[weeklyindex];
            indicatordata.high1W = Weekly.High[weeklyindex-1];
            
            indicatordata.highSMA = HighSMA();
            
            indicatordata.low0 = MarketSeries.Low[t0];
            indicatordata.low1 = MarketSeries.Low[t1];
            indicatordata.low2 = MarketSeries.Low[t2];
            indicatordata.low3 = MarketSeries.Low[t3];
            
            indicatordata.lowSMA = LowSMA();
            
            indicatordata.low0D = Daily.Low[dailyindex];
            indicatordata.low1D = Daily.Low[dailyindex-1];
            indicatordata.low0W = Weekly.Low[weeklyindex];
            indicatordata.low1W = Weekly.Low[weeklyindex-1];

            indicatordata.vol0 = MarketSeries.TickVolume[t0];
            indicatordata.vol1 = MarketSeries.TickVolume[t1];
            indicatordata.vol2 = MarketSeries.TickVolume[t2];

            indicatordata.open0 = MarketSeries.Open[t0];
            indicatordata.open1 = MarketSeries.Open[t1];
            indicatordata.open2 = MarketSeries.Open[t2];
            indicatordata.open3 = MarketSeries.Open[t3];
            
            indicatordata.open0D = Daily.Open[dailyindex];
            indicatordata.open0W = Weekly.Open[weeklyindex];

            indicatordata.close0 = MarketSeries.Close[t0];
            indicatordata.close1 = MarketSeries.Close[t1];
            indicatordata.close2 = MarketSeries.Close[t2];
            indicatordata.close3 = MarketSeries.Close[t3];

            indicatordata.ocdiff0 = Math.Round((MarketSeries.Close[t0] - MarketSeries.Open[t0]) / Symbol.PipSize, 2);
            indicatordata.ocdiff1 = Math.Round((MarketSeries.Close[t1] - MarketSeries.Open[t1]) / Symbol.PipSize, 2);
            indicatordata.ocdiff2 = Math.Round((MarketSeries.Close[t2] - MarketSeries.Open[t2]) / Symbol.PipSize, 2);
            indicatordata.ocdiff3 = Math.Round((MarketSeries.Close[t3] - MarketSeries.Open[t3]) / Symbol.PipSize, 2);
            
            indicatordata.ocdiff0D = PipDiff(Daily.Close[dailyindex], Daily.Open[dailyindex]);
            indicatordata.ocdiff1D = PipDiff(Daily.Close[dailyindex - 1], Daily.Open[dailyindex - 1]);

            indicatordata.Uwick1 = indicatordata.ocdiff1 > 0 ? (MarketSeries.High[t1] - MarketSeries.Close[t1]) / Symbol.PipSize : (MarketSeries.High[t1] - MarketSeries.Open[t1]) / Symbol.PipSize;
            indicatordata.Uwick2 = indicatordata.ocdiff2 > 0 ? (MarketSeries.High[t2] - MarketSeries.Close[t2]) / Symbol.PipSize : (MarketSeries.High[t2] - MarketSeries.Open[t2]) / Symbol.PipSize;
            indicatordata.Uwick3 = indicatordata.ocdiff3 > 0 ? (MarketSeries.High[t3] - MarketSeries.Close[t3]) / Symbol.PipSize : (MarketSeries.High[t3] - MarketSeries.Open[t3]) / Symbol.PipSize;

            indicatordata.Lwick1 = indicatordata.ocdiff1 > 0 ? (MarketSeries.Open[t1] - MarketSeries.Low[t1]) / Symbol.PipSize : (MarketSeries.Close[t1] - MarketSeries.Low[t1]) / Symbol.PipSize;
            indicatordata.Lwick2 = indicatordata.ocdiff2 > 0 ? (MarketSeries.Open[t2] - MarketSeries.Low[t2]) / Symbol.PipSize : (MarketSeries.Close[t2] - MarketSeries.Low[t2]) / Symbol.PipSize;
            indicatordata.Lwick3 = indicatordata.ocdiff3 > 0 ? (MarketSeries.Open[t3] - MarketSeries.Low[t3]) / Symbol.PipSize : (MarketSeries.Close[t3] - MarketSeries.Low[t3]) / Symbol.PipSize;

            indicatordata.sto0 = Math.Round(sto.PercentK[t0], 2);
            indicatordata.sto1 = Math.Round(sto.PercentK[t1], 2);
            indicatordata.sto2 = Math.Round(sto.PercentK[t2], 2);
            indicatordata.sto3 = Math.Round(sto.PercentK[t3], 2);

            indicatordata.cyc0 = Math.Round(sto.PercentKcyc[t0], 2);
            indicatordata.cyc1 = Math.Round(sto.PercentKcyc[t1], 2);
            indicatordata.cyc2 = Math.Round(sto.PercentKcyc[t2], 2);
            indicatordata.cyc3 = Math.Round(sto.PercentKcyc[t3], 2);

            indicatordata.ma1t0 = smoothsma1.Result[t0];
            indicatordata.ma1t1 = smoothsma1.Result[t1];
            indicatordata.ma1t2 = smoothsma1.Result[t2];
            indicatordata.ma1t3 = smoothsma1.Result[t3];

            indicatordata.ma2t0 = smoothsma2.Result[t0];
            indicatordata.ma2t1 = smoothsma2.Result[t1];
            indicatordata.ma2t2 = smoothsma2.Result[t2];
            indicatordata.ma2t3 = smoothsma2.Result[t3];

            indicatordata.ma3t0 = smoothsma3.Result[t0];
            indicatordata.ma4t0 = smoothsma4.Result[t0];

            indicatordata.ma1_ma2 = (smoothsma1.Result[t0] - smoothsma2.Result[t0]) / Symbol.PipSize;
            indicatordata.ma1_ma3 = (smoothsma1.Result[t0] - smoothsma3.Result[t0]) / Symbol.PipSize;
            indicatordata.ma1_ma4 = (smoothsma1.Result[t0] - smoothsma4.Result[t0]) / Symbol.PipSize;
            indicatordata.ma2_ma3 = (smoothsma2.Result[t0] - smoothsma3.Result[t0]) / Symbol.PipSize;
            indicatordata.ma2_ma4 = (smoothsma2.Result[t0] - smoothsma4.Result[t0]) / Symbol.PipSize;
            indicatordata.ma3_ma4 = (smoothsma3.Result[t0] - smoothsma4.Result[t0]) / Symbol.PipSize;
            
            indicatordata.LondonHour = double.IsNaN(markethours.London[t0]) ? 0 : (int)markethours.London[t0];
            indicatordata.NYHour = double.IsNaN(markethours.NewYork[t0]) ? 0 : (int)markethours.NewYork[t0];
            indicatordata.SydneyHour = double.IsNaN(markethours.Sydney[t0]) ? 0 : (int)markethours.Sydney[t0];
            indicatordata.TokyoHour = double.IsNaN(markethours.Tokyo[t0]) ? 0 : (int)markethours.Tokyo[t0];
            
            indicatordata.LondonHour1 = double.IsNaN(markethours.London[t1]) ? 0 : (int)markethours.London[t1];  
            indicatordata.NYHour1 = double.IsNaN(markethours.NewYork[t1]) ? 0 : (int)markethours.NewYork[t1];
            indicatordata.SydneyHour1 = double.IsNaN(markethours.Sydney[t1]) ? 0 : (int)markethours.Sydney[t1];
            indicatordata.TokyoHour1 = double.IsNaN(markethours.Tokyo[t1]) ? 0 : (int)markethours.Tokyo[t1];
            
            indicatordata.ocdiff1H = (Hourly.Close[hourlyindex - 1] - Hourly.Open[hourlyindex - 1]) / Symbol.PipSize;
            indicatordata.ocdiff2H = (Hourly.Close[hourlyindex - 2] - Hourly.Open[hourlyindex - 2]) / Symbol.PipSize;

            indicatordata.WklyHighDiff = PipDiff(Weekly.High[weeklyindex], MarketSeries.Close[t0]);
            indicatordata.WklyLowDiff = PipDiff(MarketSeries.Close[t0], Weekly.Low[weeklyindex]);
            
            indicatordata.DateDifference = (int)(EESTDate(MarketSeries.OpenTime[t0]).Date - EESTDate(MarketSeries.OpenTime[t1]).Date).TotalDays;
            
            indicatordata.ssum24_0 = slopesum24.SlopeSum[t0];
            indicatordata.ssum24_1 = slopesum24.SlopeSum[t1];
            indicatordata.ssum24_2 = slopesum24.SlopeSum[t2];
            indicatordata.ssum24_3 = slopesum24.SlopeSum[t3];
            
            indicatordata.ssum96_0 = slopesum96.SlopeSum[t0];
            indicatordata.ssum96_1 = slopesum96.SlopeSum[t1];
            indicatordata.ssum96_2 = slopesum96.SlopeSum[t2];
            indicatordata.ssum96_3 = slopesum96.SlopeSum[t3];
            
            indicatordata.ma1slp0 = slopesum24.ma1Slope[t0]; 
            indicatordata.ma2slp0 = slopesum24.ma2Slope[t0];
            indicatordata.ma3slp0 = slopesum96.ma1Slope[t0];
            indicatordata.ma4slp0 = slopesum96.ma2Slope[t0];
            
            indicatordata.ma1slp1 = slopesum24.ma1Slope[t1]; 
            indicatordata.ma2slp1 = slopesum24.ma2Slope[t1];
            indicatordata.ma3slp1 = slopesum96.ma1Slope[t1];
            indicatordata.ma4slp1 = slopesum96.ma2Slope[t1];
            
            indicatordata.ma1pk0=mapeaks24.maPeakDiff[t0];
            indicatordata.ma2pk0=mapeaks48.maPeakDiff[t0];
            indicatordata.ma3pk0=mapeaks96.maPeakDiff[t0];
            indicatordata.ma4pk0=mapeaks192.maPeakDiff[t0];
            
            indicatordata.ma1pk1=mapeaks24.maPeakDiff[t1];
            indicatordata.ma2pk1=mapeaks48.maPeakDiff[t1];
            indicatordata.ma3pk1=mapeaks96.maPeakDiff[t1];
            indicatordata.ma4pk1=mapeaks192.maPeakDiff[t1];
            
            indicatordata.ma24x0=madiffs24.Xsignal[t0];
            indicatordata.ma24x1=madiffs24.Xsignal[t1];
            
            int WavePositionWkly = 0;
            for (int i = 0; i <= int.MaxValue; i++)
            {
                if ((t0-i)<0){break;} 
                else if (MarketSeries.High[t0 - i] == indicatordata.high0W)
                {
                    WavePositionWkly = (i + 1) * up;
                    break;
                }
                else if (MarketSeries.Low[t0 - i] == indicatordata.low0W)
                {
                    WavePositionWkly = (i + 1) * dn;
                    break;
                }
            }
            indicatordata.LstPkWkly = WavePositionWkly;

            return indicatordata;
        }

Declare the struct variable outside the onTick event:

        idata id;

Call the function:

protected override void OnTick()
{   
            id = GetIndicatorData();
}

 


@lec0456