cannot find open position

Created at 27 Apr 2021, 14:25
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!
TR

triccomane

Joined 07.04.2021

cannot find open position
27 Apr 2021, 14:25


Hello, I do not understand why my code would always return false when I try to look for open positions. I know at some point a position must be open because I backtested this code on the chart and positions get opened.  The problem must lie between line 31 and 34, where I try to check if positions are opened. Any help would be appreciated. Thanks.

 

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 Over9K : Robot
    {
        [Parameter(DefaultValue = 3)]
        public double Exec_buffer { get; set; }

        [Parameter(DefaultValue = 0.8)]
        public double tp_ratio { get; set; }

        [Parameter(DefaultValue = 0.8)]
        public double sl_ratio { get; set; }

        private double _lastClose;

        double HighPrice = 0;
        double LowPrice = 100000;
        protected override void OnTick()
        {
            if (Server.Time.Hour == 7)
                if (HighPrice > Symbol.Ask + (Exec_buffer * Symbol.PipSize) || LowPrice < Symbol.Bid - (Exec_buffer * Symbol.PipSize))
                {
                    var SellPosition = Positions.FindAll("Sell", SymbolName, TradeType.Sell);
                    var BuyPosition = Positions.FindAll("Buy", SymbolName, TradeType.Buy);
                    Print("SellPosition.Count() == 1 ", SellPosition.Count() == 1); // always prints false
                    if ((SellPosition.Count() == 1 && BuyPosition.Count() == 0) || (BuyPosition.Count() == 1 && SellPosition.Count() == 0))
                    {
                        Print("close pending");
                        ClosePendingSell();
                        ClosePendingBuy();
                    }
                    if (MarketSeries.Close.Last(1) != _lastClose)
                    {


                        _lastClose = MarketSeries.Close.Last(1);

                    }
                }

            if (Server.Time.Hour == 7 && Server.Time.Minute == 1 && MarketSeries.Close.Last(1) != _lastClose)
            {
                HighPrice = MarketSeries.High.Last(1);
                Print("massimo ", HighPrice);
                LowPrice = MarketSeries.Low.Last(1);
                Print("minimo ", LowPrice);
                double buy_price = HighPrice + (Exec_buffer * Symbol.PipSize);
                double sell_price = LowPrice - (Exec_buffer * Symbol.PipSize);
                double tp = (HighPrice - LowPrice) * tp_ratio;
                double sl = (HighPrice - LowPrice) * sl_ratio;

                PlaceStopOrder(TradeType.Buy, SymbolName, 0.01, buy_price, "Long", sl, tp);
                Print("BUY LIMIT");
                var line_high = Chart.DrawHorizontalLine("High", HighPrice + (Exec_buffer * Symbol.PipSize), Color.White);
                var line_low = Chart.DrawHorizontalLine("Low", LowPrice - (Exec_buffer * Symbol.PipSize), Color.White);
                line_high.IsInteractive = true;
                line_low.IsInteractive = true;

                PlaceStopOrder(TradeType.Sell, SymbolName, 0.01, sell_price, "Short", sl, tp);
                Print("SELL LIMIT");
                _lastClose = MarketSeries.Close.Last(1);
            }


        }

        ///***********************CLOSE PENDING SELL
        protected void ClosePendingSell()
        {


            Print("count 1");

            foreach (var order in PendingOrders)
            {
                if (order.TradeType == TradeType.Sell)
                    CancelPendingOrderAsync(order);
                Print("close sell");
            }

        }

        //**************************CLOSE PENDING BUY
        protected void ClosePendingBuy()
        {

            Print("count 1");
            foreach (var order in PendingOrders)
            {
                if (order.TradeType == TradeType.Buy)
                    CancelPendingOrderAsync(order);
                Print("close buy");
            }
        }


    }
}

 


@triccomane
Replies

PanagiotisCharalampous
27 Apr 2021, 15:17

Hi triccomane,

The labels you use to search for positions are different from the labels you use when placing your orders

                    var SellPosition = Positions.FindAll("Sell", SymbolName, TradeType.Sell);
                    var BuyPosition = Positions.FindAll("Buy", SymbolName, TradeType.Buy);
                PlaceStopOrder(TradeType.Buy, SymbolName, 0.01, buy_price, "Long", sl, tp);

                PlaceStopOrder(TradeType.Sell, SymbolName, 0.01, sell_price, "Short", sl, tp);

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous