Positions don't open for XAU or XAG

Created at 30 Jun 2023, 23:41
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!
CT

ctid3999979

Joined 05.07.2021

Positions don't open for XAU or XAG
30 Jun 2023, 23:41


Hi

EDIT: Before reading further, I figured out I was incorrectly calculating my lot size for XAU. I always want to risk a certain percentage of my balance. I changed the lot calculation to divide by 1000 rather than 10000 and it worked:

double lots = Symbol.QuantityToVolumeInUnits(Math.Round(((Account.Balance * (RiskOfBalance / 100)) / StopLoss) / Symbol.PipValue / 1000, 2));

I'm starting to work on some cBots specifically for XAUUSD. However, for some reason, positions never open for it. Even if I don't include any conditions and just open a buy position on the open of every candle, no orders get executed. If I change to GBPUSD, it opens orders as it should be.

I've even tested it with the following, which as I said above simply opens a buy on each and every candle open.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.FullAccess)]
    public class StochasticTest : Robot
    {
        // Order Parameters
        [Parameter("Session", DefaultValue = MarketSession.London)]
        public MarketSession MarketSession { get; set; }

        [Parameter("Take Profit", Group = "Order", DefaultValue = 500, MinValue = 100, Step = 10)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss", Group = "Order", DefaultValue = 80, MinValue = 80, Step = 10)]
        public int StopLoss { get; set; }

        [Parameter("Risk (%)", Group = "Order", DefaultValue = 2, MinValue = 0.01, MaxValue = 5, Step = 0.01)]
        public double RiskOfBalance { get; set; }

        // Stochastic parameters
        [Parameter("Fast K% Period", Group = "Stochastic", DefaultValue = 5, MinValue = 2, Step = 1)]
        public int FastKPeriod { get; set; }

        [Parameter("Slow K% Period", Group = "Stochastic", DefaultValue = 3, MinValue = 2, Step = 1)]
        public int SlowKPeriod { get; set; }

        [Parameter("%D Smoothing", Group = "Stochastic", DefaultValue = 3, MinValue = 2, Step = 1)]
        public int DSmoothing { get; set; }

        [Parameter("MA Type", Group = "Stochastic", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType StochMovingAverageType { get; set; }


        private double StartingBalance;
        private StochasticOscillator StochasticIndicator;
        

        protected override void OnStart()
        {
            StartingBalance = Account.Balance;

            StochasticIndicator =
                Indicators.StochasticOscillator(FastKPeriod, SlowKPeriod, DSmoothing, StochMovingAverageType);


        }

        protected override void OnBar()
        {
            Open(TradeType.Buy);
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }

        private void Open(TradeType tradeType)
        {
            double lots = Symbol.QuantityToVolumeInUnits(Math.Round(((Account.Balance * (RiskOfBalance / 100)) / StopLoss) / Symbol.PipValue / 100000, 2));

            if (Positions.Count < 1)
            {
                Print($"Opening buy position on {SymbolName} for {lots} lots.");
                ExecuteMarketOrder(tradeType, SymbolName, lots, "comment", StopLoss, TakeProfit);
            }
        }
    }
}

 


@ctid3999979
Replies

Best.Algo.Trader
01 Jul 2023, 18:30

Hi, 

The volume for gold is typically not provided in lots. Depends on your settings in cTrader. If you check the logs of your bachtest you will see, that your lot size is always 0 and therefore rejected by the Market Order function.

But there is a better way to calculate the volume for your bot. See the amended code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class StochasticTest : Robot
    {
        // Order Parameters
        [Parameter("Session", DefaultValue = MarketSession.London)]
        public MarketSession MarketSession { get; set; }

        [Parameter("Take Profit", Group = "Order", DefaultValue = 500, MinValue = 100, Step = 10)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss", Group = "Order", DefaultValue = 80, MinValue = 80, Step = 10)]
        public int StopLoss { get; set; }

        [Parameter("Risk (%)", Group = "Order", DefaultValue = 2, MinValue = 0.01, MaxValue = 5, Step = 0.01)]
        public double RiskOfBalance { get; set; }

        // Stochastic parameters
        [Parameter("Fast K% Period", Group = "Stochastic", DefaultValue = 5, MinValue = 2, Step = 1)]
        public int FastKPeriod { get; set; }

        [Parameter("Slow K% Period", Group = "Stochastic", DefaultValue = 3, MinValue = 2, Step = 1)]
        public int SlowKPeriod { get; set; }

        [Parameter("%D Smoothing", Group = "Stochastic", DefaultValue = 3, MinValue = 2, Step = 1)]
        public int DSmoothing { get; set; }

        [Parameter("MA Type", Group = "Stochastic", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType StochMovingAverageType { get; set; }


        private double StartingBalance;
        private StochasticOscillator StochasticIndicator;
        

        protected override void OnStart()
        {
            StartingBalance = Account.Balance;

            StochasticIndicator =
                Indicators.StochasticOscillator(FastKPeriod, SlowKPeriod, DSmoothing, StochMovingAverageType);


        }

        protected override void OnBar()
        {
            Open(TradeType.Buy);
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }

        private void Open(TradeType tradeType)
        {
            double volume = Symbol.VolumeForProportionalRisk(ProportionalAmountType.Balance, RiskOfBalance, StopLoss);

            if (Positions.Count < 1)
            {
                Print($"Opening buy position on {SymbolName} for {volume} volume.");
                ExecuteMarketOrder(tradeType, SymbolName, volume, "comment", StopLoss, TakeProfit);
            }
        }
    }
}

 


@Best.Algo.Trader