closeAll positions when equity greater than balance

08 Feb 2018, 04:19closeAll positions when equity greater than balance#1
thegreat.superposts: 8since: 05 Feb 2018

Hi 

Please im creating a hedge bot and want all positions to close when equity is greater than account balance by xpips. However, the only closes profitable trades and leavie losing trades behind but I want both profitable and lossing trades to close

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ashi : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        [Parameter("ProfitTarget", DefaultValue = 300)]
        public double ProfitTarget { get; set; }
        [Parameter("Volume", DefaultValue = 10000)]
        public int Volume { get; set; }
        public double balance;
        public double lastTradePrice;
        public TradeResult lastposition;

        protected override void OnStart()
        {
            // Get the initial balance
            balance = Account.Balance;
            Print(balance);

            // Start hedge grid
            lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "buy", 0, 2);
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "buy", 0, 2);
        }

        protected override void OnTick()
        {
            // Open more trades if the price moved 5 pips
            if (lastposition.Position.Pips > 5)
            {

                lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "buy", 0, 2);
            }
            
            if (lastposition.Position.Pips < -5)
            {

                lastposition = ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "sell", 0, 2);
            }
            
            // Check if equity is in profit then shut down the grid cycle
            if (Account.Equity >= balance + 5)
            {

                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
                balance = Account.Balance;
                Print(balance);

                // Start hedge grid
                lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "buy", 0, 2);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "buy", 0, 2);

            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here


        }

    }
}

. I would be glad if anyone could help me resolve this issue.

Thanks

08 Feb 2018, 11:20#2
Panagiotis Charalampousposts: 1200since: 13 Jan 2017

Hi thegreat.super,

From what i see

                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }

closes all positions.

Why do you say that it closes only profitable ones?

Best Regards

Panagiotis 


Head of Community Management at cTrader
08 Feb 2018, 16:28#3
thegreat.superposts: 8since: 05 Feb 2018

When the cbot place 2 buy lots and 1 sell ot, the expectation is that if price move infavour of buy, and equity is more than balance, both the buy and sell trades should close together. However, when price moves in favour of buy trades and its in profit, the buy position is closed but the sell position remains instead of closing together with the buy positions.

thanks

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ashi : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        [Parameter("ProfitTarget", DefaultValue = 300)]
        public double ProfitTarget { get; set; }
        [Parameter("Volume", DefaultValue = 10000)]
        public int Volume { get; set; }
        public double balance;
        public double lastTradePrice;
        public TradeResult lastposition;

        protected override void OnStart()
        {
            // Get the initial balance
            balance = Account.Balance;
            Print(balance);

            // Start hedge grid
            lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "buy", 0, 2);
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "buy", 0, 2);
        }

        protected override void OnTick()
        {
            // Open more trades if the price moved 5 pips
            if (lastposition.Position.Pips > 5)
            {

                lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "buy", 0, 2);
            }

            if (lastposition.Position.Pips < -5)
            {

                lastposition = ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "sell", 0, 2);
            }

            // Check if equity is in profit then shut down the grid cycle
            if (Account.Equity >= balance + 2)
            {

                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
                balance = Account.Balance;
                Print(balance);

                // Start hedge grid
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "buy", 0, 2);
                ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "sell", 0, 2);

            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}



 

08 Feb 2018, 16:58#4
Panagiotis Charalampousposts: 1200since: 13 Jan 2017

Hi thegreat.super,

I understand that. But the code you posted, closes all of them. Did you try to run it on cTrader and it did not close all positions? Did you just backtest it? I am just trying to understand how you reached to the conclusion that it is not working.

Best Regards,

Panagiotis


Head of Community Management at cTrader
09 Feb 2018, 18:19#5
thegreat.superposts: 8since: 05 Feb 2018

Hello!

I tested it on demo. now its working .the new problem is that it crashes with the error:

Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.

 

Please can you eplain to me what error and where to find it because no error was reported while compiling.

Thanks

12 Feb 2018, 11:22#6
Panagiotis Charalampousposts: 1200since: 13 Jan 2017

Hi thegreat.super,

