How can i stop my cbot from taking multiple trade using the ontick?
How can i stop my cbot from taking multiple trade using the ontick?
19 Jan 2021, 09:25
{
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("MA Type", Group = "Moving Average")]
public MovingAverageType MAType { get; set; }
[Parameter("Source", Group = "Moving Average")]
public DataSeries SourceSeries { 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; }
[Parameter("Source", Group = "RSI")]
public DataSeries Source { get; set; }
[Parameter("Periods", Group = "RSI", DefaultValue = 14)]
public int Periods { get; set; }
[Parameter("Stop Loss", DefaultValue = 50)]
public int StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 60)]
public int TakeProfit { get; set; }
[Parameter(DefaultValue = 1000, MinValue = 0)]
public int Volume { get; set; }
[Parameter("MACD LongCycle", DefaultValue = 26, MinValue = 1)]
public int LongCycle { get; set; }
[Parameter("MACD ShortCycle", DefaultValue = 12, MinValue = 1)]
public int ShortCycle { get; set; }
[Parameter("MACD Period", DefaultValue = 9, MinValue = 1)]
public int MACDPeriod { get; set; }
private MovingAverage slowMa;
private MovingAverage fastMa;
private MacdCrossOver _MACD;
private RelativeStrengthIndex rsi;
private const string label = "Follow trend cBot";
protected override void OnStart()
{
fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
_MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, MACDPeriod);
}
protected override void OnTick()
{
var currentSlowMa = slowMa.Result.Last(0);
var currentFastMa = fastMa.Result.Last(0);
if (rsi.Result.LastValue > 50 && currentFastMa > currentSlowMa && _MACD.MACD.Last(1) > _MACD.Signal.Last(1))
{
ExecuteMarketOrder(TradeType.Buy, Symbol, Symbol.NormalizeVolumeInUnits(Volume), label, StopLoss, TakeProfit);
}
else if (rsi.Result.LastValue < 50 && currentFastMa < currentSlowMa && _MACD.MACD.Last(1) < _MACD.Signal.Last(1))
{
ExecuteMarketOrder(TradeType.Sell, Symbol, Symbol.NormalizeVolumeInUnits(Volume), label, StopLoss, TakeProfit);
}
}
}
}
Replies
abdulrahmanzakari007
19 Jan 2021, 11:21
RE:
PanagiotisCharalampous said:
Hi abdulrahmanzakari007,
There are several ways to achieve this. Some of them are
- Use a flag
- Check if there are other positions opened
The correct solution depends on what your cBot logic is.
Best Regards,
Panagiotis
Please can you run the code and see if there is anything you could correct or edit. Thanks
@abdulrahmanzakari007
PanagiotisCharalampous
19 Jan 2021, 11:32
Hi abdulrahmanzakari007,
I cannot change the code since I don't know what are you trying to achieve. If you don't know how to program, maybe contact a Consultant to help you with this.
Best Regards,
Panagiotis
@PanagiotisCharalampous
abdulrahmanzakari007
19 Jan 2021, 11:39
RE:
PanagiotisCharalampous said:
Hi abdulrahmanzakari007,
I cannot change the code since I don't know what are you trying to achieve. If you don't know how to program, maybe contact a Consultant to help you with this.
Best Regards,
Panagiotis
I don't have much programming knowledge but what am trying to achieve is the whenever the fastMa crosses above or below the slowma and rsi is above or below 50 and macd above or below signal line it should excute a trade.
@abdulrahmanzakari007
tradermatrix
19 Jan 2021, 14:46
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.FullAccess)]
public class FollowtrendcBot : Robot
{
[Parameter(DefaultValue = 1000)]
public double Volume { get; set; }
[Parameter("Stop Loss", DefaultValue = 50)]
public double StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 60)]
public double TakeProfit { get; set; }
[Parameter("MA Type", Group = "Moving Average")]
public MovingAverageType MAType { get; set; }
[Parameter("Source", Group = "Moving Average")]
public DataSeries SourceSeries { 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; }
[Parameter("Source", Group = "RSI")]
public DataSeries Source { get; set; }
[Parameter("Periods", Group = "RSI", DefaultValue = 14)]
public int Periods { get; set; }
[Parameter("MACD LongCycle", DefaultValue = 26, MinValue = 1)]
public int LongCycle { get; set; }
[Parameter("MACD ShortCycle", DefaultValue = 12, MinValue = 1)]
public int ShortCycle { get; set; }
[Parameter("MACD Period", DefaultValue = 9, MinValue = 1)]
public int MACDPeriod { get; set; }
private MovingAverage slowMa;
private MovingAverage fastMa;
private MacdCrossOver _MACD;
private RelativeStrengthIndex rsi;
private const string label = "Follow trend cBot";
protected override void OnStart()
{
fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
_MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, MACDPeriod);
}
protected override void OnTick()
{
var currentSlowMa = slowMa.Result.Last(0);
var currentFastMa = fastMa.Result.Last(0);
var cBotPositions = Positions.FindAll(label);
if (cBotPositions.Length >= 1)
return;
if (rsi.Result.LastValue > 50 && currentFastMa > currentSlowMa && _MACD.MACD.Last(1) > _MACD.Signal.Last(1))
{
ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit);
}
else if (rsi.Result.LastValue < 50 && currentFastMa < currentSlowMa && _MACD.MACD.Last(1) < _MACD.Signal.Last(1))
{
ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, TakeProfit);
}
}
}
}
@tradermatrix
abdulrahmanzakari007
19 Jan 2021, 20:52
RE:
tradermatrix said:
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.FullAccess)] public class FollowtrendcBot : Robot { [Parameter(DefaultValue = 1000)] public double Volume { get; set; } [Parameter("Stop Loss", DefaultValue = 50)] public double StopLoss { get; set; } [Parameter("Take Profit", DefaultValue = 60)] public double TakeProfit { get; set; } [Parameter("MA Type", Group = "Moving Average")] public MovingAverageType MAType { get; set; } [Parameter("Source", Group = "Moving Average")] public DataSeries SourceSeries { 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; } [Parameter("Source", Group = "RSI")] public DataSeries Source { get; set; } [Parameter("Periods", Group = "RSI", DefaultValue = 14)] public int Periods { get; set; } [Parameter("MACD LongCycle", DefaultValue = 26, MinValue = 1)] public int LongCycle { get; set; } [Parameter("MACD ShortCycle", DefaultValue = 12, MinValue = 1)] public int ShortCycle { get; set; } [Parameter("MACD Period", DefaultValue = 9, MinValue = 1)] public int MACDPeriod { get; set; } private MovingAverage slowMa; private MovingAverage fastMa; private MacdCrossOver _MACD; private RelativeStrengthIndex rsi; private const string label = "Follow trend cBot"; protected override void OnStart() { fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType); slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType); rsi = Indicators.RelativeStrengthIndex(Source, Periods); _MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, MACDPeriod); } protected override void OnTick() { var currentSlowMa = slowMa.Result.Last(0); var currentFastMa = fastMa.Result.Last(0); var cBotPositions = Positions.FindAll(label); if (cBotPositions.Length >= 1) return; if (rsi.Result.LastValue > 50 && currentFastMa > currentSlowMa && _MACD.MACD.Last(1) > _MACD.Signal.Last(1)) { ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit); } else if (rsi.Result.LastValue < 50 && currentFastMa < currentSlowMa && _MACD.MACD.Last(1) < _MACD.Signal.Last(1)) { ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLoss, TakeProfit); } } } }
I tried using position.FindAll(label); but it keep showing error. Please is there any order solution
@abdulrahmanzakari007
tradermatrix
19 Jan 2021, 21:30
RE: RE:
yet it works for me. can be replaced;
var cBotPositions = Positions.FindAll(label);
if (cBotPositions.Length >= 1)
return;
by:
var position = Positions.Find(label);
if (position == null)
@tradermatrix
abdulrahmanzakari007
20 Jan 2021, 10:18
RE: RE: RE:
tradermatrix said:
yet it works for me. can be replaced;
var cBotPositions = Positions.FindAll(label);
if (cBotPositions.Length >= 1)
return;by:
var position = Positions.Find(label);
if (position == null)
Thanks tradermatrix it worked
@abdulrahmanzakari007
PanagiotisCharalampous
19 Jan 2021, 09:33
Hi abdulrahmanzakari007,
There are several ways to achieve this. Some of them are
The correct solution depends on what your cBot logic is.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous