QuickStart - Place Limit Order

Spotware's avatar

Spotware since: 23 Sep 2013;

  14 Jan 2014, 14:40
QuickStart - Place Limit Order
// ---------------------------------------------------------------------------------------
//
//    In "LimitOrder" cBot places a limit order on start, with a label 
//    representing the robot, symbol and timeframe of the instance.
//    On each tick it checks if the order has been filled.
//
// ---------------------------------------------------------------------------------------
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot]
    public class LimitOrder : Robot
    {
        private bool _filled;

        [Parameter(DefaultValue = "LimitOrder cBot")]
        public string RobotLabel { get; set; }

        [Parameter(DefaultValue = 10)]
        public int Target { get; set; }

        [Parameter(DefaultValue = 10000, MinValue = 1000)]
        public int Volume { get; set; }

        [Parameter(DefaultValue = true)]
        public bool Buy { get; set; }

        [Parameter(DefaultValue = 10)]
        public double TakeProfitPips { get; set; }

        [Parameter(DefaultValue = 10)]
        public double StopLossPips { get; set; }

        protected string MyLabel
        {
            get { 
                    return string.Format("{0} {1} {2}", 
                                     RobotLabel, Symbol.Code, TimeFrame); 
                }
        }

        protected TradeType TradeType
        {
            get { return Buy ? TradeType.Buy : TradeType.Sell; }
        }

        protected override void OnStart()
        {
            double targetPrice = TradeType == TradeType.Buy
                                     ? Symbol.Ask - Target*Symbol.PipSize
                                     : Symbol.Bid + Target*Symbol.PipSize;

            PlaceLimitOrder(TradeType, Symbol, Volume, targetPrice, MyLabel, 
                             StopLossPips, TakeProfitPips);
        }

        protected override void OnTick()
        {
            if (_filled) 
                return;

            var position = Positions.Find(MyLabel);            
            if (position == null) 
                return;

            _filled = true;
              Print("Limit Order Filled: ID = {0}, Entry Price = {1}, 
                   Label = {2}", position.Id, position.EntryPrice, position.Label);
        
        }
    }
}
TRADERS FIRST™ Vote for your favorite features: https://ctrader.com/forum/suggestions
DrAlonso's avatar

DrAlonso since: 06 May 2014;

  06 May 2014, 19:09
RE: Robot Malfunction

Good evening to anybody,

I'm new in this forum trying to learn how to use C# to develop my own robots and indicator, and while I was studying this robot, I couldn't make it to print the log results described in the last lines:

              Print("Limit Order Filled: ID = {0}, Entry Price = {1}, Label = {2}", position.Id, position.EntryPrice, position.Label);

I tryied then to print the log results without any conditions of execution, having no problems in compiling but getting this error during execution: "Crashed InOnTick with NullReferenceException" . I think that the code is unable to assign to variable "position" any value.

            var position = Positions.Find(MyLabel);

Any other ideas to fix this problems? Thank you very much in advance!

Dr. Alonso

Spotware's avatar

Spotware since: 23 Sep 2013;

  07 May 2014, 09:20

If position with such label doesn't exist Positions.Find will return null. After that you need to check position variable for null as it is shown in the example:

            var position = Positions.Find(MyLabel);            
            if (position == null) 
                return;
TRADERS FIRST™ Vote for your favorite features: https://ctrader.com/forum/suggestions
DrAlonso's avatar

DrAlonso since: 06 May 2014;

  07 May 2014, 13:03
RE:

Ok, got it! So to show the last Print line in the log, the position must be triggered. If it's only placed as a Limit Order but not activated, it doesn't show the last Print line.. 

Thank you very much for your help, regards,

Dr. Alonso

HTtrader since: 19 Jul 2017;

  12 Aug 2017, 03:25

I would like to know about expiries on limit and stop orders

            PlaceLimitOrder(TradeType, Symbol, Volume, targetPrice, MyLabel, 
                             StopLossPips, TakeProfitPips, Expiration);

If I understand it correctly Expiration comes last and what is exacrtly the frame of calculation hours or minutes?

Say I wanted a limit order to have a 1hr expiry would the code be

            PlaceLimitOrder(TradeType, Symbol, Volume, targetPrice, MyLabel, 
                             StopLossPips, TakeProfitPips, 1);

or 

            PlaceLimitOrder(TradeType, Symbol, Volume, targetPrice, MyLabel, 
                             StopLossPips, TakeProfitPips, 60);

or do we declare it as a variable?

Spotware's avatar

Spotware since: 23 Sep 2013;

  16 Aug 2017, 11:22

Dear hungtonydang,

The parameter is a DateTime. See more info here. You need to specify the exact date time that the order expires. See an example below

var datetime = new DateTime(2017,8,16,11,25,0);
PlaceLimitOrder(TradeType, Symbol, Volume, targetPrice);

Best Regards,

cTrader Team

TRADERS FIRST™ Vote for your favorite features: https://ctrader.com/forum/suggestions

HTtrader since: 19 Jul 2017;

  17 Aug 2017, 00:59
Is there no way to set an expiration on a timer? I have a bot that changes with an onbar action and so would like to have a limit order only hang around for 1 hr as that is the instance I am using. Can we call on a timer?
Spotware's avatar

Spotware since: 23 Sep 2013;

  17 Aug 2017, 09:23

Dear hungtonydang,

If you need to set the expiration time one hour after placing the order, you can try something like the following

var datetime = DateTime.Now.AddHours(1);
            PlaceLimitOrder(TradeType, Symbol, Volume, targetPrice, MyLabel, StopLossPips, TakeProfitPips, datetime);

Best Regards,

cTrader Team

TRADERS FIRST™ Vote for your favorite features: https://ctrader.com/forum/suggestions

HTtrader since: 19 Jul 2017;

  05 Sep 2017, 16:23

Hi ctrader team,

So I finally got round to trsting this piece of code, however rather than give me a 1 hour expiry time it gives me a 10 hour expiry time. Even when I change it to minutes it starts from 10 hours and counts back, say I put in



var datetime = DateTime.Now.AddMinutes(45);

It gives me a 9hour and 45 minutes expiry, is this a bug or am I missing something? 

Spotware's avatar

Spotware since: 23 Sep 2013;

  06 Sep 2017, 09:37

Hi hungtonydang,

Please let us know your timezone and your cBot's time zone. This might explain the time difference you get. You can find the cBot's timezone in the cBots attributes. See the example below

    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HedgingandScalpingAlgo : Robot
    {

Best Regards,

cTrader Team

TRADERS FIRST™ Vote for your favorite features: https://ctrader.com/forum/suggestions