So my bot should operate as follows.
Buy entry:
//if ema cross to the upside happens (fastma crosses slowma to the upside)
// Place buy stop order
//if opposite ema cross happens (fastma crosses slowma to the downside)
// Close open position if buy stop order was triggered, or cancel the buy stop order if it was not triggered (which is a pending order)
vice versa for sell
i modified my source code a bit, it does cancel the pending orders now, however it does this straight away, which leads to that no trades get triggered.
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.FullAccess)]
public class NewcBot : Robot
{
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Periodsslow", DefaultValue = 30)]
public int Periodsslow { get; set; }
[Parameter("Periodsfast", DefaultValue = 30)]
public int Periodsfast { get; set; }
[Parameter("TP", DefaultValue = 2)]
public int TP { get; set; }
[Parameter("SL", DefaultValue = 100)]
public int SL { get; set; }
[Parameter("Triggerinpips", DefaultValue = 5)]
public int Trigger { get; set; }
private ExponentialMovingAverage emaslow;
private ExponentialMovingAverage emafast;
protected override void OnStart()
{
emaslow = Indicators.ExponentialMovingAverage(Source, Periodsslow);
emafast = Indicators.ExponentialMovingAverage(Source, Periodsfast);
}
protected override void OnBar()
{
var currentSlowMa = emaslow.Result.Last(1);
var currentFastMa = emafast.Result.Last(1);
var previousSlowMa = emaslow.Result.Last(2);
var previousFastMa = emafast.Result.Last(2);
var longPosition = Positions.Find("Buy", SymbolName, TradeType.Buy);
var shortPosition = Positions.Find("Sell", SymbolName, TradeType.Sell);
{
//Buy Entry
if (previousSlowMa > previousFastMa && currentSlowMa < currentFastMa)
{
if (shortPosition != null)
ClosePosition(shortPosition);
{
if(Positions.Count == 0)
PlaceStopLimitOrder(TradeType.Buy, SymbolName, VolumeInUnits, Symbol.Ask + (Trigger * Symbol.PipSize), 0.0, "Buy", SL, TP);
}
}
//Canel buy stop limit order
if (previousSlowMa < previousFastMa && currentSlowMa > currentFastMa)
{
foreach (var _PendingOrders in PendingOrders)
{
if (_PendingOrders.Label == "Buy")
{
CancelPendingOrder(_PendingOrders);
}
}
}
//Sell Entry
else if (previousSlowMa < previousFastMa && currentSlowMa > currentFastMa)
{
if (longPosition != null)
ClosePosition(longPosition);
{
if(Positions.Count == 0)
PlaceStopLimitOrder(TradeType.Sell, SymbolName, VolumeInUnits, Symbol.Bid - (Trigger * Symbol.PipSize), 0.0, "Sell", SL, TP);
}
}
}
//Cancel sell stop limit order
if (previousSlowMa > previousFastMa && currentSlowMa < currentFastMa)
{
foreach (var _PendingOrders in PendingOrders)
{
if (_PendingOrders.Label == "Sell")
{
CancelPendingOrder(_PendingOrders);
}
}
}
}
private double VolumeInUnits
{
get { return Symbol.QuantityToVolumeInUnits(Quantity); }
}
protected void CloseAllOrders()
{
foreach (var order in PendingOrders)
{
CancelPendingOrder(order);
}
}
}
}