problem

Created at 30 Sep 2020, 16:30
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!
LU

luca.tocchi

Joined 25.03.2020

problem
30 Sep 2020, 16:30


using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ema : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Stop Loss", Group = "Protection", DefaultValue = 15)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", Group = "Protection", DefaultValue = 30)]
        public int TakeProfit { get; set; }

        private ExponentialMovingAverage Ema4;
        private ExponentialMovingAverage Ema9;

        protected override void OnTick()
        {
            Ema4 = Indicators.ExponentialMovingAverage(Source, 4);
            Ema9 = Indicators.ExponentialMovingAverage(Source, 9);

            var longPosition = Positions.Find("Miaposizione", SymbolName, TradeType.Buy);
            var shortPosition = Positions.Find("Miaposizione", SymbolName, TradeType.Sell);

            if (longPosition == null && shortPosition == null)
            {
                if (Ema4.Result.HasCrossedAbove(Ema9.Result.LastValue, 0))
                {
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, 100000, "Miaposizione", StopLoss, TakeProfit);
                }
                else if (Ema4.Result.HasCrossedBelow(Ema9.Result.LastValue, 0))
                {
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, 100000, "Miaposizione", StopLoss, TakeProfit);
                }
            }
        }
    }
}

hi this is a simple example bot.
when the ema4 goes above the ema9 he goes buy while if the ema4 goes below the ema9 he goes sell.
the problem is that when the position takes the tp or the sl it opens a new one. I would like it to open only 1 in these conditions

thanks


@luca.tocchi
Replies

PanagiotisCharalampous
02 Oct 2020, 08:14

Hi Luca,

You can use a flag that will stop more positions from being placed until the conditions are reset again.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous