Get ticks from the ticks char & use for Robot
Get ticks from the ticks char & use for Robot
16 Dec 2021, 16:33
Hi guys,
I tried to get ticks from the ticks char & use for Robot, for example:
1- selecting the tick chart =200
2- get the remaining tick number (e.g. 50 of the ticks chart 200)
3-get Open, High, Low, Close of one Ticks chart ( e.g ticks chart 200).
I read api document and try write code but it not work.cTDN | cAlgo.API (ctrader.com)
My code (remove error ticks code):
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 Toan010speedpostforumL4tick : 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;
//tick error:
//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()
{
// get ticks, write to chart:
//stringBuilder.AppendLine("Ticks: " + Ticks);
//Chart.DrawStaticText("text", stringBuilder.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.Red);
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
}
}
}
Please help me how to get the above data from the tick chart?
Best Regards,
Duccauca
Replies
duccauca
18 Dec 2021, 03:38
( Updated at: 18 Dec 2021, 09:15 )
Hi amusleh
I try draw Ticks to chart, but my code error. _tickBars.Tick that's the ticks needed for robots?
Please help me to check.
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 Toan012speedforumreplyL5tick : 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;
private Bars _tickBars;
protected override void OnStart()
{
Positions.Opened += PositionsOnOpened;
Positions.Closed += PositionsOnClosed;
_tickBars = MarketData.GetBars(TimeFrame.Tick200);
_tickBars.Tick += _tickBars_Tick;
}
private void _tickBars_Tick(BarsTickEventArgs obj)
{
}
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()
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("_tickBars: " + _tickBars + " _tickBars.Tick: " + _tickBars.Tick);
Chart.DrawStaticText("text", stringBuilder.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.Red);
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)
{
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
}
}
}
@duccauca
amusleh
20 Dec 2021, 09:55
Hi,
I'm not sure what you are after?
Do you want to access tick time frames data? if that's what you want to do then you can use the above posted code of mine or this complete version:
using cAlgo.API;
using cAlgo.API.Internals;
using System.Text;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Toan012speedforumreplyL5tick : Robot
{
[Parameter("Tick TimeFrame", DefaultValue = "Tick200")]
public TimeFrame TickTimeFrame { get; set; }
private Bars _tickBars;
protected override void OnStart()
{
// Change the time frame if you want to use other tick time frames
// Or get the time frame via a cBot parameter
_tickBars = MarketData.GetBars(TimeFrame.Tick200);
// This will call the TickBars_Tick method every 200 ticks
_tickBars.Tick += TickBars_Tick;
}
private void TickBars_Tick(BarsTickEventArgs obj)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendFormat("New Tick bar received, Open: {0} | High: {1} | Low: {2} | Close: {3}", obj.Bars.LastBar.Open, obj.Bars.LastBar.High, obj.Bars.LastBar.Low, obj.Bars.LastBar.Close);
Chart.DrawStaticText("text", stringBuilder.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.Red);
}
}
}
If you are looking to execute something on each new tick, then use your cBot OnTick method or symbol Tick event in case of multiple symbols.
@amusleh
amusleh
17 Dec 2021, 09:23
Hi,
Try this:
@amusleh