Topics
Forum Topics not found
Replies
amusleh
02 May 2022, 08:49
RE:
seankiaa said:
I have been using:
ClosePosition(position, volume);
Could you clarify if this is supported in back test and has already been implemented?
Hi, Partial close is working fine, use ModifyPosition method:
using cAlgo.API;
using cAlgo.API.Internals;
namespace NewcBot
{
[Robot(AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
protected override void OnStart()
{
var result = ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin * 2);
if (result.IsSuccessful)
{
Print("Order successfully placed, modifying volume");
ModifyPosition(result.Position, result.Position.VolumeInUnits / 2);
}
else
{
Print("Order placement failed");
}
}
protected override void OnStop()
{
foreach (var position in Positions)
{
ClosePosition(position);
}
}
}
}
@amusleh
amusleh
28 Apr 2022, 09:21
Hi,
You should declare text box as a field of your cBot/Indicator class, then you will be able to access it from anywhere and change its properties, example:
using cAlgo.API;
namespace NewcBot
{
[Robot(AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
private TextBlock _textBlock;
protected override void OnStart()
{
_textBlock = new TextBlock
{
Text = "Initial text",
ForegroundColor = Color.Red,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
Chart.AddControl(_textBlock);
}
protected override void OnTick()
{
_textBlock.Text = "New text";
_textBlock.ForegroundColor = Color.Green;
}
}
}
@amusleh
amusleh
27 Apr 2022, 08:26
Hi,
That error appears because you haven't referenced the GannHighLow indicator, you have to reference it then you will be able to use it on your cBot.
Here is the referencing guide: https://help.ctrader.com/ctrader-automate/guides/indicators#referencing-custom-indicators
@amusleh
amusleh
27 Apr 2022, 08:24
( Updated at: 28 Apr 2022, 09:15 )
Hi,
The code I posted does the same thing but without suspending the thread or causing any delay, that's the fastest possible solution.
Regarding time delay, no there is no such function in API and I don't think there is a need for something like that.
@amusleh
amusleh
27 Apr 2022, 08:22
Hi,
List is part of .NET BCL collections, you can find all you need here: List<T> Class (System.Collections.Generic) | Microsoft Docs
For Linq: Language-Integrated Query (LINQ) (C#) | Microsoft Docs
@amusleh
amusleh
27 Apr 2022, 08:21
Hi,
Try this:
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SampleTrendcBot : Robot
{
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Source", Group = "Moving Average")]
public DataSeries SourceSeries { get; set; }
[Parameter("Slow MA Sigma", DefaultValue = 0.65, Group = "Moving Average")]
public double SlowMaSigma { get; set; }
[Parameter("Fast MA Sigma", DefaultValue = 0.65, Group = "Moving Average")]
public double FastMaSigma { get; set; }
[Parameter("Slow Periods", Group = "Moving Average", DefaultValue = 10)]
public int SlowPeriods { get; set; }
[Parameter("Fast Periods", Group = "Moving Average", DefaultValue = 5)]
public int FastPeriods { get; set; }
private Vidya slowMa;
private Vidya fastMa;
private const string label = "Sample Trend cBot";
protected override void OnStart()
{
fastMa = Indicators.Vidya(SourceSeries, FastPeriods, FastMaSigma);
slowMa = Indicators.Vidya(SourceSeries, SlowPeriods, SlowMaSigma);
}
protected override void OnTick()
{
var longPosition = Positions.Find(label, SymbolName, TradeType.Buy);
var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);
var currentSlowMa = slowMa.Result.Last(0);
var currentFastMa = fastMa.Result.Last(0);
var previousSlowMa = slowMa.Result.Last(1);
var previousFastMa = fastMa.Result.Last(1);
if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)
{
if (shortPosition != null)
ClosePosition(shortPosition);
ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label);
}
else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
{
if (longPosition != null)
ClosePosition(longPosition);
ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, label);
}
}
private double VolumeInUnits
{
get { return Symbol.QuantityToVolumeInUnits(Quantity); }
}
}
}
@amusleh
amusleh
26 Apr 2022, 10:35
RE: RE:
ctid2032775 said:
Hi,
just to eliminate that this is caused by the virtual machine I installed the same version on a dedicated Windows 10 Pro notebook and had exactly the same issue...
So just a few question:
- What are the "minimum requirements" to run cBots compiled with .NET 6 on a "clean" Windows 10 installation?
- Are there logs available to find out the reason of this behavior? (keep in mind that in my case the log tab doesn't show any information)
- What could be the reason that the frondend can be closed after a cBot compiled with .NET 6 was started but the process itself isn't terminated?
- How can I provide you with more detailed information to reproduce the issue and find a fix?
- And finally, does this mean that the current beta version was deployed to be tested by the users and only the "official" broker version will be stable and fully functioning?
Many thanks and regards,
Christian
Hi,
There is no minimum requirements for running or compiling a cBot, as long as cTrader desktop works on your system you should be able to compile and run cBots.
The beta version is for testing, cTrader 4.2 stable version is not released yet for brokers.
Regarding your issue:
1. Which edition of Windows you are using? x64/x86?
2. Rebuild, and run the cBot instance, then submit a troubleshoot report by pressing Ctrl+Alt+Shift+T and paste the forum thread URL on the report text box.
@amusleh
amusleh
26 Apr 2022, 10:18
RE: RE:
firemyst said:
amusleh said:
Hi,
I did understood your point, what I was trying to explain was there is no way to accurately know when a tick will result in formation of a new bar.
Regarding your issue, you can do something like this:
Okay. Cool. Thanks for that and your sample code. I haven't looked at the sample code, but had another question -- with cTrader shouldn't it (or is it already) programmed up to open a new bar regardless of whether or not a tick comes through?
It it takes the timing of a tick to actually open a new bar, then hypothetically bars could open up halfway through their time period, or even not at all.
If it is set to open a new bar regardless of whether a tick or not happens, then the example I provided seems to illustrate a bug where ticks happen before the bar is actually opened.
Thank you.
Hi,
If there is no tick then there will be no bars, cTrader only creates bars if there are any ticks otherwise it will not form any bar and the OnBar method will not be called.
@amusleh
amusleh
26 Apr 2022, 10:17
RE: RE:
instylereality2 said:
amusleh said:
Hi,
Your cBot code is executed by a single thread on an event loop, so while it's blocked inside OnStart method it can't execute OnTick or any other method/event.
If you want to keep checking for something then use the Timer, blocking the cBot thread will prevent execution of all other cBot methods, events, properties update, etc...
Hey amusleh thank you for stepping in!
So to clarify the goal apart the code itself which was just an approach yeah, the idea is the single thread in OnStart() to handle all the limit orders, which are the only type I'll be using. So I came to the conclusion that OnTick() should be handling the price monitoring system and the two voids should be executing simultaneously somehow so the strategy could proceed to the next phases depending on how the market goes without taking certain amount of time because the bot would be on for full week for example. Now my question is,
Is that possible to be achieved with a single bot or should I just split it into different bots?If it can be in one it is a life saver if not I need to adapt everything else accordingly.
(edit: parallel invoke??)
Thank you!
Hi,
I have no idea what you are trying to do, and why you need a loop inside OnStart method that will be stopped by OnTick event execution.
Use OnStart to populate or configure your bot and OnTick / OnBar / OnTimer / Events for trading.
Avoid blocking the cBot/Indicator thread for very long time as it will stop execution of all other methods and events.
What do you mean by: "the idea is the single thread in OnStart() to handle all the limit orders"
Your cBot thread is responsible for executing your cBot methods, event handlers, and updated it's properties.
@amusleh
amusleh
26 Apr 2022, 10:05
Hi,
I did understood your point, what I was trying to explain was there is no way to accurately know when a tick will result in formation of a new bar.
Regarding your issue, you can do something like this:
using cAlgo.API;
using System.Collections.Generic;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Sample : Robot
{
private readonly Dictionary<int, PositionBarOperation> _positionBarOperations = new Dictionary<int, PositionBarOperation>();
protected override void OnTick()
{
foreach (var position in Positions)
{
PositionBarOperation operation;
// If position ID is inside _positionBarOperations
// Then it means we already executed something for it
if (_positionBarOperations.TryGetValue(position.Id, out operation))
{
// It means the bar is not changed yet
if (operation.BarIndex == Bars.Count)
{
continue;
}
// Bar changed
else
{
operation.BarIndex = Bars.Count;
// Modify, close, etc...
}
}
// Position ID is not inside _positionBarOperations
else
{
operation = new PositionBarOperation
{
Position = position,
BarIndex = Bars.Count
};
_positionBarOperations.Add(position.Id, operation);
// Modify, close, etc...
}
}
}
private class PositionBarOperation
{
public Position Position { get; set; }
public int BarIndex { get; set; }
}
}
}
@amusleh
amusleh
03 May 2022, 12:15
Hi,
Most probably the value you use for stop loss is too small, try to Print the stop loss value before placing order and see how many Pips were the stop loss.
@amusleh