Bool works for currencies but not BTC, Oil or indices

Created at 07 Nov 2018, 16:47
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!
97

9718853

Joined 14.10.2014

Bool works for currencies but not BTC, Oil or indices
07 Nov 2018, 16:47


Hi all,

I'm using thso code to check if any pending orders are present before placing an order... It works great across all currency pairs but fails on Bitcoin, Oil and indices... 

Is 'Symbol' only relevent in currency pairs? 

{
                bool UplongO = false;
                foreach (var order in PendingOrders)
                {
                    if (order.TradeType == TradeType.Buy && order.Label == label && order.SymbolCode == Symbol.Code)
                    {
                        UplongO = true;
                    }
                }
                if (UplongO == false )
                {
                    PlaceLimitOrder(....)
                }

Many thanks,

Ian


@9718853
Replies

PanagiotisCharalampous
07 Nov 2018, 16:55

Hi Ian,

Can you explain to us what do you mean it fails? Can you also share the full cBot code as well as your broker?

Best Regards,

Panagiotis


@PanagiotisCharalampous

9718853
07 Nov 2018, 18:41 ( Updated at: 21 Dec 2023, 09:20 )

Hi Panagiotis,

Thank you for the quick response, much appreciated...

The bool should prevent multiple pending orders (of the same label, symbol & TradeType)... This works with all currencies in both backtesting and live trading... 

However, in oil, bitcoin and indices there are lots of orders placed despite the bool...

My Broker is IC Markets...

 

Here's the code with a few entry parmeters removed to keep it concise:

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class FIBBOT : Robot
    {
        [Parameter("rangeCalcForTPandSL", DefaultValue = 1.4)]
        public double rangeCalcForTPandSL { get; set; }

        [Parameter("Look Back Period", DefaultValue = 20)]
        public int period { get; set; }

        string label = "618Bot";

        protected override void OnStart()
        {

        }

        private double smartVolume(double riskPercent, double stopLossPips)
        {
            double risked = Account.Balance * riskPercent / 100;
            double volume = risked / stopLossPips / Symbol.PipValue;
            return Symbol.NormalizeVolumeInUnits(volume, RoundingMode.ToNearest);
            //return Symbol.NormalizeVolume(volume, RoundingMode.ToNearest);
            // Smart volume calculates risk based on a percentage of the account balance and where the stop loss is placed.
            // When a StopOrder is placed in the code below smart volume states 'smartVolume(2, 100)' the 2 being 2% of the 
            // account balance and the 100 being a SL at 100 pips...
            // This is the initial stop placement, in actual fact our trailing stop of only 5 pips brings the risk to 0.01% for the avergage 2% risked position!
        }

        private int HighestBar(DataSeries series, int period)
        {
            for (int i = 0; i <= period; i++)
            {
                if (series[series.Count - 1 - i] == series.Maximum(period))
                {
                    return i;
                }
            }
            return -1;
        }
        private int LowestBar(DataSeries series, int period)
        {
            for (int i = 0; i <= period; i++)
            {
                if (series[series.Count - 1 - i] == series.Minimum(period))
                {
                    return i;
                }
            }
            return -1;
        }
        protected override void OnBar()
        {

            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var symbolSearch = Positions.FindAll(label, Symbol);
            int Hi = HighestBar(MarketSeries.High, period);
            int Lo = LowestBar(MarketSeries.Low, period);
            var maxHigh = MarketSeries.High.Maximum(period);
            var minLow = MarketSeries.Low.Minimum(period);
            var UpPatternfib618 = minLow + (maxHigh - minLow) * 0.382;
            var TP = ((maxHigh - minLow) / Symbol.PipSize) / rangeCalcForTPandSL;
            var SL = TP / 2;

            {
                bool UplongO = false;
                foreach (var order in PendingOrders)
                {
                    if (order.TradeType == TradeType.Buy && order.Label == label && order.SymbolCode == Symbol.Code)
                    {
                        UplongO = true;
                    }
                }

                if (UplongO == false && longPosition == null)
                {
                    PlaceLimitOrder(TradeType.Buy, Symbol, smartVolume(2, SL), UpPatternfib618, label, SL, TP, null);

                }
                if (Symbol.Bid + (2 * Symbol.PipSize) > maxHigh)
                {
                    foreach (var order in PendingOrders)
                    {
                        if (order.TradeType == TradeType.Buy && order.Label == label && order.SymbolCode == Symbol.Code)
                        {
                            CancelPendingOrder(order);
                        }
                    }
                }

                var DownshortPosition = Positions.Find(label, Symbol, TradeType.Sell);
                var DownPatternfib618 = minLow + (maxHigh - minLow) * 0.618;

                {
                    bool DownshortO = false;
                    foreach (var order in PendingOrders)
                    {
                        if (order.TradeType == TradeType.Sell && order.Label == label && order.SymbolCode == Symbol.Code)
                        {
                            DownshortO = true;
                        }
                    }
                    if (DownshortO == false && DownshortPosition == null)
                    {
                        PlaceLimitOrder(TradeType.Sell, Symbol, smartVolume(2, SL), DownPatternfib618, label, SL, TP, null);
                        Print("TP = " + TP + " SL = " + SL);
                    }
                    if (Symbol.Ask - (2 * Symbol.PipSize) < minLow)
                    {
                        foreach (var order in PendingOrders)
                        {
                            if (order.TradeType == TradeType.Sell && order.Label == label && order.SymbolCode == Symbol.Code)
                            {

                                CancelPendingOrder(order);
                            }
                        }
                    }
                }
            }
        }
    }
}




 

 

 

 


