How to close position before stop loss limit hit

Created at 20 Jul 2014, 18:52
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!
HI

hiba7rain

Joined 20.07.2014

How to close position before stop loss limit hit
20 Jul 2014, 18:52


Hey All

am new to programming 

if I have placed a market order as follows 

private void Open(TradeType tradType)
        {


            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "test", StopLossInPips, TakeProfitInPips);


        }

how can I add instruction to close the opened position before even hitting the stop loss target?

for example if i would like to close the position lets say in case the ask price equals or less than the EMA or the same bar value hit the EMA

 

Thanks 


@hiba7rain
Replies

Old Account
20 Jul 2014, 19:23

Add this to you code

private void Close()
        {
            foreach (var position in Positions)
            {
                ClosePosition(position);
            }
        }

 

And when you want to close a position add Close()

Like this:

If(SMA.Result.LastValue > 1)

Close();

Hope it helped


@Old Account

hiba7rain
20 Jul 2014, 19:42

RE:

MRSV said:

Add this to you code

private void Close()
        {
            foreach (var position in Positions)
            {
                ClosePosition(position);
            }
        }

 

And when you want to close a position add Close()

Like this:

If(SMA.Result.LastValue > 1)

Close();

Hope it helped

Thanks 

but do you mean I have to add if (EMA.Result.LastValue > 1) 

Close(0; 

in place of ClosePosition(position);


@hiba7rain

hiba7rain
20 Jul 2014, 20:24

RE:

MRSV said:

Add this to you code

private void Close()
        {
            foreach (var position in Positions)
            {
                ClosePosition(position);
            }
        }

 

And when you want to close a position add Close()

Like this:

If(SMA.Result.LastValue > 1)

Close();

Hope it helped

I did as you mentioned but still the positions are closed on the stop loss parameter

any suggestions?


@hiba7rain

Old Account
21 Jul 2014, 20:57

 

This was an example of how to use the Close() method.

If(SMA.Result.LastValue > 1)

Close();

 

It is the same as the method for opening a position, but insted of opening a position it closes it.

So you have for your criteria for closing a position that is the IF("something") then close.

 

In the example i used all positions would close when the Simple Moving Average hit 1.

If you want the position to close when EMA is equal or less the the as you write it like this:

If(EMA.Result.LastValue <= Symbol.Ask)
Close();

 

 

 


@Old Account

hiba7rain
21 Jul 2014, 21:28

RE:

hanks for your reply  in fact I also got some help for friends here and I used the following method but am still having a problem that if i use the method all other positions will be closed which i dont want it to happen

the method I use is

protected override void OnTick()
        {
            CheckForEMA();
            var positions = Positions.FindAll("calgooob", Symbol, TradeType.Buy);
        }

        private void CheckForEMA()
        {
            if (ema1.Result.LastValue > Symbol.Ask)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position); 

MRSV said:

 

This was an example of how to use the Close() method.

If(SMA.Result.LastValue > 1)

Close();

 

It is the same as the method for opening a position, but insted of opening a position it closes it.

So you have for your criteria for closing a position that is the IF("something") then close.

 

In the example i used all positions would close when the Simple Moving Average hit 1.

If you want the position to close when EMA is equal or less the the as you write it like this:

If(EMA.Result.LastValue <= Symbol.Ask)
Close();

 

 

 

 


@hiba7rain

Invalid
22 Jul 2014, 09:33

RE: RE:

Hi hiba7rain.

You don't understand smth fundamental yet.

Positions - it's a collection of all of your positions.

if you execute next code:

 foreach (var position in Positions)
{
      ClosePosition(position); 
}

it will close all your positions. If you need to close specific positions, than do as you did to get them and pass them to ClosePosition method.

        protected override void OnTick()
        {
            CheckForEMA();
        }

        private void CheckForEMA()
        {
            if (ema1.Result.LastValue > Symbol.Ask)
            {
                var positionsToClose = Positions.FindAll("calgooob", Symbol, TradeType.Buy);
                foreach (var position in positionsToClose)
                {
                    ClosePosition(position);
                }
            }
        }

hiba7rain said:

hanks for your reply  in fact I also got some help for friends here and I used the following method but am still having a problem that if i use the method all other positions will be closed which i dont want it to happen

the method I use is

protected override void OnTick()
        {
            CheckForEMA();
            var positions = Positions.FindAll("calgooob", Symbol, TradeType.Buy);
        }

        private void CheckForEMA()
        {
            if (ema1.Result.LastValue > Symbol.Ask)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position); 

MRSV said:

 

This was an example of how to use the Close() method.

If(SMA.Result.LastValue > 1)

Close();

 

It is the same as the method for opening a position, but insted of opening a position it closes it.

So you have for your criteria for closing a position that is the IF("something") then close.

 

In the example i used all positions would close when the Simple Moving Average hit 1.

If you want the position to close when EMA is equal or less the the as you write it like this:

If(EMA.Result.LastValue <= Symbol.Ask)
Close();

 

 

 

 

 


@Invalid

hiba7rain
22 Jul 2014, 10:45

RE: RE: RE:

Hey Invalid,

hope all things fine with you, and thanks again, yes its a bit confusion for me understanding the functions around calgo, but any way I think I did apply the same method you described below and it didnt work with me as I remember .... actually am not sure, I will apply it again and let you know the results

Invalid said:

Hi hiba7rain.

You don't understand smth fundamental yet.

Positions - it's a collection of all of your positions.

if you execute next code:

 foreach (var position in Positions)
{
      ClosePosition(position); 
}

it will close all your positions. If you need to close specific positions, than do as you did to get them and pass them to ClosePosition method.

        protected override void OnTick()
        {
            CheckForEMA();
        }

        private void CheckForEMA()
        {
            if (ema1.Result.LastValue > Symbol.Ask)
            {
                var positionsToClose = Positions.FindAll("calgooob", Symbol, TradeType.Buy);
                foreach (var position in positionsToClose)
                {
                    ClosePosition(position);
                }
            }
        }

hiba7rain said:

hanks for your reply  in fact I also got some help for friends here and I used the following method but am still having a problem that if i use the method all other positions will be closed which i dont want it to happen

the method I use is

protected override void OnTick()
        {
            CheckForEMA();
            var positions = Positions.FindAll("calgooob", Symbol, TradeType.Buy);
        }

        private void CheckForEMA()
        {
            if (ema1.Result.LastValue > Symbol.Ask)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position); 

MRSV said:

 

This was an example of how to use the Close() method.

If(SMA.Result.LastValue > 1)

Close();

 

It is the same as the method for opening a position, but insted of opening a position it closes it.

So you have for your criteria for closing a position that is the IF("something") then close.

 

In the example i used all positions would close when the Simple Moving Average hit 1.

If you want the position to close when EMA is equal or less the the as you write it like this:

If(EMA.Result.LastValue <= Symbol.Ask)
Close();

 

 

 

 

 

 


@hiba7rain

hiba7rain
23 Jul 2014, 09:22

RE: RE: RE:

Hi Invalid,

Thanks again, yes its working fine now after i have change it as you advise me  :) it was helpful to point the missing functions to me , for the moment I will do some modifications and additions before testing the robot live and ill update you , by the way why robots stop functioning once you log out form calgo or ctrader platforms?

 

