Crashed OnBar

Created at 14 Oct 2014, 21:36
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!
JA

jagtrading

Joined 01.10.2014

Crashed OnBar
14 Oct 2014, 21:36


Ok I'm sorry to spam the forum but I really need help with this. I was told to contact partners but I refuse to pay for coding help for such a simple language. Can someone please tell me whats wrong with this code?? It crashes OnBar for some reason and i dno why. I've tried debugging it but can't figure out whats wrong with it.

 

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 stochasticbollinger : Robot
    {
        [Parameter("EMA_period", DefaultValue = 5)]
        public int EMA_Period { get; set; }

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

        [Parameter("BollingerDeviation", DefaultValue = 1)]
        public double Bol_Deviation { get; set; }

        [Parameter("StochasticPeriod", DefaultValue = 25)]
        public int Stoch_Period { get; set; }

        [Parameter("StochasticSlowing", DefaultValue = 50)]
        public int Stoch_Slowing { get; set; }

        [Parameter("BollingerStochDev", DefaultValue = 25)]
        public double BolStoch_Deviation { get; set; }

        [Parameter("BollingerStochPeriod", DefaultValue = 1)]
        public int BolStoch_Period { get; set; }

        [Parameter("ATRperiod", DefaultValue = 14)]
        public int ATR_PERIOD { get; set; }

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

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

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

        [Parameter("Margin", DefaultValue = 1.0)]
        public int _Margin { get; set; }

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

        private int p_volume;
        private string BuyLabel = "Buy Position";
        private string SellLabel = "Sell Position";
        private BollingerBands Boll_Bands;
        private BollingerBands BollStoch_Bands;
        private StochasticOscillator Stoch_;
        private AverageTrueRange ATR_;
        private ExponentialMovingAverage MAone_;



        protected override void OnStart()
        {
            p_volume = _Margin * 10000;

            Boll_Bands = Indicators.BollingerBands(MarketSeries.Close, Bol_Period, Bol_Deviation, MovingAverageType.Simple);
            Stoch_ = Indicators.StochasticOscillator(Stoch_Period, Stoch_Slowing, 1, MovingAverageType.Simple);
            BollStoch_Bands = Indicators.BollingerBands(Stoch_.PercentK, BolStoch_Period, BolStoch_Deviation, MovingAverageType.Simple);
            ATR_ = Indicators.AverageTrueRange(ATR_PERIOD, MovingAverageType.Simple);
            MAone_ = Indicators.ExponentialMovingAverage(MarketSeries.Close, EMA_Period);
        }

        protected override void OnBar()
        {
            var SellPositions = Positions.Find(SellLabel, Symbol, TradeType.Sell);

            var BuyPositions = Positions.Find(BuyLabel, Symbol, TradeType.Buy);

            var ATRTP = ATR_TAKEPROFIT * ATR_.Result.LastValue * 10000;

            var ATRSL = ATR_STOPLOSS * ATR_.Result.LastValue * 10000;



            if ((Positions.Count == 0) && (MAone_.Result.HasCrossedAbove(Boll_Bands.Bottom.LastValue, CandlePeriod)) && (Stoch_.PercentK.LastValue > BollStoch_Bands.Bottom.LastValue))
            {
                ClosePosition(SellPositions);
                ExecuteMarketOrder(TradeType.Buy, Symbol, p_volume, BuyLabel, ATRSL, ATRTP, Slippage);
            }

            else if ((Positions.Count == 0) && (MAone_.Result.HasCrossedBelow(Boll_Bands.Top.LastValue, CandlePeriod)) && (Stoch_.PercentK.LastValue < BollStoch_Bands.Top.LastValue))
            {
                ClosePosition(BuyPositions);
                ExecuteMarketOrder(TradeType.Sell, Symbol, p_volume, SellLabel, ATRSL, ATRTP, Slippage);
            }


            else if (Positions.Count == 1)
            {
                if (Positions.Find(BuyLabel) != null)
                {
                    ModifyPosition(BuyPositions, ATRSL, ATRTP);
                }
                else if (Positions.Find(SellLabel) != null)
                {
                    ModifyPosition(SellPositions, ATRSL, ATRTP);
                }
            }



        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 


@jagtrading
Replies

emeeder
14 Oct 2014, 22:29

I copy and pasted your robot into Calgo.

It doesnt crash and it finishes backtesting with no problems.

But i cant get it to trade at all. 1m, 5 m, 1h, 4h

What time frame are you running it on? and are the default settings set at tradeable levels?