SciSharp / NumSharp

High Performance Computation for N-D Tensors in .NET, similar API to NumPy.
https://github.com/SciSharp
Apache License 2.0
1.37k stars 192 forks source link

Using NumSharp with cTrader #347

Closed SigmaU closed 5 years ago

SigmaU commented 5 years ago

This is my first post so excuse me if I'm not as sharp as you guys. Great work everyone! Cheers!

I wish to use NumSharp in my cAlgo projects and version 0.1.6 works just fine but neither one of the versions that came after (0.2.x). I cannot pinpoint the cause since I'm working with a cAlgo software/api project (comes with limitations). Nothing withing NumSharp seems to work - code just stops when calling NumSharp. Tried everything - doesn't matter, it doesn't go trough the line of code. Tried Numpy.Net as well but can't seem to have it work at all (no version).

Can someone shed some light on this matter? What changed so drastically in v0.2? Am I missing something?

Here is a default project that should generate a simple np.arange(20) and print it out on the top of the chart:

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class T_0_1_NDSeries : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }
        public NDArray nd;

        protected override void Initialize()
        {
            Chart.DrawStaticText("nd1", "_", VerticalAlignment.Top, HorizontalAlignment.Center, "3F3F3F");
            nd = np.arange(20); // code stops here
            Chart.DrawStaticText("nd2", "nd: " + nd.size.ToString(), VerticalAlignment.Top, HorizontalAlignment.Center, "3F3F3F");
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
        }
    }
}
Nucs commented 5 years ago
  1. It does not make sense, it seems that VS swallows the exception if there is any thrown at all. While in Visual Studio press Ctrl+Alt+E and select the following: image

    Rerun and see if exception is thrown.

  2. After running please copy the Output Window contents to pastebin.net and post a link here.
  3. Try setting the project target to x64
SigmaU commented 5 years ago
  1. I did and nothing changed. Build is always successful no exceptions thrown (not even if i manually add a try/catch) - "nd" never generates anything. Code stops there and the second chart-print never happens.
  2. Since debug is limited or almost impossible with this software, the output doesn't help. Mainly i chose not to send a pastebin cause weather I take out completely NumSharp or leave it as is - the output is the same. (VS not being able to fully run debug on the software as i'm missing external/proprietary references - has nothing to do with NumSharp)
  3. x64 didn't help (unfortunately)

I'll contact Spotware as well on all this and get back to you. I would love to use NumSharp in these projects - it's a game changer. Thank you @Nucs !

Nucs commented 5 years ago

I don't think it has to do with our library, np.arange is very basic, doesnt have any funky unmananged code. Perhaps you have heap corruption or swallowed exception elsewhere. Im willing to help you find it if you can share your project so I can reproduce locally.

If it is heap corruption, the output window might shed light but not necessarily.

SigmaU commented 5 years ago

Yes - arange seemed just fit to test an ndarray generate. I reached out here with an issue because while v0.20.1 doesn't seem to work, v0.10.6 oddly works just fine and so I thought some major change happened at a core level. Then again i tried Numpy.Net same way and none of the version work there at all. Same odd hickup for such an easy generate.

As a full context - cTrader/cAlgo is a Spotware .Net 4.5(at the moment) windows desktop application which allows c# robots/indicators to be run within the software, live with the cAlgo interface (in this case Indicator) connected to the broker. These "Indicators" can be written within the software, Visual Studio or both. Mainly open to anything .Net related as long as data ends up back in the two main-internal methods: Initialize() & Calculate() so it can be plotted live on the software chart. What I have is a default indicator template code - empty trying to generate an ndarray and printing out its size in this case - this is just so we see it run and go trough (but it never reaches that line of code). I've set the latest .NET framework(4.7.2) and prompted, added netstandard as well (without these it wouldn't have built)

Sharing the project wouldn't be a problem since it's a small one. I'll set up a repository with everything - both v0.10.6 and v0.20.1 were added since I find this being the only clue at this point. Testing both separately might reveal something.

Appreciate it!

Nucs commented 5 years ago

I'll wait for the repositories, there is not point in testing NumSharp for these two different versions, they are completely different libraries, over 90% was rewritten.

SigmaU commented 5 years ago

Makes sense then. I originally added both and with everything you need to replicate the scenario, in any way possible. repository

Grateful!

Nucs commented 5 years ago

You are lucky I'm a quant-trader myself, After building the project it writes to calgo documents directly which then allows you to attach debugger to the cTrader process and debug the code once you add the indicator.

Heres my code changes that also print out the exception that cTrader swallows: https://pastebin.com/Hhq6XVNv The exception is printed to the (Output window) in Visual Studio

The type initializer for 'EngineCache1' threw an exception. ---> System.Security.SecurityException: Request failed. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstance[T]() at NumSharp.Backends.BackendFactory.EngineCache1..cctor() --- End of inner exception stack trace --- at NumSharp.Backends.BackendFactory.GetEngine(BackendType backendType) at NumSharp.np.arange(Int32 start, Int32 stop, Int32 step) at cAlgo.NDArray_0_20_1.Initialize() The program '[14160] cTrader.exe' has exited with code 0 (0x0). The program '[14160] cTrader.exe: Program Trace' has exited with code 0 (0x0).

Doing a quick search about the exception with the context of cTrader yielded: https://ctrader.com/forum/cbot-support/16225

And as mentioned in the forum, changing the IndicatorAttribute to AccessRights = AccessRights.FullAccess fixed the issue.

[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class NDArray_0_20_1 : Indicator
{

Read more about AccessRights here It might be enough to give it just FileSystem rights.

SigmaU commented 5 years ago

Hah^ quants, that's amazing! Then again, ... NumSharp, yeah should've seen that coming hehe. It makes so much sense now! Yes I'm familiar with the AccessRights but never considered it since usually these things don't build without it being set right and the output window in VS was mainly focusing on cTraders symbols missing and "just my code" thrown with or without NumSharp (thats why id didn't consider it relevant) and quite frankly debugging is quite limited as you can see with cTrader.

Nevertheless you've helped far more then I could've asked! So grateful! Thank you for your time!