You can find an explanation for this kind of exceptions here. The problem probably occurs because you are checking lastposition without checking if it is null first. See below

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
 
namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ashi : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        [Parameter("ProfitTarget", DefaultValue = 300)]
        public double ProfitTarget { get; set; }
        [Parameter("Volume", DefaultValue = 10000)]
        public int Volume { get; set; }
        public double balance;
        public double lastTradePrice;
        public TradeResult lastposition;
 
        protected override void OnStart()
        {
            // Get the initial balance
            balance = Account.Balance;
            Print(balance);
 
            // Start hedge grid
            lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "buy", 0, 2);
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "buy", 0, 2);
        }
 
        protected override void OnTick()
        {
            // Open more trades if the price moved 5 pips
            if (lastposition != null && lastposition.Position.Pips > 5)
            {
 
                lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "buy", 0, 2);
            }
 
            if (lastposition != null &&  lastposition.Position.Pips < -5)
            {
 
                lastposition = ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "sell", 0, 2);
            }
 
            // Check if equity is in profit then shut down the grid cycle
            if (Account.Equity >= balance + 2)
            {
 
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
                balance = Account.Balance;
                Print(balance);
 
                // Start hedge grid
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "buy", 0, 2);
                ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "sell", 0, 2);
 
            }
        }
 
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Let me know if the above resolves the problem.

Best Regards,

Panagiotis


Head of Community Management at cTrader
13 Feb 2018, 17:46RE:#7
thegreat.superposts: 8since: 05 Feb 2018

Panagiotis Charalampous said:

Hi thegreat.super,

You can find an explanation for this kind of exceptions here. The problem probably occurs because you are checking lastposition without checking if it is null first. See below

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
 
namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ashi : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        [Parameter("ProfitTarget", DefaultValue = 300)]
        public double ProfitTarget { get; set; }
        [Parameter("Volume", DefaultValue = 10000)]
        public int Volume { get; set; }
        public double balance;
        public double lastTradePrice;
        public TradeResult lastposition;
 
        protected override void OnStart()
        {
            // Get the initial balance
            balance = Account.Balance;
            Print(balance);
 
            // Start hedge grid
            lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "buy", 0, 2);
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "buy", 0, 2);
        }
 
        protected override void OnTick()
        {
            // Open more trades if the price moved 5 pips
            if (lastposition != null && lastposition.Position.Pips > 5)
            {
 
                lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "buy", 0, 2);
            }
 
            if (lastposition != null &&  lastposition.Position.Pips < -5)
            {
 
                lastposition = ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "sell", 0, 2);
            }
 
            // Check if equity is in profit then shut down the grid cycle
            if (Account.Equity >= balance + 2)
            {
 
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
                balance = Account.Balance;
                Print(balance);
 
                // Start hedge grid
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "buy", 0, 2);
                ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "sell", 0, 2);
 
            }
        }
 
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Let me know if the above resolves the problem.

Best Regards,

Panagiotis

Hi1

Please it now works well on demo account but the problem persist on live account.

Thanks

14 Feb 2018, 10:29#8
Panagiotis Charalampousposts: 1200since: 13 Jan 2017

Hi thegreat.super,

Unfortunately, it is not easy to help you if I don't have the same conditions as you. I have provided a link above that explains what this exception means. Probably a variable is used somewhere without being initialized first or having a null value. If you try to debug this cBot using Visual Studio, then it will be easy to find out where the exception is thrown and fix it. 

Best Regards,

Panagiotis


Head of Community Management at cTrader
16 Feb 2018, 07:33#9
thegreat.superposts: 8since: 05 Feb 2018

Hi!

PLEASE i TRIED DEBUGGINH IN VISUAL STUDIO AND IT SAID...

 does not contain a static 'Main' method suitable for an entry point 

 

ANY HELP?

THANKS

16 Feb 2018, 09:12#10
Panagiotis Charalampousposts: 1200since: 13 Jan 2017

Hi thegreat.super,

Here s a guide that explains how to debug cBots in Visual Studio. Paul Hayes has also created a couple of cool videos on this subject. You can find one here

Best Regards,

Panagiotis


Head of Community Management at cTrader