@9718853

PanagiotisCharalampous
09 Nov 2018, 10:12

Hi Ian,

Thank you. I could not reproduce your problem on ICMarkets. Can you please provide me with baktesting parameters and dates as well?

Best Regards,

Panagiotis


@PanagiotisCharalampous

9718853
09 Nov 2018, 11:10

Morning Panagiotis,

I think I've found the issue, I've taken the same code and added a print statement on line 84, for some reason this seems to be causing the bug.. Why might this happen?!

 

 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class FIBBOT : Robot
    {
        [Parameter("rangeCalcForTPandSL", DefaultValue = 1.4)]
        public double rangeCalcForTPandSL { get; set; }

        [Parameter("Look Back Period", DefaultValue = 20)]
        public int period { get; set; }

        string label = "618Bot";

        protected override void OnStart()
        {

        }

        private double smartVolume(double riskPercent, double stopLossPips)
        {
            double risked = Account.Balance * riskPercent / 100;
            double volume = risked / stopLossPips / Symbol.PipValue;
            return Symbol.NormalizeVolumeInUnits(volume, RoundingMode.ToNearest);
            //return Symbol.NormalizeVolume(volume, RoundingMode.ToNearest);
            // Smart volume calculates risk based on a percentage of the account balance and where the stop loss is placed.
            // When a StopOrder is placed in the code below smart volume states 'smartVolume(2, 100)' the 2 being 2% of the 
            // account balance and the 100 being a SL at 100 pips...
            // This is the initial stop placement, in actual fact our trailing stop of only 5 pips brings the risk to 0.01% for the avergage 2% risked position!
        }

        private int HighestBar(DataSeries series, int period)
        {
            for (int i = 0; i <= period; i++)
            {
                if (series[series.Count - 1 - i] == series.Maximum(period))
                {
                    return i;
                }
            }
            return -1;
        }
        private int LowestBar(DataSeries series, int period)
        {
            for (int i = 0; i <= period; i++)
            {
                if (series[series.Count - 1 - i] == series.Minimum(period))
                {
                    return i;
                }
            }
            return -1;
        }
        protected override void OnBar()
        {

            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var symbolSearch = Positions.FindAll(label, Symbol);
            int Hi = HighestBar(MarketSeries.High, period);
            int Lo = LowestBar(MarketSeries.Low, period);
            var maxHigh = MarketSeries.High.Maximum(period);
            var minLow = MarketSeries.Low.Minimum(period);
            var UpPatternfib618 = minLow + (maxHigh - minLow) * 0.382;
            var TP = ((maxHigh - minLow) / Symbol.PipSize) / rangeCalcForTPandSL;
            var SL = TP / 2;

            {
                bool UplongO = false;
                foreach (var order in PendingOrders)
                {
                    if (order.TradeType == TradeType.Buy && order.Label == label && order.SymbolCode == Symbol.Code)
                    {
                        UplongO = true;
                    }
                }

                if (UplongO == false && longPosition == null)
                    Print("1");
                {
                    PlaceLimitOrder(TradeType.Buy, Symbol, smartVolume(2, SL), UpPatternfib618, label, SL, TP, null);

                }
                if (Symbol.Bid + (2 * Symbol.PipSize) > maxHigh)
                {
                    foreach (var order in PendingOrders)
                    {
                        if (order.TradeType == TradeType.Buy && order.Label == label && order.SymbolCode == Symbol.Code)
                        {
                            CancelPendingOrder(order);
                        }
                    }
                }

                var DownshortPosition = Positions.Find(label, Symbol, TradeType.Sell);
                var DownPatternfib618 = minLow + (maxHigh - minLow) * 0.618;

                {
                    bool DownshortO = false;
                    foreach (var order in PendingOrders)
                    {
                        if (order.TradeType == TradeType.Sell && order.Label == label && order.SymbolCode == Symbol.Code)
                        {
                            DownshortO = true;
                        }
                    }
                    if (DownshortO == false && DownshortPosition == null)
                    {
                        PlaceLimitOrder(TradeType.Sell, Symbol, smartVolume(2, SL), DownPatternfib618, label, SL, TP, null);
                        Print("TP = " + TP + " SL = " + SL);
                    }
                    if (Symbol.Ask - (2 * Symbol.PipSize) < minLow)
                    {
                        foreach (var order in PendingOrders)
                        {
                            if (order.TradeType == TradeType.Sell && order.Label == label && order.SymbolCode == Symbol.Code)
                            {

                                CancelPendingOrder(order);
                            }
                        }
                    }
                }
            }
        }
    }
}

 


@9718853

9718853
09 Nov 2018, 11:13

Parameters:

BTCUSD 1hr chart

Commission = 30

Data = Tick Data from server

Dates = 28/08/17 - 07/11/18


@9718853

PanagiotisCharalampous
09 Nov 2018, 11:19

Hi Ian,

Ok it is clear now. You should have put the print statement inside the brackets. The way you put it, the brackets are not considered part of the if statement any more, only the Print() method.

Best Regards,

Panagiotis


@PanagiotisCharalampous