Crashed in Calculate with SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Created at 27 Mar 2019, 10:48
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!
FI

firemyst

Joined 26.03.2019

Crashed in Calculate with SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
27 Mar 2019, 10:48


Hi everyone:

I am receiving the error message:

"Crashed in Calculate with SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."

on this bot when I backtest against EURUSD on H1 timeframe from Jan 24th - March 23 as shown below. It appears to happen when I call the "IsRising()" or "IsFalling()" functions, but I don't know why? 

Since we can't easily debug Indicators, is anyone able to help/suggest what's causing the issue and how to resolve it?

Code and screen capture below:

Crash with Security violation

Bot code:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.WAustraliaStandardTime, AccessRights = AccessRights.None)]
    public class MySuperProfitcBot : Robot
    {
        //Specific to this robot
        [Parameter("Position Label", DefaultValue = "My Label")]
        public string MyLabel { get; set; }
        [Parameter("Stop Loss (Pips)", DefaultValue = 20.0)]
        public double StopLossInPips { get; set; }

        //For the SuperProfit Indicator
        [Parameter("Super Profit DllPeriod", DefaultValue = 35)]
        public int SuperProfitDllPeriod { get; set; }
        [Parameter("Super Profit Period", DefaultValue = 1.7)]
        public double SuperProfitPeriod { get; set; }
        [Parameter("Super Profit Moving Average Type", DefaultValue = MovingAverageType.Weighted)]
        public MovingAverageType SuperProfitMaType { get; set; }

        private SuperProfit _superProfit = new SuperProfit();

        protected override void OnStart()
        {
            // Put your initialization logic here
            MarketSeries data = MarketData.GetSeries(TimeFrame.Hour);
            DataSeries series = data.Close;

            //indicators
            _superProfit = Indicators.GetIndicator<SuperProfit>(SuperProfitDllPeriod, SuperProfitPeriod, SuperProfitMaType, series, (int)StopLossInPips, 20);

            Print("Starting SuperProfit Bot");
        }

        protected override void OnTick()
        {
            Print((_superProfit.UpSeries.IsFalling()).GetType());
            //if (_superProfit.UpSeries.IsRising())
            //    Print("hello");
            //if (_superProfit.UpSeries.IsFalling())
            //    Print("hello2");
            //if (_superProfit.DownSeries.IsRising())
            //    Print("hello3");
            //if (_superProfit.DownSeries.IsFalling())
            //    Print("hello4");

            if (SumOfShortEntryConditions() >= 5)
            {
                Print("Short Didn't crash!");
            }
            else
            {
                Print("Short Still hasn't crashed!");
            }
            if (SumOfLongEntryConditions() >= 5)
            {
                Print("Long Didn't crash!");
            }
            else
            {
                Print("Long Still hasn't crashed!");
            }
        }

        private int SumOfLongEntryConditions()
        {
            int _sumOfLongEntryConditions = 0;

            if (_superProfit.UpSeries.IsRising())
            {
                _sumOfLongEntryConditions += 1;
            }

            return _sumOfLongEntryConditions;
        }

        private int SumOfShortEntryConditions()
        {
            int _sumOfShortEntryConditions = 0;

            if (_superProfit.DownSeries.IsFalling())
            {
                _sumOfShortEntryConditions += 1;
            }

            return _sumOfShortEntryConditions;
        }

    }
}

Indicator code:

