Re-using code

Questions about MultiCharts .NET and user contributed studies.
MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re-using code

Postby MidKnight » 23 Aug 2012

Hello,

I'm trying to extract some common code into its own class but am running into problems. I'm new to C#, but not to programming. When I make a new class in its own namespace with C# 2008 Express it seems to compile just fine. But when I open up Multicharts it says the code is unverified and disables it in my chart. Furthermore, the PowerLanguage.NET editor won't compile the code saying:

Code: Select all

The type or namespace name 'PLStudies' could not be found (are you missing a using directive or an assembly reference?)...


I'm not sure why it is looking for PLStudies since I renamed the namespace with the refactoring tools in C# 2008 Express to be PLStudies.MK.

I'm trying to extract any code logic into classes so I can run them standalone within NUnit.

My thanks in advance.

MK

User avatar
Henry MultiСharts
Posts: 8383
Joined: 25 Aug 2011
Has thanked: 1200 times
Been thanked: 2679 times

Re: Re-using code

Postby Henry MultiСharts » 23 Aug 2012

Hello MidKnight,

Please send me your Studio Solution to support@multicharts.com for analysis.

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Re-using code

Postby riverTrader » 23 Aug 2012

Midnight, you're probably () going about this a bit wrong -- but see below NOTE

It sounds like what you want to do is create your own windows.dll project, to create your own library of functions you can use in Pl.net indicators. It sounds like you are trying to do that within the PlStudies project. It would be much better to create your own project than start heaping lots of code into a project you don't 'own'. The better option is to keep your indicator code in PLStudies very light -- referencing PLStudies built-in functions, but developing any reuseable code into a separate, external .dll.

What you want to do is create your .dll, then add a reference for the PL built-in studies .dll, PlTypes and maybe Trademanager.dll if you need it. Build your .dll and then add a reference to it from your indicators. You should be able to inherit from PL classes, but more likely you'll write a wrapper that calls the PL class/method.

NOTE: This SHOULD work beautifully, however I was not able to make it work with a couple of tests. When I referenced a pl.net .dll, tried to implement an interface in one of my external .dll classes, it would compile in VS, but fail in pl.net. (something like the behavior you describe).

I'd like to hear from MC whether this should be possible, and whether it is a bug or a new feature request. Either way it is an extremely important and necessary capability.

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Re-using code

Postby MidKnight » 24 Aug 2012

Hi Henry Multicharts,

Before possibly wasting your time and sending any studio solution files, I just want to post some more information in response to RiverTrader's post. Maybe what I am trying to do is the wrong way to do things.


Hiya RiverTrader,

Thanks a lot for the post. Many moons ago, I had a career in server side programming so my knowledge of dlls is pretty small. If I view the indicator or signal classes as the layer of code that is responsible for presentation, I want to be able to pull the core logic out into a reusable object. I am finding that I'll often code an indicator and then later on code a signal using much of the same code as before. That could should be abstracted out into its own class and namespace and referred to within the indicator/signal.

I'm not sure if that requires a dll to do that in the windows world. Sounds more complex than it needs to be? I don't know... All I am referring to above, is the ability to make my own class in its own namespace and have it associated in a indicator/signal object. In pretty much all languages I have used, this should be all that is required.

Is my thinking topsey turvey?

With kind regards and thanks in advance,
MK

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Re-using code

Postby riverTrader » 24 Aug 2012

I'm going to take some time this weekend to give this question a good thoughtful response because it deserves it. With the right approach mc.net becomes an incredible platform for building truly advanced re-useable and extensible trading and financial time-series analysis components. Without the right approach mc.net 'applications' will remain a bunch of scripts written in a new language: lots of code copying, and we'll all still be stuck re-inventing the millionth new permutation of the adaptive moving average.
These users thanked the author riverTrader for the post (total 4):
dahmen walterTJMidKnightStan Bokov

Dru
Posts: 107
Joined: 28 Aug 2007
Has thanked: 4 times
Been thanked: 170 times

Re: Re-using code

Postby Dru » 27 Aug 2012

riverTrader wrote:NOTE: This SHOULD work beautifully, however I was not able to make it work with a couple of tests. When I referenced a pl.net .dll, tried to implement an interface in one of my external .dll classes, it would compile in VS, but fail in pl.net. (something like the behavior you describe).

I'd like to hear from MC whether this should be possible, and whether it is a bug or a new feature request. Either way it is an extremely important and necessary capability.

What kind of problem do you have?
The simplest algorithm for extract common code looks like this:
1) create the new project in VS that will contain our common code, for example MyCommonCode
2) Add to the project references to PLTypes.dll PLStudiesProxy.dll
3) Compile the project. Copy target dll (MyCommonCode.dll) to the MC.NET directory
4) Run PLEditor.NET, create an indicator ('MyIndicator') that will use common code from your MyCommonCode.dll
5) Add reference to MyCommonCode.dll for MyIndicator, use common code from MyCommonCode.dll in MyIndicator
6) Compile it in PLEditor.NET
These users thanked the author Dru for the post (total 3):
dahmen walterMidKnightsadykov

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Re-using code

Postby MidKnight » 23 Sep 2012

So I just anted up and bought a MC.NET account and I'm ready to dive into the world. I'm still confused how to proceed with my code so it can be reusable. As mentioned earlier, I am not new to programming, but I am new to windows and C# programming. I was a professional programmer since the late 90's but mostly in server side web technologies.

