You are on page 1of 3

using System;

using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class DonchianChannelBot : Robot
{
private DateTime _startTime;
private DateTime _stopTime;
private int _tradesExecuted = 0;

[Parameter("Start Hour", DefaultValue = 10.0)]


public double StartTime { get; set; }

[Parameter("Stop Hour", DefaultValue = 12.0)]


public double StopTime { get; set; }

[Parameter("Donchian Periods", DefaultValue = 20)]


public int DonchianPeriods { get; set; }

[Parameter("Lot Size", DefaultValue = 0.1)]


public double LotSize { get; set; }

[Parameter("Pips Offset", DefaultValue = 20)]


public double PipsOffset { get; set; }

[Parameter("Take Profit", Group = "Protection", DefaultValue = 1)]


public int TakeProfit { get; set; }

[Parameter("Stop Loss", Group = "Protection", DefaultValue = 0)]


public int StopLoss { get; set; }
[Parameter("Max Trades", DefaultValue = 10)]
public int MaxTrades { get; set; }

public string InstanceName { get; set; }

private DonchianChannel donchianChannel;


private MovingAverage sma;
private int lastBullishIndex = -1;
private int lastBearishIndex = -1;

protected override void OnStart()


{
_startTime = Server.Time.Date.AddHours(StartTime);
_stopTime = Server.Time.Date.AddHours(StopTime);

Print("Start Time {0},", _startTime);


Print("Stop Time {0},", _stopTime);

donchianChannel = Indicators.DonchianChannel(MarketSeries,
DonchianPeriods);
}

protected override void OnBar()


{
if (Trade.IsExecuting)
return;

var currentHours = Server.Time.TimeOfDay.TotalHours;


bool tradeTime = StartTime < StopTime
? currentHours > StartTime && currentHours < StopTime
: currentHours < StopTime || currentHours > StartTime;

if (!tradeTime)
return;

int index = MarketSeries.Close.Count - 1;

if (index < DonchianPeriods + 1|| _tradesExecuted >= MaxTrades)


return;

// Check if previous candle was bullish


bool isPrevCandleBullish = MarketSeries.Open.Last(2) <
MarketSeries.Close.Last(2);
// Check if current candle closes bearish
bool isCurrentCandleBearish = MarketSeries.Close.Last(1) <
MarketSeries.Open.Last(1);
// Check if previous candle was bearish
bool isPrevCandleBearish = MarketSeries.Close.Last(2) <
MarketSeries.Open.Last(2);
// Check if current candle closes bullish
bool isCurrentCandleBullish = MarketSeries.Close.Last(1) >
MarketSeries.Open.Last(1);

int prevIndex = index - 1;

for (int i = prevIndex; i >= 0; i--)


{
if (MarketSeries.Close[i] > donchianChannel.Middle[i] ||
MarketSeries.Close[i] > donchianChannel.Top[i])
{
lastBullishIndex = i;
break;
}
else if (MarketSeries.Close[i] < donchianChannel.Middle[i] ||
MarketSeries.Close[i] < donchianChannel.Bottom[i])
{
lastBearishIndex = i;
break;
}
}

if (Positions.Count <= 0 && lastBullishIndex != -1 &&


isPrevCandleBullish && isCurrentCandleBearish && MarketSeries.Close.Last(1) <
MarketSeries.Close[lastBullishIndex])
{
Print("Conditions met for Sell Order!");

long volume = Symbol.QuantityToVolume(LotSize);


double entryPrice = Symbol.Bid - PipsOffset * Symbol.PipSize;
DateTime expiration = Server.Time.AddMinutes(55);
PlaceStopOrder(TradeType.Sell, Symbol, volume, entryPrice,
InstanceName, StopLoss, TakeProfit, expiration);
lastBullishIndex = -1; // Reset lastBullishIndex after placing the
sell order
_tradesExecuted++; // Increment trades executed
}

}
}
}

You might also like