Replies

galaaoui.salma
06 Aug 2020, 11:44 ( Updated at: 21 Dec 2023, 09:22 )

RE:

PanagiotisCharalampous said:

Hi galaaoui.salma,

Your problem lies here

You cannot use MarketData in a robot object that is not attached on a chart. Try passing this object as a parameter to the class.

Best Regards,

Panagiotis 

Join us on Telegram

Hi Panagiotis, 

The problem isn't only the invocation of GetMarketDepth(Symbol.Name); inside the CurrencyPair projects, passing it as a parameter to the constructor does not solve the issue because the problem is also caused by the subscription to the Updated even inside the class.

Kind Regards,

Salma Galaaoui.


@galaaoui.salma

galaaoui.salma
05 Aug 2020, 14:46

RE:
using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{

    public class CurrencyPair : Robot
    {
        public Symbol _symbol;
        public Dictionary<double, double> BUY = new Dictionary<double, double>();
        public Dictionary<double, double> SELL = new Dictionary<double, double>();
        public static Dictionary<KeyValuePair<DateTime, double>, double> BOUGHT = new Dictionary<KeyValuePair<DateTime, double>, double>();
        public static Dictionary<KeyValuePair<DateTime, double>, double> SOLD = new Dictionary<KeyValuePair<DateTime, double>, double>();
        public static double curASpot = 0;
        public static double curBSpot = 0;
        public double prevASpot = 0;
        public double prevBSpot = 0;
        public static double score;
        public static double totAScore;
        public static double totBScore;
        public MarketDepth _marketDepth;


        public CurrencyPair(Symbol sym)
        {
            this._symbol = sym;
        }

        public void StartCurrency()
        {
            _marketDepth = MarketData.GetMarketDepth(_symbol.Name);
            _marketDepth.Updated += MarketDepthUpdated;
        }


        public void MarketDepthUpdated()
        {

            Print("Started Event MarketDepthUpdated");

            _marketDepth = MarketData.GetMarketDepth(_symbol.Name);

            curBSpot = _symbol.Bid;
            curASpot = _symbol.Ask;

            Offset();

            SELL.Clear();
            BUY.Clear();

            foreach (var entry in _marketDepth.AskEntries)
                SELL.Add(entry.Price, entry.VolumeInUnits);

            foreach (var entry in _marketDepth.BidEntries)
                BUY.Add(entry.Price, entry.VolumeInUnits);


            prevBSpot = _symbol.Bid;
            prevASpot = _symbol.Ask;

            Print("Exited Event MarketDepthUpdated");
        }

        public void Offset()
        {

            DateTime now;

            Print("Offset Started");

            if ((prevBSpot == 0) || (prevBSpot == 0) || (curBSpot == 0) || (curASpot == 0))
                return;

            Print(score);


            foreach (var ask in SELL.Reverse())
                if (curASpot >= ask.Key)
                {
                    now = DateTime.UtcNow;
                    BOUGHT.Add(new KeyValuePair<DateTime, double>(now, ask.Key), ask.Value);
                    Print("{0} : BOUGHT : {1}, {2}, {3}", _symbol.Name, now, ask.Key, ask.Value);
                }
                else
                    break;

            foreach (var bid in BUY)
                if (curBSpot <= bid.Key)
                {
                    now = DateTime.UtcNow;
                    SOLD.Add(new KeyValuePair<DateTime, double>(now, bid.Key), bid.Value);
                    Print("{0} : BOUGHT : {1}, {2}, {3}", _symbol.Name, now, bid.Key, bid.Value);
                }
                else
                    break;
        }
    }


    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Playing_DoM_Sequential : Robot
    {
        protected override void OnStart()
        {
            Print("1");
            CurrencyPair EURUSD = new CurrencyPair(Symbols.GetSymbol("EURUSD"));
            EURUSD.StartCurrency();

            Print("2");
            CurrencyPair GBPUSD = new CurrencyPair(Symbols.GetSymbol("GBPUSD"));

            Print("3");
            CurrencyPair EURJPY = new CurrencyPair(Symbols.GetSymbol("EURJPY"));

            Print("4");
        }
    }
}

PanagiotisCharalampous said:

Hi galaaoui.salma,

Can you please provide the compete cBot code so that we can reproduce this behavior?

Best Regards,

Panagiotis 

Join us on Telegram

 

Hi Panagiotis, here you go, the code prints "1" and crashes with NullReferenceException

 


@galaaoui.salma