Multiple Instances

13 Oct 2017, 15:18Multiple Instances#31
Panagiotis Charalampousposts: 160since: 13 Jan 2017

Hi hungtonydang,

Did you locate which variable becomes null and you  get a NullReferenceException? If yes, just use and if statement and check if the variable is null before accessing it.

Best Regards,

Panagiotis

 


Head of Community Management at cTrader
19 Oct 2017, 23:10#32
hungtonydangposts: 59since: 19 Jul 2017

Hi Panagiotis,

It still seems to be crashing despite my efforts to work this out, I have tried the following that builds but still crashes the bot

if ((Positions.FindAll(PositionId)) != null)
{            
foreach (var position in Positions)
            {
                if (Symbol.Code == position.SymbolCode)
                {
                    if (Symbol.Ask > midbb)
                    {
                        ModifyPositionAsync(position, topnow, bottomnow);
                        Print("New Position TP price is {0}", position.TakeProfit);
                    }
                    else if (Symbol.Bid < midbb)
                    {
                        ModifyPositionAsync(position, bottomnow, topnow);
                        Print("New Position TP price is {0}", position.TakeProfit);
                    }
                }
            }
}

I then thought to add a function to find out if there is any positions with the label present with following

if ((Positions.FindAll(PositionId)) = null)

but the build doesn't work and I have tried declaring it as a variable prior

var positions = Positions.FindAll(PositionID);
if ((positions = null);
{
//do something
}
else
{
//continue
}

but that doesn't seem to work either. Am I using the wrong syntax or parts of code to identify any position in the list of positions as being null on a return of the FindAll?

20 Oct 2017, 12:33#33
Panagiotis Charalampousposts: 160since: 13 Jan 2017

Hi hungtonydang,

Positions.FindAll() does not return null. It returns an array. See documentation here. Maybe it is better to hire a professional programmer to help you with this.

Best Regards,

Panagiotis


Head of Community Management at cTrader
21 Oct 2017, 01:15#34
hungtonydangposts: 59since: 19 Jul 2017

Hi Panagiotis,

I have now resolved this issue. I have been chopping and changing between different coding languages as at work I am using vba and ladder logic and sometimes it gets lost in translation. The reason I am asking all these questions is because I am finding that calgo doesn't exactly use all c# ability as I use visual studio to do part of my programming. I hope this thread will serve as a reference point to all those out there looking to achieve what is set out in the topic space.

Regards,

Tony

23 Oct 2017, 11:49#35
Panagiotis Charalampousposts: 160since: 13 Jan 2017

Hi hungtonydang,

I am happy that you have resolved the issue. Indeed, when switching between languages, these confusions happen. cAlgo is just an API built using C# and .Net framework. So any C# code should work inside a cBot. If something is not working, it might be that you are missing a reference. However, if you mean that cAlgo.API should use additional features of C# that would make your life easier, feel free to post your suggestions in the Suggestions section.

Best Regards,

Panagiotis


Head of Community Management at cTrader