martingale without closing previous operation

Created at 11 Oct 2022, 06:35
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!
CR

cristianalejandropj2275

Joined 24.04.2022

martingale without closing previous operation
11 Oct 2022, 06:35


hello I would like this cBot not to close the position when opening the next one, but to close all when the last position reaches take profit

 

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleTrendRobot : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

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

        [Parameter("Stop Loss", DefaultValue = 50)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", DefaultValue = 60)]
        public int TakeProfit { get; set; }

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

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

        [Parameter(DefaultValue = 1000, MinValue = 0)]
        public int Volume { get; set; }
        [Parameter("RSIPeriods", DefaultValue = 8)]

        public int RSIPeriods { get; set; }
        private RelativeStrengthIndex rsi;
        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Trend Martangle Robot";

        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
            rsi = Indicators.RelativeStrengthIndex(SourceSeries, RSIPeriods);
        }

        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
            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 && rsi.Result.LastValue < 30 && longPosition == null)
            {
                if (shortPosition != null)
                    ClosePosition(shortPosition);
                ExecuteMarketOrder(TradeType.Buy, Symbol, Symbol.NormalizeVolume(Volume), label, StopLoss, TakeProfit);
            }
            else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && rsi.Result.LastValue > 70 && shortPosition == null)
            {
                if (longPosition != null)
                    ClosePosition(longPosition);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Symbol.NormalizeVolume(Volume), label, StopLoss, TakeProfit);

            }
        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
            if (position.GrossProfit < 0)
              {
                TradeType tt = TradeType.Sell;
                if(position.TradeType==TradeType.Sell) tt = TradeType.Buy;
                ExecuteMarketOrder(tt, Symbol, Symbol.NormalizeVolume(position.Volume * 1.61803), "Martingale", StopLoss, TakeProfit);
              }
             }
        }
}


@cristianalejandropj2275
Replies

Shares4us
14 Oct 2022, 15:00

Guess you should hire a programmer , it's not changing just 1 line or so.

 Guess you should hire a programmer , it's not changing just 1 line or so.

 


@Shares4us

PanagiotisChar
14 Oct 2022, 15:13

Hi there,

If you need to hire somebody for this, feel free to reach out to me at development@clickalgo.com.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar