Donchian Channel Reversal

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class DonchianChannelBot : Robot
{

private DateTime _startTime;


private DateTime _stopTime;
[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("Take Profit", Group = "Protection", DefaultValue = 1)]


public int TakeProfit { get; set; }

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


public int StopLoss { get; set; }

[Parameter("Profit Target (pips)", DefaultValue = 30)]


public int ProfitTargetPips { get; set; }
[Parameter("Num Dist", DefaultValue = 50, MinValue = 1, MaxValue = 100)]
public int NumDist { get; set; }
[Parameter("Break-even Offset (pips)", DefaultValue = 10)]
public int BreakEvenOffsetPips { get; set; }

private DonchianChannel donchianChannel;


public double distance = 0;

protected override void OnStart()


{

// Start Time is the same day at 22:00:00 Server Time


_startTime = Server.Time.Date.AddHours(StartTime);

// Stop Time is the next day at 06:00:00


_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;

// Print the values for debugging


Print("Current Bar: ", index);
Print("High: ", MarketSeries.High[index]);
Print("Low: ", MarketSeries.Low[index]);
Print("Close: ", MarketSeries.Close[index]);
Print("Donchian Top: ", donchianChannel.Top.Last(2));
Print("Donchian Bottom: ", donchianChannel.Bottom.Last(2));
distance = Math.Abs((donchianChannel.Middle.Last(1) -
MarketSeries.Close.Last(1)) / Symbol.PipSize);

// Check if the current position is in profit and hasn't been partially


closed yet
foreach (var position in Positions)
{
if (position.SymbolCode == Symbol.Code)
{
double profitInPips = position.NetProfit / Symbol.PipValue;

// Check if the profit is greater than the specified target


if (profitInPips >= ProfitTargetPips && position.TradeType ==
TradeType.Buy)
{
// Calculate the volume to close (50% of the position)
long closeVolume = position.Volume / 2;

// Close 50% of the position


ClosePosition(position, closeVolume);

// Print relevant information for debugging


Print("Closing 50% of Buy Position at Profit: ", profitInPips);
}
else if (profitInPips >= ProfitTargetPips && position.TradeType ==
TradeType.Sell)
{
// Calculate the volume to close (50% of the position)
long closeVolume = position.Volume / 2;

// Close 50% of the position


ClosePosition(position, closeVolume);
// Print relevant information for debugging
Print("Closing 50% of Sell Position at Profit: ", profitInPips);
}
}
}

// Check if the current candle is the first bullish candle closing above the
Donchian Channel top
if (MarketSeries.High.Last(2) > donchianChannel.Top.Last(2) &&
MarketSeries.Close.Last(2) > MarketSeries.Open.Last(2))
{
// Check if the next candle is bearish
if (Positions.Count <= 0 && MarketSeries.Close.Last(1) <
MarketSeries.Open.Last(1) && distance > NumDist)
{
// Print relevant information for debugging
Print("Conditions met for Sell Order!");

// Calculate and print volume


long volume = Symbol.QuantityToVolume(LotSize);
Print("Sell Volume: ", volume);

// Set stop loss 10 pips above Donchian Channel top


double stopLossPrice = donchianChannel.Top.Last(2) * Symbol.PipSize +
10;

// Open a sell position


ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "Sell Order",
stopLossPrice, TakeProfit, 0, "Sell Trade");
Print("Stop Loss Price: ", stopLossPrice);
}
}
// Check if the current candle is the first bearish candle closing below the
Donchian Channel bottom
else if (MarketSeries.Low.Last(2) < donchianChannel.Bottom.Last(2) &&
MarketSeries.Close.Last(2) < MarketSeries.Open.Last(2))
{
// Check if the next candle is bullish
if (Positions.Count <= 0 && MarketSeries.Close.Last(1) >
MarketSeries.Open.Last(1) && distance > NumDist)
{
// Print relevant information for debugging
Print("Conditions met for Buy Order!");

// Calculate and print volume


long volume = Symbol.QuantityToVolume(LotSize);
Print("Buy Volume: ", volume);

// Set stop loss 10 pips below Donchian Channel bottom


double stopLossPrice = donchianChannel.Bottom.Last(2)*Symbol.PipSize -
10;

// Open a buy position


ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "Buy Order",
stopLossPrice, TakeProfit, 0, "Buy Trade");
Print("Stop Loss Price: ", stopLossPrice);
}
}
}
}
}

You might also like