before buying or selling, how to check if i have that pair already open position ?

Created at 27 Mar 2017, 08:44
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!
HA

handiphangceo@gmail.com

Joined 22.03.2017

before buying or selling, how to check if i have that pair already open position ?
27 Mar 2017, 08:44


before buying or selling, how to check if i have that pair already open position ?

what code chould i write ?


@handiphangceo@gmail.com
Replies

201058436
27 Mar 2017, 11:08

Hi Handi

I new to programming, so please take this with a grain of salt:

            //Check To See If Any Pending Orders Are Open
            var TotalOrders = Position.Count;

            {
                //Check if TotalOrders (any position) is open, if so, do not go to next line
                if (TotalOrders = null)
                    
                    //next line
                    ExecuteMarketOrder()
            }

Hope it helps.


@201058436

201058436
27 Mar 2017, 11:09

RE: Hi Handi

AthenasGuidance said:

//Changed spelling mistake.

I'm new to programming, so please take this with a grain of salt:

            //Check To See If Any Pending Orders Are Open
            var TotalOrders = Position.Count;

            {
                //Check if TotalOrders (any position) is open, if so, do not go to next line
                if (TotalOrders = null)
                    
                    //next line
                    ExecuteMarketOrder()
            }

Hope it helps.

 


@201058436

handiphangceo@gmail.com
27 Mar 2017, 12:21

hellow, can you check is this correct..

 

protected override void OnBar()
        {
            var positionsBuy = Positions.FindAll("Buy");
            var positionsSell = Positions.FindAll("Sell");

            var distanceFromUpKumo = (Symbol.Bid - ichimoku.SenkouSpanA.Last(26)) / Symbol.PipSize;
            var distanceFromDownKumo = (ichimoku.SenkouSpanA.Last(26) - Symbol.Ask) / Symbol.PipSize;



            if (MarketSeries.Open.Last(1) <= ichimoku.SenkouSpanA.Last(27) && MarketSeries.Open.Last(1) > ichimoku.SenkouSpanB.Last(27))
            {
                if (MarketSeries.Close.Last(1) > ichimoku.SenkouSpanA.Last(27))
                {
                    if (distanceFromUpKumo <= 25)
                    {

                       //but if i put this way, i can only have 1 opened position at one momment, am i right ?

                        if (position.TradeType != TradeType.Buy && TradeType.Sell)

                        {
                        ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);

                        }
                    }
                }
            }

 


@handiphangceo@gmail.com

handiphangceo@gmail.com
27 Mar 2017, 13:43

if (position.SymbolCode == Symbol.Code && positionsBuy.Length == 0 && positionsSell.Length == 0)

 

if i put this way it cannot execute buy/sell ^^''


@handiphangceo@gmail.com

handiphangceo@gmail.com
27 Mar 2017, 13:45

RE: Hi Handi

AthenasGuidance said:

I new to programming, so please take this with a grain of salt:

            //Check To See If Any Pending Orders Are Open
            var TotalOrders = Position.Count;

            {
                //Check if TotalOrders (any position) is open, if so, do not go to next line
                if (TotalOrders = null)
                    
                    //next line
                    ExecuteMarketOrder()
            }

Hope it helps.

 thanks for replying :)


@handiphangceo@gmail.com

201058436
27 Mar 2017, 13:55

RE:

handiphangceo@gmail.com said:

hellow, can you check is this correct..

 

protected override void OnBar()
        {
            var positionsBuy = Positions.FindAll("Buy");
            var positionsSell = Positions.FindAll("Sell");

            var distanceFromUpKumo = (Symbol.Bid - ichimoku.SenkouSpanA.Last(26)) / Symbol.PipSize;
            var distanceFromDownKumo = (ichimoku.SenkouSpanA.Last(26) - Symbol.Ask) / Symbol.PipSize;



            if (MarketSeries.Open.Last(1) <= ichimoku.SenkouSpanA.Last(27) && MarketSeries.Open.Last(1) > ichimoku.SenkouSpanB.Last(27))
            {
                if (MarketSeries.Close.Last(1) > ichimoku.SenkouSpanA.Last(27))
                {
                    if (distanceFromUpKumo <= 25)
                    {

                       //but if i put this way, i can only have 1 opened position at one momment, am i right ?

                        if (position.TradeType != TradeType.Buy && TradeType.Sell)

                        {
                        ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);

                        }
                    }
                }
            }

 

 

Hi Handi

I'm sorry I don't think that is correct ..however, I could be wrong.

