Delete Pending Orders When Profit

Created at 30 Nov 2013, 15:15
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!
MA

marafi5

Joined 30.11.2013

Delete Pending Orders When Profit
30 Nov 2013, 15:15


Hello

I need a help for making cBot for a small idea: If my account is making profit , all pending order should be deleted.

Thanks


@marafi5
Replies

Cerunnos
01 Dec 2013, 09:32

RE:

marafi5 said:

Hello

I need a help for making cBot for a small idea: If my account is making profit , all pending order should be deleted.

Thanks

if (Account.Equity >= startingBalance * BalancePercent)
{
  foreach (var order in PendingOrders)
  {
  CancelPendingOrder(order);
  }

}


@Cerunnos

marafi5
02 Dec 2013, 01:52

RE: RE:

Its showed to me in cAlgo this:

Error CS0116: A namespace cannot directly contain members such as fields or methods

 

Cerunnos said:

marafi5 said:

Hello

I need a help for making cBot for a small idea: If my account is making profit , all pending order should be deleted.

Thanks

if (Account.Equity >= startingBalance * BalancePercent)
{
  foreach (var order in PendingOrders)
  {
  CancelPendingOrder(order);
  }

}

 


@marafi5

Spotware
02 Dec 2013, 11:06

You probably omitted the class declaration.

