Hi all, my bot does open position but it doesnt closes them any idea why? I use onbar because i want it to run on the native renko charts built in to ctrader. Thx in advance for the help!
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; }
// Declare and create an instance of MedianRenko indicator
//
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(0);
var currentFastMa = emafast.Result.Last(0);
var previousSlowMa = emaslow.Result.Last(1);
var previousFastMa = emafast.Result.Last(1);
var longPosition = Positions.Find("Buy", SymbolName, TradeType.Buy);
var shortPosition = Positions.Find("Sell", SymbolName, TradeType.Sell);
if(Positions.Count == 0)
{
//Buy Entry
if ( previousSlowMa < previousFastMa && currentSlowMa > currentFastMa)
{
if (shortPosition != null)
ClosePosition(shortPosition);
ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits,"Buy");
}
//Sell Entry
else if (previousSlowMa > previousFastMa && currentSlowMa < currentFastMa)
{
if (longPosition != null)
ClosePosition(longPosition);
ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell");
}
}
}
private double VolumeInUnits
{
get { return Symbol.QuantityToVolumeInUnits(Quantity); }
}
}
}