Looks like the bot is using legacy code, it still works, but some features like filtering by label did not work.
This was added to the OnStart method, the new method is to subscribe to the events.
protected override void OnStart()
{
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
ma = Indicators.MovingAverage(Source, MAPeriod, MAType);
Positions.Opened += Positions_Opened;
Positions.Closed += Positions_Closed;
}
This was added to the project replacing the opened and closed position event methods.
private void OpenPosition(TradeType command)
{
//Trade.CreateMarketOrder(command, Symbol, Volume, Label);
ExecuteMarketOrder(command, SymbolName, Volume, Label);
}
private void Positions_Opened(PositionOpenedEventArgs obj)
{
Print(obj.Position.Label);
if (obj.Position.Label == Label && obj.Position.SymbolName == SymbolName)
{
_position = obj.Position;
}
}
A new method ExecuteMarketOrder is used to submit a market order and include the label.
Here is the complete source code.
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class RsiMa : Robot
{
private Position _position;
private MovingAverage ma;
private RelativeStrengthIndex rsi;
[Parameter("Instance Name", DefaultValue = "0123")]
public string Label { get; set; }
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("RSI Periods", DefaultValue = 14)]
public int Periods { get; set; }
[Parameter("RSI Overbought", DefaultValue = 70)]
public int Overbought { get; set; }
[Parameter("RSI Oversold", DefaultValue = 30)]
public int Oversold { get; set; }
[Parameter("MA Period", DefaultValue = 200)]
public int MAPeriod { get; set; }
[Parameter("Volume", DefaultValue = 1000, MinValue = 0)]
public int Volume { get; set; }
[Parameter("MA Type")]
public MovingAverageType MAType { get; set; }
protected override void OnStart()
{
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
ma = Indicators.MovingAverage(Source, MAPeriod, MAType);
Positions.Opened += Positions_Opened;
Positions.Closed += Positions_Closed;
}
protected override void OnBar()
{
if (Trade.IsExecuting)
return;
if (rsi.Result.LastValue < Oversold && (ma.Result.LastValue < MarketSeries.Close.LastValue) && _position == null)
{
OpenPosition(TradeType.Buy);
}
if (rsi.Result.LastValue > Overbought && (ma.Result.LastValue > MarketSeries.Close.LastValue) && _position == null)
{
OpenPosition(TradeType.Sell);
}
if (_position != null)
{
if (_position.TradeType == TradeType.Buy && rsi.Result.LastValue > Overbought)
{
Trade.Close(_position);
}
if (_position.TradeType == TradeType.Sell && rsi.Result.LastValue < Oversold)
{
Trade.Close(_position);
}
}
}
private void OpenPosition(TradeType command)
{
//Trade.CreateMarketOrder(command, Symbol, Volume, Label);
ExecuteMarketOrder(command, SymbolName, Volume, Label);
}
private void Positions_Opened(PositionOpenedEventArgs obj)
{
if (obj.Position.Label == Label && obj.Position.SymbolName == SymbolName)
{
_position = obj.Position;
}
}
private void Positions_Closed(PositionClosedEventArgs obj)
{
_position = null;
}
}
}