Resources for Creating Custom DLL?

11 Mar 2014, 09:06Resources for Creating Custom DLL?#11
cTrader Teamposts: 3382since: 23 Sep 2013

In order to access Positions collection you need to obtain it from instance of Robot class. 

For example:

public class General
    {
        private int GetPosCnt(Robot robot)
        {
            int iPosCnt;
            iPosCnt = robot.Positions.Count;
            return iPosCnt;
        }
    }

 


TRADERS FIRST™ Vote for your favorite features: http://vote.spotware.com/
11 Mar 2014, 09:09RE:#12
cTrader Teamposts: 3382since: 23 Sep 2013

AimHigher said:

I am having the same problem as Hyperloop and since I am also new to C#, setting the correct references and objects is still hit or miss for me. As I believe Hyperloop was trying to do, I am trying to create a dll to hold methods that I will use in different robots. I have created other dlls that are working fine but I am struggling with using cAlgo,API in my custom dll.

I am working in VS 2010. I have added cAlgo.API in References (for this project) so I get IntelliSense and I can view it fine through the Object Browser. I also have no problem using VS2010 to write cBots in general. That part is working great. It is creating a custom dll that uses cAlgo.API dll that has me stomped.

The code I have so far for my custom dll is this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;

namespace cAlgoGeneralMethods
{
    public class General
    {
        private int GetPosCnt()
        {
        	int iPosCnt;
	        iPosCnt = Positions.Count;
        	return iPosCnt;
        }
    }
}

As anyone who knows C# (I don't) the line 

iPosCnt = Positions.Count; will not work and I would get the same error as Hyperloop got.

I have looked at the responses that Hyperloop got but I have not found a specific example of how to get it to work. I would very much appreciate a quick sample of what I need to add to get cAlgo.API objects/methods recognized in my custom dll.

Regards,

Aim

/forum/cbot-support/1739?page=2#11


TRADERS FIRST™ Vote for your favorite features: http://vote.spotware.com/
11 Mar 2014, 20:13RE:#13
AimHigherposts: 24since: 04 Jan 2014
post was removed as duplicated
25 Jul 2015, 16:55Resources for Creating Custom DLL?#14
PCWalkerposts: 21since: 19 Nov 2012

Where can I download cAlgo.API for development under VS 2013 or VS 2015?

28 Jul 2015, 10:21#15
cTrader Teamposts: 3382since: 23 Sep 2013

Dear Trader,

Open API can be used in any Microsoft Visual Studio version. Please have a look at our API Reference. The links to download the latest Trading.API are in the Resources section.


TRADERS FIRST™ Vote for your favorite features: http://vote.spotware.com/
09 Jan 2019, 18:24RE:#16
Piotr posts: 14since: 13 Mar 2017

Spotware said:

In order to access Positions collection you need to obtain it from instance of Robot class. 

For example:

public class General
    {
        private int GetPosCnt(Robot robot)
        {
            int iPosCnt;
            iPosCnt = robot.Positions.Count;
            return iPosCnt;
        }
    }

 

Dear Spotware,

 

I tried to call “GetPosCnt(robot)” in my cBot:

namespace cAlgo.Robots

{

[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]

public class sample_cBot : Robot

{

protected override void OnStart()

{

Robot robot = new Robot();

robot = Robot;

GetPosCnt(robot)


}

}

}

 

 

as following:

Robot robot = new Robot();

robot = Robot;

GetPosCnt(robot)

 

 

But it returns error of the form:

 

Could you tell me what am I doing wrong?

Could you show us the syntax of the calling method, pls.

How to effectively obtain it from instance of Robot class in the dll embedded method "GetPosCnt" so that to have access to "Positions", "MarketData" etc?

 

Regards,

Piotr

10 Jan 2019, 09:39#17
Panagiotis Charalampousposts: 2821since: 13 Jan 2017

Hi Piotr,

I am confused regarding what you are trying to do. Why do you need to call GetPosCnt(robot) from within the cBot? Where did you define this function? What do you try to accomplish with the following line of code?

robot = Robot;

In order to help you, I would be good to understand what you are trying to do.

Best Regards,

Panagiotis


Head of Community Management at cTrader
11 Jan 2019, 19:12#18
Piotr posts: 14since: 13 Mar 2017

Hello Panagiotis,

 

Thank you for your questions. Let me start from the beginning.

What is the architecture of my solution?

There is cBot (“A”), and the dynamic library .dll (“B”). Now in B I would like to embed some methods that I often use in my cBots.

For example, in B I would like to have:

  1. the calculation methods which compute my house-made oscillators,
  2. a method that calculates my commission (some brokers offer commissions that is not so easily calculable),
  3. methods that breaks-down total unrealized PnL, to PnLs’ of a “sub-strategy”,
  4. exporting my Positions to Excel etc

Now, in “A” I would like to call a method that is placed in B. But I would like to take from A to B not only arguments which are double, int, string etc, I would like to take from A to B also the instances of "Positions", "MarketData" etc.

 

I try to construct dll as follows:

using cAlgo.API;

namespace pw_cTrader
{
    public class General : Robot
    {
        public static int GetPosCnt(Robot robot)
        {
            int iPosCnt;
            iPosCnt = robot.Positions.Count;
            return iPosCnt;
        }
        
        public static void Get_bid_ask(Robot robot, out double bid, out double ask)
        {
            cAlgo.API.Internals.Symbol s1 = robot.MarketData.GetSymbol("EURUSD");
            bid = s1.Bid;
            ask = s1.Ask;
        }
    }
}

 

then I try to call method GetPosCnt() in cBot in the following way:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class sample_cBot : Robot
    {
        protected override void OnStart()
        {
            Robot robot = new Robot();
            //robot = Robot;

            int how_manyPositions = 0;
            how_manyPositions = General.GetPosCnt(robot);

            double bid, ask;
            General.Get_bid_ask(robot, out bid, out ask);


        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 

“A” enters dll but stalls on “iPosCnt = robot.Positions.Count;” row showing the error:

Could you tell me how to effectively transfer the instance of Robot class to the dll embedded method "GetPosCnt" so that to have access to "Positions", "MarketData" etc?

Regards,

Piotr

14 Jan 2019, 10:35#19
Panagiotis Charalampousposts: 2821since: 13 Jan 2017

Hi Piotr,

See below an example on how to do this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;

namespace cAlgo
{
    public class General
    {
        public static int GetPosCnt(Robot robot)
        {
            int iPosCnt;
            iPosCnt = robot.Positions.Count;
            return iPosCnt;
        }

        public static double GetLastCloseValue(Robot robot)
        {          
            return robot.MarketSeries.Close.LastValue;
        }
    }
}
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        protected override void OnStart()
        {
            Print(General.GetPosCnt(this));
            Print(General.GetLastCloseValue(this));
        }

        protected override void OnBar()
        {
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis


Head of Community Management at cTrader
14 Jan 2019, 11:33#20
Piotr posts: 14since: 13 Mar 2017

Hello Panagiotis,

Thank you for a quick response, indeed.

Works perfectly, that is what I was looking for.

Thank you & Regards,

Piotr