Tick data for Energies.

Created at 08 Mar 2018, 11:24
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!
CY

Cyril Ndiga

Joined 23.12.2017

Tick data for Energies.
08 Mar 2018, 11:24


The tick data for BRENT and NATURAL GAS are faulty and are not working during backtesting and optimisation. Is it possible that cAlgo provides accurate tick data for these two symbols.

Also, may we get more tick data for WTI CRUDE OIL? 

Two years of data is sometimes not enough.

Thanks.


@Cyril Ndiga
Replies

PanagiotisCharalampous
08 Mar 2018, 11:28

Hi Cyril,

Thanks for posting in our forum. Please tell us which broker's cAlgo you are using and send us a screenshot of the time ranges where you have detected an issue so that we can check.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Cyril Ndiga
08 Mar 2018, 12:10 ( Updated at: 21 Dec 2023, 09:20 )

RE: More Information

Panagiotis Charalampous said:

Hi Cyril,

Thanks for posting in our forum. Please tell us which broker's cAlgo you are using and send us a screenshot of the time ranges where you have detected an issue so that we can check.

Best Regards,

Panagiotis

Hello Mr. Panagiotis,

I am using FXPro's cAlgo.

When I start backtesting (or optimisation), it immediately shows that the backtexting is completed without even showing the chart till the end date of the backtesting period selected. 

Screenshots below show this problem.

It is on all timeframes.

Please assist.

Thank you.

 

 


@Cyril Ndiga

PanagiotisCharalampous
08 Mar 2018, 12:48

Hi Cyril,

Can you check the log if there is any issue with your cBot? I checked both symbols on FxPro cAlgo and I don't see any problem with the tick data. If you can send me your cBot then I can check what is going on.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Cyril Ndiga
08 Mar 2018, 12:56 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Panagiotis Charalampous said:

Hi Cyril,

Can you check the log if there is any issue with your cBot? I checked both symbols on FxPro cAlgo and I don't see any problem with the tick data. If you can send me your cBot then I can check what is going on.

Best Regards,

Panagiotis

I have just checked the log and this is what I found.

How do I resolve this error?


@Cyril Ndiga

PanagiotisCharalampous
08 Mar 2018, 12:58

Hi Cyril,

The cBot throws an exception. I will need the cBot code in order to tell you why the exception is thrown.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Cyril Ndiga
08 Mar 2018, 13:03

RE:

Panagiotis Charalampous said:

Hi Cyril,

The cBot throws an exception. I will need the cBot code in order to tell you why the exception is thrown.

Best Regards,

Panagiotis

Here it is.