Invalid said:

Hi hiba7rain.

You don't understand smth fundamental yet.

Positions - it's a collection of all of your positions.

if you execute next code:

 foreach (var position in Positions)
{
      ClosePosition(position); 
}

it will close all your positions. If you need to close specific positions, than do as you did to get them and pass them to ClosePosition method.

        protected override void OnTick()
        {
            CheckForEMA();
        }

        private void CheckForEMA()
        {
            if (ema1.Result.LastValue > Symbol.Ask)
            {
                var positionsToClose = Positions.FindAll("calgooob", Symbol, TradeType.Buy);
                foreach (var position in positionsToClose)
                {
                    ClosePosition(position);
                }
            }
        }

hiba7rain said:

hanks for your reply  in fact I also got some help for friends here and I used the following method but am still having a problem that if i use the method all other positions will be closed which i dont want it to happen

the method I use is

protected override void OnTick()
        {
            CheckForEMA();
            var positions = Positions.FindAll("calgooob", Symbol, TradeType.Buy);
        }

        private void CheckForEMA()
        {
            if (ema1.Result.LastValue > Symbol.Ask)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position); 

MRSV said:

 

This was an example of how to use the Close() method.

If(SMA.Result.LastValue > 1)

Close();

 

It is the same as the method for opening a position, but insted of opening a position it closes it.

So you have for your criteria for closing a position that is the IF("something") then close.

 

In the example i used all positions would close when the Simple Moving Average hit 1.

If you want the position to close when EMA is equal or less the the as you write it like this:

If(EMA.Result.LastValue <= Symbol.Ask)
Close();

 

 

 

 

 

 


@hiba7rain

Invalid
23 Jul 2014, 11:02

RE: RE: RE: RE:

Hi hiba7rain.

CBots are run on the client side (your machine), not on server. You can use VPS in order to keep cbot running on the server (but it will cost some money). Take a look here.

If you need to get some idea about C# language than you can read this post. You don't have to read everything there, just what you consider to be useful for you.

 

 

 

hiba7rain said:

Hi Invalid,

Thanks again, yes its working fine now after i have change it as you advise me  :) it was helpful to point the missing functions to me , for the moment I will do some modifications and additions before testing the robot live and ill update you , by the way why robots stop functioning once you log out form calgo or ctrader platforms?

 


@Invalid

hiba7rain
23 Jul 2014, 11:28

RE: RE: RE: RE: RE:

Thanks ... do you have any other link where I can read about on how to use bars?

Invalid said:

Hi hiba7rain.

CBots are run on the client side (your machine), not on server. You can use VPS in order to keep cbot running on the server (but it will cost some money). Take a look here.

If you need to get some idea about C# language than you can read this post. You don't have to read everything there, just what you consider to be useful for you.

 

 

 

hiba7rain said:

Hi Invalid,

Thanks again, yes its working fine now after i have change it as you advise me  :) it was helpful to point the missing functions to me , for the moment I will do some modifications and additions before testing the robot live and ill update you , by the way why robots stop functioning once you log out form calgo or ctrader platforms?

 

 


@hiba7rain