If the previous code did not work, try the one below, the previous one was written not copied and pasted:

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.CentralStandardTime, AccessRights = AccessRights.None)]
    public class BigBarRobot : Robot
    {
        [Parameter("Volume:", DefaultValue = 1, MinValue = 1, Step = 1)]
        public int Volume { get; set; }
        [Parameter("StopLossPips:", DefaultValue = 10, MinValue = 1, Step = 0.5)]
        public double StopLossPips { get; set; }
        [Parameter("TakeProfitPips:", DefaultValue = 20, MinValue = 1, Step = 0.5)]
        public double TakeProfitPips { get; set; }

 protected override void OnBar()
        {
            //Search All Positions Open And Label That Search "TotalPositions"
            var TotalPositions = Positions.Count;
            {
                //If The Search Labeled "TotalPositions" Reports That No Position Is Open (Null or Less Than 1 or zero), Then, ExecuteMarketOrder
                if (TotalPositions < 1)
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);
            }
        }
    }
}

Enjoy.


@201058436

201058436
27 Mar 2017, 14:04

RE: RE:

AthenasGuidance said:

handiphangceo@gmail.com said:

hellow, can you check is this correct..

 

protected override void OnBar()
        {
            var positionsBuy = Positions.FindAll("Buy");
            var positionsSell = Positions.FindAll("Sell");

            var distanceFromUpKumo = (Symbol.Bid - ichimoku.SenkouSpanA.Last(26)) / Symbol.PipSize;
            var distanceFromDownKumo = (ichimoku.SenkouSpanA.Last(26) - Symbol.Ask) / Symbol.PipSize;



            if (MarketSeries.Open.Last(1) <= ichimoku.SenkouSpanA.Last(27) && MarketSeries.Open.Last(1) > ichimoku.SenkouSpanB.Last(27))
            {
                if (MarketSeries.Close.Last(1) > ichimoku.SenkouSpanA.Last(27))
                {
                    if (distanceFromUpKumo <= 25)
                    {

                       //but if i put this way, i can only have 1 opened position at one momment, am i right ?

                        if (position.TradeType != TradeType.Buy && TradeType.Sell)

                        {
                        ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);

                        }
                    }
                }
            }

 

 

Hi Handi

I'm sorry I don't think that is correct ..however, I could be wrong.

If the previous code did not work, try the one below, the previous one was written not copied and pasted:

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.CentralStandardTime, AccessRights = AccessRights.None)]
    public class BigBarRobot : Robot
    {
        [Parameter("Volume:", DefaultValue = 1, MinValue = 1, Step = 1)]
        public int Volume { get; set; }
        [Parameter("StopLossPips:", DefaultValue = 10, MinValue = 1, Step = 0.5)]
        public double StopLossPips { get; set; }
        [Parameter("TakeProfitPips:", DefaultValue = 20, MinValue = 1, Step = 0.5)]
        public double TakeProfitPips { get; set; }

 protected override void OnBar()
        {
            //Search All Positions Open And Label That Search "TotalPositions"
            var TotalPositions = Positions.Count;
            {
                //If The Search Labeled "TotalPositions" Reports That No Position Is Open (Null or Less Than 1 or zero), Then, ExecuteMarketOrder
                if (TotalPositions < 1)
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);
            }
        }
    }
}

Enjoy.

 

..Now I only wish someone would help me with my coding problem.. "/forum/cbot-support/11404".


@201058436

handiphangceo@gmail.com
27 Mar 2017, 14:33

RE: RE:

AthenasGuidance said:

handiphangceo@gmail.com said:

hellow, can you check is this correct..

 

protected override void OnBar()
        {
            var positionsBuy = Positions.FindAll("Buy");
            var positionsSell = Positions.FindAll("Sell");

            var distanceFromUpKumo = (Symbol.Bid - ichimoku.SenkouSpanA.Last(26)) / Symbol.PipSize;
            var distanceFromDownKumo = (ichimoku.SenkouSpanA.Last(26) - Symbol.Ask) / Symbol.PipSize;



            if (MarketSeries.Open.Last(1) <= ichimoku.SenkouSpanA.Last(27) && MarketSeries.Open.Last(1) > ichimoku.SenkouSpanB.Last(27))
            {
                if (MarketSeries.Close.Last(1) > ichimoku.SenkouSpanA.Last(27))
                {
                    if (distanceFromUpKumo <= 25)
                    {

                       //but if i put this way, i can only have 1 opened position at one momment, am i right ?

                        if (position.TradeType != TradeType.Buy && TradeType.Sell)

                        {
                        ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);

                        }
                    }
                }
            }

 

 

Hi Handi

I'm sorry I don't think that is correct ..however, I could be wrong.

If the previous code did not work, try the one below, the previous one was written not copied and pasted:

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.CentralStandardTime, AccessRights = AccessRights.None)]
    public class BigBarRobot : Robot
    {
        [Parameter("Volume:", DefaultValue = 1, MinValue = 1, Step = 1)]
        public int Volume { get; set; }
        [Parameter("StopLossPips:", DefaultValue = 10, MinValue = 1, Step = 0.5)]
        public double StopLossPips { get; set; }
        [Parameter("TakeProfitPips:", DefaultValue = 20, MinValue = 1, Step = 0.5)]
        public double TakeProfitPips { get; set; }

 protected override void OnBar()
        {
            //Search All Positions Open And Label That Search "TotalPositions"
            var TotalPositions = Positions.Count;
            {
                //If The Search Labeled "TotalPositions" Reports That No Position Is Open (Null or Less Than 1 or zero), Then, ExecuteMarketOrder
                if (TotalPositions < 1)
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);
            }
        }
    }
}

Enjoy.

Hi, i tried and i can only open position for 1 transaction :)


@handiphangceo@gmail.com

201058436
27 Mar 2017, 14:43

RE: RE: RE:

handiphangceo@gmail.com said:

 

Hi, i tried and i can only open position for 1 transaction :)

 

 

What do you mean by 'Transaction'?

,,Do you mean pair?


@201058436

handiphangceo@gmail.com
27 Mar 2017, 14:48

RE: RE: RE: RE:

AthenasGuidance said:

handiphangceo@gmail.com said:

 

Hi, i tried and i can only open position for 1 transaction :)

 

 

What do you mean by 'Transaction'?

,,Do you mean pair?

Yes Sir.

i sorry for my broken English, 

so why im asking the code is because the bot keep opening multiple transaction, example : it buy EURUSD, then another minute it buy EURUSD again.

i want to check if i already have EURUSD opened, then the bot no need to open position anymore. But i want them to keep able buy/sell another pair :)


@handiphangceo@gmail.com

201058436
27 Mar 2017, 15:18

RE: RE: RE: RE: RE:

handiphangceo@gmail.com said:

Yes Sir.

i sorry for my broken English, 

so why im asking the code is because the bot keep opening multiple transaction, example : it buy EURUSD, then another minute it buy EURUSD again.

i want to check if i already have EURUSD opened, then the bot no need to open position anymore. But i want them to keep able buy/sell another pair :)

Try this, if it doesn't work, I can't help you anymore.. my programming is too limited:

 protected override void OnBar()
        {        
            foreach (Position Position in Positions.FindAll("Buy", null, TradeType.Buy))
            {
                if (Position.SymbolCode != Symbol.Code)
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);
            }
        }

@201058436

201058436
27 Mar 2017, 15:25

RE: RE: RE: RE: RE: RE:

You may also have to add this:

&& Position.Label == "Buy")

 


@201058436

201058436
27 Mar 2017, 15:28

RE: RE: RE: RE: RE: RE: RE:

AthenasGuidance said:

You may also have to add this:

&& Position.Label == "Buy")

..and of course, an else if function for the "Sell" positions.

For example:

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.CentralStandardTime, AccessRights = AccessRights.None)]
    public class BigBarRobot : Robot
    {
        [Parameter("Volume:", DefaultValue = 1, MinValue = 1, Step = 1)]
        public int Volume { get; set; }
        [Parameter("StopLossPips:", DefaultValue = 10, MinValue = 1, Step = 0.5)]
        public double StopLossPips { get; set; }
        [Parameter("TakeProfitPips:", DefaultValue = 20, MinValue = 1, Step = 0.5)]
        public double TakeProfitPips { get; set; }

protected override void OnBar()
        {  
            foreach (Position Position in Positions.FindAll("Buy", null, TradeType.Buy))
            foreach (Position Position in Positions.FindAll("Buy", null, TradeType.Buy))
            {
                if (Position.SymbolCode != Symbol.Code && Position.Label == "Buy")
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);
                else if (Position.SymbolCode != Symbol.Code && Position.Label == "Sell")
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Sell", StopLossPips, TakeProfitPips);
            }
        }
    }
}

And then of course, add all of your other IF statements that you need, your other conditions.


@201058436

201058436
27 Mar 2017, 15:29

RE: RE: RE: RE: RE: RE: RE: RE:
 
            foreach (Position Position in Positions.FindAll("Buy", null, TradeType.Buy))
            foreach (Position Position in Positions.FindAll("Sell", null, TradeType.Sell))

@201058436

201058436
27 Mar 2017, 15:30

RE: RE: RE: RE: RE: RE: RE: RE: RE:

AthenasGuidance said:

        {  
            foreach (Position PositionLong in Positions.FindAll("Buy", null, TradeType.Buy))
            foreach (Position PositionShort in Positions.FindAll("Buy", null, TradeType.Buy))
            {
                if (PositionLong.SymbolCode != Symbol.Code && PositionLong.Label == "Buy")
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);
                else if (PositionShort.SymbolCode != Symbol.Code && PositionShort.Label == "Sell")
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Sell", StopLossPips, TakeProfitPips);
            }
        }
    }
}

 

