Topics
Forum Topics not found
Replies
amusleh
02 Nov 2021, 08:14
RE: RE:
SYUNHUA said:
amusleh said:
This sample might help you:
using cAlgo.API; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class PauseEvents : Robot { private bool _isPaused; protected override void OnStart() { Chart.MouseDown += Chart_MouseDown; } private void Chart_MouseDown(ChartMouseEventArgs obj) { if (obj.ShiftKey) { _isPaused = true; Print("Events paused"); } else if (obj.CtrlKey) { _isPaused = false; Print("Events running"); } } protected override void OnTick() { if (_isPaused) return; } protected override void OnBar() { if (_isPaused) return; } } }
If you press shift + click then it will pause the OnTick/Bar, and if you press Ctrl + Click then it will allow the code on OnTick/Bar after is pause check to execute.
it is not working, just printed log, I have deleted onBar or Ontick to test. what else do I need to do
Hi,
The above sample only disables OnTick/OnBar, it doesn't pause back tester.
@amusleh
amusleh
01 Nov 2021, 08:26
Hi,
Please read this tutorial: How to Calculate Margin for Forex Trades (valutrades.com)
You have to convert the required margin from symbol base currency to your account currency.
For CFDs you can't get the base currency with automate API, that's the problem, but for Forex the base currency is the first currency on symbol pair.
@amusleh
amusleh
01 Nov 2021, 08:19
Hi,
Try this:
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
private ExponentialMovingAverage _ema;
protected override void OnStart()
{
_ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 200);
}
protected override void OnBar()
{
if (Bars.ClosePrices.Last(1) > _ema.Result.Last(1))
{
// Buy
}
else if (Bars.ClosePrices.Last(1) < _ema.Result.Last(1))
{
// Sell
}
}
}
}
@amusleh
amusleh
29 Oct 2021, 08:52
Hi,
You can easily change Lots to Units, use Symo QuantityToVolumeInUnits method.
@amusleh
amusleh
29 Oct 2021, 08:50
Hi,
Please learn basics of C# before developing indicators/cBot.
For date and time you can use C# DateTime, DateTimeOffset, and TimeSpan types.
To get time from user via a parameter you can use string for parameter type and then parse it to any of the above mentioned types.
@amusleh
amusleh
27 Oct 2021, 11:56
Hi,
The original position ID is on the comment of opposite position, so you can match them by using the opposite position comment and the original position ID.
To get original position ID from opposite position you can split its comment with "_", the second value will be ID, then parse the ID with long.Parse and then you can compare it with original position ID for matching.
foreach (var pos in positionsCopy)
{
// if position comment is empty then skip it
if (string.IsNullOrWhiteSpace(pos.Comment)) continue;
var commentSplit = pos.Comment.Split('_');
if (commentSplit.Length < 2) continue;
var originalPositionIdString = commentSplit[1];
long originalPositionId;
if (!long.TryParse(originalPositionIdString, out originalPositionId)) continue;
var originalPosition = Positions.FirstOrDefault(position => position.Id == originalPositionId);
if (originalPosition == null) continue;
// Now you have the original position on originalPosition variable
}
@amusleh
amusleh
27 Oct 2021, 08:30
( Updated at: 27 Oct 2021, 08:31 )
Hi,
No, you can't get the angle from API, but you can calculate it if you want to:
private double GetAngle(ChartTrendLine trendLine)
{
var x1 = Bars.OpenTimes.GetIndexByTime(trendLine.Time1);
var x2= Bars.OpenTimes.GetIndexByTime(trendLine.Time2);
double xDiff = x2 - x1;
var yDiff = trendLine.Y2 - trendLine.Y1;
return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
}
The above method will return the angle of a trend line in Degree, it will not work properly if your trend line is drawn in future time, because it uses bar indices for x axis.
If you to get the angle from API then please open a thread on forum suggestions section.
@amusleh
amusleh
27 Oct 2021, 08:20
Hi,
The code I posted opens an opposite position for each of your bot positions based on given condition.
What do you mean by lots of positions? do you want to set a maximum position number?
If you can tell us what exactly you want to implement on your cBot then I will be able to help you.
@amusleh
amusleh
26 Oct 2021, 09:22
( Updated at: 26 Oct 2021, 09:23 )
Hi,
Try this:
private double GetMax(DateTime startTime, DateTime endTime)
{
var startBarIndex = Bars.OpenTimes.GetIndexByTime(startTime);
var endBarIndex = Bars.OpenTimes.GetIndexByTime(endTime);
var max = double.MinValue;
for (var barIndex = startBarIndex; barIndex <= endBarIndex; barIndex++)
{
max = Math.Max(Bars.HighPrices[barIndex], max);
}
return max;
}
private double GetMin(DateTime startTime, DateTime endTime)
{
var startBarIndex = Bars.OpenTimes.GetIndexByTime(startTime);
var endBarIndex = Bars.OpenTimes.GetIndexByTime(endTime);
var min = double.MaxValue;
for (var barIndex = startBarIndex; barIndex <= endBarIndex; barIndex++)
{
min = Math.Min(Bars.LowPrices[barIndex], min);
}
return min;
}
This should work on all chart types as long as the data is available on your chart in between start and end time.
@amusleh
amusleh
26 Oct 2021, 09:15
Hi,
You should use OnTick not OnBar, and the issue with your code is that you are opening multiple opposite positions for a single position, try this:
using System;
using System.Linq;
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
protected override void OnTick()
{
CreateOpposite();
}
private void CreateOpposite()
{
var positionsCopy = Positions.ToArray();
foreach (var pos in positionsCopy)
{
var comment = string.Format("Opposite_{0}", pos.Id);
if (Positions.Any(position => string.Equals(position.Comment, comment, StringComparison.OrdinalIgnoreCase)))
{
continue;
}
var sym = pos.SymbolName;
var type = pos.TradeType;
var cmd = pos.Comment;
var ov = pos.VolumeInUnits;
double atr = ATR[sym].Result.LastValue;
if (type == TradeType.Buy && pos.Pips <= -5 * atr)
{
ExecuteMarketOrder(TradeType.Sell, sym, ov, "EA", 0, 0, comment);
}
if (type == TradeType.Sell && pos.Pips <= -5 * atr)
{
ExecuteMarketOrder(TradeType.Buy, sym, ov, "EA", 0, 0, comment);
}
}
}
}
}
I use comment to prevent opening multiple opposite positions for a single position, each position will have only one opposite position.
@amusleh
amusleh
25 Oct 2021, 09:53
Hi,
MarketSeries is obsolete, you should use Bars: cAlgo API Reference - Bars Interface (ctrader.com)
To get lowest price in last 20 bars you can use Bars.LowPrices.Minimum(20).
@amusleh
amusleh
25 Oct 2021, 09:51
RE: RE:
velu130486 said:
Hi Ahmad,
Thanks for your reply. Actually my request is to double the volume on every 4th position count not based on time.
Because sometimes my bot create more than 4 - 6 positions within a hour depends on moving average signal. So the volume is increased on each and every position which I would like to control it, so I would like to increase the volume only on 4,8,12,16 position for both buy and sell positions etc.,
Thanks and Regards
R. Vadivelan
amusleh said:
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(); } } } }
Hi,
You can do that by using position label to differentiate your cBot positions from other positions, then use Positions collection to count the number of your cBot open positions.
Then you can multiply the number of positions to volume or any other number you want to, for example if your cBot had 4 open positions you can multiply the volume by 2.
@amusleh
amusleh
03 Nov 2021, 07:58
Hi,
Your Stochastic_Flipe_Sep-30 indicator causing this issue, we can't help you unless you post the code of that indicator.
If an indicator/cBot is causing such as issue the only way you can fix it is to tweak the indicator/cBot code.
@amusleh