Please help me for Get data candle 0 and Get id number of order
Please help me for Get data candle 0 and Get id number of order
08 Dec 2021, 13:25
Hi guys,
I find it attractive by cTrade software and want to trade on this software. I am programming bots to trade but I have encountered some syntax that do not know how to handle so I would like receive help from Spotware and the coders. The supported content is as follows:
1- Get data candle 0
I got the correct data (candle 1) by syntax: double CloseN1 = MarketSeries.Close.Last(1);
I take data (candle 0) by syntax: double HighN0 = MarketSeries.High.Last(0); => But data are not correct. what is the syntax for zero candlestick?
2- write text or parameters of the variables controlled out of the chart:
I often write the current data (of variables) to the price chart when backtesting using visual display mode to check if the bot running process is correct. It's more convenient than writing with alert or print.
I wrote the following syntax text on Ctrade ok:
I wrote the following syntax text on Ctrade:
protected override void OnBar()
{
ChartObjects.DrawText("Sample Text", "Sample Text", StaticPosition.Left);
}
I tried write a variable but it didn't work. what is the syntax for write the current data (of variables) to the price chart?
3-declare the ExpirationTime for pending orders so that the order delete when the order is not executed:
+ option 1: after 30 minutes of pending orders are not executed, delete. but my code couldn't run and reported errors:
If(CountBuy==0)
{
DateTime ExpirationTime = Server.Time.AddMinute(30);
PlaceStopOrder(TradeType.Buy, Symbol, 100000, Symbol.Ask + KcStop * Symbol.PipSize, BuyLabel, StopLossInPips, ExpirationTime);
CountBuy=1;
}
+ option 2: I want when the current candle closes and the pending order are not executed. what is the syntax for this?
4- allows Bot01 to open a new order (e.g. buy), only if the status of the old buy order has closed (importance for me)
I call cBots of cTrade called Bot01, Bot02... for example
at MT4, the id number of each trade order generated every time a new order is issued, is the only key to recognize that the order is not duplicated with another.
at MT4, I use this key for the purpose that Bot01 always has only 1 simultaneous open order + not affected by other open orders of Bot02, Bot03 ....
I use by: the order opens, Bot01 records this key. when the order is closed, Bot01 will find in the order history the order with that key => eligible for the Bot to open a new order
At cTrade I haven't found a way to do something like this because the labeling for each order is a text chain that does not do the above because it is not the only key because the orders are all the same label.
If labeling is the current time, and write that metric in each order I do not know the syntax so I have not tried it . That's only approximate because time data isn't the only key. In a period of 1 second, it is still possible to open a buy order at the same time and a sell order has the same time number. Then the bot will misimmugn it.
please ask in cTrade the syntax find and remember this key number + the historical search syntax to see the order is closed
(please see the photo with the example on mt4 to illustrate the section 4.5 question)
5-Ask the syntax for write notes on bot orders (like the comment section of MT4) so that when opened see the order history of the account is easy to distinguish the order opened by different Bots
6- Ask the syntax for bot to get deep market volume data calculated support into the order
Thanks you for reading
Best Regards,
duccauca
Replies
duccauca
11 Dec 2021, 05:40
Hi amusleh
Today I've programmed some content according to your instructions as follows:
Code cBot run ok, backtest for gbpusd from 1/11/21 to 10-12-21 with H1 (temporarily remove the error parts to run):
using System;
using cAlgo.API;
using System.Linq;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot("PendingTestRobott")]
public class PendingTestRobot : Robot
{
[Parameter("Position Label", DefaultValue = "BuyLabel")]
public string BuyLabel { get; set; }
[Parameter("Position Label", DefaultValue = "SejLabel")]
public string SejLabel { get; set; }
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Stop Loss (pips)", Group = "Protection", DefaultValue = 30, MinValue = 1)]
public int StopLossInPips { get; set; }
[Parameter("Take Profit (pips)", Group = "Protection", DefaultValue = 60, MinValue = 1)]
public int TakeProfitInPips { get; set; }
[Parameter("Candle", DefaultValue = 20, MinValue = 1)]
public int CandleLeng { get; set; }
[Parameter("StopDistance", DefaultValue = 3, MinValue = 1)]
public int StopDistance { get; set; }
int BuyL1, SejL1;
int BuyId, SejId;
protected override void OnStart()
{
Positions.Opened += PositionsOnOpened;
Positions.Closed += PositionsOnClosed;
}
private void PositionsOnOpened(PositionOpenedEventArgs args)
{
var position = args.Position;
if (position.Label == BuyLabel)
{
BuyId = position.Id;
Print("Position open= {0}. Id= {1}, waiting for trigger", position.Label, BuyId);
}
if (position.Label == SejLabel)
{
SejId = position.Id;
Print("Position open= {0}. Id= {1}, waiting for trigger", position.Label, SejId);
}
}
private void PositionsOnClosed(PositionClosedEventArgs args)
{
var position = args.Position;
if (position.Label == BuyLabel)
if (position.Id == BuyId)
{
BuyId = 0;
BuyL1 = 0;
}
if (position.Label == SejLabel)
if (position.Id == SejId)
{
SejId = 0;
SejL1 = 0;
}
}
protected override void OnTick()
{
double HighN0 = MarketSeries.High.Last(0);
double LowN0 = MarketSeries.Low.Last(0);
double BuyDistance = (Symbol.Ask - LowN0) / 1E-05;
double SejDistance = (HighN0 - Symbol.Bid) / 1E-05;
//buy stop
if (BuyDistance > CandleLeng && BuyL1 == 0)
{
/*
//ExpirationTime error start:
//DateTime ExpirationTime = Server.Time.AddMinute(10);
//PlaceStopOrder(TradeType.Buy, Symbol, 100000, Symbol.Ask + StopDistance * Symbol.PipSize, BuyLabel, StopLossInPips,TakeProfitInPips,ExpirationTime);
//ExpirationTime error end
*/
//no ExpirationTime (no error):
PlaceStopOrder(TradeType.Buy, Symbol, 100000, Symbol.Ask + StopDistance * Symbol.PipSize, BuyLabel, StopLossInPips, TakeProfitInPips);
BuyL1 = 1;
Print("Buy stop. LowN0= ", LowN0, " Ask= ", Symbol.Ask, " Open= ", Symbol.Ask + StopDistance * Symbol.PipSize, " Distance=", Symbol.Ask - LowN0);
}
//buy stop end
//sej stop
if (SejDistance > CandleLeng && SejL1 == 0)
{
PlaceStopOrder(TradeType.Sell, Symbol, 100000, Symbol.Bid - StopDistance * Symbol.PipSize, SejLabel, StopLossInPips, TakeProfitInPips);
SejL1 = 1;
Print("Sej stop. HighN0= ", HighN0, " Bid= ", Symbol.Bid, " Open= ", Symbol.Bid - StopDistance * Symbol.PipSize, " Distance=", HighN0 - Symbol.Bid);
}
//sej stop end
}
}
/*
//draw text error start:
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("BuyL1: " + BuyL1 + " SejL1: " + SejL1);
//draw text error end:
*/
//public class
}
==> In this Bot, part 4 allows Bot01 to open a new order....(reset new order by Id) => ok.
And some of the error parts I framed /**/ to temporarily not run:
Part 3 (error ExpirationTime) please help me to write the ExpirationTime+ comments for order:
//ExpirationTime error start:
//DateTime ExpirationTime = Server.Time.AddMinute(10);
//PlaceStopOrder(TradeType.Buy, Symbol, 100000, Symbol.Ask + StopDistance * Symbol.PipSize, BuyLabel, StopLossInPips,TakeProfitInPips,ExpirationTime);
//ExpirationTime error end
Part 2- write text or parameters of the variables controlled out of the chart (error) :
//draw text error start:
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("BuyL1: " + BuyL1 + " SejL1: " + SejL1);
//draw text error end
Please help me to write
Best Regards,
Duccauca
@duccauca
amusleh
13 Dec 2021, 09:46
Hi,
Try this please:
using System;
using cAlgo.API;
using System.Linq;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Text;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class PendingTestRobot : Robot
{
[Parameter("Position Label", DefaultValue = "BuyLabel")]
public string BuyLabel { get; set; }
[Parameter("Position Label", DefaultValue = "SejLabel")]
public string SejLabel { get; set; }
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Stop Loss (pips)", Group = "Protection", DefaultValue = 30, MinValue = 1)]
public int StopLossInPips { get; set; }
[Parameter("Take Profit (pips)", Group = "Protection", DefaultValue = 60, MinValue = 1)]
public int TakeProfitInPips { get; set; }
[Parameter("Candle", DefaultValue = 20, MinValue = 1)]
public int CandleLeng { get; set; }
[Parameter("StopDistance", DefaultValue = 3, MinValue = 1)]
public int StopDistance { get; set; }
private int BuyL1, SejL1;
private int BuyId, SejId;
protected override void OnStart()
{
// You don't have to use StringBuilder, use it only if you want to draw multi-line text
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("BuyL1: " + BuyL1 + " SejL1: " + SejL1);
Chart.DrawStaticText("text", stringBuilder.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.Red);
Positions.Opened += PositionsOnOpened;
Positions.Closed += PositionsOnClosed;
}
private void PositionsOnOpened(PositionOpenedEventArgs args)
{
var position = args.Position;
if (position.Label == BuyLabel)
{
BuyId = position.Id;
Print("Position open= {0}. Id= {1}, waiting for trigger", position.Label, BuyId);
}
if (position.Label == SejLabel)
{
SejId = position.Id;
Print("Position open= {0}. Id= {1}, waiting for trigger", position.Label, SejId);
}
}
private void PositionsOnClosed(PositionClosedEventArgs args)
{
var position = args.Position;
if (position.Label == BuyLabel)
if (position.Id == BuyId)
{
BuyId = 0;
BuyL1 = 0;
}
if (position.Label == SejLabel)
if (position.Id == SejId)
{
SejId = 0;
SejL1 = 0;
}
}
protected override void OnTick()
{
double HighN0 = Bars.HighPrices.Last(0);
double LowN0 = Bars.LowPrices.Last(0);
double BuyDistance = (Symbol.Ask - LowN0) / 1E-05;
double SejDistance = (HighN0 - Symbol.Bid) / 1E-05;
//buy stop
if (BuyDistance > CandleLeng && BuyL1 == 0)
{
DateTime ExpirationTime = Server.Time.AddMinutes(10);
PlaceStopOrder(TradeType.Buy, SymbolName, 100000, Symbol.Ask + StopDistance * Symbol.PipSize, BuyLabel, StopLossInPips, TakeProfitInPips, ExpirationTime);
BuyL1 = 1;
Print("Buy stop. LowN0= ", LowN0, " Ask= ", Symbol.Ask, " Open= ", Symbol.Ask + StopDistance * Symbol.PipSize, " Distance=", Symbol.Ask - LowN0);
}
//buy stop end
//sej stop
if (SejDistance > CandleLeng && SejL1 == 0)
{
PlaceStopOrder(TradeType.Sell, SymbolName, 100000, Symbol.Bid - StopDistance * Symbol.PipSize, SejLabel, StopLossInPips, TakeProfitInPips);
SejL1 = 1;
Print("Sej stop. HighN0= ", HighN0, " Bid= ", Symbol.Bid, " Open= ", Symbol.Bid - StopDistance * Symbol.PipSize, " Distance=", HighN0 - Symbol.Bid);
}
//sej stop end
}
}
}
@amusleh
duccauca
15 Dec 2021, 06:39
Thanks amusleh for your reply. I was write text to chart & expire time ok.
I read your link for deep market and read carefuly all help for deep market syntax and try write code as following:
using System;
using cAlgo.API;
using System.Linq;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Text;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Toan008speedpostforumL3 : Robot
{
[Parameter("Position Label", DefaultValue = "BuyLabel")]
public string BuyLabel { get; set; }
[Parameter("Position Label", DefaultValue = "SejLabel")]
public string SejLabel { get; set; }
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Stop Loss (pips)", Group = "Protection", DefaultValue = 30, MinValue = 1)]
public int StopLossInPips { get; set; }
[Parameter("Take Profit (pips)", Group = "Protection", DefaultValue = 60, MinValue = 1)]
public int TakeProfitInPips { get; set; }
[Parameter("Candle", DefaultValue = 20, MinValue = 1)]
public int CandleLeng { get; set; }
[Parameter("StopDistance", DefaultValue = 3, MinValue = 1)]
public int StopDistance { get; set; }
private int BuyL1, SejL1;
private int BuyId, SejId;
/*
[Parameter("BidEntries", DefaultValue = 0, MinValue = 0)]
public IReadonlyList BidEntries { get; }
*/
//public Ticks Ticks { get; }
protected override void OnStart()
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("BuyL1: " + BuyL1 + " SejL1: " + SejL1);
Chart.DrawStaticText("text", stringBuilder.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.Red);
Positions.Opened += PositionsOnOpened;
Positions.Closed += PositionsOnClosed;
}
private void PositionsOnOpened(PositionOpenedEventArgs args)
{
var position = args.Position;
if (position.Label == BuyLabel)
{
BuyId = position.Id;
Print("Position open= {0}. Id= {1}, waiting for trigger", position.Label, BuyId);
}
if (position.Label == SejLabel)
{
SejId = position.Id;
Print("Position open= {0}. Id= {1}, waiting for trigger", position.Label, SejId);
}
}
private void PositionsOnClosed(PositionClosedEventArgs args)
{
var position = args.Position;
if (position.Label == BuyLabel)
if (position.Id == BuyId)
{
BuyId = 0;
BuyL1 = 0;
}
if (position.Label == SejLabel)
if (position.Id == SejId)
{
SejId = 0;
SejL1 = 0;
}
}
protected override void OnTick()
{
double HighN0 = Bars.HighPrices.Last(0);
double LowN0 = Bars.LowPrices.Last(0);
double BuyDistance = (Symbol.Ask - LowN0) / 1E-05;
double SejDistance = (HighN0 - Symbol.Bid) / 1E-05;
//buy stop
if (BuyDistance > CandleLeng && BuyL1 == 10)
{
DateTime ExpirationTime = Server.Time.AddMinutes(10);
PlaceStopOrder(TradeType.Buy, SymbolName, 100000, Symbol.Ask + StopDistance * Symbol.PipSize, BuyLabel, StopLossInPips, TakeProfitInPips, ExpirationTime);
BuyL1 = 1;
Print("Buy stop. LowN0= ", LowN0, " Ask= ", Symbol.Ask, " Open= ", Symbol.Ask + StopDistance * Symbol.PipSize, " Distance=", Symbol.Ask - LowN0);
}
//buy stop end
//sej stop
if (SejDistance > CandleLeng && SejL1 == 10)
{
DateTime ExpirationTime = Server.Time.AddMinutes(10);
PlaceStopOrder(TradeType.Sell, SymbolName, 100000, Symbol.Bid - StopDistance * Symbol.PipSize, SejLabel, StopLossInPips, TakeProfitInPips, ExpirationTime);
SejL1 = 1;
Print("Sej stop. HighN0= ", HighN0, " Bid= ", Symbol.Bid, " Open= ", Symbol.Bid - StopDistance * Symbol.PipSize, " Distance=", HighN0 - Symbol.Bid);
}
//sej stop end
//stringBuilder.AppendLine("Ticks: " + Ticks);
//Chart.DrawStaticText("text", stringBuilder.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.Red);
}
}
/*
foreach (var entry in _marketDepth.BidEntries)
{
volume = entry.Volume;
entryPrice = entry.Price;
}
*/
}
But this code error. I simply want to take out a specific data such as the total buy or sell volume ( or price of volume, other parameters) to support the calculation of the order. And the your link for indicators is just to show the screen, so I don't know how to get that data for robot. Can you introduce me deep market data example of the robot?
In addition, I tried to take data from the tick , for example selecting the tick chart =200, and robot get the remaining tick number (e.g. 50) of the tick chart 200 but my code error so that I close error code by /* */. Can you show me how to get the above two data from the tick chart for the robot?
Best Regards,
Duccauca
@duccauca
amusleh
15 Dec 2021, 09:36
Hi,
Try this:
using cAlgo.API;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Test : Robot
{
private int _askNo;
private int _bidNo;
private double _askVolumeTotal;
private double _bidVolumeTotal;
private MarketDepth _marketDepth;
protected override void OnStart()
{
_marketDepth = MarketData.GetMarketDepth(SymbolName);
_marketDepth.Updated += MarketDepth_Updated;
}
private void MarketDepth_Updated()
{
var askNo = 0;
var bidNo = 0;
var askVolumeTotal = 0.0;
var bidVolumeTotal = 0.0;
foreach (var entry in _marketDepth.AskEntries)
{
askVolumeTotal += entry.VolumeInUnits;
askNo++;
}
foreach (var entry in _marketDepth.BidEntries)
{
bidVolumeTotal += entry.VolumeInUnits;
bidNo++;
}
_askVolumeTotal = askVolumeTotal;
_bidVolumeTotal = bidVolumeTotal;
_askNo = askNo;
_bidNo = bidNo;
}
}
}
The "_askVolumeTotal" and "_bidVolumeTotal" will always have the total volume of bid/ask orders on order book.
The "_askNo" and "_bidNo" will always have to number of bid/ask orders on order book.
@amusleh
duccauca
16 Dec 2021, 09:15
Hi amusleh
Thanks for your reply. I'm try get data deep market. I write to the chart and all data always = 0:_askVolumeTotal , _bidVolumeTotal ,_askNo ,_bidNo.
So these data I haven't used to make EA calculate. Is it wrong to export data to the chart ?
Please help me to check
using cAlgo.API;
using cAlgo.API.Internals;
using System.Text;
using System.Linq;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Test : Robot
{
private int _askNo;
private int _bidNo;
private double _askVolumeTotal;
private double _bidVolumeTotal;
private MarketDepth _marketDepth;
protected override void OnStart()
{
_marketDepth = MarketData.GetMarketDepth(SymbolName);
_marketDepth.Updated += MarketDepth_Updated;
}
private void MarketDepth_Updated()
{
var askNo = 0;
var bidNo = 0;
var askVolumeTotal = 0.0;
var bidVolumeTotal = 0.0;
foreach (var entry in _marketDepth.AskEntries)
{
askVolumeTotal += entry.VolumeInUnits;
askNo++;
}
foreach (var entry in _marketDepth.BidEntries)
{
bidVolumeTotal += entry.VolumeInUnits;
bidNo++;
}
_askVolumeTotal = askVolumeTotal;
_bidVolumeTotal = bidVolumeTotal;
_askNo = askNo;
_bidNo = bidNo;
}
protected override void OnTick()
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("_askVolumeTotal: " + _askVolumeTotal + " _bidVolumeTotal: " + _bidVolumeTotal + " _askNo: " + _askNo + " _bidNo: " + _bidNo);
Chart.DrawStaticText("text", stringBuilder.ToString(), VerticalAlignment.Top, HorizontalAlignment.Left, Color.Red);
}
}
}
@duccauca
duccauca
16 Dec 2021, 11:46
( Updated at: 16 Dec 2021, 11:50 )
Hi amusleh
I forgot not to live trade. So it worked ok. It's a shame that having a back test is too easy to test effective of Robot. I'll try using this data for Robot.
If you have any examples of tick charts for Robot sent to me please. because I look at the tutorial and try it still can't.
Thank you very much
@duccauca
amusleh
09 Dec 2021, 09:35
Hi,
1. To get the last candle data which is not closed yet you can use Bars.Last(0) or Bars[Bars.Count - 1].
2. For displaying the data on chart you can use either chart controls or chart objects, please the examples on API references:
cAlgo API Reference - Symbol Interface (ctrader.com)
cAlgo API Reference - ChartText Interface (ctrader.com)
cAlgo API Reference - ChartStaticText Interface (ctrader.com)
3. You can use pending order expiry time parameter to set a time based expiry for your pending orders, for 30 minute your code looks correct to me, you have to post the error then I will be able to help you.
4. In cTrader each order or position that you open will have their own ID property, those IDs are unique and you can access it by using position or pending order Id property, you can also assign label or comment for orders if you want to, for more please check the API reference examples:
cAlgo API Reference - Position Interface (ctrader.com)
cAlgo API Reference - Positions Interface (ctrader.com)
6. Not sure what do you mean by deep market data, do you mean market order book data? check the API references example for market depth:
cAlgo API Reference - MarketDepth Interface (ctrader.com)
Please open separate threads for each of your questions in future, that will make it much easier for us to respond.
@amusleh