help editing code

Created at 09 Aug 2019, 20:22
TR

tradewin87@gmail.com

Joined 09.08.2019

help editing code
09 Aug 2019, 20:22


Good day. Sorry for my english. There is a bot code that should open positions in the direction of the prevailing candle, bull or bear. In fact, I see that the bot opens the transaction the other way around. Those. when a bullish candle appears he sells; when a bearish candle he buys. This is true for Renko charts. Can anyone help with this? It is necessary for the bot to open buy transactions when a specified number of bullish candles is formed, sales transactions are vice versa. In addition, if possible, make a trailing stop in it. Thanks for the help and advice.

// -------------------------------------------------------------------------------
//
//    This is a Template used as a guideline to build your own Robot. 
//    Please use the “Feedback” tab to provide us with your suggestions about cAlgo’s API.
//
// -------------------------------------------------------------------------------

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

namespace cAlgo.Robots
{
    [Robot()]
    public class gr1200 : Robot
    {
        [Parameter("BarCount", DefaultValue = 4)]
        public int BarCount { get; set; }

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

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

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

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

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

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

        private int PosOpen = 0;
        private int OpenIndex = 0;
        private double StartBalanse;
        private DateTime dt;

        protected override void OnStart()
        {
            StartBalanse = Account.Balance;
            dt = Server.Time;
        }

        protected override void OnTick()
        {
            int last = MarketSeries.Close.Count - 1;
            if (!(MarketSeries.Open[last] == MarketSeries.High[last] && MarketSeries.Open[last] == MarketSeries.Low[last]))
                return;
            if (dt.Date != Server.Time.Date)
            {
                StartBalanse = Account.Balance;
                dt = Server.Time;
            }

            double bp = (StartBalanse - Account.Balance) / (StartBalanse / 100);
            if (bp > 0 && bp >= MaxDropDown && MaxDropDown != 0)
                return;
            if (bp < 0 && Math.Abs(bp) >= MaxProfit && MaxProfit != 0)
                return;
            if (BarCount < 1)
            {
                Print("Мало баров для анализа тренда. BarCount должно быть больше или равно 1");
                return;
            }
            if (PosOpen < MaxOrders)
            {
                if (OpenIndex == 0 || last - OpenIndex > BarCount)
                {
                    if (IsBuy(last))
                    {
                        Trade.CreateBuyMarketOrder(Symbol, Volume);
                        PosOpen++;
                        OpenIndex = last;
                    }
                    if (IsSell(last))
                    {
                        Trade.CreateSellMarketOrder(Symbol, Volume);
                        PosOpen++;
                        OpenIndex = last;
                    }
                }
            }
        }

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

        protected override void OnPositionOpened(Position openedPosition)
        {
            if (openedPosition.TradeType == TradeType.Buy)
                Trade.ModifyPosition(openedPosition, Symbol.Ask - StopLoss * Symbol.PointSize, Symbol.Ask + TakeProfit * Symbol.PointSize);
            if (openedPosition.TradeType == TradeType.Sell)
                Trade.ModifyPosition(openedPosition, Symbol.Bid + StopLoss * Symbol.PointSize, Symbol.Bid - TakeProfit * Symbol.PointSize);
        }

        protected override void OnPositionClosed(Position position)
        {
            PosOpen--;
            if (PosOpen < 0)
                PosOpen = 0;
        }

        private bool IsBuy(int last)
        {

            for (int i = BarCount; i > 0; i--)
            {
                if (MarketSeries.Open[last - i] < MarketSeries.Close[last - i])
                    return false;
                if (i < 2)
                    continue;
                if (MarketSeries.High[last - i] > MarketSeries.High[last - i - 1])
                    return false;
            }
            return true;
        }

        private bool IsSell(int last)
        {

            for (int i = BarCount; i > 0; i--)
            {
                if (MarketSeries.Open[last - i] > MarketSeries.Close[last - i])
                    return false;
                if (i < 2)
                    continue;
                if (MarketSeries.Low[last - i] < MarketSeries.Low[last - i - 1])
                    return false;
            }
            return true;
        }
    }
}


@tradewin87@gmail.com
Replies

PanagiotisCharalampous
12 Aug 2019, 09:13

Hi,

Thanks for posting in our forum. If you are sure the cBot works the other way round than the way you wish it to work, just switch the order placement orders in the following code part

                    if (IsBuy(last))
                    {
                        Trade.CreateBuyMarketOrder(Symbol, Volume);
                        PosOpen++;
                        OpenIndex = last;
                    }
                    if (IsSell(last))
                    {
                        Trade.CreateSellMarketOrder(Symbol, Volume);
                        PosOpen++;
                        OpenIndex = last;
                    }

To add a trailing stop loss, just set the relevant parameter to true in the ExecuteMarketOrder function.

Best Regards,

Panagiotis


@PanagiotisCharalampous