Max open trades

Created at 10 Nov 2022, 00:16
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!
VV

vvictord

Joined 10.11.2022

Max open trades
10 Nov 2022, 00:16


Hi, guys.. I have to modify a bot to set a maximum trades of whatever number I give it.

At the moment it opens max one trade but I want to be able to tell the bot to have 3 open trades at the same time...

Is there a way to do it?

Leaving the code here.
------------------------------------------------------------------------------------------------------------------------------------------

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

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class ThreeBarInsideBar : Robot
    {
        int upClose;
        int upCloseBefore;
        int insideBar;
        int downClose;
        int downCloseBefore;
        int counter =0;
        Position position;
        
        [Parameter(DefaultValue = 10000)]
        public int Volume { get; set; }
        
        [Parameter("Stop Loss (pips)", DefaultValue = 10)]
        public int StopLoss { get; set; }
    
        [Parameter("Take Profit (pips)", DefaultValue = 10)]
        public int TakeProfit { get; set; }

        protected override void OnBar()
        {    
            if(Trade.IsExecuting){
                return;
            }
            if(MarketSeries.Close[MarketSeries.Close.Count-1] > MarketSeries.Close[MarketSeries.High.Count-2]){
                upClose = 1;
            }else{
                upClose = 0;
            }
            
            if(MarketSeries.Close[MarketSeries.Close.Count-3] > MarketSeries.Close[MarketSeries.Close.Count-4]){
                upCloseBefore = 1;
            }else{
                upCloseBefore = 0;
            }
            
            if((MarketSeries.High[MarketSeries.Close.Count-2] < MarketSeries.High[MarketSeries.Close.Count-3])
            &&(MarketSeries.Low[MarketSeries.Close.Count-2]> MarketSeries.Low[MarketSeries.Close.Count-3])){
                insideBar = 1;
            }else{
                insideBar = 0;
            }
            
            if(MarketSeries.Close[MarketSeries.Close.Count-1] < MarketSeries.Close[MarketSeries.Low.Count-2]){
                downClose = 1;
            }else{
                downClose = 0;
            }
            
            if(MarketSeries.Close[MarketSeries.Close.Count-3] < MarketSeries.Close[MarketSeries.Close.Count-4]){
                downCloseBefore = 1;
            }else{
                downCloseBefore = 0;
            }
            
            if(counter == 0){
                if(upClose == 1 && insideBar == 1 && upCloseBefore == 1){
                    Trade.CreateMarketOrder(TradeType.Buy,Symbol,Volume);
                }
                if( downClose == 1 && insideBar == 1 && downCloseBefore == 1){
                    Trade.CreateMarketOrder(TradeType.Sell,Symbol,Volume);
                }
            }
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            position = openedPosition;
            counter = 1;
            Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), GetAbsoluteTakeProfit(openedPosition, TakeProfit));
        }
        
        protected override void OnPositionClosed(Position position)
        {    
            counter=0;
        }
        
        private double GetAbsoluteStopLoss(Position position, int stopLossInPips)
        {
            return position.TradeType == TradeType.Buy
                ? position.EntryPrice - Symbol.PipSize * stopLossInPips
                : position.EntryPrice + Symbol.PipSize * stopLossInPips;
        }
        
        private double GetAbsoluteTakeProfit(Position position, int takeProfitInPips)
        {
            return position.TradeType == TradeType.Buy
                ? position.EntryPrice + Symbol.PipSize * takeProfitInPips
                : position.EntryPrice - Symbol.PipSize * takeProfitInPips;
        }
    }
}


@vvictord
Replies

vvictord
15 Nov 2022, 02:58

Pleaseeee

Please, someone who can help me with this?


@vvictord

vvictord
16 Nov 2022, 01:15

RE:

PanagiotisChar said:

Hi there,

If you need professional help, contact us at development@clickalgo.com

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

Hi,

thanks for your answer.
I can not afford professional help.

I just want to learn what code to use for this task. I will learn the rest.

Thanks a lot though


@vvictord

vvictord
24 Nov 2022, 00:25

Still looking for someone with these skills hehe


@vvictord

firemyst
24 Nov 2022, 05:59

RE:

vvictord said:

Still looking for someone with these skills hehe

All you have to do is similar to the following:

 

//Create your parameter
 [Parameter("Max Num of Open Positions", DefaultValue = 3)]
        public int MaxNumberOfAllowedOpenTrades { get; set; }



//And in your code, do the check:

if (Positions.Count <= MaxNumberOfAllowedOpenTrades)
{
    //do your stuff
}
else
{
   Print ("Too many positions already opened.");
}

 


@firemyst