Bot not opening opposite position on PositionClose

Created at 16 Feb 2018, 00:08
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!
TH

thoy1

Joined 15.02.2018

Bot not opening opposite position on PositionClose
16 Feb 2018, 00:08


So the idea with this bot is to open an initial position, then if the market is favourable, create more positions in a grid.

If at any point the SL on one of the positions is triggered, all the open positions will be closed and a new position in the opposite direction will be placed.

The problem is it is not creating an opposite position after the SL is triggered. Any help is appreciated.

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 AATracer : Robot
    {
        [Parameter("Volume", DefaultValue = 1000)]
        public int _volume { get; set; }
        [Parameter("Gap Size", DefaultValue = 2)]
        public int _gapsize { get; set; }
        [Parameter("Stop Loss", DefaultValue = 3)]
        public int _stoploss { get; set; }
        public TradeResult lastposition;

        protected override void OnStart()
        {
            // Put initialization logic here
            lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, _volume, "last", _stoploss, 0);
        }

        protected override void OnTick()
        {
            // Open more trades if the price moved favorably x amount of pips
            if (lastposition != null && lastposition.Position.Pips > _gapsize && lastposition.Position.TradeType == TradeType.Buy)
            {
                lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, _volume, "last", _stoploss, 0);
            }
            if (lastposition != null && lastposition.Position.Pips > _gapsize && lastposition.Position.TradeType == TradeType.Sell)
            {
                lastposition = ExecuteMarketOrder(TradeType.Sell, Symbol, _volume, "last", _stoploss, 0);
            }
        }

        protected override void OnPositionClosed(Position last)
        {
            foreach (var position in Positions)
            {
                if (position.NetProfit > -1)
                {
                    ClosePosition(position);
                }
            }
            if (last.TradeType == TradeType.Buy)
            {
                lastposition = ExecuteMarketOrder(TradeType.Sell, Symbol, _volume, "last", _stoploss, 0);
            }
            else
            {
                lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, _volume, "last", _stoploss, 0);
            }
        }

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

 


@thoy1
Replies

PanagiotisCharalampous
16 Feb 2018, 09:18

Hi thoy1,

Just to make sure what you are asking for, you need this part 

        protected override void OnPositionClosed(Position last)
        {
            foreach (var position in Positions)
            {
                if (position.NetProfit > -1)
                {
                    ClosePosition(position);
                }
            }

not to trigger OnPositionsClosed again, right?

Best Regards,

Panagiotis


@PanagiotisCharalampous

thoy1
18 Feb 2018, 04:43

RE:

Hi Panagiotis,

 

Thanks for the response. I ended up getting the issue sorted, after discovering a loop in the close/open opposite sequence a few changes were needed. I have posted the finished bot below, works 100% with back testing complete too - and resulted in a steady 20-25° downward slope. Hopefully the script below can help anybody else wanting to explore the idea further :)

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 AATracer : Robot
    {
        [Parameter("Volume", DefaultValue = 1000)]
        public int _volume { get; set; }
        [Parameter("Gap Size", DefaultValue = 15)]
        public int _gapsize { get; set; }
        [Parameter("Take Profit", DefaultValue = 15)]
        public int _takeprofit { get; set; }
        [Parameter("Stop Loss", DefaultValue = 15)]
        public int _stoploss { get; set; }
        public double balance;
        public bool stopping = false;
        public TradeType last;
        public TradeResult lastposition;
        public HistoricalTrade tradetype;
        public PositionClosedEventArgs args;

        protected override void OnStart()
        {
            // Put initialization logic here
            Positions.Closed += PositionsClosed;
            lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, _volume, "last", _stoploss, _takeprofit);
        }

        protected override void OnTick()
        {
            // Open more trades if the price moved x amount of pips
            if (lastposition != null && lastposition.Position.Pips > _gapsize)
            {
                if (lastposition.Position.TradeType == TradeType.Buy)
                {
                    lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, _volume, "last", _stoploss, _takeprofit);
                }
                else if (lastposition.Position.TradeType == TradeType.Sell)
                {
                    lastposition = ExecuteMarketOrder(TradeType.Sell, Symbol, _volume, "last", _stoploss, _takeprofit);
                }
            }

            if (stopping == true)
            {
                // Close all open positions
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                    // Timer.Start(TimeSpan.FromSeconds(10000));
                }
                System.Threading.Thread.Sleep(30000);
                stopping = false;
            }
            else if (stopping == false)
            {
                if (Positions.Count == 0)
                {
                    // Open opposite trade
                    if (last == TradeType.Buy)
                    {
                        lastposition = ExecuteMarketOrder(TradeType.Sell, Symbol, _volume, "last", _stoploss, _takeprofit);
                    }
                    else
                    {
                        lastposition = ExecuteMarketOrder(TradeType.Buy, Symbol, _volume, "last", _stoploss, _takeprofit);
                    }
                }
            }
        }

        private void PositionsClosed(PositionClosedEventArgs args)
        {
            stopping = true;
            last = args.Position.TradeType;
        }

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

 


@thoy1