Need Help to Modify code

Created at 16 May 2024, 12:56
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!
DI

dipakvz

Joined 16.05.2024

Need Help to Modify code
16 May 2024, 12:56


I found below code from ctrader cbot website.

I need to add following function to this code.

  1. function to close all position when equity balance reached account balance+user defined profit.
  2. user defined max open position of buy & sell

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

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class NdnghiaMACD : Robot
    {
        private MacdCrossOver _MACD;

        [Parameter("Initial Volume Percent", DefaultValue = 0.01, MinValue = 0.01)]
        public double InitialVolumePercent { get; set; }

        [Parameter("Period", DefaultValue = 1100)]
        public int Period { get; set; }

        [Parameter("Long Cycle", DefaultValue = 2)]
        public int LongCycle { get; set; }

        [Parameter("Short Cycle", DefaultValue = 1)]
        public int ShortCycle { get; set; }

        [Parameter("Stop Loss", DefaultValue = 200)]
        public int StopLoss { get; set; }
        [Parameter("Take Profit", DefaultValue = 200)]
        public int TakeProfit { get; set; }

        [Parameter("Signal-line crossover true:if Signal-line crossover false: Zero crossover", DefaultValue = true)]
        public bool IsSignalLineCrossover { get; set; }

        protected override void OnStart()
        {
            _MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, Period);
        }

        protected override void OnBar()
        {
            var volumne = Math.Floor(Account.Balance * 0 * InitialVolumePercent / 100) + 1;


            {


                if (_MACD.MACD.Last(2) < _MACD.Signal.Last(2) && _MACD.MACD.Last(1) > _MACD.Signal.Last(1))
                {
                    var position = Positions.Find("NdnghiaMACD");
                    if (position != null && position.TradeType == TradeType.Sell)
                    {

                    }
                    ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "NdnghiaMACD", 0, 60);

                    // ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
                }

                if (_MACD.MACD.Last(2) < _MACD.Signal.Last(2) && _MACD.MACD.Last(1) > _MACD.Signal.Last(1))
                {
                    var position = Positions.Find("NdnghiaMACD");
                    if (position != null && position.TradeType == TradeType.Buy)
                    {

                    }
                    ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "NdnghiaMACD", 0, 60);
                    // ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
                }


                {

                    {

                    }
                }
            }
        }

    }

}

 

Thanks in advance for help

 


@dipakvz
Replies

PanagiotisCharalampous
17 May 2024, 05:28

Hi there,

You can consider assigning the job to a professional. You can find some below

https://ctrader.com/consultants

Best regards,

Panagiotis


@PanagiotisCharalampous