Replies

enoque_santos
04 Nov 2019, 21:56

Panagiotis,

Thank you,

Regards,

Enoque.


@enoque_santos

enoque_santos
22 Oct 2019, 05:34

RE:

Panagiotis Charalampous said:

Hi enoque_santos,

The problem occurs because you are trying to access a property of the object that is returned by the Find() function without checking if the function actually returns a value. For example below

            if (Positions.Find("buyPosition00").Pips >= TakeProfitInPips)
            {
                CloseAllPositions();
            }

To fix this you need to make the relevant checks wherever this is applicable. See below an example

            if (Positions.Find("buyPosition00") != null && Positions.Find("buyPosition00").Pips >= TakeProfitInPips)
            {
                CloseAllPositions();
            }

Best Regards,

Panagiotis

Hello, Panagiotis,

The error was fixed; thank you very much !!!

Best Regards,

Enoque.


@enoque_santos

enoque_santos
18 Oct 2019, 17:58

Sure.  Here you are.  Thank you.

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class hml_Buy_OnTimer_Jaboke_Backtest : Robot
    {
        [Parameter("Stop Loss (pips)", DefaultValue = 60)]
        public int StopLossInPips { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 30)]
        public int TakeProfitInPips { get; set; }

        [Parameter("ZonaDePerigo (pips)", DefaultValue = 30)]
        public int ZonaDePerigo { get; set; }

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

        [Parameter("Volume (Lots)", DefaultValue = 1000)]
        public double Volume { get; set; }

        protected override void OnStart()
        {
            Timer.Start(TimeSpan.FromMilliseconds(100));
        }

        protected override void OnTimer()
        {
            if (variavelDeControle == 0 || variavelDeControle == 1)
            {
                trataEntrada01();
            }

            if (variavelDeControle == 2 || variavelDeControle == 3)
            {
                trataEntrada02();
            }

            if (variavelDeControle == 4 || variavelDeControle == 5)
            {
                trataEntrada03();
            }

            if (variavelDeControle == 6 || variavelDeControle == 7)
            {
                trataEntrada04();
            }

            if (variavelDeControle == 8 || variavelDeControle == 9)
            {
                trataEntrada05();
            }

            if (variavelDeControle == 10 || variavelDeControle == 11)
            {
                trataEntrada06();
            }
        }

        private void trataEntrada01()
        {
            if (variavelDeControle == 0)
            {
                var buyPosition00 = ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "buyPosition00", StopLossInPips, TakeProfitInPips);
                if (!buyPosition00.IsSuccessful)
                {
                    CloseAllPositions();
                }
                variavelDeControle = 1;
            }

            if (Positions.Find("buyPosition00").Pips >= TakeProfitInPips)
            {
                CloseAllPositions();
            }
            else
            {
                if (Positions.Find("buyPosition00").Pips <= -ZonaDePerigo)
                {
                    Volume = Volume * 3;
                    variavelDeControle = 2;
                }
            }
        }

        private void trataEntrada02()
        {
            if (variavelDeControle == 2)
            {
                var sellPosition01 = ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "sellPosition01", StopLossInPips, TakeProfitInPips);
                if (!sellPosition01.IsSuccessful)
                {
                    CloseAllPositions();
                }
                variavelDeControle = 3;
            }

            if (Positions.Find("sellPosition01").Pips >= TakeProfitInPips)
            {
                CloseAllPositions();
            }
            else
            {
                if (Positions.Find("sellPosition01").Pips <= -ZonaDePerigo)
                {
                    Volume = Volume * 2;
                    variavelDeControle = 4;
                }
            }
        }

        private void trataEntrada03()
        {
            if (variavelDeControle == 4)
            {
                var buyPosition02 = ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "buyPosition02", StopLossInPips, TakeProfitInPips);
                if (!buyPosition02.IsSuccessful)
                {
                    CloseAllPositions();
                }
                variavelDeControle = 5;
            }

            if (Positions.Find("buyPosition02").Pips >= TakeProfitInPips)
            {
                CloseAllPositions();
            }
            else
            {
                if (Positions.Find("buyPosition02").Pips <= -ZonaDePerigo)
                {
                    Volume = Volume * 2;
                    variavelDeControle = 6;
                }
            }
        }

        private void trataEntrada04()
        {
            if (variavelDeControle == 6)
            {
                var sellPosition02 = ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "sellPosition02", StopLossInPips, TakeProfitInPips);
                if (!sellPosition02.IsSuccessful)
                {
                    CloseAllPositions();
                }
                variavelDeControle = 7;
            }

            if (Positions.Find("sellPosition02").Pips >= TakeProfitInPips)
            {
                CloseAllPositions();
            }
            else
            {
                if (Positions.Find("sellPosition02").Pips <= -ZonaDePerigo)
                {
                    Volume = Volume * 2;
                    variavelDeControle = 8;
                }
            }

        }

        private void trataEntrada05()
        {
            if (variavelDeControle == 8)
            {
                var buyPosition03 = ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "buyPosition03", StopLossInPips, TakeProfitInPips);
                if (!buyPosition03.IsSuccessful)
                {
                    CloseAllPositions();
                }
                variavelDeControle = 9;
            }

            if (Positions.Find("buyPosition03").Pips >= TakeProfitInPips)
            {
                CloseAllPositions();
            }
            else
            {
                if (Positions.Find("buyPosition03").Pips <= -ZonaDePerigo)
                {
                    Volume = Volume * 2;
                    variavelDeControle = 10;
                }
            }
        }

        private void trataEntrada06()
        {
            if (variavelDeControle == 10)
            {
                var sellPosition03 = ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "sellPosition03", StopLossInPips, TakeProfitInPips);
                if (!sellPosition03.IsSuccessful)
                {
                    CloseAllPositions();
                }
                variavelDeControle = 11;
            }

            if (Positions.Find("sellPosition03").Pips >= TakeProfitInPips)
            {
                CloseAllPositions();
            }
            else
            {
                if (Positions.Find("sellPosition03").Pips <= -ZonaDePerigo)
                {
                    variavelDeControle = 12;
                }
            }
        }

        private void CloseAllPositions()
        {
            foreach (var p in Positions)
            {
                if (p != null)
                {
                    ClosePositionAsync(p);
                }
            }
            variavelDeControle = 0;
        }

        protected override void OnTick()
        {
        }

        protected override void OnStop()
        {
        }
    }
}


@enoque_santos