Struggling with coding on cAlgo

Created at 22 Oct 2015, 17:11
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!
ST

strugglingwithc

Joined 22.10.2015

Struggling with coding on cAlgo
22 Oct 2015, 17:11


Hi,

Firstly I am not a great coder and it is not for the lack of trying. I was a able to get things done on the Protrader 2 platform which uses C#, however I must admit  -  i just got it to work i.e.my skills are not great

Here is what I am trying to do
1) Obtain the 'Top of the book' bid and ask volumes for a currency pair, do some simple caluclations and display it as a histogram. It stores the values historically.

No matter what I do I cannot get the data onto the histogram. I simply do not know how.

Primary problems are -  1) how to get level 2 top of book volumes into a variable
                                -  2) how can i get some calculated data into the histogram. Simple calculations  -  additions and divisions.



So far I have looked through the following examples  -

/forum/indicator-support/1005#

/forum/indicator-support/5358

/algos/indicators/show/915

/algos/indicators/show/723

/forum/calgo-reference-samples/253


If someone can just do this, I would be eternally great full and I will be able to figure out things from there on..


Below is what I have done so far. I must admit it is beyond laughable, so please be considerate. And the code has been edited numerous times ( numerous attempts) and the below one is the last attempt which I just pasted here so that someone who has a look can have an idea of what I am after ( apart from having a good laugh)

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class test : Indicator
    {

        [Output("AskVol-BidVol Hist", Color = Colors.Blue, PlotType = PlotType.Histogram, Thickness = 2)]
        public IndicatorDataSeries AminusBhist { get; set; }
        [Output("AskVol-BidVol Line", Color = Colors.Yellow, Thickness = 1)]
        public IndicatorDataSeries Result { get; set; }





        public MarketDepth _marketDepth;

        public double bid0avg = 0;
        public double ask0avg = 0;
        public double bid0avgline = 0;
        public double ask0avgline = 0;

        public double delta0avg = 0;
        public double tobBcount = 0;


        public double bid0 = 0;
        public double ask0 = 0;
        public double bid0total = 0;
        public double ask0total = 0;
        public double avgcount = 0;

        public double tobA = 0;
        public double tobB = 0;


        public double curtobA = 0;
        public double prevtobA = 0;

        public double curtobB = 0;
        public double prevtobB = 0;

        protected override void Initialize()
        {
            //  Get Market Depth
            _marketDepth = MarketData.GetMarketDepth(Symbol);
            // subscribe to event Updated
            _marketDepth.Updated += MarketDepthUpdated;
        }

        void MarketDepthUpdated()
        {

            foreach (var entry in _marketDepth.AskEntries)
            {
                tobA = _marketDepth.AskEntries[0].Volume;

//trying to get the top of the book data into the variable
            }


            foreach (var entry in _marketDepth.AskEntries)
            {
                tobB = _marketDepth.BidEntries[0].Volume;
            }



            curtobB = tobB;

            curtobA = tobA;

            if ((curtobB - prevtobB != 0) || (curtobA - prevtobA != 0))
            {

                avgcount = avgcount + 1;

                bid0 = curtobB;

                ask0 = curtobA;



                bid0total += bid0;

                ask0total += ask0;


                bid0avg = bid0total / avgcount;

                ask0avg = ask0total / avgcount;



                ask0avgline += ask0avg;
                bid0avgline += bid0avg;

// tyring to get the value of 'delta0avg' on to the histogram
             delta0avg = ask0avgline - bid0avgline;  


            }
            prevtobB = curtobB;
            prevtobA = curtobA;

        }




        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
        }
    }
}



 

 




 

 




       
      



    





 

 

 

 


@strugglingwithc
Replies

LSR2412
22 Oct 2015, 17:43

I am not expert..but histogram is plotted by AminusBhist data...which is not existing

 

AminusBhist must be equal to something..in order to be plotted


@LSR2412

LSR2412
22 Oct 2015, 17:45

 

try this

  delta0avg = ask0avgline - bid0avgline; 

AminusBhist = delta0avg


@LSR2412

strugglingwithc
22 Oct 2015, 18:20

Appreciate you trying to help me out. I did try them both out during my numerous attempts. There is more to it i.e. adding data to the 'indicatordataseries' type is different than a 'double' character type which is what the result of 'ask0avgline-bidoavgline' will give. I have not figured this out. In most of the examples  -  an 'index' is used and I do not understand how.


