These two must be simple but they are killing me !!

Created at 30 Nov 2015, 04: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!
SI

sim_trader

Joined 03.08.2013

These two must be simple but they are killing me !!
30 Nov 2015, 04:11


Hi Everyone,

Thanks again for all your help in the past. I have been struggling for a couple of days trying to implement 2 relatively simple things.  I have tried reading the forum and C# websites and the help.  I am still very new to C. 99% things I have solved, but these are killing me !

 

Problem 1

I am trying to simply read in the current market depth volume for the present bid and the present ask.  In the simplest way possible. 

Presently my code is this

              MarketDepth marketDepth = MarketData.GetMarketDepth(Symbol);

               var volumeask = marketDepth.AskEntries[0].Volume;
               var volumebid = marketDepth.BidEntries[0].Volume;

It complies but then crashes the cbot at runtime. What is the proper way ?

            

Problem 2

I simply need to get the maximum ask price and minimum bid price over the last 200 ticks.  In MT4 I would simple build an array and store the last 200 bid/ask values which I could then use for many things. However, in C# I cannot get it work. This runs and compiles and is placed within “On Tick()”

           

            double[] bids;
            bids = new double[200];

            foreach (int i in bids)
            {
                bids[i + 1] = bids[i];
            }
            bids[0] = Symbol.Bid;


            double[] asks;
            asks = new double[200];
            foreach (int i in asks)
            {
                asks[i + 1] = asks[i];
            }
            bids[0] = Symbol.Bid;


            var high = bids.Max();
            var low = asks.Min();

 

But only the most recent value is stored. I think it is the  “bids = new double[200];”  line. It resets the array every tick. However, I cannot find away around this ? I am new to C# arrays, I have read and read and cannot find the solution.

Feel  free to suggest any alterative that get the maximum ask prices and minimum bid price over the last 200 ticks. I am sure there is an easier way.  Additionally (or alternatively) if anyone can show me how to correct my own code above, I am eager to learn.

Thanks so much, I really appreciate everyone's time.

Sim


@sim_trader
Replies

Spotware
30 Nov 2015, 19:59

Dear Trader,

In regards to your first issue, the error you are getting is:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

It means that you are trying to retrieve a volume at the time where no ask, bit entries exist. A simple if statement to check the amount of ask and bit entries will resolve your issue.

Please have a look at the following code snippet.

if (marketDepth.AskEntries.Count != 0)
{
//TODO
}

Regarding your second issue. From a single look at your code we can say that you don’t store the last 200 bid/ask values. We recommend you to collect bid and ask prices in RunTime and then try to access them.

We would like to inform you that we do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You also can contact one of our Partners or post a job in Development Jobs section for further coding assistance.


@Spotware

sim_trader
01 Dec 2015, 19:12

Thanks

Hi Spotware,

Thanks loads for your help with the Market Depth. In terms of the second issue, I am trying to collect the bid and ask into an array at runtime. I will keep reading about arrays and find out where I have messed up. It is the only think keeping me from going live at present.  I will keep reading.

Thanks again,

Cheers, Andre

 


@sim_trader