0% found this document useful (0 votes)
55 views5 pages

DonchianChannel Reversal+Volume

Uploaded by

Sadiq Bako
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views5 pages

DonchianChannel Reversal+Volume

Uploaded by

Sadiq Bako
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

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("Period", DefaultValue = 14, MinValue = 1)]
public int Period { get; set; }
[Parameter("Donchian Periods", DefaultValue = 20)]
public int DonchianPeriods { get; set; }
[Parameter("Lot Size", DefaultValue = 0.1)]
public double LotSize { get; set; }
[Parameter("Last Tick Volumes Count", DefaultValue = 13, MinValue = 1)]
public int LastTickVolumesCount { 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("Include Trailing Stop", DefaultValue = false)]
public bool IncludeTrailingStop { get; set; }

[Parameter("Trailing Stop Trigger (pips)", DefaultValue = 20, MinValue =


1)]
public int TrailingStopTrigger { get; set; }

[Parameter("Trailing Stop Steps (pips)", DefaultValue = 10)]


public double TrailingStopSteps { 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("Cand Size", DefaultValue = 50, MinValue = 1, MaxValue = 100)]
public int CandSize { get; set; }
[Parameter("Vol Size", DefaultValue = 50, MinValue = 1, MaxValue = 5000)]
public int VolSize { get; set; }
[Parameter("Break-even Offset (pips)", DefaultValue = 10)]
public int BreakEvenOffsetPips { get; set; }

private DonchianChannel donchianChannel;


public double distance = 0;
public double CandleSize = 0;
public double CandleSize2 = 0;
public double VolDiff = 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));
Print("Donchian Middle: ", donchianChannel.Middle.Last(2));
distance = Math.Abs((donchianChannel.Middle.Last(1) -
MarketSeries.Close.Last(1)) / Symbol.PipSize);
CandleSize = Math.Abs((MarketSeries.Close.Last(1) -
MarketSeries.Open.Last(1)) / Symbol.PipSize);
CandleSize2 = Math.Abs((MarketSeries.Close.Last(2) - MarketSeries.Open.Last(2))
/ 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)
{
// 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 {position.TradeType} Position at Profit:
{profitInPips}");
}

// Trailing Stop Logic


if (IncludeTrailingStop)
{
double triggerLevel = position.EntryPrice;

// Determine the trailing stop level based on trade direction


if (position.TradeType == TradeType.Buy)
{
triggerLevel += TrailingStopTrigger * Symbol.PipSize;
}
else if (position.TradeType == TradeType.Sell)
{
triggerLevel -= TrailingStopTrigger * Symbol.PipSize;
}

// Check if the current price has reached the trailing stop trigger
level
if ((position.TradeType == TradeType.Buy &&
MarketSeries.High.LastValue >= triggerLevel) ||
(position.TradeType == TradeType.Sell &&
MarketSeries.Low.LastValue <= triggerLevel))
{
double newStopLevel = (position.TradeType == TradeType.Buy)
? MarketSeries.High.LastValue - TrailingStopSteps *
Symbol.PipSize
: MarketSeries.Low.LastValue + TrailingStopSteps *
Symbol.PipSize;

// Modify stop loss or take profit based on trade direction


ModifyPosition(position, newStopLevel, position.TakeProfit);
}
}
}
}

// Check if there is enough historical data


if (MarketSeries.TickVolume.Count < Period + LastTickVolumesCount)
return;

// Calculate tick volumes


double currentTickVolume = MarketSeries.TickVolume.Last(1);
double previousTickVolume = MarketSeries.TickVolume.Last(2);

// Print the values to the console


Print("Current Tick Volume: {0}", currentTickVolume);
Print("Previous Tick Volume: {0}", previousTickVolume);

bool isPreviousVolumeGreater = true;

// Check if previousTickVolume is greater than the last N tick volumes


for (int i = 1; i <= LastTickVolumesCount; i++)
{
double lastTickVolume = MarketSeries.TickVolume.Last(i + 1);

Print("Tick Volume {0} bars ago: {1}", i + 1, lastTickVolume);

if (previousTickVolume < lastTickVolume)


{
isPreviousVolumeGreater = false;
break;
}
}
// Add debug prints
Print("Checking conditions for trade execution...");
Print("isPreviousVolumeGreater: {0}", isPreviousVolumeGreater);

// 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) && isPreviousVolumeGreater||
MarketSeries.Close.Last(2) > donchianChannel.Top.Last(2) &&
MarketSeries.Close.Last(2) > MarketSeries.Open.Last(2) && isPreviousVolumeGreater)
{
// Add more debug prints for conditions
Print("Conditions met for entering a sell order...");

// Check if the next candle is bearish


if (Positions.Count <= 0 && MarketSeries.Close.Last(1) <
MarketSeries.Open.Last(1) && CandleSize > CandSize && currentTickVolume >=
(previousTickVolume-VolSize))
{
// Print relevant information for debugging
Print("Conditions met for Sell Order!");

// Calculate and print volume


//long volume = Symbol.QuantityToVolume(LotSize);
long volume = Symbol.QuantityToVolume(Account.Balance/5000);
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", StopLoss,
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) && isPreviousVolumeGreater||
MarketSeries.Close.Last(2) < donchianChannel.Bottom.Last(2) &&
MarketSeries.Close.Last(2) < MarketSeries.Open.Last(2) && isPreviousVolumeGreater)
{
// Add more debug prints for conditions
Print("Conditions met for entering a buy order...");

// Check if the next candle is bullish


if (Positions.Count <= 0 && MarketSeries.Close.Last(1) >
MarketSeries.Open.Last(1) && CandleSize > CandSize && currentTickVolume >=
(previousTickVolume-VolSize))
{
// Print relevant information for debugging
Print("Conditions met for Buy Order!");

// Calculate and print volume


//long volume = Symbol.QuantityToVolume(LotSize);
long volume = Symbol.QuantityToVolume(Account.Balance/5000);
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", StopLoss,
TakeProfit, 0, "Buy Trade");
Print("Stop Loss Price: ", stopLossPrice);
}
}
}
}
}

You might also like