Resources for Creating Custom DLL?

Created at 24 Oct 2013, 09:23
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

Resources for Creating Custom DLL?
24 Oct 2013, 09:23


HI,

I'm wondering if anyone has examples/resources about writing custom DLLs for cAlgo. 

I would like to compile a library of commonly used methods so I can develop robots/indicators faster. 

Thanks!


@Hyperloop
Replies

hichem
24 Oct 2013, 09:26

RE:

To develop a .dll in visual studio import the cAlgo.API.dll in your project.

After compiling the .dll with Visual Studio you can import it in cAlgo by clicking add Reference, and select the .dll you created.

 

 

Hyperloop said:

HI,

I'm wondering if anyone has examples/resources about writing custom DLLs for cAlgo. 

I would like to compile a library of commonly used methods so I can develop robots/indicators faster. 

Thanks!

 


@hichem

Hyperloop
24 Oct 2013, 20:52

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

namespace PBLibrary
{
    public class stdFun
    {
        // Generate Label function
        // string InstanceID = GenerateLabel("123", "1");
        private string GenerateLabel(string argStrategyID, string argVersionID)
        {
            string FinalLabel = argStrategyID + "-" + argVersionID + "-" + Symbol.Code + "-" + TimeFrame.ToString();
            return FinalLabel;
        }
    }
}

Error    1    An object reference is required for the non-static field, method, or property 'cAlgo.API.Internals.Symbol.Code.get'    C:\Users\Clark\Documents\Visual Studio 2010\Projects\PBLibrary\PBLibrary\Class1.cs    15    76    PBLibrary
Error    2    An object reference is required for the non-static field, method, or property 'object.ToString()'    C:\Users\Clark\Documents\Visual Studio 2010\Projects\PBLibrary\PBLibrary\Class1.cs    15    96    PBLibrary

I'm currently getting this error when I am trying to build this library. When simply copied and pasted this method into a Robot it would run fine. I'm a little new to C# so any help would be appreciate.


@Hyperloop

hichem
24 Oct 2013, 23:01

RE:

The Symbol object is a property of the Robot class. It is undefined inside your stdFun class

 

Hyperloop said:

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

namespace PBLibrary
{
    public class stdFun
    {
        // Generate Label function
        // string InstanceID = GenerateLabel("123", "1");
        private string GenerateLabel(string argStrategyID, string argVersionID)
        {
            string FinalLabel = argStrategyID + "-" + argVersionID + "-" + Symbol.Code + "-" + TimeFrame.ToString();
            return FinalLabel;
        }
    }
}

Error    1    An object reference is required for the non-static field, method, or property 'cAlgo.API.Internals.Symbol.Code.get'    C:\Users\Clark\Documents\Visual Studio 2010\Projects\PBLibrary\PBLibrary\Class1.cs    15    76    PBLibrary
Error    2    An object reference is required for the non-static field, method, or property 'object.ToString()'    C:\Users\Clark\Documents\Visual Studio 2010\Projects\PBLibrary\PBLibrary\Class1.cs    15    96    PBLibrary

I'm currently getting this error when I am trying to build this library. When simply copied and pasted this method into a Robot it would run fine. I'm a little new to C# so any help would be appreciate.

 


@hichem

Hyperloop
25 Oct 2013, 22:20

Hi, 

Thanks for all your help. I'm still a little confused, could you kindly provide an example code so I know what you mean?

Thanks again. :)


@Hyperloop

BestFXBot
29 Oct 2013, 11:11

Library of Trading Tools

This article explains how  import the cAlgo.API.dll in your project. - http://carbonfx.io/2013/08/using-visual-studio-with-calgo/

Once you make the necessary links, you can start building different tools for your projects and import these functions through

using CarbonFx.Utilities; // example


 


@BestFXBot

Hyperloop
29 Oct 2013, 21:33

RE: Library of Trading Tools

BestFXBot said:

This article explains how  import the cAlgo.API.dll in your project. - http://carbonfx.io/2013/08/using-visual-studio-with-calgo/

Once you make the necessary links, you can start building different tools for your projects and import these functions through

using CarbonFx.Utilities; // example
 

Thank you very much, I'll give it a shot. :)


@Hyperloop

PCWalker
05 Jan 2014, 13:05

Better Support for DLL and cAlgo in Visual Studio

Is there any progress in the development of Visual Studio developments for cAlgo?

Thank you.


@PCWalker

Spotware
07 Jan 2014, 10:56

RE: Better Support for DLL and cAlgo in Visual Studio

PCWalker said:

Is there any progress in the development of Visual Studio developments for cAlgo?

Thank you.

Please see /forum/calgo-support/1333?page=2#11


@Spotware

AimHigher
10 Mar 2014, 18:53

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


@AimHigher

Spotware
11 Mar 2014, 09:06

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;
        }
    }

 


@Spotware

Spotware
11 Mar 2014, 09:09

RE:

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


@Spotware

AimHigher
11 Mar 2014, 20:13

RE:
post was removed as duplicated
@AimHigher

PCWalker
25 Jul 2015, 16:55

Resources for Creating Custom DLL?

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


@PCWalker

Spotware
28 Jul 2015, 10:21

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.


@Spotware

kontodospamu5
09 Jan 2019, 18:24 ( Updated at: 21 Dec 2023, 09:21 )

RE:

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


@kontodospamu5

PanagiotisCharalampous
10 Jan 2019, 09:39

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


@PanagiotisCharalampous

kontodospamu5
11 Jan 2019, 19:12 ( Updated at: 21 Dec 2023, 09:21 )

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


@kontodospamu5

PanagiotisCharalampous
14 Jan 2019, 10:35

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


@PanagiotisCharalampous

kontodospamu5
14 Jan 2019, 11:33

Hello Panagiotis,

Thank you for a quick response, indeed.

Works perfectly, that is what I was looking for.

Thank you & Regards,

Piotr


@kontodospamu5