using System;
using cAlgo.API;
using System.Runtime.InteropServices;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SuperProfit : Indicator
    {
        // Alert
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

        [Parameter(DefaultValue = 35)]
        public int DllPeriod { get; set; }

        [Parameter(DefaultValue = 1.7)]
        public double Period { get; set; }

        [Parameter(DefaultValue = MovingAverageType.Weighted)]
        public MovingAverageType MaType { get; set; }

        [Parameter()]
        public DataSeries Price { get; set; }

        [Parameter(DefaultValue = 5)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 20)]
        public int TakeProfit { get; set; }

        [Output("Up", PlotType = PlotType.DiscontinuousLine, Color = Colors.Green, Thickness = 5)]
        public IndicatorDataSeries UpSeries { get; set; }

        [Output("Down", PlotType = PlotType.DiscontinuousLine, Color = Colors.Red, Thickness = 5)]
        public IndicatorDataSeries DownSeries { get; set; }


        private DateTime _openTime;

        private MovingAverage _movingAverage1;
        private MovingAverage _movingAverage2;
        private MovingAverage _movingAverage3;
        private IndicatorDataSeries _dataSeries;
        private IndicatorDataSeries _trend;


        protected override void Initialize()
        {
            _dataSeries = CreateDataSeries();
            _trend = CreateDataSeries();

            var period1 = (int)Math.Floor(DllPeriod / Period);
            var period2 = (int)Math.Floor(Math.Sqrt(DllPeriod));

            _movingAverage1 = Indicators.MovingAverage(Price, period1, MaType);
            _movingAverage2 = Indicators.MovingAverage(Price, DllPeriod, MaType);
            _movingAverage3 = Indicators.MovingAverage(_dataSeries, period2, MaType);

        }

        public override void Calculate(int index)
        {
            if (index < 1)
                return;

            _dataSeries[index] = 2.0 * _movingAverage1.Result[index] - _movingAverage2.Result[index];
            _trend[index] = _trend[index - 1];

            if (_movingAverage3.Result[index] > _movingAverage3.Result[index - 1])
                _trend[index] = 1;
            else if (_movingAverage3.Result[index] < _movingAverage3.Result[index - 1])
                _trend[index] = -1;

            if (_trend[index] > 0)
            {
                UpSeries[index] = _movingAverage3.Result[index];

                if (_trend[index - 1] < 0.0)
                {
                    UpSeries[index - 1] = _movingAverage3.Result[index - 1];

                    if (IsLastBar)
                    {
                        var stopLoss = MarketSeries.Low[index - 1] - StopLoss * Symbol.PipSize;
                        var takeProfit = MarketSeries.Close[index] + TakeProfit * Symbol.PipSize;
                        var entryPrice = MarketSeries.Close[index - 1];

                        if (MarketSeries.OpenTime[index] != _openTime)
                        {
                            _openTime = MarketSeries.OpenTime[index];
                            DisplayAlert("Buy signal", takeProfit, stopLoss, entryPrice);
                        }
                    }
                }

                DownSeries[index] = double.NaN;
            }
            else if (_trend[index] < 0)
            {
                DownSeries[index] = _movingAverage3.Result[index];

                if (_trend[index - 1] > 0.0)
                {
                    DownSeries[index - 1] = _movingAverage3.Result[index - 1];

                    if (IsLastBar)
                    {
                        var stopLoss = MarketSeries.High[index - 1] + StopLoss * Symbol.PipSize;
                        var takeProfit = MarketSeries.Close[index] - TakeProfit * Symbol.PipSize;
                        var entryPrice = MarketSeries.Close[index - 1];

                        if (MarketSeries.OpenTime[index] != _openTime)
                        {
                            _openTime = MarketSeries.OpenTime[index];
                            DisplayAlert("Sell signal", takeProfit, stopLoss, entryPrice);
                        }
                    }
                }

                UpSeries[index] = double.NaN;
            }

        }

        protected void DisplayAlert(string tradyTypeSignal, double takeProfit, double stopLoss, double entryPrice)
        {
            string entryPricetext = entryPrice != 0.0 ? string.Format(" at price {0}", Math.Round(entryPrice, 4)) : "";
            string takeProfitText = takeProfit != 0.0 ? string.Format(", TP on  {0}", Math.Round(takeProfit, 4)) : "";
            string stopLossText = stopLoss != 0.0 ? string.Format(", SL on {0}", Math.Round(stopLoss, 4)) : "";

            var alertMessage = string.Format("{0} {1} {2} {3} {4}", tradyTypeSignal, entryPricetext, takeProfitText, stopLossText, Symbol.Code);

            MessageBox(new IntPtr(0), alertMessage, "Trade Signal", 0);

        }
    }
}

 

Thank you :-)

 

 


@firemyst
Replies

PanagiotisCharalampous
27 Mar 2019, 11:24

Hi FireMyst,

Try changing your cBot and indicator access rights e.g. try AccessRights = AccessRights.FullAccess.

Best Regards,

Panagiotis


@PanagiotisCharalampous

firemyst
28 Mar 2019, 01:39

RE:

Panagiotis Charalampous said:

Hi FireMyst,

Try changing your cBot and indicator access rights e.g. try AccessRights = AccessRights.FullAccess.

Best Regards,

Panagiotis

Thank you! That was the issue exactly. Except, in this case, I only had to change the access rights on the bot, and not the indicator.

Funnily enough, after your response, I found the reference and guide online for access rights. Here it currently is for everyone else's reference:

https://ctrader.com/api/guides/access_rights

 


@firemyst