Multiple positions - trades being closed at same time instead of separately

Created at 29 May 2013, 10:44
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!
KY

kyro747

Joined 19.05.2013

Multiple positions - trades being closed at same time instead of separately
29 May 2013, 10:44


Having the following problem:

I'm allowing multiple positions (long and short). When the long position exit is triggered with the condition (Buyexit) the short position also closes at the same time. Below is the code for exiting.

I couldn't work out how to refer to the trades by the labels I assigned them on entry instead e.g. "Long Entry" , "Short Entry". 

 

How can I rectify this problem?

 

Thanks

 

 

 

foreach (var position in Account.Positions) 
            {    
            
                  if (position.TradeType == TradeType.Buy)
                  {
                
                        if (Buyexit )
                        
                            {
                                  
                                  Print("Profit Exit");
                                  Trade.Close(position);
                                  
                            }
                     
                  }
               
                  if (_position.TradeType == TradeType.Sell)
                  {
           
                          if (Sellexit)
                              
                              {
                                  
                                  Print("Profit Exit");
                                  Trade.Close(position);
                                  
                              }
          
                  }
                             
           }


@kyro747
Replies

kyro747
29 May 2013, 10:46

Apologies, code should read:

 

 

foreach (var position in Account.Positions) 
            {    
            
                  if (_position.TradeType == TradeType.Buy)
                  {
                
                        if (Buyexit )
                        
                            {
                                  
                                  Print("Profit Exit");
                                  Trade.Close(position);
                                  
                            }
                     
                  }
               
                  if (_position.TradeType == TradeType.Sell)
                  {
           
                          if (Sellexit)
                              
                              {
                                  
                                  Print("Profit Exit");
                                  Trade.Close(position);
                                  
                              }
          
                  }
                             
           }


@kyro747

cAlgo_Fanatic
29 May 2013, 15:00

"When the long position exit is triggered with the condition (Buyexit) the short position also closes...I couldn't work out how to refer to the trades by the labels".
Do you mean that all positions long and short will close under the specified label?

If so, the code should be:

            if (Buyexit)
                foreach (var position in Account.Positions)
                {
                    if (position.Label == Label) // will close all positions of the account under this Label
                    {
                        Print("Profit Exit");
                        Trade.Close(position);
                    }
                }

Where Label may be an input parameter or constant field.


Note:
In the code snippets you provided making the distinction between postition and _position, the second code snippet is incorrect. It is assumed that _position is a field. A field holding a Position object is usually used when the program has only one position open at a time. This field is set at OnPositionOpened and is reset (null) at OnPositionClosed. 
In case you want to handle more than one positions with your robot you would use a List like in this List example.
In case you want to handle all positions of the account you would use the Account.Positions list like in the above code snippet.
Therefore, if you use a foreach statement to loop through all positions in the account you refer to the variable declared in the foreach statement, in this case "position" and you may give this variable whichever name you like, i.e. foreach(var pos in Account.Positions){...}. Then within the body of the foreach statement (inside the curly brackets) you will refer to this variable: 

 

            foreach(var pos in Account.Positions)
            {
                if (pos.Label == Label)
                {
                    //...
                }
            }




@cAlgo_Fanatic

kyro747
29 May 2013, 17:50

Thankyou for the reply.

 

I mean that if (Buyexit) is true then I want to exit all long trades, but not exit any short trades.

"Where Label may be an input parameter or constant field."

Can you please give me an example of how to do this so I can reference individual trades or just long or short trades?

 

I have tried using the multiple positions example code with no success yet.


@kyro747

kyro747
30 May 2013, 04:44

Also as a side note:

Is it possible to have the same robot running on multiple currencies at same time? As a test I have tried running on 4 and it keeps stopping 2 of them and only running 2....


@kyro747

cAlgo_Fanatic
30 May 2013, 13:13

RE:
 
 if (position.TradeType == TradeType.Buy && position.Label == Label)

The above will check that the position of the account is a Buy and it has the specified label.

 

 if (position.TradeType == TradeType.Buy)

The above will check that the position of the account is a Buy

 if (position.Label == Label)

The above will check that the position of the account has the specified label.

 

 if (position.TradeType == TradeType.Buy || position.Label == Label)

The above will check that the position of the account is a Buy or has the specified label.

 

        [Parameter(DefaultValue = "my Robot")]
        public string Label { get; set; }

The above is an input parameter for a label.

Also see:

/api/position/label

/forum/whats-new/labels-and-a-new-method-to-create-orders

/algos/robots/show/259


@cAlgo_Fanatic

kyro747
02 Jun 2013, 12:50

Thankyou


@kyro747