Stoploss

Created at 11 Oct 2012, 17:12
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!
PH

phamvanthanh

Joined 07.10.2012

Stoploss
11 Oct 2012, 17:12


Hi,

I am quite new to Calgo

Please help me to to set stoploss for position or to close all positions if grossprofit <= x.

thanks

 


@phamvanthanh
Replies

tradermatrix
11 Oct 2012, 17:37

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot]
    public class SampleCloseProfitablePositions : Robot
    {
        protected override void OnStart()
        {
            foreach (var position in Account.Positions)
            {
                if (position.GrossProfit < 0)
                {
                    Trade.Close(position);
                }
            }
        }
    }
}


@tradermatrix

tradermatrix
11 Oct 2012, 17:48

CLOSE NEGATIVE BALANCE.....closes all ordre.ajuster <0 or < -1 or -2 or -3 ... etc.

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot]
    public class  CLOSENEGATIVEBALANCE: Robot
    {
      
    protected override void OnTick()
{        
    if( Account.Equity - Account.Balance < 0 )
    {
    foreach (var position in Account.Positions)
           {
                   Trade.Close(position);
           }
    }
}
}
}

 


@tradermatrix

tradermatrix
11 Oct 2012, 17:56

CLOSE POSITIVE BALANCE.....closes all ordre.ajuster >0 or > 1 or >2 or 3 ... etc.

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot]
    public class CLOSEPOSITIVEBALANCE : Robot
    {
        protected override void OnTick()
{        
    if( Account.Equity - Account.Balance > 0)
    {
    foreach (var position in Account.Positions)
           {
                   Trade.Close(position);
           }
    }
}
}
}

 

 

 

 


@tradermatrix

tradermatrix
11 Oct 2012, 18:16

CLOSE BALANCE........closes all ordre.ajuster >0 or > 1 or >2 or 3 and  <0 or < -1 or -2 or -3 ... etc.

you can include these robots to other robots.

@+

 

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot]
    public class CLOSEBALANCE : Robot
    {
          protected override void OnTick()
        {
            if (Account.Equity - Account.Balance > 1 || Account.Equity - Account.Balance < -1)
            {
                foreach (var position in Account.Positions)
                {
                    Trade.Close(position);
                }
            }
        }
   }
}


@tradermatrix

tradermatrix
11 Oct 2012, 18:53

Attention: the code of the last 3 robots affects all other robots.
For instance one Robot may be closing all positions that are open for the account.
but these robots are engaged in a click to close all open trades. for example the scalping
and also to protect the trades following a sudden market movement.

@+

 


@tradermatrix

admin
12 Oct 2012, 10:26

Hello,

 

This is a simple robot that opens a position and then sets stop loss and take profit: /forum/calgo-reference-samples/69

To learn how to set the stop loss you may look at the reference guide: /docs/reference/calgo/api/internals/itrade/modifyposition

Also, you can look at the sample robots that are included in cAlgo, namely, SampleMartingaleRobot,  SampleBuyTrailing, SampleSellTrailing, SampleSARTrailingStop

 

 

 


@admin

phamvanthanh
13 Oct 2012, 11:57

Hi, tradermatrix,

thank so much for your answers.

 


@phamvanthanh