Opened Positions using ExecuteMarketOrder

Created at 15 Jan 2014, 22:15
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!
DI

Diego_Troncoso

Joined 17.12.2013

Opened Positions using ExecuteMarketOrder
15 Jan 2014, 22:15


I've made a substition of Trade.CreateBuyMarketOrder(Symbol, volume); to 
ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "Robot2"); and after that the robot stopped reading all my
commands for opened positions.

This is how it recently is written:

protected override void OnPositionOpened(Position openedPosition)
{

 

}

How can I fix that?

Thank you.


@Diego_Troncoso
Replies

Spotware
16 Jan 2014, 09:09

You will have to subscribe a method and define it like in the following code:

protected override void OnStart()
{
    Positions.Opened += OnPositionsOpened;
    ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "Robot2"); 
}
 
private void OnPositionsOpened(PositionOpenedEventArgs args)
{
    var position = args.Position;
    if(position.Label == "Robot2")
    {
       // position has been opened  by robot
    }  
}

Note that the method will be called whenever a position is opened whether by this robot or else. So, you will have to check the position's label to make sure that it is the robot's position that is opened.

Alternatively, you can use the asynchronous equivalent method with a callback function.

See the what's new section on the new trading api as well as the guide.


@Spotware