No ExecuteOrder if If there is already an open position

Created at 12 Aug 2022, 17:05
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!
CE

Cesare_Spani

Joined 12.08.2022

No ExecuteOrder if If there is already an open position
12 Aug 2022, 17:05


Hi, thanks in advance for your help.

In this code I have set the opening of a position if a certain cross condition occurs between the two SMAs.

Now, I would like that if a live market position is already open, a new one is not opened until the current one reaches the TP or SL.

How can I integrate the code then to say that it does not have to open more than one live position? Thank you

 

using System;
using System.Collections.Generic;
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 NewcBot2 : Robot
    {
        private TradeType Direction = TradeType.Buy;

        [Parameter("SMA1 Period")]
        public int PeriodsSma1 { get; set; }
        [Parameter("SMA2 Period")]
        public int PeriodsSma2 { get; set; }

      


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

        private SimpleMovingAverage _sma1 { get; set; }
        private SimpleMovingAverage _sma2 { get; set; }


        protected override void OnTick()
        {

            _sma1 = Indicators.SimpleMovingAverage(Source, PeriodsSma1);
            _sma2 = Indicators.SimpleMovingAverage(Source, PeriodsSma2);

            Print("Decimal ", Symbol.Digits);

            if (_sma1.Result.LastValue < _sma2.Result.LastValue)
            {
 Direction = TradeType.Buy;
 }
 else
{
Direction = TradeType.Sell;
}
ExecuteMarketOrder(Direction, SymbolName, Symbol.QuantityToVolumeInUnits(0.03), "CesareLive", 4, 4);
}

}

}


@Cesare_Spani
Replies

profitstreet.trade
14 Aug 2022, 18:31

Just Like this

using System;
using System.Collections.Generic;
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 NewcBot2 : Robot
    {
        private TradeType Direction = TradeType.Buy;

        [Parameter("SMA1 Period")]
        public int PeriodsSma1 { get; set; }
        [Parameter("SMA2 Period")]
        public int PeriodsSma2 { get; set; }

      


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

        private SimpleMovingAverage _sma1 { get; set; }
        private SimpleMovingAverage _sma2 { get; set; }


        protected override void OnTick()
        {

            _sma1 = Indicators.SimpleMovingAverage(Source, PeriodsSma1);
            _sma2 = Indicators.SimpleMovingAverage(Source, PeriodsSma2);

            Print("Decimal ", Symbol.Digits);

            if (_sma1.Result.LastValue < _sma2.Result.LastValue)
               {
                    Direction = TradeType.Buy;
                }
            else
                  {
                    Direction = TradeType.Sell;
                   }
                     if(Positions.FindAll("CesareLive", SymbolName,Direction).Length == 0)
                        {
                           ExecuteMarketOrder(Direction, SymbolName, Symbol.QuantityToVolumeInUnits(0.03), "CesareLive", 4, 4);
                        }
                 }

            }

}


@profitstreet.trade