Close Position on the next bar

Created at 30 May 2016, 14:29
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!
DE

DELETED_USER

Joined 08.04.2016 Blocked

Close Position on the next bar
30 May 2016, 14:29


Hi all, 

The strategy creates a position onBar (daily). I want the strategy to close the position on the next bar no matter what. 

Could you please help me how I can achieve that. 

Thanks

 


Replies

MRSV
30 May 2016, 16:50

RE:

marc.sischka said:

Hi all, 

The strategy creates a position onBar (daily). I want the strategy to close the position on the next bar no matter what. 

Could you please help me how I can achieve that. 

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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnBar()
        {
            if (Positions.Count != 0)
            {
                ClosePosition(Positions.Find("pos"));
            }
            
            ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "pos");
        }

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

Set timeframe to daily. You didn't say if you wantet to open a buy or sell position, so I set it to open a buy position.


@MRSV

Old Account
30 May 2016, 16:58

RE: RE:

MRSV said:

marc.sischka said:

Hi all, 

The strategy creates a position onBar (daily). I want the strategy to close the position on the next bar no matter what. 

Could you please help me how I can achieve that. 

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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnBar()
        {
            if (Positions.Count != 0)
            {
                ClosePosition(Positions.Find("pos"));
            }
            
            ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "pos");
        }

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

Set timeframe to daily. You didn't say if you wantet to open a buy or sell position, so I set it to open a buy position.

 


@Old Account

DELETED_USER
20 Jul 2016, 13:50

Hi, 

If I have multiple algos running in 1 account does if (Positions.Count != 0) take into consideration the positions of other strategies or just of the Algo where the code is executed?

Best,

Marc

 


DELETED_USER
18 Aug 2016, 20:56

RE: RE:

I am getting a NullException error for 

(Positions.Count != 0)

 

MRSV said:

marc.sischka said:

Hi all, 

The strategy creates a position onBar (daily). I want the strategy to close the position on the next bar no matter what. 

Could you please help me how I can achieve that. 

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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnBar()
        {
            if (Positions.Count != 0)
            {
                ClosePosition(Positions.Find("pos"));
            }
            
            ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "pos");
        }

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

Set timeframe to daily. You didn't say if you wantet to open a buy or sell position, so I set it to open a buy position.

 


DELETED_USER
18 Aug 2016, 20:57

RE: RE: RE:
I am getting a Null Exeception error for Positions.Count != 0. Any thoughts?

Old Account said:

MRSV said:

marc.sischka said:

Hi all, 

The strategy creates a position onBar (daily). I want the strategy to close the position on the next bar no matter what. 

Could you please help me how I can achieve that. 

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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnBar()
        {
            if (Positions.Count != 0)
            {
                ClosePosition(Positions.Find("pos"));
            }
            
            ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "pos");
        }

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

Set timeframe to daily. You didn't say if you wantet to open a buy or sell position, so I set it to open a buy position.