TECHNICAL ERROR WHEN ROBOT TRIES TO PLACE 2 PENDING STOP ORDERS

Created at 20 Nov 2012, 00:17
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!
odomike's avatar

odomike

Joined 15.09.2012

TECHNICAL ERROR WHEN ROBOT TRIES TO PLACE 2 PENDING STOP ORDERS
20 Nov 2012, 00:17


Hi Support,

I downloaded and am using the News Robot. I havent really had any issues with cAlgo till now. Each time I try to place 2 pending STOP ORDERS with the News Trader, it starts with placing the  SELL ORDER FIRST and this is always successful, but on trying to place the BUY ORDER, I always get this error

 

"YOUR REQUEST TO BUY STOP ORDER FOR 10K OF EURUSD WAS NOT SENT DUE TO TECHNICAL ERROR"


What could possibly be causing this and what can I do to resolve it? I am pasting the code for the robot so you can have a look and let me know what could be wrong and how I can correct this. I need this to worl perfectly.

Thank you in advance.

 

Michael.

 

N.B: Here is the code for the robot.

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

namespace cAlgo.Robots
{
    [Robot]
    public class NewsRobot : Robot        
    {
    	[Parameter("News Day (1-5)", DefaultValue=1, MinValue=1, MaxValue=5)]
    	public int NewsDay  { get; set; }
    	    	
    	[Parameter("News Hour", DefaultValue=14, MinValue=0, MaxValue=23)]
    	public int NewsHour  { get; set; }
    	
    	[Parameter("News Minute", DefaultValue=30, MinValue=0, MaxValue=59)]
    	public int NewsMinute  { get; set; }
    
    	[Parameter("Pips away", DefaultValue=10)]
    	public int PipsAway  { get; set; }
    
    	[Parameter("Take Profit", DefaultValue=50)]
    	public int TakeProfit  { get; set; }   

		[Parameter("Stop Loss", DefaultValue=10)]
    	public int StopLoss  { get; set; }
    	    	
    	[Parameter("Volume", DefaultValue=100000, MinValue=1000)]
    	public int Volume  { get; set; }    	   	
    	    	
    	[Parameter("Seconds Before", DefaultValue=5, MinValue=1)]
    	public int SecondsBefore  { get; set; }
    	
    	[Parameter("Seconds Timeout", DefaultValue=10, MinValue=1)]
    	public int SecondsTimeout  { get; set; }
    	
    	[Parameter("One Cancels Other", DefaultValue=1, MinValue=0, MaxValue=1)]
    	public int Oco  { get; set; }
    	
    	private bool _ordersCreated;
    	private PendingOrder _buyOrder;
    	private PendingOrder _sellOrder;
    	    	
        protected override void OnTick()
        {        	
			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 expirationTime = triggerTime.AddSeconds(SecondsTimeout);
					
					var sellOrderTargetPrice = Symbol.Bid - PipsAway * Symbol.PipSize;					
					Trade.CreateSellStopOrder(Symbol, Volume, sellOrderTargetPrice,
						sellOrderTargetPrice + StopLoss * Symbol.PipSize, sellOrderTargetPrice - TakeProfit * Symbol.PipSize, expirationTime);	

					var buyOrderTargetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
					Trade.CreateBuyStopOrder(Symbol, Volume, buyOrderTargetPrice,
						buyOrderTargetPrice - StopLoss * Symbol.PipSize, buyOrderTargetPrice, expirationTime);
				}
			}
        }
        
		protected override void OnPendingOrderCreated(PendingOrder newOrder)
		{
			if (newOrder.TradeType == TradeType.Buy)
				_buyOrder = newOrder;
			else
				_sellOrder = newOrder;
		}
        
		protected override void OnPositionOpened(Position openedPosition)
		{
			if (Oco == 1)
			{
				Trade.DeletePendingOrder(_buyOrder);
				Trade.DeletePendingOrder(_sellOrder);
			}
		}        
    }
}




@odomike
Replies

admin
20 Nov 2012, 11:27

The technical error is due to the take profit value in the CreateBuyStopOrder. It cannot be the same as the target price.

 


@admin

odomike
20 Nov 2012, 11:36

How do I correct this please? I need the exact code that can make the necessary corrections so it can start working properly and place the 2 pending stop orders as required of the robot.


@odomike

admin
20 Nov 2012, 11:56

Here it is
odomike said:

How do I correct this please? I need the exact code that can make the necessary corrections so it can start working properly and place the 2 pending stop orders as required of the robot.

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

