Hi,
The entry signal of my bot is when the faster ma crosses the slower ma to the upside, my bot should place a buy limit order plus 10 pips from the current price.
And when the faster ma crosses the slower ma to the downside, my bot should place a sell limit order minus 10 pips from the current price.
However when I backtest, my bot enters exactly at the crossover and not plus/minus 10 pips from the current price.
Do you have any idea what I did worng?
Source 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
{
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("TP", DefaultValue = 20)]
public int TP { get; set; }
[Parameter("SL", DefaultValue = 10)]
public int SL { 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 = 50)]
public int SlowPeriods { get; set; }
[Parameter("Fast Periods", Group = "Moving Average", DefaultValue = 16)]
public int FastPeriods { get; set; }
private MovingAverage slowMa;
private MovingAverage fastMa;
private const string label = "Sample Trend cBot";
protected override void OnStart()
{
fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
}
protected override void OnTick()
{
var longPosition = Positions.Find("Buy Limit", SymbolName, TradeType.Buy);
var shortPosition = Positions.Find("Sell Limit", SymbolName, TradeType.Sell);
var currentSlowMa = slowMa.Result.Last(0);
var currentFastMa = fastMa.Result.Last(0);
var previousSlowMa = slowMa.Result.Last(1);
var previousFastMa = fastMa.Result.Last(1);
if (Positions.Count == 0)
{
//Buy Entry
if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)
{
if (PendingOrders.Count(item => item.OrderType == PendingOrderType.Limit && item.TradeType == TradeType.Buy) == 0)
PlaceLimitOrder(TradeType.Buy, SymbolName, VolumeInUnits, Symbol.Ask + 10 * Symbol.PipSize, "Buy Limit", SL, TP);
}
//Sell Entry
else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
{
if (PendingOrders.Count(item => item.OrderType == PendingOrderType.Limit && item.TradeType == TradeType.Sell) == 0)
PlaceLimitOrder(TradeType.Sell, SymbolName, VolumeInUnits, Symbol.Bid - 10 * Symbol.PipSize, "Sell Limit", SL, TP);
}
}
}
private double VolumeInUnits
{
get { return Symbol.QuantityToVolumeInUnits(Quantity); }
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}