I wish there were a way to edit a post or simply delete them entirely.


@201058436

201058436
27 Mar 2017, 15:31

RE: RE: RE: RE: RE: RE: RE: RE: RE: RE:

AthenasGuidance said:

AthenasGuidance said:

        {  
            foreach (Position PositionLong in Positions.FindAll("Buy", null, TradeType.Buy))
            foreach (Position PositionShort in Positions.FindAll("Buy", null, TradeType.Buy))
            {
                if (PositionLong.SymbolCode != Symbol.Code && PositionLong.Label == "Buy")
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);
                else if (PositionShort.SymbolCode != Symbol.Code && PositionShort.Label == "Sell")
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Sell", StopLossPips, TakeProfitPips);
            }
        }
    }
}

I don't know what happened to the previous code.


@201058436

201058436
27 Mar 2017, 15:40

RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE:

AthenasGuidance said:

Please change the second foreach functions label from "Buy" to "Sell".


@201058436

handiphangceo@gmail.com
27 Mar 2017, 15:47

RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE:

AthenasGuidance said:

AthenasGuidance said:

Please change the second foreach functions label from "Buy" to "Sell".

build succeeded but cannot execute buy or sell, anyway thanks for helping me sir, i hope someone can help yours too :)


@handiphangceo@gmail.com

201058436
27 Mar 2017, 15:58

RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE:

handiphangceo@gmail.com said:

build succeeded but cannot execute buy or sell, anyway thanks for helping me sir, i hope someone can help yours too :)

Would it be alright to have a look at your code in full, even if it has to be privately exchanged?


@201058436

201058436
27 Mar 2017, 16:39

RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE:

handiphangceo@gmail.com said:

build succeeded but cannot execute buy or sell, anyway thanks for helping me sir, i hope someone can help yours too :)

It could be because of the foreach statement (Position instead of (var PositionLong..

        {  
            foreach (var PositionLong in Positions.FindAll("Buy", null, TradeType.Buy))
            foreach (var PositionShort in Positions.FindAll("Sell", null, TradeType.Sell))
            {
                if (PositionLong.SymbolCode != Symbol.Code && PositionLong.Label == "Buy")
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);
                else if (PositionShort.SymbolCode != Symbol.Code && PositionShort.Label == "Sell")
                    ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "Sell", StopLossPips, TakeProfitPips);
            }
        }
    }
}

 


@201058436

201058436
27 Mar 2017, 17:05

RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE:

AthenasGuidance said:

It could be because of the foreach statement (Position instead of (var PositionLong..

And you may need these before the foreach statements:

            var PositionLong = Positions.Find("Buy", null, TradeType.Buy);
            var PositionShort = Positions.Find("Sell, null, TradeType.Sell);

 


@201058436

handiphangceo@gmail.com
27 Mar 2017, 17:29

RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE:

AthenasGuidance said:

AthenasGuidance said:

It could be because of the foreach statement (Position instead of (var PositionLong..

And you may need these before the foreach statements:

            var PositionLong = Positions.Find("Buy", null, TradeType.Buy);
            var PositionShort = Positions.Find("Sell, null, TradeType.Sell);

 

alot or error > . <

sure i can show you, send me your email address


@handiphangceo@gmail.com

201058436
27 Mar 2017, 17:48

RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE:

handiphangceo@gmail.com said:

AthenasGuidance said:

AthenasGuidance said:

It could be because of the foreach statement (Position instead of (var PositionLong..

And you may need these before the foreach statements:

            var PositionLong = Positions.Find("Buy", null, TradeType.Buy);
            var PositionShort = Positions.Find("Sell, null, TradeType.Sell);

 

alot or error > . <

sure i can show you, send me your email address

 

Email: AthenasGuidance@sharklasers.com

It's a temporary disposable email.


@201058436

handiphangceo@gmail.com
27 Mar 2017, 17:57

RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE:

AthenasGuidance said:

handiphangceo@gmail.com said:

AthenasGuidance said:

AthenasGuidance said:

It could be because of the foreach statement (Position instead of (var PositionLong..

And you may need these before the foreach statements:

            var PositionLong = Positions.Find("Buy", null, TradeType.Buy);
            var PositionShort = Positions.Find("Sell, null, TradeType.Sell);

 

alot or error > . <

sure i can show you, send me your email address

 

Email: AthenasGuidance@sharklasers.com

It's a temporary disposable email.

done


@handiphangceo@gmail.com

201058436
27 Mar 2017, 18:42

RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE:

done

I've sent you an email, did you get it?


@201058436

201058436
27 Mar 2017, 20:25

RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE: RE:

AthenasGuidance said:

done

I've sent you an email, did you get it?

I don't know why, but that code should have solve the issue?!? ..I'm sorry it didn't.


@201058436