change sample trend cbot

Created at 23 Mar 2020, 08:53
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!
DA

dannyilumi

Joined 20.03.2020

change sample trend cbot
23 Mar 2020, 08:53


Hello i need help to change the sample trend cbot code not to place a sell trade after a buy exit and not to place a buy after a sell exit


@dannyilumi
Replies

PanagiotisCharalampous
23 Mar 2020, 09:35

Hi dannyilumi,

What should it do instead?

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

dannyilumi
24 Mar 2020, 03:07

RE:

PanagiotisCharalampous said:

Hi dannyilumi,

What should it do instead?

Best Regards,

Panagiotis 

Join us on Telegram

 

Hello ,when the sample trend cbot open,s a buy trade on the moving average cross over and closes on the cross over on the down side I don't want it to open a sell ,The same for a cross over for a sell I don't want it to open a buy but just close the sell


@dannyilumi

PanagiotisCharalampous
24 Mar 2020, 08:44

Hi dannyilumi,

See below an example

// -------------------------------------------------------------------------------------------------
//
//    This code is a cTrader Automate API example.
//
//    This cBot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk.
//    
//    All changes to this file might be lost on the next application update.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
//    The "Sample Trend cBot" will buy when fast period moving average crosses the slow period moving average and sell when 
//    the fast period moving average crosses the slow period moving average. The orders are closed when an opposite signal 
//    is generated. There can only by one Buy or Sell order at any time.
//
// -------------------------------------------------------------------------------------------------

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 SampleTrendcBot : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("MA Type", Group = "Moving Average")]
        public MovingAverageType MAType { get; set; }

        [Parameter("Source", Group = "Moving Average")]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Slow Periods", Group = "Moving Average", DefaultValue = 10)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", Group = "Moving Average", DefaultValue = 5)]
        public int FastPeriods { get; set; }


        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Sample Trend cBot";
        private bool _buyOpened;
        private bool _sellOpened;

        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }

        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, SymbolName, TradeType.Buy);
            var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);

            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)
            {
                if (shortPosition != null)
                    ClosePosition(shortPosition);
                if (!_buyOpened)
                {
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label);
                    _buyOpened = true;
                }
            }
            else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
            {
                if (longPosition != null)
                    ClosePosition(longPosition);
                if (!_sellOpened)
                {
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, label);
                    _sellOpened = true;
                }
            }
        }

        private double VolumeInUnits
        {
            get { return Symbol.QuantityToVolumeInUnits(Quantity); }
        }
    }
}

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

dannyilumi
06 Apr 2020, 08:14

cbot trend sample

Hello, thanks for the cbot trend code, I was typing this code to for a new cbot but i couldnt find where the on stop method would begin, if its ok could you please specify that

 method 


@dannyilumi