Pending order bot. 21 Error ? How fix ?

Created at 28 Nov 2019, 00:35
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!
TG

tgjobscv

Joined 05.12.2018

Pending order bot. 21 Error ? How fix ?
28 Nov 2019, 00:35


WilliamsPctR and HistoricalVolatility 

How improve this bot ?

 

using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot()]
    public class Uchechukwu : Robot
    {

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

        [Parameter(DefaultValue = 100)]
        public int WprPeriod { get; set; }

        [Parameter("HV Period", DefaultValue = 14)]
        public int HVPeriod { get; set; }

        [Parameter("HV Barhistory", DefaultValue = 400)]
        public int HVBarhistory { get; set; }

        [Parameter(DefaultValue = 2)]
        public int HVD { get; set; }

        [Parameter(DefaultValue = 100000)]
        public int Volume { get; set; }

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

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

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

        [Parameter("MaxLoss", DefaultValue = -500.0)]
        public double MaxLoss { get; set; }


        private Position position;
        private PendingOrder pendingorder;
        private HistoricalVolatility _Hvol;
        private WilliamsPctR _Wp;

        ///
        /// Initialize Indicators
        ///
        protected override void OnStart()
        {
            _Hvol = Indicators.HistoricalVolatility(Source, HVPeriod, HVBarhistory, HVD);
            _Wp = Indicators.WilliamsPctR(WprPeriod);

        }
        protected override void OnTick()
        {
            if (Trade.IsExecuting)
                return;

            double Hvol = _Hvol.Result.LastValue;
            double Wp = _Wp.Result.LastValue;
            double Time = Server.Time.Hour;


            if (Time >= 2 && Time <= 23 && Hvol >= 0.003 && Hvol <= 0.02 && Wp < -80 && position == null)
            {
                if (pendingorder == null)
                {
                    Trade.CreateBuyStopOrder(Symbol, Volume, Symbol.Bid, Symbol.Bid - Symbol.PipSize * StopLoss, null, Server.Time.AddSeconds(20));
                }

            }
            if (Time >= 2 && Time <= 23 && Hvol >= 0.003 && Hvol <= 0.02 && Wp > -20 && position == null)
            {
                if (pendingorder == null)
                {
                    Trade.CreateSellStopOrder(Symbol, Volume, Symbol.Ask, Symbol.Ask + Symbol.PipSize * StopLoss, null, Server.Time.AddSeconds(20));
                }

            }


            if (position == null)
                return;
            {
                if (position != null && position.TradeType == TradeType.Sell)
                {
                    double distance = position.EntryPrice - Symbol.Ask;

                    if (distance > Trigger * Symbol.PipSize)
                    {
                        double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;

                        if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                        {
                            Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                        }
                    }
                }
                else
                {
                    double distance = Symbol.Bid - position.EntryPrice;

                    if (distance > Trigger * Symbol.PipSize)
                    {
                        double newStopLossPrice = Symbol.Bid - TrailingStop * Symbol.PipSize;
                        if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                        {
                            Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                        }
                    }
                }
            }
        }

        protected override void OnPendingOrderCreated(PendingOrder newOrder)
        {
            pendingorder = newOrder;

            if (pendingorder != null && pendingorder.SymbolCode == Symbol.Code)
            {
                if (position != null && position.SymbolCode == Symbol.Code || Symbol.Spread > 2 || (pendingorder.TargetPrice - Symbol.Bid) * Symbol.PipSize > 2 || (Symbol.Ask - pendingorder.TargetPrice) * Symbol.PipSize > 2)
                {
                    Trade.DeletePendingOrder(pendingorder);
                }

            }
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            position = openedPosition;

            if (position != null && position.GrossProfit < MaxLoss)
            {
                Trade.Close(position);
            }
        }
    }
}

 


@tgjobscv
Replies

PanagiotisCharalampous
28 Nov 2019, 08:58

Hi tgjobscv,

There is only one issue, in line 52

_Hvol = Indicators.HistoricalVolatility(Source, HVPeriod, HVBarhistory, HVD);

This indicator takes only three parameters but you are passing four.

Best Regards,

Panagiotis


@PanagiotisCharalampous

tgjobscv
28 Nov 2019, 20:17

thanks. now ok 


@tgjobscv

tgjobscv
29 Nov 2019, 00:20

WilliamsPctR and HistoricalVolatility 

How improve this bot ?


@tgjobscv