need help
need help
13 May 2022, 17:02
Hello guys
This robot does not work
I do not know where the problem is. Please if anyone knows, help me, thank you.
PS: buy position to open when the high price above Fractal closes and that be above the sma
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots13
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class FractalsSample : Robot
{
[Parameter(DefaultValue = 250)]
private int Period { get; set; }
[Parameter()]
private SimpleMovingAverage _sma;
private DataSeries Source { get; set; }
private double _volumeInUnits;
private Fractals _fractals;
[Parameter("Volume (Lots)", DefaultValue = 0.5)]
public double VolumeInLots { get; set; }
[Parameter("Stop Loss (Pips)", DefaultValue = 20)]
private double StopLossInPips { get; set; }
[Parameter("Take Profit (Pips)", DefaultValue = 60)]
private double TakeProfitInPips { get; set; }
[Parameter("Label", DefaultValue = "Sample")]
private string Label { get; set; }
private Position[] BotPositions
{
get { return Positions.FindAll(Label); }
}
protected override void OnStart()
{
_sma = Indicators.SimpleMovingAverage(Source, Period);
_volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
_fractals = Indicators.Fractals(5);
}
protected override void OnBar()
{
var lastIndex = MarketSeries.Close.Count - 1;
double close = MarketSeries.Close[lastIndex - 1];
double sma = _sma.Result[lastIndex - 1];
double upfr = _fractals.UpFractal[lastIndex - 1];
double dwfr = _fractals.DownFractal[lastIndex - 1];
if (upfr < close)
{
ClosePositions(TradeType.Sell);
if (sma < close && Positions.Count == 0)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
}
}
else if (dwfr > close)
{
ClosePositions(TradeType.Buy);
if (sma > close && Positions.Count == 0)
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
}
}
}
private void ClosePositions(TradeType tradeType)
{
foreach (var position in BotPositions)
{
if (position.TradeType != tradeType)
continue;
ClosePosition(position);
}
}
}
}
//////////////////////////////
amusleh
16 May 2022, 09:57 ( Updated at: 16 May 2022, 09:58 )
Hi,
There are several issues on your code, and you have to remember that Fractals are lagging indicator, and you have to access it's x previous values based on the period to passed to it.
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.UTC, AccessRights = AccessRights.None)] public class NewcBot : Robot { private double _volumeInUnits; private Fractals _fractals; private SimpleMovingAverage _sma; [Parameter(DefaultValue = 20)] public int Period { get; set; } [Parameter("Source")] public DataSeries Source { get; set; } [Parameter("Volume (Lots)", DefaultValue = 0.5)] public double VolumeInLots { get; set; } [Parameter("Stop Loss (Pips)", DefaultValue = 20)] public double StopLossInPips { get; set; } [Parameter("Take Profit (Pips)", DefaultValue = 60)] public double TakeProfitInPips { get; set; } [Parameter("Label", DefaultValue = "Sample")] public string Label { get; set; } private Position[] BotPositions { get { return Positions.FindAll(Label); } } protected override void OnStart() { _sma = Indicators.SimpleMovingAverage(Source, Period); _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots); _fractals = Indicators.Fractals(5); } protected override void OnBar() { double close = Bars.ClosePrices.Last(1); double sma = _sma.Result.Last(1); double upfr = _fractals.UpFractal.Last(5); double dwfr = _fractals.DownFractal.Last(5); if (upfr < close) { ClosePositions(TradeType.Sell); if (sma < close && Positions.Count == 0) { ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips); } } else if (dwfr > close) { ClosePositions(TradeType.Buy); if (sma > close && Positions.Count == 0) { ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips); } } } private void ClosePositions(TradeType tradeType) { foreach (var position in BotPositions) { if (position.TradeType != tradeType) continue; ClosePosition(position); } } } }
@amusleh