Calculating PipValue - Error in cross calculation

Created at 30 Oct 2013, 07:22
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!
Hyperloop's avatar

Hyperloop

Joined 23.10.2013

Calculating PipValue - Error in cross calculation
30 Oct 2013, 07:22


public bool IsSymbol(string argSymbolCode)
        {
            try
            {
                Symbol XXXYYY = MarketData.GetSymbol(argSymbolCode);
            } catch (Exception ex)
            {
                return false;
            }

            return true;
        }
        public double CalculatePipValue(string argSymbolCode)
        {
            double FinalPipValue = 0;

            string BaseCurr = argSymbolCode.Substring(0, 3);
            string QuoteCurr = argSymbolCode.Substring(3, 3);
            string AccountCurr = Account.Currency;

            bool CheckZZZXXX = IsSymbol(QuoteCurr + AccountCurr);

            if (QuoteCurr != "JPY" || AccountCurr == "JPY")
            {
                if (AccountCurr == QuoteCurr && AccountCurr != "JPY")
                {
                    FinalPipValue = 10;
                }
                else if (AccountCurr == "JPY" && QuoteCurr == "JPY")
                {
                    FinalPipValue = 1000;
                }
                else if (AccountCurr == BaseCurr)
                {
                    Symbol XXXYYY = MarketData.GetSymbol(AccountCurr + QuoteCurr);
                    double XXXYYYp = (XXXYYY.Ask + XXXYYY.Bid) / 2;
                    FinalPipValue = 10 / XXXYYYp;
                }
                else if (AccountCurr != BaseCurr && AccountCurr != QuoteCurr)
                {
                    if (CheckZZZXXX)
                    {
                        double ZZZXXXp = MarketData.GetSymbol(QuoteCurr + AccountCurr).Ask;
                        FinalPipValue = 10 * ZZZXXXp;
                    }
                    else
                    {
                        double XXXZZZp = MarketData.GetSymbol(AccountCurr + QuoteCurr).Bid;
                        FinalPipValue = 10 / XXXZZZp;
                    }
                }
            }
            else
            {
                Symbol XXXJPY = MarketData.GetSymbol(AccountCurr + "JPY");
                double XXXJPYp = (XXXJPY.Ask + XXXJPY.Bid) / 2;
                FinalPipValue = 1000 / XXXJPYp;
            }
            return FinalPipValue;
        }

This is a method I wrote which retrieves the current pip value of any symbol in the value of the account currency. Everything works, except the bold "else" block which deals with crosses. 

 

Oddly enough if I just put a simple FinalPipValue = 10; it will run fine, as soon as I call the GetSymbol, nothing happens. 

Any help would be appreciated and feel free to take this code if you want, once it's fixed.

:)


@Hyperloop
Replies

Hyperloop
30 Oct 2013, 07:23

The bold didn't format correctly. Lines 34-38 is what I am talking about.


@Hyperloop

Kate
30 Oct 2013, 11:12

Try this code:

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class PipValueTest : Robot
    {
        protected override void OnStart()
        {
            var symbol = MarketData.GetSymbol("GBPCAD");
            Print("PipValue of {0} is {1:0.0000000} ", symbol.Code, GetPipValue(symbol));

            Stop();
        }

        private double GetPipValue(Symbol symbol)
        {
            var quoteCurrency = symbol.Code.Substring(3, 3);

            if (quoteCurrency == Account.Currency)
                return 1;

            var rate = GetConversionRate(quoteCurrency, Account.Currency);
            Print("Conversion rate {0} -> {1} is {2:0.000}", quoteCurrency, Account.Currency, rate);

            var pipValue = symbol.PipSize * rate;
            return pipValue;
        }

        private double GetConversionRate(string fromCurrency, string toCurrency)
        {
            Symbol symbol = TryGetSymbol(fromCurrency + toCurrency);

            if (symbol != null)
                return symbol.Bid;

            symbol = TryGetSymbol(toCurrency + fromCurrency);
            return 1 / symbol.Bid;
        }

        private Symbol TryGetSymbol(string symbolCode)
        {
            try
            {
                Symbol symbol = MarketData.GetSymbol(symbolCode);
                if (symbol.Bid == 0.0)
                    return null;
                return symbol;
            } catch
            {
                return null;
            }
        }
    }
}

 


@Kate

Hyperloop
30 Oct 2013, 20:07

Hi,

I just ran your code on the GBPCAD, and got the error:

"Crashed in OnStart with NullReferenceException: Object reference not set to an instance of an object."


@Hyperloop

Spotware
11 Nov 2013, 15:31

PipValue property is added to Symbol class. Please check new release of Spotware cAlgo (1.12).


@Spotware