Close all open trade

Created at 17 Jul 2013, 17:25
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

diogene823

Joined 20.05.2013

Close all open trade
17 Jul 2013, 17:25


Hi i will that my robot close all the opened position if the equity is XXX (parameter).

I have find this:

var netProfit = 0.0;

            foreach (var openedPosition in Account.Positions)
            {
              netProfit += openedPosition.NetProfit;               
            }
            if(Account.Equity > Equity) 
            {
            foreach (var openedPosition in Account.Positions)
            {
               Trade.Close(openedPosition);
               Stop();
            }

but so the robot close only one trade, the oldest, them shut down himself properly, but al trade are open.
I searched a lot on the site but did not find anything.

Can someone help me?

Thanks and Regards

 


@diogene823
Replies

breakermind
17 Jul 2013, 17:50

RE:
diogene823 said:

Hi i will that my robot close all the opened position if the equity is XXX (parameter).

I have find this:

var netProfit = 0.0;

            foreach (var openedPosition in Account.Positions)
            {
              netProfit += openedPosition.NetProfit;               
            }
            if(Account.Equity > Equity) 
            {
            foreach (var openedPosition in Account.Positions)
            {
               Trade.Close(openedPosition);
               Stop();
            }

but so the robot close only one trade, the oldest, them shut down himself properly, but al trade are open.
I searched a lot on the site but did not find anything.

Can someone help me?

Thanks and Regards

 

Hi

it works for me

    protected override void OnPositionOpened(Position openedPosition)
    {
    
    if(Account.Equity > 1){
            foreach (var position in Account.Positions)
            {
                //if (position.GrossProfit  > 0)
                //{
                    Trade.Close(position);
                //}
            }
    }

    }

Regards


@breakermind

diogene823
17 Jul 2013, 17:57

Thanks breakermind, but your robot close all trade only if this trade have a profit > of zero

I need a robot that close all the trade (the trade in profit and the trade in loss) if the equity is XXX,

it's different.

 

Thanks and Regards


@diogene823

diogene823
17 Jul 2013, 18:00

so in need only this:

var netProfit = 0.0;

            foreach (var openedPosition in Account.Positions)
            {
              netProfit += openedPosition.NetProfit;               
            }
            if(Account.Equity > Equity) 
            {
            foreach (var openedPosition in Account.Positions)
            {
               Trade.Close(ALL THE POSITION; PLEASE DARLING ROBOT CLOSE ALL; PLEASE!!!!!);
               Stop();
            }


but so don't work hahahaah


@diogene823

breakermind
17 Jul 2013, 19:47

RE:
diogene823 said:

so in need only this:

var netProfit = 0.0;

            foreach (var openedPosition in Account.Positions)
            {
              netProfit += openedPosition.NetProfit;               
            }
            if(Account.Equity > Equity) 
            {
            foreach (var openedPosition in Account.Positions)
            {
               Trade.Close(ALL THE POSITION; PLEASE DARLING ROBOT CLOSE ALL; PLEASE!!!!!);
               Stop();
            }


but so don't work hahahaah

Welcome back,

I do not think you tested what I wrote :) because you knew  It close all position !!!

First learn what these two characters / / do.


Regards and bye.

 


@breakermind

diogene823
18 Jul 2013, 17:41

Hi yes it works, i'm wrong understand your post for two reasons:

1) i know the characters // but you write

                //if (position.GrossProfit  > 0)
                //{
                    Trade.Close(position);
                //}

and i have see:

//if (position.GrossProfit  > 0)
                //{
                //     Trade.Close(position);
                //}

2) before opening this topic i have test this:


 if(Account.Equity > 1){
            foreach (var position in Account.Positions)
            {
                {
                    Trade.Close(position);

                    Stop();
                }
            }
    }


But so the robot close only the oldest trade and off himself.

Now i understand why!!!


I solved this way: 


protected override void OnPositionClosed(Position position)
        {
            if(Account.Positions.Count == 0)
            {
                Stop();
               } 
        }

So the robot shut down himself only after closing all.

I am truly grateful for your suggestion. Thanks and Bye

 


@diogene823

Nego
18 Jul 2013, 19:33

RE:
diogene823 said:

Hi i will that my robot close all the opened position if the equity is XXX (parameter).

I have find this:

var netProfit = 0.0;

            foreach (var openedPosition in Account.Positions)
            {
              netProfit += openedPosition.NetProfit;               
            }
            if(Account.Equity > Equity) 
            {
            foreach (var openedPosition in Account.Positions)
            {
               Trade.Close(openedPosition);
               Stop();
            }

but so the robot close only one trade, the oldest, them shut down himself properly, but al trade are open.
I searched a lot on the site but did not find anything.

Can someone help me?

Thanks and Regards

 

Hi there, I really dont understand why you have the Stop(); within the foreach part.

Try this:

var netProfit = 0.0;

            foreach (var openedPosition in Account.Positions)
            {
              netProfit += openedPosition.NetProfit;               
            }
            if(Account.Equity > Equity) 
            {
            foreach (var openedPosition in Account.Positions)
            {
               Trade.Close(openedPosition);
            }
            Stop();

Now the Robot stops only after all positions have been closed. I hope this helps.

Nego


@Nego

diogene823
19 Jul 2013, 08:29

hahahah thanks Nego.

"Hi there, I really dont understand why you have the Stop(); within the foreach part."

Because i am THE JACKASS of the development, i write my first line of code two week ago.

Im going crazy for 4 days for the difference about this:

foreach (var openedPosition in Account.Positions)
            {
               Trade.Close(openedPosition);
            }
            Stop();

AND THIS

foreach (var openedPosition in Account.Positions) { Trade.Close(openedPosition);
Stop(); }

I'm going crazy because i don't understand that the foreach is a loop i think that the loop is a specific order, and Trade.Close(openedPoistion) is a order to close all position, not a loop to close one position every round of loop. Another software that i use, not for trading, have the loop only if you declare the loop. (this software is not a development software, is a object development software).

Sorry for my poor english.

Bye and thank you

 

@diogene823

Nego
19 Jul 2013, 11:52

Hehe, this happens to the best of us, trust me. 

It is just too bad that cAlgo doesn't come with a debugger. 


@Nego

fzlogic
19 Jul 2013, 12:12

You can attach the Visual Studio debugger:

/forum/cbot-support/831


@fzlogic