Entry signal plus 10 pips, Cbot doesn't work.
Entry signal plus 10 pips, Cbot doesn't work.
03 Mar 2022, 11:45
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
}
}
}
Replies
isabakas11
04 Mar 2022, 17:44
RE: Thx, that worked.
amusleh said:
Hi,
When you place a limit order you want to buy/sell something in a better price, not worse price than current price.
The issue with your code was the price of orders, for buy limit order you were adding 10 pips to current price, it's not how limit orders work, actually you should minus 10 pips from current price to buy it on a better price, here is your code that works:
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 } } }
If you want to buy something or sell something on worse price than current price then use stop order, with a stop order you can place an order to buy something 10 pips above it's current price.
@isabakas11
amusleh
04 Mar 2022, 07:37
Hi,
When you place a limit order you want to buy/sell something in a better price, not worse price than current price.
The issue with your code was the price of orders, for buy limit order you were adding 10 pips to current price, it's not how limit orders work, actually you should minus 10 pips from current price to buy it on a better price, here is your code that works:
If you want to buy something or sell something on worse price than current price then use stop order, with a stop order you can place an order to buy something 10 pips above it's current price.
@amusleh