Link to home
Start Free TrialLog in
Avatar of skij
skijFlag for Canada

asked on

ASP.NET / C#: Sort by NetProfit

How can I sort PositionsToClose by NetProfit?
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleSmartBotRobot : Robot
    {
        protected override void OnStart()
        {
            Positions PositionsToClose = Positions;
            foreach (Position p in PositionsToClose)
            {
                Print(Math.Abs(p.NetProfit));
            }
        }
    }
}

Open in new window

SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of skij

ASKER

Thanks, käµfm³d.  That is a good but it does not work.  Here is the error it returns:
Error CS1061: 'cAlgo.API.Positions' does not contain a definition for 'Orderby' and no extension method 'Orderby' accepting a first argument of type 'cAlgo.API.Positions' could be found (are you missing a using directive or an assembly reference?)

Here is the documentation for Positions:
http://ctdn.com/api/reference/positions

Here is the application:
http://spotware.ctrader.com/calgo-spotware-setup.exe
You can implement PositionsToClose as a Generic List of collections instead of an array and then extend it to implement the interface IComparable and override CompareTo method. Then use the Sort() method on your generic list to sort by any property you wish. In your case NetProfit.

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

namespace Sorting_a_List
{
    // a simple class to store in the list
    public class Employee : IComparable<Employee>
    {
        private int empID;

        public Employee(int empID)
        {
            this.empID = empID;
        }

        public override string ToString( )
        {
            return empID.ToString( );
        }

        public bool Equals(Employee other)
        {
            if (this.empID == other.empID)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        // Comparer delegates back to Employee
        // Employee uses the integer's default
        // CompareTo method

        public int CompareTo(Employee rhs)
        {
            return this.empID.CompareTo(rhs.empID);
        }

    }
    public class Tester
    {
        static void Main( )
        {

            List<Employee> empList = new List<Employee>( );
            List<Int32> intList = new List<Int32>( );

            // generate random numbers for both the
            // integers and the employee IDs
            Random r = new Random( );

            // populate the list
            for (int i = 0; i < 5; i++)
            {
                // add a random employee id
                empList.Add(new Employee(r.Next(10) + 100));

                // add a random integer
                intList.Add(r.Next(10));
            }

            // display all the contents of the int list
            Console.WriteLine("List<int> before sorting:");
            for (int i = 0; i < intList.Count; i++)
            {
                Console.Write("{0} ", intList[i].ToString( ));
            }
            Console.WriteLine("\n");

            // display all the contents of the Employee list
            Console.WriteLine("List<Employee> before sorting:");
            for (int i = 0; i < empList.Count; i++)
            {
                Console.Write("{0} ", empList[i].ToString( ));
            }
            Console.WriteLine("\n");

            // sort and display the int list
            Console.WriteLine("List<int>after sorting:");
            intList.Sort( );
            for (int i = 0; i < intList.Count; i++)
            {
                Console.Write("{0} ", intList[i].ToString( ));
            }
            Console.WriteLine("\n");

            // sort and display the Employee list
            Console.WriteLine("List<Employee>after sorting:");

            //Employee.EmployeeComparer c = Employee.GetComparer( );
            //empList.Sort(c);

            empList.Sort( );

            // display all the contents of the Employee list
            for (int i = 0; i < empList.Count; i++)
            {
                Console.Write("{0} ", empList[i].ToString( ));
            }
            Console.WriteLine("\n");

        }
    }
}
The output looks something like this:
List<int> before sorting:
6 9 8 3 6
List<Employee> before sorting:
108 103 107 102 109
List<int>after sorting:
3 6 6 8 9
List<Employee>after sorting:
102 103 107 108 109

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of skij

ASKER

I was able to get it to work with LINQ.  The B in OrderBy had to be capitalized!
foreach (Position p in PositionsToClose.OrderBy(item => item.NetProfit))

Open in new window