openedPosition.TakeProfit and openedPosition.Stoploss are null in the OnOpenedPosition event?

Created at 27 Nov 2012, 06:03
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!
lec0456's avatar

lec0456

Joined 14.11.2012

openedPosition.TakeProfit and openedPosition.Stoploss are null in the OnOpenedPosition event?
27 Nov 2012, 06:03


I called a Trade.CreateBuyStopOrder, the log shows that the pending order was placed and then filled with the stoploss and takeprofit defined.  However when It calls the OnPositionOpened event, Why is it that the openedPosition.TakeProfit and openedPosition.Stoploss are null? 

My intention is to modify one and leave the other as defined when I created the pending order.

 


@lec0456
Replies

admin
29 Nov 2012, 12:36

I'm not sure why that is maybe you can share your code. In the meantime please test this and tell us if it solves your problem:

using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot]
    public class TestCreateBuyStopOrder:Robot
    {

        protected override void OnTick()
        {
            if(Trade.IsExecuting) return;
            double targetPrice = Symbol.Ask + Symbol.PipSize;

            if (Account.PendingOrders.Count == 0 && Account.Positions.Count == 0)
                Trade.CreateBuyStopOrder(Symbol, 1000, targetPrice, targetPrice - Symbol.PipSize, targetPrice + Symbol.PipSize, Server.Time.AddMinutes(10));
        }
        protected override void OnPendingOrderCreated(PendingOrder newOrder)
        {
            Print("Pending order created");
            Print("Stop Loss {0}", newOrder.StopLoss);
            Print("Take Profit {0}", newOrder.TakeProfit);

        }

        protected override void  OnPositionOpened(Position openedPosition)
        {
            Print("Position opened");
            Print("Stop Loss {0}", openedPosition.StopLoss);
            Print("Take Profit {0}", openedPosition.TakeProfit);
        }

        protected override void OnError(Error error)
        {
            Stop();
        }
    }
}




@admin

lec0456
01 Dec 2012, 09:50 ( Updated at: 21 Dec 2023, 09:20 )

RE:
Yup, copied and compiled your code and I get the same problem.  It doesn't return the stoploss or takeprofit OnPositionOpened.  look at my log...

@lec0456

lec0456
14 Dec 2012, 17:10

Whats the status on this issue??   It doesn't return the stoploss or takeprofit OnPositionOpened.  look at my log...


@lec0456

admin
04 Jan 2013, 16:29

We will look into this. Thank your for bringing it to our attention.

 


@admin

admin
04 Jan 2013, 17:24

The reason this is, is that the Stop Loss and Take Profit are set right after the OnPosistionOpened event.  The workaround for this would be to modify the SL/TP in another function called if a Position field set in the OnPositionOpened is not null.

i.e.

        Position _pos;

        protected override void OnTick()
        {
            if (Trade.IsExecuting) return;
            double targetPrice = Symbol.Ask + Symbol.PipSize;

            if (Account.PendingOrders.Count == 0 && Account.Positions.Count == 0)
                Trade.CreateBuyLimitOrder(Symbol, 1000, targetPrice, targetPrice - Symbol.PipSize, targetPrice + Symbol.PipSize, Server.Time.AddMinutes(10));

            if (_pos != null)
            {
                // call method to modify SL/TP
                Print("Stop Loss {0}", _pos.StopLoss);
                Print("Take Profit {0}", _pos.TakeProfit);
            }

        }
        protected override void OnPendingOrderCreated(PendingOrder newOrder)
        {
            Print("Pending order created");
            Print("Stop Loss {0}", newOrder.StopLoss);
            Print("Take Profit {0}", newOrder.TakeProfit);

        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            _pos = openedPosition;
        }




@admin