Add Max Positions to code
Add Max Positions to code
08 Jul 2020, 19:09
Hi guys, Im backtesting this MA code, its working well for now, Im trying to improve it with max number of positions but it didnt work, anyone can help me?
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class EMAcBot1 : Robot
{
[Parameter("Max No of Positions", DefaultValue = 100, MinValue = 1, MaxValue = 1000, Step = 1)]
public int NoOfPositions { get; set; }
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("Periods", DefaultValue = 14)]
public int Periods { get; set; }
[Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Stop Loss", DefaultValue = 10)]
public int sl { get; set; }
[Parameter("Take Profit", DefaultValue = 10)]
public int tp { get; set; }
private ExponentialMovingAverage ema;
protected override void OnStart()
{
ema = Indicators.ExponentialMovingAverage(Source, Periods);
}
protected override void OnBar()
{
int index = MarketSeries.Close.Count - 1;
if (ema.Result[index - 2] < MarketSeries.Close[index - 2] && ema.Result[index - 1] > MarketSeries.Close[index - 1])
{
Open(TradeType.Sell);
}
else if (ema.Result[index - 2] > MarketSeries.Close[index - 2] && ema.Result[index - 1] < MarketSeries.Close[index - 1])
{
Open(TradeType.Buy);
}
}
private void Open(TradeType tradeType)
{
var volumeInUnits = Symbol.QuantityToVolume(Quantity);
ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "EMA", sl, tp);
}
}
}
Replies
luciancastro89
13 Jul 2020, 00:51
Many thanks for your help, I'm going to backtest it right now!!
@luciancastro89
PanagiotisCharalampous
09 Jul 2020, 08:28
Hi luciancastro89,
You can use the statement below to check the number of positions before executing your trading
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous