How do I count and print all sell and buy positions separately?

Created at 26 Jul 2013, 02:39
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!
kricka's avatar

kricka

Joined 13.03.2013

How do I count and print all sell and buy positions separately?
26 Jul 2013, 02:39


Hi,

tried to write the code to count and print only sell positions to the log, even if I have buy positions as well.

Hope you can help me with this.

Thanks..

 


@kricka
Replies

breakermind
26 Jul 2013, 10:36

RE:
kricka said:

Hi,

tried to write the code to count and print only sell positions to the log, even if I have buy positions as well.

Hope you can help me with this.

Thanks..

 

Hi,

maybe something like this

public int sell;
sell = 0;
foreach (var position in Account.Positions)
{
  if (position.TradeType == TradeType.Sell)
   {
    sell = sell +1;
   }
}
Print("Sell positions: ");
Print(sell);

 

I have not tested this code.


if something you do not know to find search for answers in the finished robots

Regards

 

 


@breakermind

kricka
27 Jul 2013, 02:35

Thanks breakermind,

going to try the code out some more, did not get it work the first try but I'll see after some more testing.


@kricka

kricka
17 Aug 2013, 05:50

Hi Spotware,

maybe you can give me the correct code on my question how to count and print total Sell positions to the log?

Thanks..


@kricka

breakermind
17 Aug 2013, 13:51

RE:

kricka said:

Hi Spotware,

maybe you can give me the correct code on my question how to count and print total Sell positions to the log?

Thanks..

Hi

Tested count positions and print all positions number to log

Bye

// -------------------------------------------------------------------------------
//
//    This is a Template used as a guideline to build your own Robot. 
//
// -------------------------------------------------------------------------------

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

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

        private Position _position;


        [Parameter(DefaultValue = 10000, MinValue = 1000)]
        public int Volume { get; set; }
//================================================================================
//                                                                      OnStart
//================================================================================
        protected override void OnStart()
        {

            // delete if You dont need add positions on start
            Buy();
            Buy();

            Sell();
            Sell();
            Sell();


            Buy();
            Buy();
            Buy();


            Sell();
            Sell();
            Sell();
            Sell();
            Sell();
        }

//================================================================================
//                                                                       OnTick
//================================================================================
        protected override void OnTick()
        {

        }

//================================================================================
//                                                                        OnBar
//================================================================================
        protected override void OnBar()
        {

        }

//================================================================================
//                                                             OnPositionOpened
//================================================================================
        protected override void OnPositionOpened(Position openedPosition)
        {
            int BuyPos = 0;
            int SellPos = 0;

            foreach (var position in Account.Positions)
            {

                if (position.TradeType == TradeType.Buy)
                {
                    BuyPos = BuyPos + 1;
                    //or
                    // BuyPos++;
                }
                if (position.TradeType == TradeType.Sell)
                {
                    SellPos = SellPos + 1;
                    //or
                    // SellPos++;

                }

            }

            Print("All Buy positions: " + BuyPos);
            Print("All Sell positions: " + SellPos);


        }
        // end OnPositionOpened
//================================================================================
//                                                             OnPositionClosed
//================================================================================
        protected override void OnPositionClosed(Position closedPosition)
        {

        }

//================================================================================
//                                                                       OnStop
//================================================================================
        protected override void OnStop()
        {

        }

//================================================================================
//                                                                     Function
//================================================================================
        private void Buy()
        {
            Trade.CreateBuyMarketOrder(Symbol, Volume);
        }

        private void Sell()
        {
            Trade.CreateSellMarketOrder(Symbol, Volume);
        }

        private void ClosePosition()
        {
            if (_position != null)
            {
                Trade.Close(_position);
                _position = null;
            }
        }
//================================================================================
//                                                                    Robot end
//================================================================================
    }
}

@breakermind

kricka
18 Aug 2013, 02:19

Thanks breakermind, once agin!

Will test the code when the market opens tomorrow. Good that you got it working and printed OK.

One question though my robot do not opening positions only closes positions, so it'll be interesting to see if it it will work in my robot. 

 


@kricka