Close Position when it reaches certain loss
Close Position when it reaches certain loss
06 Jun 2013, 19:23
Hi,
I am trying to get my robot to close only the positions that have a loss of a specified amount. (e.g. 15 pips/$15) I have used some code that I found on this site but I think what it is doing is closing all new positions if there are any open positions with a loss of the specified amount. Instead I want it to only close the open positions with that loss and allow new positions to be placed until they reach that loss and can be closed out too.
I would appreciate some guidance in this matter.
Thanks!
Here is the code:
foreach (var position in Account.Positions)
{
if (position.NetProfit <= -20)
{
ClosePosition();
}
Replies
Delta_Gamma
07 Jun 2013, 17:14
Ah thank you, I see the error now, I had a variable called _position being called.
Thanks
@Delta_Gamma
cAlgo_Fanatic
07 Jun 2013, 11:39
You probably need to modify your code to this:
in the foreach loop you are looping through each open position in the account and refering to it with the variable name "position" (bold).
So, using another method ClosePosition(), you would have to pass this variable as a parameter like so:
In order to refer to the same variable which to close. Otherwise, you will be closing which position? In the ClosePosition() method you are probably refering to another global field which you gave the same name to (position).
You may want to change the name of the variables so that you can differentiate between them, i.e. latestPosition, pos, etc. You may give them any name for as long as it is not a c# or cAlgo reserved identifier.
Read more on c#.
@cAlgo_Fanatic