How to realize the netgross based on maximum risk?

Created at 19 Feb 2021, 01:32
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!
CH

ChannelTrader

Joined 31.03.2020

How to realize the netgross based on maximum risk?
19 Feb 2021, 01:32


Hi,I have a question.

I'd like to know if there is a way to close the positions based on the maximum risk of the operation and closing all the opened positions.

Also,I'm trying to do a grid based on the first position opened, but my code open another position instead of read all the positions opened and open one after the asked distance of the first.

Whats wrong with my code, please help

Here follows my code

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class VENDEDOR : Robot
    {
        [Parameter("Long position?", Group = "Basic Setup", DefaultValue = true)]
        public bool IfOpenBuy { get; set; }

        [Parameter("First order volume", Group = "Basic Setup", DefaultValue = 1000, MinValue = 1, Step = 1)]
        public double InitialVolume { get; set; }

        [Parameter("Distance of trades", Group = "Basic Setup", DefaultValue = 10, MinValue = 10)]
        public int GridSizeInPoints { get; set; }
        [Parameter("Target Pips", DefaultValue = 10)]
        public double TargetPips { get; set; }

      

        private string thiscBotLabel;
        bool _positionInitialized;


        protected override void OnStart()
        {
            
            // Set position label to cBot name
            thiscBotLabel = "Vendedor";
            // Normalize volume in case a wrong volume was entered
            if (InitialVolume != (InitialVolume = Symbol.NormalizeVolumeInUnits(InitialVolume)))
            {
                Print("Initial volume entered incorrectly, volume has been changed to ", InitialVolume);
            }
         
        }


        protected override void OnTick()
        {

            if (TradeType.Buy > 0 && Positions.Count > 0 )
            {
                if (Positions.Count(x => x.TradeType == TradeType.Buy && x.SymbolName == SymbolName && x.Label == thiscBotLabel) == 0)
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, InitialVolume, thiscBotLabel, null, null);

                else if (Symbol.Ask <= Positions.FindAll(thiscBotLabel, SymbolName, TradeType.Buy).Last().EntryPrice - GridSizeInPoints * Symbol.PipSize)
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, InitialVolume, thiscBotLabel, null, null);
            }

            if(TradeType.Buy > 0 && Positions.Count > 0)
            {
                if (Positions.Count(x => x.TradeType == TradeType.Sell && x.SymbolName == SymbolName && x.Label == thiscBotLabel) == 0)
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, InitialVolume, thiscBotLabel, null, null);
                else if (Symbol.Bid >= Positions.FindAll(thiscBotLabel, SymbolName, TradeType.Sell).Last().EntryPrice + GridSizeInPoints * Symbol.PipSize)
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, InitialVolume, thiscBotLabel, null, null);
            }

          

        }
        protected override void OnBar()
        {
            var positions = this.Positions.FindAll(thiscBotLabel);
            double netProfit = positions.Sum(x => x.NetProfit);

            if (netProfit >= TargetPips * Symbol.PipSize)
            {
                foreach (var position in positions)
                {
                    ClosePosition(position);
                }
            }

        }
        protected override void OnStop()
        {
         

        }
    }
}
 

.

 


@ChannelTrader