My common approach to an idea is to first code it into an indicator and then extract the guts of indicator into a strategy. If I didn't care about re-useing the code I'd just cut and paste. But I know all too well that down the road this can turn into a maintenance nightmare. Under my traditional programming approach, I would just extract the common code into its own class(es) and in doing so, the indicator and the strategy start to resemble more of a main() type function with a very high level language created through my re-usable classes. I had tried to do this when I was still using the demo but for some reason I couldn't get the classes to be seen. I'll try it again (I just moved across the country and haven't done anything MC related for 2-3 weeks!) and refresh my memory.

So I ask you MC.nexperts - what is "the way" of accomplishing code re-use for MC.NET? Is dll the only recommended way? It just seems more complicated and possibly more tedious to debug and test than it needs to be.....

With sincere gratitude in advance,
MK

Dru
Posts: 107
Joined: 28 Aug 2007
Has thanked: 4 times
Been thanked: 170 times

Re: Re-using code

Postby Dru » 26 Sep 2012

MidKnight wrote:So I ask you MC.nexperts - what is "the way" of accomplishing code re-use for MC.NET? Is dll the only recommended way? It just seems more complicated and possibly more tedious to debug and test than it needs to be.....

The simplest way is:
1) Create a New Function, call it "MyCommonCode" for ex.
2) Insert all your common code into this function. Just delete all generated code and insert yours. The function will be always uncompiled, that is ok.
3) Now you can use your common code in any indicator or signal.
These users thanked the author Dru for the post:
MidKnight

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Re-using code

Postby MidKnight » 26 Sep 2012

Hi Dru - thanks for the reply.

Often the reusable code needs to hold some state and/or may need calculate more than one data point. Using a function doesn't help for these situations and you need an object.

I'm guessing from your response that my old approach outlined above just isn't done in this world and as such I need to explore the dll route I guess. Makes things a bit more frustrating initially as I just want to get the code done and get on with the research rather than getting bogged down in this dll nonsense....

Thanks again,
MK

Dru
Posts: 107
Joined: 28 Aug 2007
Has thanked: 4 times
Been thanked: 170 times

Re: Re-using code

Postby Dru » 27 Sep 2012

MidKnight wrote:Often the reusable code needs to hold some state and/or may need calculate more than one data point. Using a function doesn't help for these situations and you need an object.

You don't read my post carefully.
Insert all your common code into this FUNCTION CS FILE. Just delete all generated code and insert yours. The function will be always uncompiled, that is ok. We don't use this "fake function" as real function. We use it as our common code STORAGE.
"Common code" - is all your CLASSES and TYPES.
Keep in mind that all *.cs files (signals, indicators and functions) compiled into ONE assembly (dll).
These users thanked the author Dru for the post:
MidKnight

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Re-using code

Postby MidKnight » 27 Sep 2012

Thanks for highlighting what I missed. Sorry about that. I got confused when when I tried to make a new function and it was wanting a return type in the new function creation wizard. This made me think that maybe there is some metadata associated with the function - common with some other platforms.

I'll do as you say and try again.

With thanks,
MK

holya
Posts: 40
Joined: 28 Aug 2012
Location: Vancouver, Canada
Has thanked: 8 times
Been thanked: 1 time

Re: Re-using code

Postby holya » 20 Oct 2012

Ok, I just did everything Dr. Dru said up there to Add a class in a separate Project in VS2010. This is the steps I took:
1. Added a new project(MyClasses) under the same solution(PLStudies2010).
2. Added bunch of classes to MyClasses.
3. Added references to PLTypes and PLStudiesProxy to MyClasses.
4. Compiled MyClasses successfully.
5. Copied MyClasses.dll to the MC.net directory(C:\Program Files\TS Support\MultiCharts .NET64)
5. Everything's super under VS, but when I open PLEditor and compile, those indicators that use MyClasses are now in red color, and when I compile it doesn't recognize MyClasses.

What am I doing wrong?

Any help would be appreciated.

holya
Posts: 40
Joined: 28 Aug 2012
Location: Vancouver, Canada
Has thanked: 8 times
Been thanked: 1 time

Re: Re-using code

Postby holya » 20 Oct 2012

Oh by the way, I did add a reference to MyClasses in PLStudies2010 as well.

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Re-using code

Postby MidKnight » 20 Oct 2012

Hi there,

I'm not sure what you are missing, I too could not get it to work as a separate dll. But I could get re-usable classes working like Dru said in his most recent post. I've added some extra details, and that is:

- create a new function in the power language editor
- compile it in the power language editor
- now VS recognizes the changes and does a reload
- open the new function
- remove all the code
- add my class in my own namespace
- refer to code in other class files without issue after adding a "using" statement

Maybe this helps you somewhat....

With kind regards,
MK

holya
Posts: 40
Joined: 28 Aug 2012
Location: Vancouver, Canada
Has thanked: 8 times
Been thanked: 1 time

Re: Re-using code

Postby holya » 20 Oct 2012

Thanks for the info. But putting my classes into Functions doesn't solve a whole lot! I wanted a separate project. Everytime PLEditor is opened it actually creates a new project and kicks out everything other than PLStudies2010.

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Re-using code

Postby MidKnight » 21 Oct 2012

You made the same mistake I did when Dru mentioned doing it. You are not putting your code into functions, but you make use of the MC.net new function facility so that your class can be recognized both within VS and MC.net. After you make your "function" delete all the code there and put in whatever you want.......


Return to “MultiCharts .NET”