Open positions in opposed

Created at 12 May 2015, 10:44
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!
Forex19's avatar

Forex19

Joined 13.06.2013

Open positions in opposed
12 May 2015, 10:44


Hi, 
I would like to develop a CBOT that after opening the first position, opened a second position (a number of pips away from the first position) with volume double and in opposite direction to the first position.

The first position is opened according to a logic, that I have developed without particular problems.
I do need some suggestions for the execution of the positions, as indicated at the beginning.

Thanks.


@Forex19
Replies

Waxy
13 May 2015, 03:39

Here's the code, please notice, you didn't say anything about the 1st position being closed.

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 XROpenPositionsinOpposed : Robot
    {
        [Parameter("Label", DefaultValue = "MyLabel")]
        public string MyLabel { get; set; }

        bool _isSent;

        protected override void OnStart()
        {
            ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, MyLabel);
        }

        protected override void OnTick()
        {
            var x = Positions.Find(MyLabel);

            if (x != null && !_isSent)
            {
                if (x.TradeType == TradeType.Buy)
                {
                    PlaceStopOrder(TradeType.Sell, Symbol, (x.Volume * 2), x.EntryPrice - 20 * Symbol.PipSize);
                    _isSent = !_isSent;
                }
                else
                {
                    PlaceStopOrder(TradeType.Buy, Symbol, (x.Volume * 2), x.EntryPrice + 20 * Symbol.PipSize);
                    _isSent = !_isSent;
                }
            }
        }
    }
}

 


@Waxy

Forex19
13 May 2015, 18:10

RE:

Hi, now I try it.
To close the 1a pos, I add the TP in "ExecuteMarketOrder"

Bye and thanks for the suggestion.

Waxy said:

Here's the code, please notice, you didn't say anything about the 1st position being closed.

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 XROpenPositionsinOpposed : Robot
    {
        [Parameter("Label", DefaultValue = "MyLabel")]
        public string MyLabel { get; set; }

        bool _isSent;

        protected override void OnStart()
        {
            ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, MyLabel);
        }

        protected override void OnTick()
        {
            var x = Positions.Find(MyLabel);

            if (x != null && !_isSent)
            {
                if (x.TradeType == TradeType.Buy)
                {
                    PlaceStopOrder(TradeType.Sell, Symbol, (x.Volume * 2), x.EntryPrice - 20 * Symbol.PipSize);
                    _isSent = !_isSent;
                }
                else
                {
                    PlaceStopOrder(TradeType.Buy, Symbol, (x.Volume * 2), x.EntryPrice + 20 * Symbol.PipSize);
                    _isSent = !_isSent;
                }
            }
        }
    }
}

 

 


@Forex19