How to call a reference

Created at 25 Oct 2013, 19:25
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!
jhtrader's avatar

jhtrader

Joined 15.10.2013 Blocked

How to call a reference
25 Oct 2013, 19:25


I would like to call a class fxPortfolio(string ptype) from my robot which would be a separate file. using a reference.

The fxPortfolio class is:

using System;
using System.Collections.Generic;
//using cAlgo.API;
//using cAlgo.API.Internals;
//using cAlgo.API.Requests;

namespace cAlgo.API.Collections
{
    [Robot(TimeZone = TimeZones.CentralStandardTime)]

    public class fxPortfolio : object
    {

        public string[,,] portfolioOptions = new string[,,] 
        {
            {
                {

                    "US",
                    "Buy",
                    "USDCHF"

                }

            }
        };
        //Array Ends above    

        public void getPortfolio(string pName)
        {

            for (int col = 0; col < portfolioOptions.GetLength(0); col++)
                for (int row = 0; row < portfolioOptions.GetLength(1); row++)
                    for (int depth = 0; depth < portfolioOptions.GetLength(2); depth++)

                        if (portfolioOptions[col, row, depth] == pName)
                            Console.WriteLine("Hello");
            //Print("{0},{1},{2}", portfolioOptions[col, row, depth].ToString());


        }

//end class 

    }

// end namespace

}


        }
      }

 

QUESTIONS

Firstly please explain why I cant use the Print method when I try compiling it with the Print method it returns errors..

Second how do I compile this class and reference it... and call it from myRobot?
 

Many thanks.

 

 

 

 

 

 

 

 


 


Replies

Spotware
29 Oct 2013, 14:53

RE:

jhtrader said:

QUESTIONS

Firstly please explain why I cant use the Print method when I try compiling it with the Print method it returns errors..

See the example below on how to use the Print method in your class.

Second how do I compile this class and reference it... and call it from myRobot?

Compile the same way. Reference by adding a reference to your class algo file.

Your class:

using System;

namespace Portofolio
{
    public class PortfolioOptions
    {
        Array portfolioOptions = Array.CreateInstance(typeof(string), 1, 1, 3);

        Action<object> print;

        public PortfolioOptions(Action<object> print)
        {
            this.print = print;

            portfolioOptions.SetValue("US", 0, 0, 0);
            portfolioOptions.SetValue("Buy", 0, 0, 1);
            portfolioOptions.SetValue("USDCHF", 0, 0, 2);
        }

        public void CheckOptions(string pName, Action<object> print)
        {
            foreach (string option in portfolioOptions)
            {
                if (option == pName)
                    print(pName);
            }
        }
    }
}

Robot using your class:

//#reference: Portofolio.algo

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class NewRobot : Robot
    {
        protected override void OnStart()
        {
            PortfolioOptions portofolio = new PortfolioOptions(Print);
            portofolio.CheckOptions(Symbol.Code, Print);
        }
    }
}

 

Further, see the examples at the end of these documents for Array syntax
http://msdn.microsoft.com/en-us/library/System.Array(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/bybh3298.aspx


@Spotware

jhtrader
08 Nov 2013, 21:54

This generates the following errors...

I put the class into its own cAlgo class.. the class compiles when inside the robot.

However when I followed your instructions and put the class into a separate file and referenced it

 

I get the following error when compiling the fxPortfolioClass..

1. The type or namespace name Position cannot be found.  If I remove the reference to the Position I get the error "Unable to load Assembly - Assembly must contain a single type"

2. I get the error the type or namespace fxPortfolioClass cannot be found in the robot.

The file is in the C:\Users\user\Documents\cAlgo\Sources\Robots

I checked to ensure spelling... what

***** Robot

//#reference: fxPortfolioClass.algo
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using System.Collections.Generic;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.Indicators;
using System.IO;
using fxPortfolioClass;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class ScoutRobot : Robot
    { ...}

}

**** fxPortfolioClass

using System;
using System.Linq;
using System.Collections.Generic;

namespace fxPortfolioClass
{
    public class Portfolio
    {


        public string portfolioName;
        public bool isLong;
        public double volume;
        public List<Position> _myPositions = new List<Position>();

    }

}

 


daemon
11 Nov 2013, 09:54

You need to include cAlgo.API and attach the [Robot()] attribute to fxPortfolioClass


@daemon

jhtrader
11 Nov 2013, 19:49

Not sure that is what I want

I am trying to build a generic reference class not a robot.

The example above given does not include the [Robot()] attribute to fxPortfolioClass, this would defeat the purpose.

 


daemon
12 Nov 2013, 09:55

RE: Not sure that is what I want

jhtrader said:

I am trying to build a generic reference class not a robot.

The example above given does not include the [Robot()] attribute to fxPortfolioClass, this would defeat the purpose.

 

You do need to include cAlgo.API because that is where Position is defined.
You don't need to add the robot attribute. But you will get Unable to load assembly message. 


@daemon

jhtrader
12 Nov 2013, 19:18

umm... I dont know what you mean

Yes, if I add the cAlgo.API reference I get the unable to load assembly message..

