Last Opened Position Entry Price to compare with current price
            
                 20 Feb 2022, 18:48
            
                    
Hi!
Could anyone help with a question on a robot I'm making?
I'm stuck on a rule I need to do and I don't know how.
I need the bot to open a new sell position only if the price is below the entry price of last open sell position and for the bot to open a new buy position only if the price is above entry price of t the last open buy position.
But I don't know what function to use to look up the value of the last open position and compare it to the current price.
Below part of code:
protected override void OnBar()
        {
            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))
            {
                Position[] positions = Positions.FindAll("ContraBuy");
                if (positions.Length < MaxOrders)
                {
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, "ContraBuy");
                }
            }
            else 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))
            {
                Position[] positions = Positions.FindAll("ContraSell");
                if (positions.Length < MaxOrders)
                {
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, "ContraSell");
                }
            }
        }
Thanks!
Replies
                     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
                     amusleh
                     22 Feb 2022, 08:59
                                    
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
            }
@amusleh
                     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
                     amusleh
                     23 Feb 2022, 08:42
                                            ( Updated at: 23 Feb 2022, 08:44 )
                                    
Hi,
The issue is when there is no open position then it will not open a new position, so you have to ignore entry price check when there is no open position.
Example:
        protected override void OnBar()
        {
            Position[] buyPositions = Positions.FindAll("ContraBuy");
            var lastBuyPosition = buyPositions.Where(position => position.TradeType == TradeType.Buy).OrderBy(position => position.EntryTime).LastOrDefault();
            // if there is no open buy position then it will not check the position entry price
            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) && (lastBuyPosition == null || Bars.ClosePrices.Last(1) < lastBuyPosition.EntryPrice))
            {
                if (buyPositions.Length < MaxOrders)
                {
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, "ContraBuy");
                }
            }
            Position[] sellPositions = Positions.FindAll("ContraSell");
            var lastSellPosition = sellPositions.Where(position => position.TradeType == TradeType.Sell).OrderBy(position => position.EntryTime).LastOrDefault();
            // if there is no open sell position then it will not check the position entry price
            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) && (lastSellPosition == null || Bars.ClosePrices.Last(1) > lastSellPosition.EntryPrice))
            {
                if (sellPositions.Length < MaxOrders)
                {
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, volumeInUnits, "ContraSell");
                }
            }
        }
@amusleh

amusleh
21 Feb 2022, 09:37 ( Updated at: 21 Feb 2022, 09:40 )
Hi,
You can use Positions closed event with history, example:
@amusleh