using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
// このサンプルcBotは、重心インジケーターの使い方を示しています
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class CenterOfGravitySample : Robot
{
private double _volumeInUnits;
private CenterOfGravity _centerOfGravity;
[Parameter("Volume (Lots)", DefaultValue = 0.01)]
public double VolumeInLots { get; set; }
[Parameter("Stop Loss (Pips)", DefaultValue = 10)]
public double StopLossInPips { get; set; }
[Parameter("Take Profit (Pips)", DefaultValue = 10)]
public double TakeProfitInPips { get; set; }
[Parameter("Label", DefaultValue = "Sample")]
public string Label { get; set; }
public Position[] BotPositions
{
get
{
return Positions.FindAll(Label);
}
}
protected override void OnStart()
{
_volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
_centerOfGravity = Indicators.CenterOfGravity(10<span="p">);
}
protected override void OnBar()
{
if (_centerOfGravity.Result.<span="n">Last(1) > _centerOfGravity.<span="n">Lag<span="p">.<span="n">Last<span="p">(1<span="p">) && _centerOfGravity<span="p">.<span="n">Result<span="p">.<span="n">Last<span="p">(2<span="p">) <= _centerOfGravity<span="p">.<span="n">Lag<span="p">.<span="n">Last<span="p">(2<span="p">))
{
ClosePositions<span="p">(<span="n">TradeType.<span="n">Sell<span="p">);
ExecuteMarketOrder<span="p">(<span="n">TradeType<span="p">.<span="n">Buy<span="p">, SymbolName<span="p">, _volumeInUnits<span="p">, Label<span="p">, StopLossInPips<span="p">, TakeProfitInPips<span="p">);
<span="p">}
else if <span="p">(_centerOfGravity<span="p">.<span="n">Result<span="p">.<span="n">Last<span="p">(<span="m">1<span="p">) < _centerOfGravity<span="p">.<span="n">Lag<span="p">.<span="n">Last<span="p">(<span="m">1<span="p">) && _centerOfGravity<span="p">.<span="n">Result<span="p">.<span="n">Last<span="p">(<span="m">2<span="p">) >= _centerOfGravity<span="p">.<span="n">Lag<span="p">.<span="n">Last<span="p">(<span="m">2<span="p">))
<span="p">{
<span="n">ClosePositions<span="p">(<span="n">TradeType<span="p">.<span="n">Buy<span="p">);
<span="n">ExecuteMarketOrder<span="p">(<span="n">TradeType<span="p">.<span="n">Sell<span="p">, <span="n">SymbolName<span="p">, <span="n">_volumeInUnits<span="p">, <span="n">Label<span="p">, <span="n">StopLossInPips<span="p">, <span="n">TakeProfitInPips<span="p">);
<span="p">}
<span="p">}
private void ClosePositions<span="p">(TradeType tradeType<span="p">)
<span="p">{
foreach <span="p">(var position <span="k">in BotPositions<span="p">)
<span="p">{
<span="k">if <span="p">(<span="n">position<span="p">.<span="n">TradeType != tradeType<span="p">) continue<span="p">;
ClosePosition<span="p">(position<span="p">);
}
<span="p">}
<span="p">}
<span="p">}