Please assist.

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 Stochastic_Expert : Robot
    {
        [Parameter("Volume", DefaultValue = 1000)]
        public int volume { get; set; }

        [Parameter("Use volume in Percentage", DefaultValue = true)]
        public bool PercentLot { get; set; }

        [Parameter("Percent", DefaultValue = 5)]
        public double Percent { get; set; }

        [Parameter("Fixed leverage", DefaultValue = false)]
        public bool fixedleverage { get; set; }

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

        [Parameter("Your comment", DefaultValue = "Project Echelon")]
        public string comment { get; set; }

        [Parameter("Stop Loss", DefaultValue = 20)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", DefaultValue = 10.3)]
        public int TakeProfit { get; set; }

        [Parameter("Use output for reverse signal", DefaultValue = true)]
        public bool UseReverse { get; set; }

        [Parameter("-----Stochastic Parameters-----", DefaultValue = "")]
        public string ParametersStochastic { get; set; }

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

        [Parameter("D Period", DefaultValue = 3)]
        public int InpDPeriod { get; set; }

        [Parameter("Slowing", DefaultValue = 3)]
        public int InpSlowing { get; set; }

        [Parameter("Moving period", DefaultValue = 72)]
        public int PeriodMA { get; set; }

        [Parameter("MA METHOD", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType ENUM_MA_METHOD { get; set; }

        [Parameter("MA APPLIED cl=1,op=2,hi=3,low=4", DefaultValue = 1, MaxValue = 4, MinValue = 1)]
        public int ENUM_APPLIED_PRICE { get; set; }

        [Parameter("Range for MA direction(10max)", DefaultValue = 5)]
        public int Diapazon { get; set; }

        [Parameter("Use Slope moving average", DefaultValue = false)]
        public bool UseSlope { get; set; }

        [Parameter("Slope moving average", DefaultValue = 10)]
        public int Slope { get; set; }

        [Parameter("Magic number", DefaultValue = 777)]
        public int Magic { get; set; }

        [Parameter("Use Trailing", DefaultValue = true)]
        public bool UseTrailing { get; set; }

        [Parameter("Trailing Stop Start", DefaultValue = 10)]
        public int TrailingStop { get; set; }

        [Parameter("Trailing Step Distance", DefaultValue = 5)]
        public int TrailingStep { get; set; }

        [Parameter("Use Drawdown", DefaultValue = false)]
        public bool UseDrawdown { get; set; }

        [Parameter("Drawdown (%)", DefaultValue = 100)]
        public int Drawdown { get; set; }

        [Parameter("Use maximum transactions", DefaultValue = false)]
        public bool UseMaxtransactions { get; set; }

        [Parameter("Maximum transactions", DefaultValue = 50)]
        public int Maxtransactions { get; set; }

        private double MovingA = 0, MovingB = 0, SL = 0, TP = 0, TStop = 0, TStep = 0, StopLevel = 0, Stoch1 = 0, StochSign1 = 0, BR = 0,
        Depo = 0, Bal = 0, SE = 0;
        private static DateTime prevtime;
        private MovingAverage ma1;
        private MovingAverage ma2;
        private StochasticOscillator stoch1;
        private StochasticOscillator stoch2;
        private DataSeries series;
        private double leverage;

        protected override void OnStart()
        {
            Print("Symbol leverage:  " + Symbol.PreciseLeverage);

            series = MarketSeries.Close;
            switch (ENUM_APPLIED_PRICE)
            {
                case 2:
                    series = MarketSeries.Open;
                    break;
                case 3:
                    series = MarketSeries.High;
                    break;
                case 4:
                    series = MarketSeries.Low;
                    break;

            }

            if (Symbol.Digits == 3 || Symbol.Digits == 5)
            {
                Slope *= 1;
                StopLoss *= 1;
                TakeProfit *= 1;
                TrailingStop *= 1;
                TrailingStep *= 1;
            }

            if (Diapazon > 10)
            {
                Diapazon = 10;
            }
            Depo = (Account.Balance / 100) * Drawdown;
            Bal = Account.Balance;
            leverage = Symbol.PreciseLeverage;
            if (fixedleverage)
                leverage = Account.PreciseLeverage;
            //Comment("");

            ma1 = Indicators.MovingAverage(series, PeriodMA, ENUM_MA_METHOD);
            ma2 = Indicators.MovingAverage(series, PeriodMA, ENUM_MA_METHOD);
            stoch1 = Indicators.StochasticOscillator(InpKPeriod, InpSlowing, InpDPeriod, MovingAverageType.Simple);
            stoch2 = Indicators.StochasticOscillator(InpKPeriod, InpSlowing, InpDPeriod, MovingAverageType.Simple);
        }

        protected override void OnTick()
        {
            SE = Math.Round(Slope * Symbol.PipSize, Symbol.Digits);
            SL = Math.Round(StopLoss * Symbol.PipSize, Symbol.Digits);
            TP = Math.Round(TakeProfit * Symbol.PipSize, Symbol.Digits);
            BR = Math.Round(TrailingStop * Symbol.PipSize, Symbol.Digits);
            TStop = Math.Round(TrailingStop * Symbol.PipSize, Symbol.Digits);
            TStep = Math.Round(TrailingStep * Symbol.PipSize, Symbol.Digits);
            StopLevel = Math.Round(0 * Symbol.PipSize, Symbol.Digits);
            if (TP < StopLevel && TP != 0)
            {
                TP = StopLevel;
                Print("Take Profit too small!!! Use =", StopLevel);
            }
            if (SL < StopLevel && SL != 0)
            {
                SL = StopLevel;
                Print("Stop Loss too small!!! Use =", StopLevel);
            }
            if (TStop < StopLevel)
            {
                TStop = StopLevel;
                Print("Trailing Stop too small!!! Use =", StopLevel);
            }
            MovingA = Math.Round(ma1.Result.Last(1), Symbol.Digits);
            MovingB = Math.Round(ma1.Result.Last(Diapazon), Symbol.Digits);
            Stoch1 = Math.Round(stoch1.PercentK.Last(1), Symbol.Digits);
            StochSign1 = Math.Round(stoch2.PercentD.Last(1), Symbol.Digits);

            if (UseTrailing == true)
            {
                BU();
                Trailing();
            }
            if (UseReverse == true)
            {
                if (CountOrders(TradeType.Buy) >= 1 && Stoch1 < StochSign1 && Stoch1 > 80)
                {
                    CloseOrders(TradeType.Buy);
                }
                if (CountOrders(TradeType.Sell) >= 1 && Stoch1 > StochSign1 && Stoch1 < 20)
                {
                    CloseOrders(TradeType.Sell);
                }
            }
            if (UseDrawdown == true)
            {
                if (Bal - Account.Equity > Depo)
                {
                    CloseOrders(TradeType.Buy);
                    CloseOrders(TradeType.Sell);
                    ChartObjects.DrawText("comment", "Equity is less than the permissible level !!!", StaticPosition.TopLeft, Colors.YellowGreen);
                }
            }
            if (MarketSeries.OpenTime.LastValue == prevtime)
                return;
            prevtime = MarketSeries.OpenTime.LastValue;

            if (UseSlope)
            {
                //&& DIHigh1>DILow1 && DIHigh2<DILow2
                if (Stoch1 > StochSign1 && Stoch1 < 20 && UseMaxtransactions == true && CountOtherOrders() < Maxtransactions && CountOrders(TradeType.Buy) == 0 && MovingA > MovingB + SE && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Buy);
                }
                //&& DIHigh1>DILow1 && DIHigh2<DILow2
                if (Stoch1 > StochSign1 && Stoch1 < 20 && UseMaxtransactions == false && CountOrders(TradeType.Buy) == 0 && MovingA > MovingB + SE && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Buy);
                }
                //&& DIHigh1<DILow1 && DIHigh2>DILow2
                if (Stoch1 < StochSign1 && Stoch1 > 80 && UseMaxtransactions == true && CountOtherOrders() < Maxtransactions && CountOrders(TradeType.Sell) == 0 && MovingA < MovingB - SE && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Sell);
                }
                // && DIHigh1<DILow1 && DIHigh2>DILow2
                if (Stoch1 < StochSign1 && Stoch1 > 80 && UseMaxtransactions == false && CountOrders(TradeType.Sell) == 0 && MovingA < MovingB - SE && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Sell);
                }
            }
            else
            {
                //&& DIHigh1>DILow1 && DIHigh2<DILow2
                if (Stoch1 > StochSign1 && Stoch1 < 20 && UseMaxtransactions == true && CountOtherOrders() < Maxtransactions && CountOrders(TradeType.Buy) == 0 && MovingA > MovingB && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Buy);
                }
                //&& DIHigh1>DILow1 && DIHigh2<DILow2
                if (Stoch1 > StochSign1 && Stoch1 < 20 && UseMaxtransactions == false && CountOrders(TradeType.Buy) == 0 && MovingA > MovingB && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Buy);
                }
                //&& DIHigh1<DILow1 && DIHigh2>DILow2
                if (Stoch1 < StochSign1 && Stoch1 > 80 && UseMaxtransactions == true && CountOtherOrders() < Maxtransactions && CountOrders(TradeType.Sell) == 0 && MovingA < MovingB && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Sell);
                }
                // && DIHigh1<DILow1 && DIHigh2>DILow2
                if (Stoch1 < StochSign1 && Stoch1 > 80 && UseMaxtransactions == false && CountOrders(TradeType.Sell) == 0 && MovingA < MovingB && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Sell);
                }
            }

        }

        void OpenOrders(TradeType cmd)
        {
            double TPMod = 0;
            double SLMod = 0;
            double price = 0;

            if (cmd == TradeType.Buy)
            {
                price = Symbol.Ask;
            }
            if (cmd == TradeType.Sell)
            {
                price = Symbol.Bid;
            }
            var trade = ExecuteMarketOrder(cmd, Symbol, GetVolume(), Magic.ToString(), null, null, Slippage, comment);
            if (trade.IsSuccessful)
            {

                if (cmd == TradeType.Buy)
                {
                    if (TP == 0 && SL == 0)
                        return;
                    if (TP == 0)
                    {
                        TPMod = TP;
                    }
                    else
                    {
                        TPMod = Math.Round(Symbol.Ask + TP, Symbol.Digits);
                    }
                    if (SL == 0)
                    {
                        SLMod = SL;
                    }
                    else
                    {
                        SLMod = Math.Round(Symbol.Bid - SL, Symbol.Digits);
                    }
                    var tmod = ModifyPosition(trade.Position, SLMod, TPMod);
                    if (!tmod.IsSuccessful)
                    {
                        Print("Order ", cmd, " not modify! Error = ", tmod.Error);
                    }
                }
                if (cmd == TradeType.Sell)
                {
                    if (TP == 0 && SL == 0)
                        return;
                    if (TP == 0)
                    {
                        TPMod = TP;
                    }
                    else
                    {
                        TPMod = Math.Round(Symbol.Bid - TP, Symbol.Digits);
                    }
                    if (SL == 0)
                    {
                        SLMod = SL;
                    }
                    else
                    {
                        SLMod = Math.Round(Symbol.Ask + SL, Symbol.Digits);
                    }
                    var tmod = ModifyPosition(trade.Position, SLMod, TPMod);
                    if (!tmod.IsSuccessful)
                    {
                        Print("Order ", cmd, " not modify! Error = ", tmod.Error);
                    }
                }

            }
            else
            {
                Print("Order ", cmd, " not send! Error = ", trade.Error);
            }
        }

        int CountOrders(TradeType cmd)
        {
            int count = 0;
            count = Positions.Count(pos => pos.SymbolCode == Symbol.Code && pos.Label == Magic.ToString() && pos.TradeType == cmd);
            return (count);
        }
        void CloseOrders(TradeType cmd)
        {
            foreach (var Order in Positions)
            {

                if (Order.SymbolCode == Symbol.Code && Order.Label == Magic.ToString())
                {
                    if (Order.TradeType == TradeType.Buy && cmd == TradeType.Buy)
                    {
                        var trade = ClosePosition(Order);
                        if (!trade.IsSuccessful)
                        {
                            Print("Order BUY not close! Error = ", trade.Error);
                        }
                    }
                    if (Order.TradeType == TradeType.Sell && cmd == TradeType.Sell)
                    {
                        var trade = ClosePosition(Order);
                        if (!trade.IsSuccessful)
                        {
                            Print("Order SELL not close! Error = ", trade.Error);
                        }
                    }
                }

            }
        }

        void BU()
        {
            double TPBU = 0;
            foreach (var Order in Positions)
            {

                if (Order.SymbolCode == Symbol.Code && Order.Label == Magic.ToString())
                {
                    if (Order.TradeType == TradeType.Buy && Order.StopLoss != Order.EntryPrice && Symbol.Ask > (Order.EntryPrice + BR))
                    {
                        if (Order.StopLoss > Order.EntryPrice)
                            return;
                        if (TP == 0)
                        {
                            TPBU = 0;
                        }
                        else
                        {
                            TPBU = Order.EntryPrice + TP;
                        }
                        var trade = ModifyPosition(Order, Order.EntryPrice, TPBU);
                        if (!trade.IsSuccessful)
                        {
                            Print("Order BUY not Breakeven! Error = ", trade.Error);
                        }
                    }
                    if (Order.TradeType == TradeType.Sell && Order.StopLoss != Order.EntryPrice && Symbol.Bid < (Order.EntryPrice - BR))
                    {
                        if (Order.StopLoss < Order.EntryPrice)
                            return;
                        if (TP == 0)
                        {
                            TPBU = 0;
                        }
                        else
                        {
                            TPBU = Order.EntryPrice - TP;
                        }
                        var trade = ModifyPosition(Order, Order.EntryPrice, TPBU);
                        if (!trade.IsSuccessful)
                        {
                            Print("Order SELL not Breakeven! Error = ", trade.Error);
                        }
                    }
                }

            }
        }

        void Trailing()
        {
            double TPBU = 0;
            foreach (var Order in Positions)
            {

                if (Order.SymbolCode == Symbol.Code && Order.Label == Magic.ToString())
                {
                    if (Order.TradeType == TradeType.Buy)
                    {
                        if (Symbol.Bid - Order.EntryPrice > TStop)
                        {
                            if (Order.StopLoss < Symbol.Bid - (TStop + TStep))
                            {
                                if (Order.TakeProfit == 0 || (Order.TakeProfit - Symbol.Ask) >= StopLevel)
                                {
                                    if (TP == 0)
                                    {
                                        TPBU = 0;
                                    }
                                    else
                                    {
                                        TPBU = Order.EntryPrice + TP;
                                    }
                                    SL = Math.Round(Symbol.Bid - TStop, Symbol.Digits);
                                    if (Order.StopLoss != SL)
                                    {
                                        var trade = ModifyPosition(Order, SL, TPBU);
                                        if (!trade.IsSuccessful)
                                        {
                                            Print("BUY Trailing Stop False! Error = ", trade.Error);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (Order.TradeType == TradeType.Sell)
                    {
                        if (Order.EntryPrice - Symbol.Ask > TStop)
                        {
                            if (Order.StopLoss > Symbol.Ask + (TStop + TStep) || Order.StopLoss == 0)
                            {
                                if (Order.TakeProfit == 0 || (Symbol.Bid - Order.TakeProfit) >= StopLevel)
                                {
                                    if (TP == 0)
                                    {
                                        TPBU = 0;
                                    }
                                    else
                                    {
                                        TPBU = Order.EntryPrice - TP;
                                    }
                                    SL = Math.Round(Symbol.Ask + TStop, Symbol.Digits);
                                    if (Order.StopLoss != SL)
                                    {
                                        var trade = ModifyPosition(Order, SL, TPBU);
                                        if (!trade.IsSuccessful)
                                        {
                                            Print("SELL Trailing Stop False! Error = ", trade.Error);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
        }

        int CountOtherOrders()
        {
            int count = 0;
            foreach (var Order in Positions)
            {

                if (Order.SymbolCode != Symbol.Code && Order.Label == Magic.ToString())
                    count++;

            }
            return (count);
        }

        private long GetVolume()
        {
            if (!PercentLot)
                return (volume);
            var vol = Account.FreeMargin * leverage;
            var vol2 = vol * Percent / 100;

            string sym = Symbol.Code;
            if (sym.Length >= 4 && Symbol.Code.Substring(0, 3) == "USD")
            {
                return (Symbol.NormalizeVolume(vol2));
            }
            if (sym.Length >= 4 && Symbol.Code.Substring(3, 3) == "USD")
            {

                return (Symbol.NormalizeVolume(vol2 / Symbol.Bid));
            }
            if (sym.Length >= 4 && !(Symbol.Code.Substring(0, 3) == "USD") && !(Symbol.Code.Substring(3, 3) == "USD"))
            {
                Symbol cross = MarketData.GetSymbol("USD" + Symbol.Code.Substring(3, 3));
                return (Symbol.NormalizeVolume(vol2 / Symbol.Bid / cross.Bid));
            }
            if (sym.Length < 4)
            {
                return (Symbol.NormalizeVolume(vol2 / Symbol.Bid));
            }
            return (Symbol.NormalizeVolume(vol2));

        }

        protected override void OnStop()
        {


            // Put your deinitialization logic here
        }
    }
}

 


@Cyril Ndiga

Cyril Ndiga
08 Mar 2018, 13:11 ( Updated at: 21 Dec 2023, 09:20 )

RE: RE:

Cyril Ndiga said:

Panagiotis Charalampous said:

Hi Cyril,

The cBot throws an exception. I will need the cBot code in order to tell you why the exception is thrown.

Best Regards,

Panagiotis

Here it is.

Please assist.

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 Stochastic_Expert : Robot
    {
        [Parameter("Volume", DefaultValue = 1000)]
        public int volume { get; set; }

        [Parameter("Use volume in Percentage", DefaultValue = true)]
        public bool PercentLot { get; set; }

        [Parameter("Percent", DefaultValue = 5)]
        public double Percent { get; set; }

        [Parameter("Fixed leverage", DefaultValue = false)]
        public bool fixedleverage { get; set; }

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

        [Parameter("Your comment", DefaultValue = "Project Echelon")]
        public string comment { get; set; }

        [Parameter("Stop Loss", DefaultValue = 20)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", DefaultValue = 10.3)]
        public int TakeProfit { get; set; }

        [Parameter("Use output for reverse signal", DefaultValue = true)]
        public bool UseReverse { get; set; }

        [Parameter("-----Stochastic Parameters-----", DefaultValue = "")]
        public string ParametersStochastic { get; set; }

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

        [Parameter("D Period", DefaultValue = 3)]
        public int InpDPeriod { get; set; }

        [Parameter("Slowing", DefaultValue = 3)]
        public int InpSlowing { get; set; }

        [Parameter("Moving period", DefaultValue = 72)]
        public int PeriodMA { get; set; }

        [Parameter("MA METHOD", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType ENUM_MA_METHOD { get; set; }

        [Parameter("MA APPLIED cl=1,op=2,hi=3,low=4", DefaultValue = 1, MaxValue = 4, MinValue = 1)]
        public int ENUM_APPLIED_PRICE { get; set; }

        [Parameter("Range for MA direction(10max)", DefaultValue = 5)]
        public int Diapazon { get; set; }

        [Parameter("Use Slope moving average", DefaultValue = false)]
        public bool UseSlope { get; set; }

        [Parameter("Slope moving average", DefaultValue = 10)]
        public int Slope { get; set; }

        [Parameter("Magic number", DefaultValue = 777)]
        public int Magic { get; set; }

        [Parameter("Use Trailing", DefaultValue = true)]
        public bool UseTrailing { get; set; }

        [Parameter("Trailing Stop Start", DefaultValue = 10)]
        public int TrailingStop { get; set; }

        [Parameter("Trailing Step Distance", DefaultValue = 5)]
        public int TrailingStep { get; set; }

        [Parameter("Use Drawdown", DefaultValue = false)]
        public bool UseDrawdown { get; set; }

        [Parameter("Drawdown (%)", DefaultValue = 100)]
        public int Drawdown { get; set; }

        [Parameter("Use maximum transactions", DefaultValue = false)]
        public bool UseMaxtransactions { get; set; }

        [Parameter("Maximum transactions", DefaultValue = 50)]
        public int Maxtransactions { get; set; }

        private double MovingA = 0, MovingB = 0, SL = 0, TP = 0, TStop = 0, TStep = 0, StopLevel = 0, Stoch1 = 0, StochSign1 = 0, BR = 0,
        Depo = 0, Bal = 0, SE = 0;
        private static DateTime prevtime;
        private MovingAverage ma1;
        private MovingAverage ma2;
        private StochasticOscillator stoch1;
        private StochasticOscillator stoch2;
        private DataSeries series;
        private double leverage;

        protected override void OnStart()
        {
            Print("Symbol leverage:  " + Symbol.PreciseLeverage);

            series = MarketSeries.Close;
            switch (ENUM_APPLIED_PRICE)
            {
                case 2:
                    series = MarketSeries.Open;
                    break;
                case 3:
                    series = MarketSeries.High;
                    break;
                case 4:
                    series = MarketSeries.Low;
                    break;

            }

            if (Symbol.Digits == 3 || Symbol.Digits == 5)
            {
                Slope *= 1;
                StopLoss *= 1;
                TakeProfit *= 1;
                TrailingStop *= 1;
                TrailingStep *= 1;
            }

            if (Diapazon > 10)
            {
                Diapazon = 10;
            }
            Depo = (Account.Balance / 100) * Drawdown;
            Bal = Account.Balance;
            leverage = Symbol.PreciseLeverage;
            if (fixedleverage)
                leverage = Account.PreciseLeverage;
            //Comment("");

            ma1 = Indicators.MovingAverage(series, PeriodMA, ENUM_MA_METHOD);
            ma2 = Indicators.MovingAverage(series, PeriodMA, ENUM_MA_METHOD);
            stoch1 = Indicators.StochasticOscillator(InpKPeriod, InpSlowing, InpDPeriod, MovingAverageType.Simple);
            stoch2 = Indicators.StochasticOscillator(InpKPeriod, InpSlowing, InpDPeriod, MovingAverageType.Simple);
        }

        protected override void OnTick()
        {
            SE = Math.Round(Slope * Symbol.PipSize, Symbol.Digits);
            SL = Math.Round(StopLoss * Symbol.PipSize, Symbol.Digits);
            TP = Math.Round(TakeProfit * Symbol.PipSize, Symbol.Digits);
            BR = Math.Round(TrailingStop * Symbol.PipSize, Symbol.Digits);
            TStop = Math.Round(TrailingStop * Symbol.PipSize, Symbol.Digits);
            TStep = Math.Round(TrailingStep * Symbol.PipSize, Symbol.Digits);
            StopLevel = Math.Round(0 * Symbol.PipSize, Symbol.Digits);
            if (TP < StopLevel && TP != 0)
            {
                TP = StopLevel;
                Print("Take Profit too small!!! Use =", StopLevel);
            }
            if (SL < StopLevel && SL != 0)
            {
                SL = StopLevel;
                Print("Stop Loss too small!!! Use =", StopLevel);
            }
            if (TStop < StopLevel)
            {
                TStop = StopLevel;
                Print("Trailing Stop too small!!! Use =", StopLevel);
            }
            MovingA = Math.Round(ma1.Result.Last(1), Symbol.Digits);
            MovingB = Math.Round(ma1.Result.Last(Diapazon), Symbol.Digits);
            Stoch1 = Math.Round(stoch1.PercentK.Last(1), Symbol.Digits);
            StochSign1 = Math.Round(stoch2.PercentD.Last(1), Symbol.Digits);

            if (UseTrailing == true)
            {
                BU();
                Trailing();
            }
            if (UseReverse == true)
            {
                if (CountOrders(TradeType.Buy) >= 1 && Stoch1 < StochSign1 && Stoch1 > 80)
                {
                    CloseOrders(TradeType.Buy);
                }
                if (CountOrders(TradeType.Sell) >= 1 && Stoch1 > StochSign1 && Stoch1 < 20)
                {
                    CloseOrders(TradeType.Sell);
                }
            }
            if (UseDrawdown == true)
            {
                if (Bal - Account.Equity > Depo)
                {
                    CloseOrders(TradeType.Buy);
                    CloseOrders(TradeType.Sell);
                    ChartObjects.DrawText("comment", "Equity is less than the permissible level !!!", StaticPosition.TopLeft, Colors.YellowGreen);
                }
            }
            if (MarketSeries.OpenTime.LastValue == prevtime)
                return;
            prevtime = MarketSeries.OpenTime.LastValue;

            if (UseSlope)
            {
                //&& DIHigh1>DILow1 && DIHigh2<DILow2
                if (Stoch1 > StochSign1 && Stoch1 < 20 && UseMaxtransactions == true && CountOtherOrders() < Maxtransactions && CountOrders(TradeType.Buy) == 0 && MovingA > MovingB + SE && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Buy);
                }
                //&& DIHigh1>DILow1 && DIHigh2<DILow2
                if (Stoch1 > StochSign1 && Stoch1 < 20 && UseMaxtransactions == false && CountOrders(TradeType.Buy) == 0 && MovingA > MovingB + SE && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Buy);
                }
                //&& DIHigh1<DILow1 && DIHigh2>DILow2
                if (Stoch1 < StochSign1 && Stoch1 > 80 && UseMaxtransactions == true && CountOtherOrders() < Maxtransactions && CountOrders(TradeType.Sell) == 0 && MovingA < MovingB - SE && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Sell);
                }
                // && DIHigh1<DILow1 && DIHigh2>DILow2
                if (Stoch1 < StochSign1 && Stoch1 > 80 && UseMaxtransactions == false && CountOrders(TradeType.Sell) == 0 && MovingA < MovingB - SE && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Sell);
                }
            }
            else
            {
                //&& DIHigh1>DILow1 && DIHigh2<DILow2
                if (Stoch1 > StochSign1 && Stoch1 < 20 && UseMaxtransactions == true && CountOtherOrders() < Maxtransactions && CountOrders(TradeType.Buy) == 0 && MovingA > MovingB && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Buy);
                }
                //&& DIHigh1>DILow1 && DIHigh2<DILow2
                if (Stoch1 > StochSign1 && Stoch1 < 20 && UseMaxtransactions == false && CountOrders(TradeType.Buy) == 0 && MovingA > MovingB && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Buy);
                }
                //&& DIHigh1<DILow1 && DIHigh2>DILow2
                if (Stoch1 < StochSign1 && Stoch1 > 80 && UseMaxtransactions == true && CountOtherOrders() < Maxtransactions && CountOrders(TradeType.Sell) == 0 && MovingA < MovingB && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Sell);
                }
                // && DIHigh1<DILow1 && DIHigh2>DILow2
                if (Stoch1 < StochSign1 && Stoch1 > 80 && UseMaxtransactions == false && CountOrders(TradeType.Sell) == 0 && MovingA < MovingB && Bal - Account.Equity < Depo)
                {
                    OpenOrders(TradeType.Sell);
                }
            }

        }

        void OpenOrders(TradeType cmd)
        {
            double TPMod = 0;
            double SLMod = 0;
            double price = 0;

            if (cmd == TradeType.Buy)
            {
                price = Symbol.Ask;
            }
            if (cmd == TradeType.Sell)
            {
                price = Symbol.Bid;
            }
            var trade = ExecuteMarketOrder(cmd, Symbol, GetVolume(), Magic.ToString(), null, null, Slippage, comment);
            if (trade.IsSuccessful)
            {

                if (cmd == TradeType.Buy)
                {
                    if (TP == 0 && SL == 0)
                        return;
                    if (TP == 0)
                    {
                        TPMod = TP;
                    }
                    else
                    {
                        TPMod = Math.Round(Symbol.Ask + TP, Symbol.Digits);
                    }
                    if (SL == 0)
                    {
                        SLMod = SL;
                    }
                    else
                    {
                        SLMod = Math.Round(Symbol.Bid - SL, Symbol.Digits);
                    }
                    var tmod = ModifyPosition(trade.Position, SLMod, TPMod);
                    if (!tmod.IsSuccessful)
                    {
                        Print("Order ", cmd, " not modify! Error = ", tmod.Error);
                    }
                }
                if (cmd == TradeType.Sell)
                {
                    if (TP == 0 && SL == 0)
                        return;
                    if (TP == 0)
                    {
                        TPMod = TP;
                    }
                    else
                    {
                        TPMod = Math.Round(Symbol.Bid - TP, Symbol.Digits);
                    }
                    if (SL == 0)
                    {
                        SLMod = SL;
                    }
                    else
                    {
                        SLMod = Math.Round(Symbol.Ask + SL, Symbol.Digits);
                    }
                    var tmod = ModifyPosition(trade.Position, SLMod, TPMod);
                    if (!tmod.IsSuccessful)
                    {
                        Print("Order ", cmd, " not modify! Error = ", tmod.Error);
                    }
                }

            }
            else
            {
                Print("Order ", cmd, " not send! Error = ", trade.Error);
            }
        }

        int CountOrders(TradeType cmd)
        {
            int count = 0;
            count = Positions.Count(pos => pos.SymbolCode == Symbol.Code && pos.Label == Magic.ToString() && pos.TradeType == cmd);
            return (count);
        }
        void CloseOrders(TradeType cmd)
        {
            foreach (var Order in Positions)
            {

                if (Order.SymbolCode == Symbol.Code && Order.Label == Magic.ToString())
                {
                    if (Order.TradeType == TradeType.Buy && cmd == TradeType.Buy)
                    {
                        var trade = ClosePosition(Order);
                        if (!trade.IsSuccessful)
                        {
                            Print("Order BUY not close! Error = ", trade.Error);
                        }
                    }
                    if (Order.TradeType == TradeType.Sell && cmd == TradeType.Sell)
                    {
                        var trade = ClosePosition(Order);
                        if (!trade.IsSuccessful)
                        {
                            Print("Order SELL not close! Error = ", trade.Error);
                        }
                    }
                }

            }
        }

        void BU()
        {
            double TPBU = 0;
            foreach (var Order in Positions)
            {

                if (Order.SymbolCode == Symbol.Code && Order.Label == Magic.ToString())
                {
                    if (Order.TradeType == TradeType.Buy && Order.StopLoss != Order.EntryPrice && Symbol.Ask > (Order.EntryPrice + BR))
                    {
                        if (Order.StopLoss > Order.EntryPrice)
                            return;
                        if (TP == 0)
                        {
                            TPBU = 0;
                        }
                        else
                        {
                            TPBU = Order.EntryPrice + TP;
                        }
                        var trade = ModifyPosition(Order, Order.EntryPrice, TPBU);
                        if (!trade.IsSuccessful)
                        {
                            Print("Order BUY not Breakeven! Error = ", trade.Error);
                        }
                    }
                    if (Order.TradeType == TradeType.Sell && Order.StopLoss != Order.EntryPrice && Symbol.Bid < (Order.EntryPrice - BR))
                    {
                        if (Order.StopLoss < Order.EntryPrice)
                            return;
                        if (TP == 0)
                        {
                            TPBU = 0;
                        }
                        else
                        {
                            TPBU = Order.EntryPrice - TP;
                        }
                        var trade = ModifyPosition(Order, Order.EntryPrice, TPBU);
                        if (!trade.IsSuccessful)
                        {
                            Print("Order SELL not Breakeven! Error = ", trade.Error);
                        }
                    }
                }

            }
        }

        void Trailing()
        {
            double TPBU = 0;
            foreach (var Order in Positions)
            {

                if (Order.SymbolCode == Symbol.Code && Order.Label == Magic.ToString())
                {
                    if (Order.TradeType == TradeType.Buy)
                    {
                        if (Symbol.Bid - Order.EntryPrice > TStop)
                        {
                            if (Order.StopLoss < Symbol.Bid - (TStop + TStep))
                            {
                                if (Order.TakeProfit == 0 || (Order.TakeProfit - Symbol.Ask) >= StopLevel)
                                {
                                    if (TP == 0)
                                    {
                                        TPBU = 0;
                                    }
                                    else
                                    {
                                        TPBU = Order.EntryPrice + TP;
                                    }
                                    SL = Math.Round(Symbol.Bid - TStop, Symbol.Digits);
                                    if (Order.StopLoss != SL)
                                    {
                                        var trade = ModifyPosition(Order, SL, TPBU);
                                        if (!trade.IsSuccessful)
                                        {
                                            Print("BUY Trailing Stop False! Error = ", trade.Error);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (Order.TradeType == TradeType.Sell)
                    {
                        if (Order.EntryPrice - Symbol.Ask > TStop)
                        {
                            if (Order.StopLoss > Symbol.Ask + (TStop + TStep) || Order.StopLoss == 0)
                            {
                                if (Order.TakeProfit == 0 || (Symbol.Bid - Order.TakeProfit) >= StopLevel)
                                {
                                    if (TP == 0)
                                    {
                                        TPBU = 0;
                                    }
                                    else
                                    {
                                        TPBU = Order.EntryPrice - TP;
                                    }
                                    SL = Math.Round(Symbol.Ask + TStop, Symbol.Digits);
                                    if (Order.StopLoss != SL)
                                    {
                                        var trade = ModifyPosition(Order, SL, TPBU);
                                        if (!trade.IsSuccessful)
                                        {
                                            Print("SELL Trailing Stop False! Error = ", trade.Error);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
        }

        int CountOtherOrders()
        {
            int count = 0;
            foreach (var Order in Positions)
            {

                if (Order.SymbolCode != Symbol.Code && Order.Label == Magic.ToString())
                    count++;

            }
            return (count);
        }

        private long GetVolume()
        {
            if (!PercentLot)
                return (volume);
            var vol = Account.FreeMargin * leverage;
            var vol2 = vol * Percent / 100;

            string sym = Symbol.Code;
            if (sym.Length >= 4 && Symbol.Code.Substring(0, 3) == "USD")
            {
                return (Symbol.NormalizeVolume(vol2));
            }
            if (sym.Length >= 4 && Symbol.Code.Substring(3, 3) == "USD")
            {

                return (Symbol.NormalizeVolume(vol2 / Symbol.Bid));
            }
            if (sym.Length >= 4 && !(Symbol.Code.Substring(0, 3) == "USD") && !(Symbol.Code.Substring(3, 3) == "USD"))
            {
                Symbol cross = MarketData.GetSymbol("USD" + Symbol.Code.Substring(3, 3));
                return (Symbol.NormalizeVolume(vol2 / Symbol.Bid / cross.Bid));
            }
            if (sym.Length < 4)
            {
                return (Symbol.NormalizeVolume(vol2 / Symbol.Bid));
            }
            return (Symbol.NormalizeVolume(vol2));

        }

        protected override void OnStop()
        {


            // Put your deinitialization logic here
        }
    }
}

 

This is the error on Natural Gas.


@Cyril Ndiga

PanagiotisCharalampous
08 Mar 2018, 14:27

Hi Cyril,

Your exceptions are thrown from the following two statements

1)

Symbol.Code.Substring(3, 3)

BRENT has only 5 characters so this statement will throw exception for BRENT.

2)

MarketData.GetSymbol("USD" + Symbol.Code.Substring(3, 3))

This will return "USD.GA". This is not a valid symbol. Please reconsider your GetVolume() function logic.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Cyril Ndiga
08 Mar 2018, 14:48

RE:

Panagiotis Charalampous said:

Hi Cyril,

Your exceptions are thrown from the following two statements

1)

Symbol.Code.Substring(3, 3)

BRENT has only 5 characters so this statement will throw exception for BRENT.

2)

MarketData.GetSymbol("USD" + Symbol.Code.Substring(3, 3))

This will return "USD.GA". This is not a valid symbol. Please reconsider your GetVolume() function logic.

Best Regards,

Panagiotis

Alright Thanks.

Letme look into that.


@Cyril Ndiga