How to reverse closing order in 'Sample Close Profirable Position cBot'

Created at 20 Mar 2017, 11:26
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
DA

How to reverse closing order in 'Sample Close Profirable Position cBot'
20 Mar 2017, 11:26


How to reverse closing order in 'Sample Close Profirable Position cBot'

The algo closes position from the least profit first,

Is there any way to close position from the highest profit ?

And the process seems rather slow compare to the 'Close All' command.

Thank you.


@davideng5841@hotmail.com
Replies

tradermatrix
20 Mar 2017, 14:16

HI

c's can be better..? with back commissions deducted, to avoid bad surprises...

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


namespace cAlgo.Robots
{
    [Robot()]
    public class LIQUIDATE : Robot
    {


        [Parameter("TotalProfit", DefaultValue = 5)]
        public double TotalProfit { get; set; }

        protected override void OnStart()
        {



            string text = "★ Liquidate ★ By TradermatriX ★";

            base.ChartObjects.DrawText("★ Liquidate ★ By TradermatriX ★", text, StaticPosition.TopLeft, new Colors?(Colors.Lime));



        }
        protected override void OnTick()
        {


            var netProfit = 0.0;
            foreach (var openedPosition in Positions)
            {
                netProfit += openedPosition.NetProfit + openedPosition.Commissions;
            }
            ChartObjects.DrawText("a", netProfit.ToString(), StaticPosition.BottomRight, new Colors?(Colors.Lime));

            {
                if (Account.Equity - Account.Balance > TotalProfit )
                {

                    foreach (var openedPosition in Positions)
                    {

                        ClosePosition(openedPosition);


                        foreach (var pendingOrder in PendingOrders)
                        {

                            CancelPendingOrder(pendingOrder);



                        }
                    }
                }
            }
        }
    }
}


 


@tradermatrix

davideng5841@hotmail.com
20 Mar 2017, 14:55

RE:

 

But this version requires 'unlimited access rights', it there any to bypass it ?

Or an error uring bulid ?

Thank you very much,

 

tradermatrix said:

HI

c's can be better..? with back commissions deducted, to avoid bad surprises...

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


namespace cAlgo.Robots
{
    [Robot()]
    public class LIQUIDATE : Robot
    {


        [Parameter("TotalProfit", DefaultValue = 5)]
        public double TotalProfit { get; set; }

        protected override void OnStart()
        {



            string text = "★ Liquidate ★ By TradermatriX ★";

            base.ChartObjects.DrawText("★ Liquidate ★ By TradermatriX ★", text, StaticPosition.TopLeft, new Colors?(Colors.Lime));



        }
        protected override void OnTick()
        {


            var netProfit = 0.0;
            foreach (var openedPosition in Positions)
            {
                netProfit += openedPosition.NetProfit + openedPosition.Commissions;
            }
            ChartObjects.DrawText("a", netProfit.ToString(), StaticPosition.BottomRight, new Colors?(Colors.Lime));

            {
                if (Account.Equity - Account.Balance > TotalProfit )
                {

                    foreach (var openedPosition in Positions)
                    {

                        ClosePosition(openedPosition);


                        foreach (var pendingOrder in PendingOrders)
                        {

                            CancelPendingOrder(pendingOrder);



                        }
                    }
                }
            }
        }
    }
}


 

 


@davideng5841@hotmail.com

tradermatrix
20 Mar 2017, 16:29

RE: RE:

davideng5841@hotmail.com said:

 

But this version requires 'unlimited access rights', it there any to bypass it ?

Or an error uring bulid ?

Thank you very much,

 

tradermatrix said:

HI

c's can be better..? with back commissions deducted, to avoid bad surprises...

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


namespace cAlgo.Robots
{
    [Robot()]
    public class LIQUIDATE : Robot
    {


        [Parameter("TotalProfit", DefaultValue = 5)]
        public double TotalProfit { get; set; }

        protected override void OnStart()
        {



            string text = "★ Liquidate ★ By TradermatriX ★";

            base.ChartObjects.DrawText("★ Liquidate ★ By TradermatriX ★", text, StaticPosition.TopLeft, new Colors?(Colors.Lime));



        }
        protected override void OnTick()
        {


            var netProfit = 0.0;
            foreach (var openedPosition in Positions)
            {
                netProfit += openedPosition.NetProfit + openedPosition.Commissions;
            }
            ChartObjects.DrawText("a", netProfit.ToString(), StaticPosition.BottomRight, new Colors?(Colors.Lime));

            {
                if (Account.Equity - Account.Balance > TotalProfit )
                {

                    foreach (var openedPosition in Positions)
                    {

                        ClosePosition(openedPosition);


                        foreach (var pendingOrder in PendingOrders)
                        {

                            CancelPendingOrder(pendingOrder);



                        }
                    }
                }
            }
        }
    }
}


want to use you for all your post open or l include in a bot.

If you want to include in a bot give me a code and I will help you

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

namespace cAlgo
{
    [Robot("Sample close profitable positions", TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleCloseProfitablePositionscBot : Robot
    {



        [Parameter("TotalProfit", DefaultValue = 5)]
        public double TotalProfit { get; set; }

        protected override void OnStart()
        {



            string text = "★ Liquidate ★ By TradermatriX ★";

            base.ChartObjects.DrawText("★ Liquidate ★ By TradermatriX ★", text, StaticPosition.TopLeft, new Colors?(Colors.Lime));



        }
        protected override void OnTick()
        {


            var netProfit = 0.0;
            foreach (var openedPosition in Positions)
            {
                netProfit += openedPosition.NetProfit + openedPosition.Commissions;
            }
            ChartObjects.DrawText("a", netProfit.ToString(), StaticPosition.BottomRight, new Colors?(Colors.Lime));

            {
                if (Account.Equity - Account.Balance > TotalProfit)
                {

                    foreach (var openedPosition in Positions)
                    {

                        ClosePosition(openedPosition);


                        foreach (var pendingOrder in PendingOrders)
                        {

                            CancelPendingOrder(pendingOrder);



                        }
                    }
                }
            }
        }
    }
}

 

 

 


@tradermatrix