How to find the previous and next numbers that ends in "00" regardless of cTrader platform?

Created at 28 May 2020, 09:15
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!
FI

firemyst

Joined 26.03.2019

How to find the previous and next numbers that ends in "00" regardless of cTrader platform?
28 May 2020, 09:15


Hi all:

For every chart on every cTrader based platform, how can I find the next closest number that ends in zero-zero "00"?

I don't want to have to create a list of every symbol and specify what to divide/round by.

So I thought I would try basing it on Symbol.Digits, but different brokers have different "digits" for the same symbol! For instance, IC Markets has 2 digits for the DOW "US30" whereas Pepperstone has 1 digit for it.

Some examples of what I want:

US30: 25,371.4 --> 25400.00  (Pepperstone 1 digit)

US30 25,371.43 --> 25400.00  (IC Markets 2 digits)

GER30: 11810.2 --> 11900.0 (Pepperstone Dax 1 digit)

DE30: 11810.20 --> 11900.00 (IC Markets Dax 2 digits)

USDJPY 107.821 --> 108.00

XAUUSD 1717.87 --> 1718.00

GBPUSD 1.22540 --> 1.2300

AUDUSD 0.66197 --> 0.6700

Worse case scenario is to write a routine that starts from 0 and calculates every 100 pips until we get to the first 100 pip interval above the current price? That seems ridiculous to have to do though.

Thoughts anyone? @Spotware?

Thank you.


@firemyst
Replies

PanagiotisCharalampous
28 May 2020, 09:33

Hi firemyst,

Usually such functionality is achieved using equations in the form of number % step * step for the lower number and number % step * (step + 1). 

For example, let's assume you have 24555 and you want the closest 100 step

Lower step is 24555 % 100 * 100 = 24500

Upper step is 24555 % 100 * 101 = 24600

Hence the closest is 24600.

I hope this helps.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

firemyst
28 May 2020, 10:21

RE:

PanagiotisCharalampous said:

Hi firemyst,

Usually such functionality is achieved using equations in the form of number % step * step for the lower number and number % step * (step + 1). 

For example, let's assume you have 24555 and you want the closest 100 step

Lower step is 24555 % 100 * 100 = 24500

Upper step is 24555 % 100 * 101 = 24600

Hence the closest is 24600.

I hope this helps.

Best Regards,

Panagiotis 

Join us on Telegram

 

Thanks for your feedback @Panagiotis.

What you've stated doesn't work for decimal numbers.

Take my examples listed. With your number you truncate the decimal point. How do you know how many decimal places to truncate?

For example, 0.66197. It's AUDUSD forex pair, and with both brokers the symbol.digits value is 4, so you can implement this formula:

double factor = 10 * (100 / (double)(Math.Pow(10, symbolDigits)));
return (Math.Truncate(price / factor) * factor) + (factor);

Same with any JPY pair because even though there's less digits, the symbol.digits value is the same between brokers.

But for the US30 and other index markets, it doesn't work because the number of digits varies between brokers. Same with the German DAX, Uk FTSE, etc.

So the above formula will work perfectly with Pepperstone because their market indexes all have a symbol.digits value of 1; however, with IC Markets, the code breaks because their symbol.digits = 2. I think Spotware does the same with the US 30.

This means then to get the formula to work, I have to implement some sort of tuple or dictionary object that maps brokers to markets and specify what the symbol.digits should be.

Eg, in this case I'd have to have an entry for Spotware / US30 / 1.

But I don't want to have to create and maintain a list of all brokers for all market symbols where the Symbol.Digit value varies, and am hoping there's some other programmatic way of implementing this (like starting from 0 and going every 100 pips).

Any other thoughts?

Thank you.


@firemyst

PanagiotisCharalampous
28 May 2020, 10:59

Hi firemyst,

Why don't you use Symbol.Digits?

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

firemyst
28 May 2020, 11:35

RE:

PanagiotisCharalampous said:

Hi firemyst,

Why don't you use Symbol.Digits?

Best Regards,

Panagiotis 

Join us on Telegram

I do.

Sorry - the code above is the two lines in the function.

"symbolDigits" is the "Symbol.Digits" parameter.

public static double GetNextHundredth(int symbolDigits, double price)
{
    double factor = 10 * (100 / (double)(Math.Pow(10, symbolDigits)));
    return (Math.Truncate(price / factor) * factor) + (factor);
}

//usage
nextHundredth = GetNextHundredth(Symbol.Digits, Bars.ClosePrices.Last(0));


As I said, it works for the US30 on Pepperstone because Symbol.Digits = 1; for SpotWare and IC Markets Symbol.Digits = 2 for the US 30; Same for the DE30/GER30, UK100, AUS200, etc. All the major indexes - different brokers have different values for Symbol.Digits.

It works perfectly across all known Forex Pairs because every broker seems to have them as Symbol.Digits = 4.

I haven't looked into Metals, Oils, and other symbols yet.

 


@firemyst