Check if position with specific label is closed and never open one with that label again.

Created at 03 Jan 2023, 09:19
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!
CT

ctid2434759

Joined 02.08.2022

Check if position with specific label is closed and never open one with that label again.
03 Jan 2023, 09:19


Hi there,

This code is simplified for solving this specific problem.

I want to check if a position with the Label of "X" has closed.
If it has closed I want to add that into my if statement so that another one doesn't open again.

Something like this?
 if (CheckPositions < 2 && currentSymbolOrderLabel < 1 && PositionClosed.Label = "X" < 1 ) ??

So if there is less than 2 total open positions and less than one of the label "X" and the label "X" position has not already closed then ExecuteMarketRangeOrder.

Aka, if position Label "X" has closed do NOT ExecuteMarketRangeOrder for "X" again..

Thanks
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.SingaporeStandardTime, AccessRights = AccessRights.None)]
    public class ClosePositionOnTime : Robot
    
    

    
    {

        // Just the current simulator pairs global variables.
        private double _highPrice;
        private double _lmtPrice;


        protected override void OnStart()
        {

            Positions.Closed += PositionsOnClosed;
            
             // Get the high Price of the last candle for this pair
            _highPrice = Bars.HighPrices.Maximum(2);
            // Then set the price 1 pip bellow that (so i can allow it to act like a LMT order)
            _lmtPrice = _highPrice - 1 * Symbol.PipSize;
            
            // Draw this LMT order or price line on the chart so i can see the entry point.
            var highLine = Chart.DrawHorizontalLine("Low", _lmtPrice, Color.Blue);
            highLine.IsInteractive = true;          
          

            
        }

  
       protected override void OnTick()
       {           
 
            // Get the current Price, which is the symbols previous ASK price.
            // Then inside the onTick method this keeps checking every tick if the ask price is bellow the entry price.
            var _previousAsk = Symbol.Ask;
            
            
            // Check if the current price is higher than the previous ticks ASK price
            if (_lmtPrice != 0 && _previousAsk < _lmtPrice)
            {
            
                //See if there are any Open positions
                var CheckPositions = Positions.Count;    
               
                
                var currentSymbolOrderLabel = Positions.FindAll("X").Length;
                    
                //If "CheckPositions" Reports That less than 2 total positions are open and no existing current order with the label "Current Symbol Order" is open, Then ExecuteMarketOrder

                // HOW DO I ALSO CHECK IF THIS POSITION HAS CLOSED.
                // So if the order with Label "Current Symbol Order" has filled and closed do not fill it again
                // There will be multiple position Labels so it will need to check through the closed positions and find the matching Label
                if (CheckPositions < 2 && currentSymbolOrderLabel < 1 ) {                                 
            
                    // Place a buy order
                    // At the current ask price because its bellow the trigger price with a range of 1 pip.
                    ExecuteMarketRangeOrder(TradeType.Buy, SymbolName, 1000, 1, Symbol.Ask, "X", null, 25);

                }
            
           }
           
               
                
       }
       
       
       // Position Closed event that gets the label.
       private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            var pos = args.Position;
            var label = pos.Label;
            Print("Closed Position: " + label);
        }
       


        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

 


@ctid2434759
Replies

firemyst
09 Mar 2023, 08:09

RE:

ctid2434759 said:

Hi there,

This code is simplified for solving this specific problem.

I want to check if a position with the Label of "X" has closed.
If it has closed I want to add that into my if statement so that another one doesn't open again.

Something like this?
 if (CheckPositions < 2 && currentSymbolOrderLabel < 1 && PositionClosed.Label = "X" < 1 ) ??

So if there is less than 2 total open positions and less than one of the label "X" and the label "X" position has not already closed then ExecuteMarketRangeOrder.

Aka, if position Label "X" has closed do NOT ExecuteMarketRangeOrder for "X" again..

Thanks
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.SingaporeStandardTime, AccessRights = AccessRights.None)]
    public class ClosePositionOnTime : Robot
    
    

    
    {

        // Just the current simulator pairs global variables.
        private double _highPrice;
        private double _lmtPrice;


        protected override void OnStart()
        {

            Positions.Closed += PositionsOnClosed;
            
             // Get the high Price of the last candle for this pair
            _highPrice = Bars.HighPrices.Maximum(2);
            // Then set the price 1 pip bellow that (so i can allow it to act like a LMT order)
            _lmtPrice = _highPrice - 1 * Symbol.PipSize;
            
            // Draw this LMT order or price line on the chart so i can see the entry point.
            var highLine = Chart.DrawHorizontalLine("Low", _lmtPrice, Color.Blue);
            highLine.IsInteractive = true;          
          

            
        }

  
       protected override void OnTick()
       {           
 
            // Get the current Price, which is the symbols previous ASK price.
            // Then inside the onTick method this keeps checking every tick if the ask price is bellow the entry price.
            var _previousAsk = Symbol.Ask;
            
            
            // Check if the current price is higher than the previous ticks ASK price
            if (_lmtPrice != 0 && _previousAsk < _lmtPrice)
            {
            
                //See if there are any Open positions
                var CheckPositions = Positions.Count;    
               
                
                var currentSymbolOrderLabel = Positions.FindAll("X").Length;
                    
                //If "CheckPositions" Reports That less than 2 total positions are open and no existing current order with the label "Current Symbol Order" is open, Then ExecuteMarketOrder

                // HOW DO I ALSO CHECK IF THIS POSITION HAS CLOSED.
                // So if the order with Label "Current Symbol Order" has filled and closed do not fill it again
                // There will be multiple position Labels so it will need to check through the closed positions and find the matching Label
                if (CheckPositions < 2 && currentSymbolOrderLabel < 1 ) {                                 
            
                    // Place a buy order
                    // At the current ask price because its bellow the trigger price with a range of 1 pip.
                    ExecuteMarketRangeOrder(TradeType.Buy, SymbolName, 1000, 1, Symbol.Ask, "X", null, 25);

                }
            
           }
           
               
                
       }
       
       
       // Position Closed event that gets the label.
       private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            var pos = args.Position;
            var label = pos.Label;
            Print("Closed Position: " + label);
        }
       


        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

 

You need to search the Historical Trade object it seems like.

HistoricalTrade ht = History.FindLast(the_label_to_look_for);

 


@firemyst