Bot uses wrong parameters. HELP, PLEASE!!!

Created at 01 Feb 2023, 08:49
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

Bot uses wrong parameters. HELP, PLEASE!!!
01 Feb 2023, 08:49


Guys,

I have a bot that has been running alright but since today the bot places trades with the wrong parameters.
I have a bot with 5 instances (5 different pairs). Sometimes the bot opens a EURUSD position with the stop loss and take profit from GBPCAD, for example, compeltly ignoring the parameters i gave to EURUSD.
Also sometimes it just places TP and SL at weird points. Nothing to do with the bot.

Can someone please check the code and tell me if I am missing something?

Thanks

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

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class EduardoBOT6 : 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; }

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

        [Parameter("To", DefaultValue = 10)]
        public int To { get; set; }
        [Parameter("Use Equity Stop Out", DefaultValue = true, Group = "Risk Management")]
        public bool UseMaxEquityStopOut { get; set; }
        [Parameter("Equity Stop Out (profit)", DefaultValue = 500, Group = "Risk Management")]
        public double MaxEquityProfit { get; set; }
        [Parameter("Equity Stop Out (loss)", DefaultValue = 500, Group = "Risk Management")]
        public double MaxEquityLoss { get; set; }
        [Parameter("Instance", DefaultValue = "Instance")]
        public string Instance { get; set; }

        private double _maxEquity;
        private double _maxBalance;
        private double _startingEquity;

        protected override void OnStart()
        {
            // Put your initialization logic here
            _maxEquity = Account.Equity;
            _maxBalance = Account.Balance;
            _startingEquity = Account.Equity;

        }

        protected override void OnBar()
        {
            if (Server.Time.Hour >= From && Server.Time.Hour < To)
            {
                if (Trade.IsExecuting)
                {
                    return;
                }
                if (MarketSeries.Close[MarketSeries.Close.Count - 1] > MarketSeries.Close[MarketSeries.Close.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.High.Count - 2] < MarketSeries.High[MarketSeries.High.Count - 3]) && (MarketSeries.Low[MarketSeries.Low.Count - 2] > MarketSeries.Low[MarketSeries.Low.Count - 3]))
                {
                    insideBar = 1;
                }
                else
                {
                    insideBar = 0;
                }

                if (MarketSeries.Close[MarketSeries.Close.Count - 1] < MarketSeries.Close[MarketSeries.Close.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 OnTick()
        {


            if (UseMaxEquityStopOut)
            {
                if (Account.Equity > _startingEquity + MaxEquityProfit)
                {

                    {
                        position.Close();
                    }
                    Print("cBot stopped due to  Equity Stop Out (profit)");
                    Stop();
                }

                if (Account.Equity < _startingEquity - MaxEquityLoss)
                {

                    {
                        position.Close();
                    }
                    Print("cBot stopped due to  Equity Stop Out (loss)");
                    Stop();
                }
            }
        }

        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

PanagiotisChar
01 Feb 2023, 12:29

Hi there,

This happens because OnPositionOpened() is triggered for all the account's positions. You need to check that the opened position has the same symbol as the cBot, before modifying the take profit and stop loss.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar

vvictord
01 Feb 2023, 14:13

RE:

PanagiotisChar said:

Hi there,

This happens because OnPositionOpened() is triggered for all the account's positions. You need to check that the opened position has the same symbol as the cBot, before modifying the take profit and stop loss.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 

MAN!!!!

Really appreciate you answering here!!!

I will try this tomorrow morning and come back with feedback.
Thanks thanks


@vvictord

vvictord
03 Feb 2023, 04:42

RE:

PanagiotisChar said:

Hi there,

This happens because OnPositionOpened() is triggered for all the account's positions. You need to check that the opened position has the same symbol as the cBot, before modifying the take profit and stop loss.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 

Hi boss, I tried but no luck.. could you please indicate me what you mean by "You need to check that the opened position has the same symbol as the cBot", please?

Appreciate it


@vvictord

vvictord
03 Feb 2023, 04:42

RE:

PanagiotisChar said:

Hi there,

This happens because OnPositionOpened() is triggered for all the account's positions. You need to check that the opened position has the same symbol as the cBot, before modifying the take profit and stop loss.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 

Hi boss, I tried but no luck.. could you please indicate me what you mean by "You need to check that the opened position has the same symbol as the cBot", please?

Appreciate it


@vvictord

PanagiotisChar
03 Feb 2023, 09:28

Hi there,

Here you go


        protected override void OnPositionOpened(Position openedPosition)
        {

            if (openedPosition.SymbolName == SymbolName)
            {
                // Do something
            }
        }

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar

vvictord
03 Feb 2023, 14:12

RE:

PanagiotisChar said:

Hi there,

Here you go


        protected override void OnPositionOpened(Position openedPosition)
        {

            if (openedPosition.SymbolName == SymbolName)
            {
                // Do something
            }
        }

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

YESSSSSSSSSS!!!! You solved the issue!!!
THANKS THANKS THANKS!!!

Only thing now is that before it was opening only one trade per pair and a new one would not open until the previous one is closed.
Now it opens as many as it identifies.
Do you know how to set a max number of trades that I can set up as parameter?

I am so glad you could help me!
THANKS


@vvictord