Replies

lazaromg
22 Feb 2022, 21:13

RE:

amusleh said:

Hi,

Sorry for my misunderstanding, try this:

            var lastBuyPosition = Positions.Where(position => position.TradeType == TradeType.Buy).OrderBy(position => position.EntryTime).LastOrDefault();

            // You must check for null, because there might be no open buy position
            if (lastBuyPosition != null)
            {
                var lastBuyPositionEntryPrice = lastBuyPosition.EntryPrice;

                // Do anything you want to
            }

            var lastSellPosition = Positions.Where(position => position.TradeType == TradeType.Sell).OrderBy(position => position.EntryTime).LastOrDefault();

            // You must check for null, because there might be no open sell position
            if (lastSellPosition != null)
            {
                var lastSellPositionEntryPrice = lastSellPosition.EntryPrice;

                // Do anything you want to
            }

 

Hi amusleh,
I've tried your recommendation, but it's not working.
Do not open any trades now.
I don't know if I'm erring in logic.
Can you help me?
Thanks again.

A snippet of my code:

protected override void OnBar()
{
    var lastBuyPosition = Positions.Where(position => position.TradeType == TradeType.Buy).OrderBy(position => position.EntryTime).LastOrDefault();
            if (lastBuyPosition != null)
            {
                var lastBuyPositionEntryPrice = lastBuyPosition.EntryPrice;

                // Do anything you want to
                if (Bars.ClosePrices.Last(1) < Bars.OpenPrices.Last(1) && Bars.ClosePrices.Last(1) < ema.Result.Last(1) && Bars.ClosePrices.Last(1) < Bars.ClosePrices.Last(2) && Bars.ClosePrices.Last(1) < lastBuyPositionEntryPrice)
                {
                    Position[] positions = Positions.FindAll("ContraBuy");

                    if (positions.Length < MaxOrders)
                    {
                        ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, "ContraBuy");
                    }
                }
            }

            var lastSellPosition = Positions.Where(position => position.TradeType == TradeType.Sell).OrderBy(position => position.EntryTime).LastOrDefault();

            // You must check for null, because there might be no open sell position
            if (lastSellPosition != null)
            {
                var lastSellPositionEntryPrice = lastSellPosition.EntryPrice;

                // Do anything you want to
                if (Bars.ClosePrices.Last(1) > Bars.OpenPrices.Last(1) && Bars.ClosePrices.Last(1) > ema.Result.Last(1) && Bars.ClosePrices.Last(1) > Bars.ClosePrices.Last(2) && Bars.ClosePrices.Last(1) > lastSellPositionEntryPrice)
                {
                    Position[] positions = Positions.FindAll("ContraSell");
                    if (positions.Length < MaxOrders)
                    {
                        ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, "ContraSell");
                    }
                }
            }
}


@lazaromg

lazaromg
22 Feb 2022, 01:22

RE:

amusleh said:

Hi,

You can use Positions closed event with history, example:

using cAlgo.API;
using System.Linq;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private double _lastBuyPositionPrice, _lastSellPositionPrice;

        protected override void OnStart()
        {
            var lastBuyTrade = History.Where(trade => trade.TradeType == TradeType.Buy).OrderBy(trade => trade.ClosingTime).LastOrDefault();

            if (lastBuyTrade != null)
            {
                _lastBuyPositionPrice = lastBuyTrade.EntryPrice;
            }

            var lastSellTrade = History.Where(trade => trade.TradeType == TradeType.Sell).OrderBy(trade => trade.ClosingTime).LastOrDefault();

            if (lastSellTrade != null)
            {
                _lastSellPositionPrice = lastSellTrade.EntryPrice;
            }

            Positions.Closed += Positions_Closed;
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            if (obj.Position.TradeType == TradeType.Buy)
            {
                _lastBuyPositionPrice = obj.Position.EntryPrice;
            }
            else
            {
                _lastSellPositionPrice = obj.Position.EntryPrice;
            }
        }
    }
}

 

Hi amusleh,

Thank you very much by your help, but I need verify entry price of positions that are still opened and not that was closed.
And compare price of bar closed with entry price of last position.
I don't know if I can be clear to tell.....
Thanks!


@lazaromg