using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
// このサンプルcBotはKeltner Channelsインジケーターの使用方法を示しています
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class KeltnerChannelsSample : Robot
{
private double _volumeInUnits;
private KeltnerChannels _keltnerChannels;
[Parameter("Volume (Lots)", DefaultValue = 0.01)]
public double VolumeInLots { get; set; }
[Parameter("Label", DefaultValue = "Sample")]
public string Label { get; set; }
[Parameter("Source")]
public DataSeries Source { get; set; }
public Position[] BotPositions
{
get
{
return Positions.FindAll(Label);
}
}
protected override void OnStart()
{
_volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
_keltnerChannels = Indicators.KeltnerChannels(20, MovingAverageType.Exponential, 10, MovingAverageType.Simple, 2);
}
protected override void OnBar()
{
if (Bars.LowPrices.Last(1) < _keltnerChannels.Lower.Last()
)
{
if (BotPositions.Length == 0)
{
var position = Trade.OpenPosition(Symbol.Bid, _volumeInUnits, Label);
Print("Long position opened at: " + position.EntryPrice);
}
}
else
{
if (Bars.HighPrices.Last(1) > _keltnerChannels.Upper.Last()
)
{
if (BotPositions.Length > 0)
{
var position = BotPositions[0];
Trade.ClosePosition(position);
Print("Position closed at: " + position.ClosePrice);
}
}
}
}
}
}