how to make cBot opens multiple positions to overcome the broker maximum position size limit of 100 indices?
how to make cBot opens multiple positions to overcome the broker maximum position size limit of 100 indices?
01 Jul 2022, 10:07
how to make this cBot opens multiple positions to overcome the broker maximum position size limit of 100 indices?
here is the code:
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
{
#region Parameters
[Parameter("Volume", DefaultValue = 100)]
public double Volume { get; set; }
[Parameter("Period", DefaultValue = 14, Group = "Simple Moving Average")]
public int SMAPeriod { get; set; }
[Parameter("Period", DefaultValue = 14, Group = "Relative Strength Index")]
public int RSIPeriod { get; set; }
#endregion
#region Private Variables
RelativeStrengthIndex _rsi;
SimpleMovingAverage _sma;
#endregion
#region Methods
protected override void OnStart()
{
// Put your initialization logic here
_rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RSIPeriod);
_sma = Indicators.SimpleMovingAverage(Bars.ClosePrices, SMAPeriod);
}
protected override void OnBar()
{
// Put your core logic here
if (Positions.Count(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Buy) == 0 && Bars.ClosePrices.Last(1) > _sma.Result.Last(1) && Bars.OpenPrices.Last(1) < _sma.Result.Last(1))
{
foreach (var position in Positions.Where(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Sell))
position.Close();
ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "");
}
if (Positions.Count(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Sell) == 0 && Bars.ClosePrices.Last(1) < _sma.Result.Last(1) && Bars.OpenPrices.Last(1) > _sma.Result.Last(1))
{
foreach (var position in Positions.Where(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Buy))
position.Close();
ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "");
}
if (_rsi.Result.Last(1) < 70)
{
foreach (var position in Positions.Where(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Buy))
position.Close();
}
if (_rsi.Result.Last(1) > 30)
{
foreach (var position in Positions.Where(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Sell))
position.Close();
}
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
#endregion
}
}