Replies

freemoniescapital
25 Oct 2022, 09:00

RE: RE:

Hi pick,

 

I think this works. Thank you so much :)

 

pick said:

freemoniescapital said:

Hi Panagiotis,

Appreciate your response and apologies for the late reply. If I understand correctly, is the following code correct?

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

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class TEST : Robot
    {
        protected override void OnStart()
        {
             PRINTINGCLASS printingClass = new(this);
             printingClass.Print(this);
        }

        protected override void OnTick()
        {

        }

        protected override void OnStop()
        {

        }

        public class PRINTINGCLASS
        {
            public PRINTINGCLASS(Algo algo)
            {
                algo.Print("Hello");
            }
            
            public void Print(Algo algo)
            {
                algo.Print("Hello");
            }
        }
    }
}

Is there a way to do this without having to pass Algo as a method argument?

Thanks!

 

Personally, I would approach it more like this: having a static instance of the helper class.

 

public class TEST : Robot
{
	protected override void OnStart()
	{
		new AlgoHelper(this);
		AlgoHelper.getInstance().print("start");
	}
	
	protected override void OnTick()
	{
		AlgoHelper.getInstance().print("tick");
	}
	
	protected override void OnStop()
	{
		AlgoHelper.getInstance().print("stop");
	}    
}

public class AlgoHelper 
{
	
	private static AlgoHelper thisInstance;
	private Algo algo;
	
	public AlgoHelper(Algo a)
	{
		thisInstance = this;
		algo = a;
	}
	
	public static AlgoHelper getInstance()
	{
		return thisInstance;
	}
	
	public void print(string s)
	{
		algo.print(s);
		
		//You may want to ensure it runs in main thread:
		/*
		algo.BeginInvokeOnMainThread(() =>
		{
			algo.print(s);
		});
		*/
	}
}

You could make the algo variable publicly gettable and access that from any other utility class you may create. However - be warned - you cannot use this solution if you are running two instances of the same indicator on the same chart - as it seems they share an environment. 

 


@freemoniescapital

freemoniescapital
19 Oct 2022, 17:15

Hi Panagiotis,

Appreciate your response and apologies for the late reply. If I understand correctly, is the following code correct?

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

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class TEST : Robot
    {
        protected override void OnStart()
        {
             PRINTINGCLASS printingClass = new(this);
             printingClass.Print(this);
        }

        protected override void OnTick()
        {

        }

        protected override void OnStop()
        {

        }

        public class PRINTINGCLASS
        {
            public PRINTINGCLASS(Algo algo)
            {
                algo.Print("Hello");
            }
            
            public void Print(Algo algo)
            {
                algo.Print("Hello");
            }
        }
    }
}

Is there a way to do this without having to pass Algo as a method argument?

Thanks!

 


@freemoniescapital

freemoniescapital
18 Oct 2022, 14:00

Thanks for your reply

Hi,

Thanks for your reply. Do you have an example on how to implement this? Do you mean creating an instance of the mainclass within the subclass and using the 'ref' keyword? My apologies because I am not strong in C#. Thanks a lot.


@freemoniescapital