The structure is

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class NewRobot : Robot
    {
...

You can use the New button in the cAlgo editor for convenience.


@Spotware

marafi5
04 Dec 2013, 22:15

Hello

 "I made this code but its works only when I open it , after that it will not work automaticlly , I think that its needs a code line ''for'

 

namespace cAlgo.Robots
{

    [Robot(TimeZone = TimeZones.UTC)]
    public class SampleCloseProfitablePositions : Robot
    {
        protected override void OnStart()
        {
            foreach (var position in PendingOrders)
            {

                for (int i = 0; i > 1; i--)
                    if (Account.Equity >= 0)
                    {
                        CancelPendingOrder(position);
                    }

            }

        }

    }

}

 


@marafi5

adaled
05 Dec 2013, 09:33

You have to remove the for (int i = 0; i > 1; i--). It doesn't have any meaning, it is formulated wrong. Maybe read a little tutorial on programming.

The foreach statement is a loop. It will go through all elements in the list. You don't need another loop inside that.

Also, the if(Account.Equity >=0) doesn't make sense to be inside the foreach loop, since it will be the same in each iteration. it should be outside to reduce extra steps.

 


@adaled

jeex
05 Dec 2013, 10:31

Just help the poor bugger

As not every trader is a potential programmer, let's just help marafi5: First put this method just beneath the OnTick(){ } method.

private void closeWhenPos()
        {
            double profit = 0;

            // first make a sum of all the open trades' profits
            foreach (Position p in Account.Positions)
                profit += p.GrossProfit;

            // when profit is negative, we're done
            if (profit <= 0)
                return;

            // otherwise profit > 0 so close all pending orders
            foreach (PendingOrder o in Account.PendingOrders)
                Trade.DeletePendingOrder(o);

            // then stop the robot
            Stop();
        }

Then put the line closeWhenPos(); into you ontick method.

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

Start the Robot on whatever pair you want - the pair does not matter in this case - and as soon as the total profit of open positions > 0, the pending orders will be closed. That could be immediate, or after hours of waiting.

I use a similar bot to close all positions when my daily (or weekly) target is met:

private void closeWhenTarget(double target)
        {
            double profit = 0;

            // first make a sum of all the open trades' profits
            foreach (Position p in Account.Positions)
                profit += p.GrossProfit;

            // when profit is negative, we're done
            if (profit < target)
                return;

            // otherwise profit > 0 so close all pending orders
            foreach (PendingOrder o in Account.PendingOrders)
                Trade.DeletePendingOrder(o);

            // and close all open positions
            foreach (Position p in Account.Positions)
                Trade.Close(p);

            // then stop the robot
            Stop();
        }

Where the variable target is set by a parameter.

Hoped this helps you out on your quest.


@jeex

nirin
16 Dec 2013, 18:09

RE: Just help the poor bugger

jeex said:

As not every trader is a potential programmer, let's just help marafi5: First put this method just beneath the OnTick(){ } method.

private void closeWhenPos()
        {
            double profit = 0;

            // first make a sum of all the open trades' profits
            foreach (Position p in Account.Positions)
                profit += p.GrossProfit;

            // when profit is negative, we're done
            if (profit <= 0)
                return;

            // otherwise profit > 0 so close all pending orders
            foreach (PendingOrder o in Account.PendingOrders)
                Trade.DeletePendingOrder(o);

            // then stop the robot
            Stop();
        }

Then put the line closeWhenPos(); into you ontick method.

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

Start the Robot on whatever pair you want - the pair does not matter in this case - and as soon as the total profit of open positions > 0, the pending orders will be closed. That could be immediate, or after hours of waiting.

I use a similar bot to close all positions when my daily (or weekly) target is met:

private void closeWhenTarget(double target)
        {
            double profit = 0;

            // first make a sum of all the open trades' profits
            foreach (Position p in Account.Positions)
                profit += p.GrossProfit;

            // when profit is negative, we're done
            if (profit < target)
                return;

            // otherwise profit > 0 so close all pending orders
            foreach (PendingOrder o in Account.PendingOrders)
                Trade.DeletePendingOrder(o);

            // and close all open positions
            foreach (Position p in Account.Positions)
                Trade.Close(p);

            // then stop the robot
            Stop();
        }

Where the variable target is set by a parameter.

Hoped this helps you out on your quest.

Hi,

If the robot contains the takeprofit, does it still work?


@nirin

jeex
16 Dec 2013, 22:24

Jep

Yes, the script has nothing to do with takeprofit or stoploss.
 


@jeex

GoldnOil
26 Dec 2013, 11:08

RE:

can SPOTWARE help me, in choosing some excellent coders for my robot.  like one with some good history.  I just want to build a very basic robot, so it helps me learn coding also and i don't have to sit in from of the screen all day !   I am ready to pay through western union on personal name, if someone is ready. my budget is USD 50.  Check-out below to see what i really want.

 

******************************************************************************

A simple Buy and Sell Robot

 

Name : SK Pips Buy Sell Robot - Dec'13

 

Concept :

This would be simple robot  that "opens a position" of Buy or Sell depending on certain conditions.

 

User  Inputs :

(1) "Select Item" = Gold, Silver or Currencies or Oil

(2) "Start Price" = 1190 (when this price is crossed, the robot will trigger. i.e. it starts

 

(3) "Open Buy" = +200 pips (when a certain Pips as defined are reached, then an BUY position is opened, and a 'Current Pip' is reset to zero again)

(4) "Delay Buy Time" = 60 sec  (the time robots waits, before opening a Buy position and sees the price is still above +200 pips or as defined by user in # (3).

 

(5) "Open Sell" = -150 pips (when a certain Pips as defined are reached, then a SELL position is opened, a 'Current Pip' is reset to zero again)

(6) "Delay Sell Time" = 60 sec  (the time robots waits, before opening a SELL position and sees the price is still above +200 pips or as defined by user in # (5), then only it opens the Sell Position.

 

(7) "Numbers of Buy" = 5  (max. numbers of buys to open)

(8) "Numbers of Sell" = 3  (max. numbers of sells to open)

 

(9) "Buy Size" = 10 oz (the size of the position to be opened)

(10) "Sell Size" = 5 oz (the size of the position to be opened)

 

(11) "Stop Equity Profit" = +USD 100 (if this increase is reached in the original equity, the Robots close all positions).

(12) "Stop Equity Loss" = -USD 20 (if this decrease is reached in the original equity, the Robots close all positions).

(13) "Warning" = 500% margin  (to flag a margin on screen, through flashing and sound that margin is lower, then the defined user limit.

(14) "After Close All" = repeat or stop  (if all transactions has been closed, then should the Robot start again or stop and wait for user)

 

Some constants and values :

(a) Master Pips :  it records the the overall Pips status, when the Robot started.

(b) Current Pip : it is reset to 'zero', when ever a Position is opened and Robot always checks it value before opening a position

(c) Total Buy Count = records the number of buy position has been opened so far.

(d) Total Sell Count = records the number of buy position has been opened so far.

 

The Theme :

Robot activates/starts when certain price as mentioned is reached.

Now it watches the pips as defined by the user.

When the price increases by that certain pips as mentioned by the user, it starts monitoring the time as mentioned by the user, and if after that time has passed and the PRICE is still above that certain pips mentioned by the user, IT OPENS the position, resets the 'current pips' back to zero and monitor the price again. It opens position on the 'current pips' value and not on 'master pips' value.

 

It only opens DEFINED number of BUYS & SELLs by the user. And when equity has reached a certain defined target, it closes all the positions and start again or wait for the user.

It has some warning for the user to take control, and disengage the robot.

 

*****************************************************************************************************************************

 

 


@GoldnOil

jeremy.vostik
04 Jun 2016, 19:31

Hi, your code does not work. It closes my trades in Profit after just 0.4 pips.

What's wrong?


@jeremy.vostik

fxclicker
15 Oct 2017, 15:25

How to get all pending position based on pair specific only and cancel the pending orders once the pair is gaining profit.

You got 5 pending order buy in AUDUSD and 2 of them are triggered then it gaining profit, you wanted to cancel the remaining 3 nad move the position into positive like trailing stop.


@fxclicker