@strugglingwithc

Spotware
22 Oct 2015, 20:32

Dear Trader,

Please have a look at our Indicators Guides.

For your information the index parameter, represents each bar, ranging from 0 up to the current (last) bar. 


@Spotware

LSR2412
23 Oct 2015, 09:07

it works!!! :)

 

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class test : Indicator
    {

        [Output("AskVol-BidVol Hist", Color = Colors.Blue, PlotType = PlotType.Histogram, Thickness = 2)]
        public IndicatorDataSeries AminusBhist { get; set; }
        [Output("AskVol-BidVol Line", Color = Colors.Yellow, Thickness = 1)]
        public IndicatorDataSeries Result { get; set; }





        public MarketDepth _marketDepth;

        public double bid0avg = 0;
        public double ask0avg = 0;
        public double bid0avgline = 0;
        public double ask0avgline = 0;

        public double delta0avg = 0;
        public double tobBcount = 0;


        public double bid0 = 0;
        public double ask0 = 0;
        public double bid0total = 0;
        public double ask0total = 0;
        public double avgcount = 0;

        public double tobA = 0;
        public double tobB = 0;


        public double curtobA = 0;
        public double prevtobA = 0;

        public double curtobB = 0;
        public double prevtobB = 0;

        protected override void Initialize()
        {
            //  Get Market Depth
            _marketDepth = MarketData.GetMarketDepth(Symbol);
            // subscribe to event Updated
            _marketDepth.Updated += MarketDepthUpdated;
        }

        void MarketDepthUpdated()
        {

            foreach (var entry in _marketDepth.AskEntries)
            {
                tobA = _marketDepth.AskEntries[0].Volume;

//trying to get the top of the book data into the variable
            }


            foreach (var entry in _marketDepth.AskEntries)
            {
                tobB = _marketDepth.BidEntries[0].Volume;
            }



            curtobB = tobB;

            curtobA = tobA;

            if ((curtobB - prevtobB != 0) || (curtobA - prevtobA != 0))
            {

                avgcount = avgcount + 1;

                bid0 = curtobB;

                ask0 = curtobA;



                bid0total += bid0;

                ask0total += ask0;


                bid0avg = bid0total / avgcount;

                ask0avg = ask0total / avgcount;



                ask0avgline += ask0avg;
                bid0avgline += bid0avg;

// tyring to get the value of 'delta0avg' on to the histogram
                delta0avg = ask0avgline - bid0avgline;


            }
            prevtobB = curtobB;
            prevtobA = curtobA;

        }




        public override void Calculate(int index)
        {
            AminusBhist[index] = delta0avg;
        }
    }
}


 


@LSR2412

LSR2412
23 Oct 2015, 09:31

this is cTrader example for market depth

hope this helps

using cAlgo.API;
namespace cAlgo.Indicators
{
    [Indicator()]
    public class Level2 : Indicator
    {
        [Output("BidEntries", Color = Colors.Red, PlotType = PlotType.Histogram, Thickness = 5)]
        public IndicatorDataSeries BidResult { get; set; }
        [Output("AskEntries", Color = Colors.Blue, PlotType = PlotType.Histogram, Thickness = 5)]
        public IndicatorDataSeries AskResult { get; set; }
        MarketDepth GBPUSD;
        private int _askNo;
        private int _bidNo;
        protected override void Initialize()
        {
            GBPUSD = MarketData.GetMarketDepth(Symbol);
            GBPUSD.Updated += OnGbpUsdUpdated;
        }
        void OnGbpUsdUpdated()
        {
            _askNo = 0;
            _bidNo = 0;
            var index = MarketSeries.Close.Count - 1;
            for (var i = 0; i < GBPUSD.AskEntries.Count; i++)
                AskResult[index - i] = double.NaN;
            foreach (var entry in GBPUSD.AskEntries)
            {
                AskResult[index - _askNo] = (-1) * entry.Volume;
                _askNo++;
            }
            for (var i = 0; i < GBPUSD.BidEntries.Count; i++)
                BidResult[index - i] = double.NaN;
            foreach (var entry in GBPUSD.BidEntries)
            {
                BidResult[index - _bidNo] = entry.Volume;
                _bidNo++;
            }
        }
        public override void Calculate(int index)
        {
        }
    }
}

 


@LSR2412

strugglingwithc
27 Oct 2015, 16:52

Thanks a lot Hjozan and spotware :)

 


@strugglingwithc