Here is the code, please let me know if you can get this to run.

Here is the ROBOT

//#reference: TestClass.algo

// -------------------------------------------------------------------------------------------------
//
//  This robot illustrates an example of calling an external class    
//
//
// -------------------------------------------------------------------------------------------------


using System;
using System.Linq;
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.Indicators;
using TestClass;


namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.CentralStandardTime)]

    public class BasicBuyRobot : Robot
    {

        [Parameter(DefaultValue = "myRobot")]
        public string LabelName { get; set; }

        [Parameter("Source")]
        // CLOSE, HIGH, LOW... 
        public DataSeries Source { get; set; }


        //Initialising Robot the first time
        protected override void OnStart()
        {
            //Create portfolio
            Portfolio p = new Portfolio("AUD", true, 10000);
            p.printPortfolio();
        }


        //*************************
        //called on each onBar() or onTick()
        //*************************    
        protected override void OnTick()
        {

            //--------------------------
            //Do nothing if system is busy
            //--------------------------

            if (Trade.IsExecuting)
                return;

            //close portfolio


        }

//end Robot

    }

// end Robot namespace
}


//**********************************************************************

HERE IS THE CLASS

using System;
using cAlgo.API;
namespace TestClass
{

    public class Portfolio
    {


        public string portfolioName;
        public bool isLong;
        public double volume;
        public DateTime OpenDate;
        public DateTime lastUpdated;


        //Constructor with portfolio type
        public Portfolio(string pname, bool tradeDir, double vol)
        {
            portfolioName = pname;
            isLong = tradeDir;
            volume = vol;
            OpenDate = System.DateTime.Now;
            lastUpdated = OpenDate;

        }


        public void printPortfolio()
        {
            int OrdCount = 1;
            Console.WriteLine("************************************************");

            Console.WriteLine("Portfolio {0} was open on {1} with volume = {2}", portfolioName, OpenDate, volume);

            Console.WriteLine("**************||||||||||||||||*****************");

        }




//end Portfolio class above
    }

//end namespace
}





 

 

 

 

 

 

 

 

 


jhtrader
12 Nov 2013, 19:27

I understand what you mean... but.

 

Changing the class by adding

using System;
using cAlgo.API;

namespace TestClass
{
    [Robot()]
    public class Portfolio : Robot
    {  //...
    }

}

 

Will compile but this doesnt make sense why do I need to make the class a robot?  What is the implications of declaring it a Robot?

jhtrader said:

Yes, if I add the cAlgo.API reference I get the unable to load assembly message..

Here is the code, please let me know if you can get this to run.

Here is the ROBOT

//#reference: TestClass.algo

// -------------------------------------------------------------------------------------------------
//
//  This robot illustrates an example of calling an external class    
//
//
// -------------------------------------------------------------------------------------------------


using System;
using System.Linq;
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.Indicators;
using TestClass;


namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.CentralStandardTime)]

    public class BasicBuyRobot : Robot
    {

        [Parameter(DefaultValue = "myRobot")]
        public string LabelName { get; set; }

        [Parameter("Source")]
        // CLOSE, HIGH, LOW... 
        public DataSeries Source { get; set; }


        //Initialising Robot the first time
        protected override void OnStart()
        {
            //Create portfolio
            Portfolio p = new Portfolio("AUD", true, 10000);
            p.printPortfolio();
        }


        //*************************
        //called on each onBar() or onTick()
        //*************************    
        protected override void OnTick()
        {

            //--------------------------
            //Do nothing if system is busy
            //--------------------------

            if (Trade.IsExecuting)
                return;

            //close portfolio


        }

//end Robot

    }

// end Robot namespace
}


//**********************************************************************

HERE IS THE CLASS

using System;
using cAlgo.API;
namespace TestClass
{

    public class Portfolio
    {


        public string portfolioName;
        public bool isLong;
        public double volume;
        public DateTime OpenDate;
        public DateTime lastUpdated;


        //Constructor with portfolio type
        public Portfolio(string pname, bool tradeDir, double vol)
        {
            portfolioName = pname;
            isLong = tradeDir;
            volume = vol;
            OpenDate = System.DateTime.Now;
            lastUpdated = OpenDate;

        }


        public void printPortfolio()
        {
            int OrdCount = 1;
            Console.WriteLine("************************************************");

            Console.WriteLine("Portfolio {0} was open on {1} with volume = {2}", portfolioName, OpenDate, volume);

            Console.WriteLine("**************||||||||||||||||*****************");

        }




//end Portfolio class above
    }

//end namespace
}





 

 

 

 

 

 

 

 

 

 


daemon
13 Nov 2013, 09:44

RE: I understand what you mean... but.

jhtrader said:

 

Changing the class by adding

using System;
using cAlgo.API;

namespace TestClass
{
    [Robot()]
    public class Portfolio : Robot
    {  //...
    }

}

 

Will compile but this doesnt make sense why do I need to make the class a robot?  What is the implications of declaring it a Robot?

You don't have to, it will compile without the robot attribute. Just ignore the message. 


@daemon