Newbie learns to code Cbot

Created at 09 Jun 2014, 12:50
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!
TE

tete13

Joined 06.05.2014

Newbie learns to code Cbot
09 Jun 2014, 12:50


I'm a newbie in C# language. I want to code a simple Cbot with the following functions:

1. Place a sell stop order at specificed time (ex: 30s before news).

2. Modify sell stop order just before news (ex: 5s before news)

3. Add trailing stopsloss if the sell order was filled.

4. Delete sell stop order after News (ex: 15s after news)


@tete13
Replies

tete13
09 Jun 2014, 12:56

1. Place a sell stop order at specificed time (ex: 30s before news).

In this step, I want to place a sell stop order 30s before news, the open price is far from current market price (let's say 50 pips) without stop loss, take profit and expiration time.

I use this code and it works well.

using System; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class Stoporder : Robot { [Parameter("News Day (1-5)", DefaultValue = 1, MinValue = 1, MaxValue = 5)] public int NewsDay { get; set; } [Parameter("News Hour", DefaultValue = 9, MinValue = 0, MaxValue = 23)] public int NewsHour { get; set; } [Parameter("News Minute", DefaultValue = 30, MinValue = 0, MaxValue = 59)] public int NewsMinute { get; set; } [Parameter("Volume", DefaultValue = 10000, MinValue = 10000)] public int Volume { get; set; } [Parameter("Seconds Before", DefaultValue = 30, MinValue = 1)] public int SecondsBefore { get; set; } private bool _ordersCreated; private const string Label = "News Robot"; protected override void OnStart() { MarketData.GetMarketDepth(Symbol).Updated += PlaceStopOrders; } // Place stop order protected override void OnTick() { PlaceStopOrders(); } void PlaceStopOrders() { if ((int)Server.Time.DayOfWeek == NewsDay && !_ordersCreated) { var triggerTime = new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, NewsHour, NewsMinute, 0); if (Server.Time <= triggerTime && (triggerTime - Server.Time).TotalSeconds <= SecondsBefore) { _ordersCreated = true; var SellStop1 = Symbol.Bid - 50 * Symbol.PipSize; PlaceStopOrder(TradeType.Sell, Symbol, Volume, SellStop1); } } } } }

 


@tete13

tete13
09 Jun 2014, 13:09

Sorry. The code did not indicate correctlty.

 

using System; 
using cAlgo.API; 
using cAlgo.API.Indicators; 
using cAlgo.Indicators; namespace cAlgo.Robots 
{ 
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] 
public class Stoporder : Robot 
{ 
[Parameter("News Day (1-5)", DefaultValue = 1, MinValue = 1, MaxValue = 5)] 
public int NewsDay { get; set; } 

[Parameter("News Hour", DefaultValue = 9, MinValue = 0, MaxValue = 23)] 
public int NewsHour { get; set; } 

[Parameter("News Minute", DefaultValue = 30, MinValue = 0, MaxValue = 59)] 
public int NewsMinute { get; set; } 

[Parameter("Volume", DefaultValue = 10000, MinValue = 10000)] 
public int Volume { get; set; } 
[Parameter("Seconds Before", DefaultValue = 30, MinValue = 1)] 
public int SecondsBefore { get; set; } 
private bool _ordersCreated; 
private const string Label = "News Robot"; 
protected override void OnStart() 
{ 
MarketData.GetMarketDepth(Symbol).Updated += PlaceStopOrders; 
} 
// Place stop order 
protected override void OnTick() 
{
 PlaceStopOrders(); 
 } 
  void PlaceStopOrders() 
  { 
   if ((int)Server.Time.DayOfWeek == NewsDay && !_ordersCreated) 
    { var triggerTime = new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, NewsHour, NewsMinute, 0); 
      if (Server.Time <= triggerTime && (triggerTime - Server.Time).TotalSeconds <= SecondsBefor) 
      { _ordersCreated = true; 
      var SellStop1 = Symbol.Bid - 50 * Symbol.PipSize; 
      PlaceStopOrder(TradeType.Sell, Symbol, Volume, SellStop1); 
      }
    } 
   } 
  } 
}

 


@tete13

tete13
09 Jun 2014, 13:17

Step 2: Modify sell stop order just before news (ex: 5s before news)

I want to modify sell stop order just 5s before the news. Let's assume I want the open price is 10 pips away from current price, stop loss is 30 pips, take profit is 60 pips, expiration time is 15s after the news.

I add the following code into my Cbot:

// Modify Stop order        
        protected override void OnBar()
        {
            foreach (var order in PendingOrders)
            {
                if ((int)Server.Time.DayOfWeek == NewsDay && !_ordersCreated)
                {
                    var triggerTime = new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, NewsHour, NewsMinute, 0);

                    if (Server.Time <= triggerTime && (triggerTime - Server.Time).TotalSeconds <= SecondsModify)
                    {
                        var expirationTime = triggerTime.AddSeconds(SecondsTimeout);
                        double newPrice = Symbol.Bid - 10 * Symbol.PipSize;
                        ModifyPendingOrder(order, newPrice, 30, 60, expirationTime);

                    }
                }
            }
        }
    }
}

There was not any error when I built the cBot, however price was not modified.

Can you help me point out the mistake in this code?

I also added 2 parameters into cBot

[Parameter("Seconds Modify", DefaultValue = 5, MinValue = 1)]
public int SecondsModify { get; set; }

[Parameter("Seconds Timeout", DefaultValue = 15, MinValue = 1)]
public int SecondsTimeout { get; set; }

 

 


@tete13