Topics
Forum Topics not found
Replies
amusleh
25 Oct 2021, 09:42
RE: RE: RE: RE:
yuval.ein said:
Hello
I have failed converting my code from a single to multi symbol. Perhaps you can attach a code sample of how you converted one of cTrader cBot samples (such as Sample Aligator, Sample Bears Power etc.) from single to multi symbol. An important note: The code must support running on a back test as well the same way (meaning one chart will run several symbols).
Thanks
Hi,
Here is the sample martingale cBot that works on two symbols, you must provide the symbol names:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SampleMartingalecBot : Robot
{
[Parameter("Initial Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double InitialQuantity { get; set; }
[Parameter("Stop Loss", Group = "Protection", DefaultValue = 40)]
public int StopLoss { get; set; }
[Parameter("Take Profit", Group = "Protection", DefaultValue = 40)]
public int TakeProfit { get; set; }
[Parameter("First Symbol", Group = "Symbols")]
public string FirstSymbolName { get; set; }
[Parameter("Second Symbol", Group = "Symbols")]
public string SecondSymbolName { get; set; }
private Random random = new Random();
protected override void OnStart()
{
Positions.Closed += OnPositionsClosed;
var firstSymbol = Symbols.GetSymbol(FirstSymbolName);
ExecuteOrder(InitialQuantity, firstSymbol, GetRandomTradeType());
var secondSymbol = Symbols.GetSymbol(SecondSymbolName);
ExecuteOrder(InitialQuantity, secondSymbol, GetRandomTradeType());
}
private void ExecuteOrder(double quantity, Symbol symbol, TradeType tradeType)
{
var volumeInUnits = symbol.QuantityToVolumeInUnits(quantity);
var result = ExecuteMarketOrder(tradeType, symbol.Name, volumeInUnits, "Martingale", StopLoss, TakeProfit);
if (result.Error == ErrorCode.NoMoney)
Stop();
}
private void OnPositionsClosed(PositionClosedEventArgs args)
{
Print("Closed");
var position = args.Position;
if (position.Label != "Martingale") return;
var symbol = Symbols.GetSymbol(args.Position.SymbolName);
if (position.GrossProfit > 0)
{
ExecuteOrder(InitialQuantity, symbol, GetRandomTradeType());
}
else
{
ExecuteOrder(position.Quantity * 2, symbol, position.TradeType);
}
}
private TradeType GetRandomTradeType()
{
return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
}
}
}
@amusleh
amusleh
21 Oct 2021, 09:47
Hi,
Please check our code example for Fibonacci retracement: cAlgo API Reference - ChartFibonacciRetracement Interface (ctrader.com)
Or check our Fibonacci drawing indicator code:
@amusleh
amusleh
21 Oct 2021, 09:44
Hi,
Please try our code examples for pending orders: cAlgo API Reference - PendingOrder Interface (ctrader.com)
@amusleh
amusleh
21 Oct 2021, 09:43
Hi,
You can use timer, set the interval to 4 hours and double the volume whenever the timer elapsed:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.EasternStandardTime, AccessRights = AccessRights.None)]
public class HighVolumeHours : Robot
{
private double _volume;
protected override void OnStart()
{
// Here we set the initial volume to symbol minimum volume
_volume = Symbol.VolumeInUnitsMin;
// Here we start the timer and we set the interval to 4 hours
Timer.Start(TimeSpan.FromHours(4));
}
protected override void OnTimer()
{
// double the volume whenever OnTimer is called
_volume *= 2;
// Stop the timer if we reached the symbol maximum volume
if (_volume >= Symbol.VolumeInUnitsMax)
{
_volume = Symbol.VolumeInUnitsMax;
Timer.Stop();
}
}
}
}
@amusleh
amusleh
21 Oct 2021, 09:38
RE: RE:
emmasystems said:
amusleh said:
Hi,
There is no relation between take profit and pending orders expiry time.
To set expiry time you should use Server.Time not your system time (DateTime.Now).
You can find samples for placing/modifying/canceling pending orders here: cAlgo API Reference - PendingOrder Interface (ctrader.com)
(This is in reference to pending order exipiration Time in which trade is deleted ,
Hi,
Sorry, but I don't understand what you are looking for? test the cBot examples on API references.
@amusleh
amusleh
21 Oct 2021, 09:37
RE: RE:
emmasystems said:
amusleh said:
Hi,
Please make clear what you want to do?
You should use Bars instead of MarketSeries.
You can find code examples on how to use Bars here: cAlgo API Reference - Bars Interface (ctrader.com)
if(if (Bars.HighPrices.Last(0) > (MarketSeries.High.Last(1) &&PendingOrder > 4(distance){ PlaceBuylimitOrder} .)
Not sure if above is correct for a 'BuylimitOrder set after compaing distance and bar highlow in cTrader cbot.
Hi,
What you want to compare? your code is not valid C# code.
When you post code use the editor post code option and select C# from languages list.
@amusleh
amusleh
21 Oct 2021, 09:35
Hi,
try this:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.EasternStandardTime, AccessRights = AccessRights.None)]
public class HighVolumeHours : Robot
{
[Parameter("Instance Name", DefaultValue = "")]
public string InstanceName { get; set; }
[Parameter("Start Trading", DefaultValue = 6)]
public double StartTime { get; set; }
[Parameter("Stop Trading", DefaultValue = 16)]
public double StopTime { get; set; }
[Parameter("Source")]
public DataSeries SourceSeries { get; set; }
[Parameter("Fast Type")]
public MovingAverageType Fast { get; set; }
[Parameter("Fast Period", DefaultValue = 21)]
public int FastPeriod { get; set; }
[Parameter("Medium Type")]
public MovingAverageType Medium { get; set; }
[Parameter("Medium Period", DefaultValue = 55)]
public int MediumPeriod { get; set; }
[Parameter("Bias Type")]
public MovingAverageType Bias { get; set; }
[Parameter("Bias Period", DefaultValue = 233)]
public int BiasPeriod { get; set; }
[Parameter("Stop Loss", DefaultValue = 20)]
public int StopLoss { get; set; }
[Parameter("Include Trailing Stop", DefaultValue = false)]
public bool IncludeTrailingStop { get; set; }
[Parameter("Trailing Stop Trigger (pips)", DefaultValue = 20)]
public int TrailingStopTrigger { get; set; }
[Parameter("Trailing Stop Step (pips)", DefaultValue = 20)]
public int TrailingStopStep { get; set; }
[Parameter("Position Limit", MinValue = 1, Step = 1)]
public int PositionLimit { get; set; }
[Parameter("Risk %", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double RiskPerTrade { get; set; }
private MovingAverage slowMa;
private MovingAverage mediumMa;
private MovingAverage fastMa;
private DateTime _startTime;
private DateTime _stopTime;
protected override void OnStart()
{
fastMa = Indicators.MovingAverage(SourceSeries, FastPeriod, Fast);
mediumMa = Indicators.MovingAverage(SourceSeries, MediumPeriod, Medium);
slowMa = Indicators.MovingAverage(SourceSeries, BiasPeriod, Bias);
_startTime = Server.Time.Date.AddHours(StartTime);
_stopTime = Server.Time.Date.AddHours(StopTime);
}
protected override void OnTick()
{
if (IncludeTrailingStop)
{
SetTrailingStop();
}
}
protected override void OnBar()
{
int index = Bars.Count - 2;
Entry(index);
}
private void Entry(int index)
{
var currentHours = Server.Time.TimeOfDay.TotalHours;
bool istimecorrect = currentHours > StartTime && currentHours < StopTime;
if (!istimecorrect)
return;
// Buy Only
if (Bars.ClosePrices[index] > slowMa.Result[index])
{
// if fast crosses medium upward
if (Positions.Count < PositionLimit && fastMa.Result[index] > mediumMa.Result[index] && fastMa.Result[index - 1] < mediumMa.Result[index - 1])
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, GetVolume(StopLoss), InstanceName, StopLoss, null);
}
}
// Sell only
else if (Bars.ClosePrices[index] < slowMa.Result[index])
{
// if fast crosses medium downward
if (Positions.Count < PositionLimit && fastMa.Result[index] < mediumMa.Result[index] && fastMa.Result[index - 1] > mediumMa.Result[index - 1])
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, GetVolume(StopLoss), InstanceName, StopLoss, null);
}
}
}
private void SetTrailingStop()
{
var sellPositions = Positions.FindAll(InstanceName, SymbolName, TradeType.Sell);
foreach (var position in sellPositions)
{
double distance = position.EntryPrice - Symbol.Ask;
if (distance < TrailingStopTrigger * Symbol.PipSize)
continue;
double newStopLossPrice = Symbol.Ask + TrailingStopStep * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
{
ModifyPosition(position, newStopLossPrice, null);
}
}
var buyPositions = Positions.FindAll(InstanceName, SymbolName, TradeType.Buy);
foreach (var position in buyPositions)
{
double distance = Symbol.Bid - position.EntryPrice;
if (distance < TrailingStopTrigger * Symbol.PipSize)
continue;
double newStopLossPrice = Symbol.Bid - TrailingStopStep * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
{
ModifyPosition(position, newStopLossPrice, null);
}
}
}
private double GetVolume(double? stopLossPips = null)
{
double costPerPip = (double)((int)(Symbol.PipValue * 10000000)) / 100;
// Change this to Account.Balance if you want to
double baseNumber = Account.Equity;
double sizeInLots = Math.Round((baseNumber * RiskPerTrade / 100) / (stopLossPips.Value * costPerPip), 2);
var result = Symbol.QuantityToVolumeInUnits(sizeInLots);
if (result > Symbol.VolumeInUnitsMax)
{
result = Symbol.VolumeInUnitsMax;
}
else if (result < Symbol.VolumeInUnitsMin)
{
result = Symbol.VolumeInUnitsMin;
}
else if (result % Symbol.VolumeInUnitsStep != 0)
{
result = result - (result % Symbol.VolumeInUnitsStep);
}
return result;
}
}
}
@amusleh
amusleh
20 Oct 2021, 07:43
Hi,
Please make clear what you want to do?
You should use Bars instead of MarketSeries.
You can find code examples on how to use Bars here: cAlgo API Reference - Bars Interface (ctrader.com)
@amusleh
amusleh
20 Oct 2021, 07:41
Hi,
There is no relation between take profit and pending orders expiry time.
To set expiry time you should use Server.Time not your system time (DateTime.Now).
You can find samples for placing/modifying/canceling pending orders here: cAlgo API Reference - PendingOrder Interface (ctrader.com)
@amusleh
amusleh
19 Oct 2021, 08:40
RE: RE:
yuval.ein said:
I cannot upload the code because it's discret. What is the meaning of the error code?
Thanks
We haven't received this error report, can you please post a sample code that will be able to replicate this error?
Can you back test other sample cBots? does this error occur on all cBots or just one specific cBot?
@amusleh
amusleh
18 Oct 2021, 09:06
RE: RE:
MuttleyBerks said:
amusleh said:
Hi,
You can only have access to the chart objects that your indicator/cBot is attached to, you can't access other charts objects.
Just wanted to check before I requested an enhancement. You can access market data through different symbols so it's logical returning objects from different charts should be available from the API.
Hi,
Not logical based on current design of cTrader automate, as each indicator/cBot is attached to one single chart and its area of work is only that chart.
You can create a suggestion for this and if other users voted for it the team might consider to add such a feature.
@amusleh
amusleh
18 Oct 2021, 09:04
Hi,
You can use ChartTrendLine CalculateY method, this will give you the price of trend line based on x axis of chart (bar index or time).
Here is an example:
using cAlgo.API;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class NewcBot : Robot
{
private ChartTrendLine _trendLine;
protected override void OnStart()
{
_trendLine = Chart.DrawTrendLine("trendline", Chart.FirstVisibleBarIndex, Chart.BottomY, Chart.LastVisibleBarIndex, Chart.TopY, Color.Red);
}
protected override void OnTick()
{
var trendLinePriceValue = _trendLine.CalculateY(Bars.OpenTimes.LastValue);
// if price crossd the trend line upward then close the position
if (Symbol.Bid >= trendLinePriceValue)
{
// close position here
}
}
}
}
@amusleh
amusleh
18 Oct 2021, 08:57
RE: RE:
emmasystems said:
amusleh said:
There are Filled, Modified, and Canceled events for PendingOrders you can use, and there are similar events of positions like Closed, Modified, and Opened.
Please , how are Pending orders placed in ctrader code like example , CreatePendingOrder, does that have to be used even modifying or setting Limit. Thanks
Hi,
You can find cBot examples for how to place/modify/cancel pending orders here: cAlgo API Reference - PendingOrder Interface (ctrader.com)
@amusleh
amusleh
25 Oct 2021, 09:45
Hi,
No, right now its not possible, you can't get your broker symbol categories from automate API.
You can open a thread on forum suggestions section for this feature.
If you want to categorize symbols by your self and then access the categories you can use watchlists from automate API.
@amusleh