distance of commands

Created at 20 Jul 2018, 22:20
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!
TR

tradermatrix

Joined 24.07.2012

distance of commands
20 Jul 2018, 22:20


Hello
I need a piece of code.
there are many examples for LimitOrder or StopOrder.
but I am looking for a solution for ExecuteMarketOrder.
with this small example below the cbot opens commands that are closed together when the total profit is reached.
  But it is possible that even after a long time, open trades have almost identical prices.
An accumulation of orders at almost identical prices is dangerous.
That's why I want to replace this method with another ...
  can you help me create a space or for example> or <30 pips (Pipstep), if the last Buy Euro command is 1.2000, the indicator will accept and open the command if this one is > 1.2030 or <1.1970.

cordially

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 TrendcBot : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter()]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Slow Periods", DefaultValue = 10)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", DefaultValue = 5)]
        public int FastPeriods { get; set; }

        [Parameter("PipStep ", DefaultValue = 10, MinValue = 1)]
        public int PipStep { get; set; }

        [Parameter("Quantity (Lots)", DefaultValue = 0.1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

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

        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Trend cBot";

        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }

        protected override void OnBar()
        {

            var volumeInUnits = Symbol.QuantityToVolume(Quantity);
            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);

            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa)
            {

                ExecuteMarketOrder(TradeType.Buy, Symbol, volumeInUnits, label);
            }
        }

        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);

                    }
                }
            }
        }
    }
}

 

 


@tradermatrix