Topics
Replies

netmstnet
03 Feb 2020, 15:20

RE:

PanagiotisCharalampous said:

Hi netmstnet,

You can try this condition

            if (Positions.First().NetProfit > AverageTakeProfit)
                Positions.First().Close();

Best Regards,

Panagiotis 

Join us on Telegram

You mean like this:

if ((Positions.First().NetProfit+ Positions.Last().NetProfit) > AverageTakeProfit)
                Positions.First().Close();
                Positions.Last().Close();

 


@netmstnet

netmstnet
03 Feb 2020, 14:24

RE:

PanagiotisCharalampous said:

Hi netmstnet,

You can use First() and Last() functions. See below an example

Positions.First().Close();

Best Regards,

Panagiotis 

Join us on Telegram

 

hm, i think need more help, here is the code:

// Close all buy positions if all buy positions' target profit is met
            if (Positions.Count(x => x.TradeType == TradeType.Buy && x.SymbolName == SymbolName && x.Label == ThiscBotLabel) >= 2)
            {
               
                if (Positions.Where(x => x.TradeType == TradeType.Buy && x.SymbolName == SymbolName && x.Label == ThiscBotLabel).Average(x => x.NetProfit) >= FirstVolume * AverageTakeProfit * Symbol.PipSize)
                {
                    foreach (var position in Positions)
                    {
                        if (position.TradeType == TradeType.Buy && position.SymbolName == SymbolName && position.Label == ThiscBotLabel)
                            ClosePosition(position);
                    }
                }
            }
            // Close all sell positions if all sell positions' target profit is met
            if (Positions.Count(x => x.TradeType == TradeType.Sell && x.SymbolName == SymbolName && x.Label == ThiscBotLabel) >= 2)
            {
                if (Positions.Where(x => x.TradeType == TradeType.Sell && x.SymbolName == SymbolName && x.Label == ThiscBotLabel).Average(x => x.NetProfit) >= FirstVolume * AverageTakeProfit * Symbol.PipSize)
                {
                    foreach (var position in Positions)
                    {
                        if (position.TradeType == TradeType.Sell && position.SymbolName == SymbolName && position.Label == ThiscBotLabel)
                            ClosePosition(position);
                    }
                }
            }

 

i need to select first and last positions, then calculate if  proift of this positions NetProfit > AverageTakeProfit, close first and last.

p.s. sorry my english


@netmstnet

netmstnet
03 Feb 2020, 14:06

RE:

PanagiotisCharalampous said:

Hi netmstnet,

You can use First() and Last() functions. See below an example

Positions.First().Close();

Best Regards,

Panagiotis 

Join us on Telegram

 

Thank you, Panagiotis.


@netmstnet

netmstnet
27 Jan 2020, 12:10

RE: RE:

leonardohurtado said:

lucian said:

Start with this code:

 

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 EMACross_RSI : Robot
    {
        [Parameter("Source")]
        public DataSeries SourceSeries { get; set; }
        [Parameter("Label", DefaultValue = "EMA")]
        public string label { get; set; }

        [Parameter("Slow Periods", DefaultValue = 30)]
        public int SlowPeriods { get; set; }
        [Parameter("Medium Periods", DefaultValue = 12)]
        public int MediumPeriods { get; set; }
        [Parameter("Fast Periods", DefaultValue = 5)]
        public int FastPeriods { get; set; }

        [Parameter("Stop Loss", DefaultValue = 10)]
        public int SL { get; set; }
        [Parameter("Take Profit", DefaultValue = 10)]
        public double TP { get; set; }
        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        private ExponentialMovingAverage slowMa;
        private ExponentialMovingAverage mediumMa;
        private ExponentialMovingAverage fastMa;


        protected override void OnStart()
        {

            fastMa = Indicators.ExponentialMovingAverage(SourceSeries, FastPeriods);
            mediumMa = Indicators.ExponentialMovingAverage(SourceSeries, MediumPeriods);
            slowMa = Indicators.ExponentialMovingAverage(SourceSeries, SlowPeriods);

        }

        protected override void OnBar()
        {
            int index = MarketSeries.OpenTime.Count - 2;

            if ((fastMa.Result[index] > slowMa.Result[index]) && (mediumMa.Result[index] > slowMa.Result[index]) && (fastMa.Result[index - 1] < slowMa.Result[index - 1]) && (mediumMa.Result[index - 1] < slowMa.Result[index - 1]))
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label, SL, TP);

            }
            else if ((fastMa.Result[index] < slowMa.Result[index]) && (mediumMa.Result[index] < slowMa.Result[index]) && (fastMa.Result[index - 1] > slowMa.Result[index - 1]) && (mediumMa.Result[index - 1] > slowMa.Result[index - 1]))
            {

                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label, SL, TP);

            }

        }
        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }
    }
}

Also you can download 3xEMA Indicator

Hello Lucian,

Thanks a lot for this coding, I wanted to ask you if there is a way of making it so it only takes 1 position at a time? Meaning, it will close the current position when the opposite signal happens.

I am not a coder, so if you could help me that would be super kind and appreciated.

Warm regards, Leonardo.

Try this:

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 EMACross_RSI : Robot
    {
        [Parameter("Source")]
        public DataSeries SourceSeries { get; set; }
        [Parameter("Label", DefaultValue = "EMA")]
        public string label { get; set; }

        [Parameter("Slow Periods", DefaultValue = 30)]
        public int SlowPeriods { get; set; }
        [Parameter("Medium Periods", DefaultValue = 12)]
        public int MediumPeriods { get; set; }
        [Parameter("Fast Periods", DefaultValue = 5)]
        public int FastPeriods { get; set; }

        [Parameter("Stop Loss", DefaultValue = 10)]
        public int SL { get; set; }
        [Parameter("Take Profit", DefaultValue = 10)]
        public double TP { get; set; }
        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        private ExponentialMovingAverage slowMa;
        private ExponentialMovingAverage mediumMa;
        private ExponentialMovingAverage fastMa;


        protected override void OnStart()
        {

            fastMa = Indicators.ExponentialMovingAverage(SourceSeries, FastPeriods);
            mediumMa = Indicators.ExponentialMovingAverage(SourceSeries, MediumPeriods);
            slowMa = Indicators.ExponentialMovingAverage(SourceSeries, SlowPeriods);

        }


        protected override void OnBar()
        {
            int index = MarketSeries.OpenTime.Count - 2;
            var longPosition = Positions.Find(label, SymbolName, TradeType.Buy);
            var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);

            if ((fastMa.Result[index] > slowMa.Result[index]) && (mediumMa.Result[index] > slowMa.Result[index]) && (fastMa.Result[index - 1] < slowMa.Result[index - 1]) && (mediumMa.Result[index - 1] < slowMa.Result[index - 1]) && shortPosition == null)
            {
                if (longPosition != null)
                    ClosePosition(longPosition);

                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label, SL, TP);

            }
            else if ((fastMa.Result[index] < slowMa.Result[index]) && (mediumMa.Result[index] < slowMa.Result[index]) && (fastMa.Result[index - 1] > slowMa.Result[index - 1]) && (mediumMa.Result[index - 1] > slowMa.Result[index - 1]) && longPosition == null)
            {

                if (shortPosition != null)
                    ClosePosition(shortPosition);
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label, SL, TP);

            }

        }
        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }
    }
}

 


@netmstnet