namespace cAlgo.Robots
{
    [Robot]
    public class NewsRobot : Robot        
    {
    	[Parameter("News Day (1-5)", DefaultValue=1, MinValue=1, MaxValue=5)]
    	public int NewsDay  { get; set; }
    	    	
    	[Parameter("News Hour", DefaultValue=14, MinValue=0, MaxValue=23)]
    	public int NewsHour  { get; set; }
    	
    	[Parameter("News Minute", DefaultValue=30, MinValue=0, MaxValue=59)]
    	public int NewsMinute  { get; set; }
    
    	[Parameter("Pips away", DefaultValue=10)]
    	public int PipsAway  { get; set; }
    
    	[Parameter("Take Profit", DefaultValue=50)]
    	public int TakeProfit  { get; set; }   

		[Parameter("Stop Loss", DefaultValue=10)]
    	public int StopLoss  { get; set; }
    	    	
    	[Parameter("Volume", DefaultValue=100000, MinValue=1000)]
    	public int Volume  { get; set; }    	   	
    	    	
    	[Parameter("Seconds Before", DefaultValue=5, MinValue=1)]
    	public int SecondsBefore  { get; set; }
    	
    	[Parameter("Seconds Timeout", DefaultValue=10, MinValue=1)]
    	public int SecondsTimeout  { get; set; }
    	
    	[Parameter("One Cancels Other", DefaultValue=1, MinValue=0, MaxValue=1)]
    	public int Oco  { get; set; }
    	
    	private bool _ordersCreated;
    	private PendingOrder _buyOrder;
    	private PendingOrder _sellOrder;
    	    	
        protected override void OnTick()
        {        	
			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 expirationTime = triggerTime.AddSeconds(SecondsTimeout);
					
					var sellOrderTargetPrice = Symbol.Bid - PipsAway * Symbol.PipSize;					
					Trade.CreateSellStopOrder(Symbol, Volume, sellOrderTargetPrice,
						sellOrderTargetPrice + StopLoss * Symbol.PipSize, sellOrderTargetPrice - TakeProfit * Symbol.PipSize, expirationTime);	

					var buyOrderTargetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
					Trade.CreateBuyStopOrder(Symbol, Volume, buyOrderTargetPrice,
						buyOrderTargetPrice - StopLoss * Symbol.PipSize, buyOrderTargetPrice + TakeProfit * Symbol.PipSize, expirationTime);
				}
			}
        }
        
		protected override void OnPendingOrderCreated(PendingOrder newOrder)
		{
			if (newOrder.TradeType == TradeType.Buy)
				_buyOrder = newOrder;
			else
				_sellOrder = newOrder;
		}
        
		protected override void OnPositionOpened(Position openedPosition)
		{
			if (Oco == 1)
			{
				Trade.DeletePendingOrder(_buyOrder);
				Trade.DeletePendingOrder(_sellOrder);
			}
		}        
    }
}



@admin

odomike
21 Nov 2012, 13:45

Ok thank you som much Admin. It works perfectly now. I really appreciate your help, but I need another one. Please can you tell me what code I can integrate in the robot to enable it check for spread? For instance, my broker widens the spread just before News Release. I want the robot to be able to watch the spread and automatically readjust the pending orders, still maintaining the same dstance from the current market price, but it will stop adjusting at the News Time.

Example ...

News time = 11:00am

Robot places 2 pending stop orders by 10:59 and adjusts the pending orders to follow the market prices (Ask and Bid), still maintaining the same distance from the market prices and if the spread starts widening, it will also widen its pending orders to maintain distance. But it leaves the pending orders intact (stops adjusting) by exactly 11:00am.

Please let me know if this is achievable and what code I can use to achieve it.

Again, thank you so much for your super awesom support. I appreciate so much :)

 

~Michael.


@odomike

admin
21 Nov 2012, 16:10

Yes it is achievable. You can use Symbol.Spread to check for the spread.

 


@admin

odomike
21 Nov 2012, 18:01

Ok, thanks Admin. I will try to implement the Symbol Spread parameter in the Robot. Will that also control the automatic adjustment of the pending orders before News time? If there is any other problem, I will let you know.

 

Thank a lot.

 

~Michael.


@odomike

odomike
21 Nov 2012, 18:25

I have to admit that I dont know anything about cAlgo programming. Admin, please help me with the specific code that can implement the spread checking in the news robot. So that if the spread exceeds a specific amount, it cancels the pending order. Please help me this one last time.

 

I appreciate all the help you have given me so far. Thanks a million fold.

 

~Michael.


@odomike