Gann Hilo
Gann Hilo
25 Apr 2022, 21:38
Hello, can anyone help me?
I want to add a condition to this indicator but I do not know how to code
I want this indicator to have another Gan Hilo
For example, two GannHilo 10 and 100, and the position opens when the price crosses both, but when the price returns from the Gun 10, the position closes.
My language is not English so I hope you understand what I mean
And thank you very much for helping me
this is code:
// -------------------------------------------------------------------------------
//
// This is a Robot based on the GannHighLow Indicator.
// If close price rises above the GannHighLow indicator a buy is triggered and
// if the prices falls below the GannHighLow indicator a sell is triggered.
// There is only one position open at a time. So, if a buy is triggered and a sell is open
// the sell will be closed.
//
// The default parameters use no SL/TP (Default to zero)
// If SLTrigger and TrailingStop are greater than zero then Trailing stop is used
//
// -------------------------------------------------------------------------------
using System;
using cAlgo.API;
using cAlgo.API.Requests;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot()]
public class GannHiLoRobot : Robot
{
private GannHighLow _gannHighLowIndicator;
private Position _position;
[Parameter("Period", DefaultValue = 50)]
public int Period { get; set; }
[Parameter("Volume", DefaultValue = 10000)]
public int Volume { get; set; }
[Parameter(DefaultValue = 0)]
public int StopLoss { get; set; }
[Parameter(DefaultValue = 0)]
public int TakeProfit { get; set; }
[Parameter(DefaultValue = 0)]
public double SLTrigger { get; set; }
[Parameter(DefaultValue = 0)]
public double TrailingStop { get; set; }
private bool _isTrigerred;
protected bool UseTrailingStop
{
get { return SLTrigger > 0.0 && TrailingStop > 0.0; }
}
protected override void OnStart()
{
_gannHighLowIndicator = Indicators.GetIndicator<GannHighLow>(Period);
}
protected override void OnTick()
{
if (Trade.IsExecuting || _position == null)
return;
if (UseTrailingStop)
Trail();
}
/// <summary>
/// If close price rises above the GannHighLow indicator a buy is triggered and
/// if the prices falls below the GannHighLow indicator a sell is triggered.
/// </summary>
protected override void OnBar()
{
bool isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
bool isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;
if (_gannHighLowIndicator.Result.HasCrossedAbove(MarketSeries.Close, 1) && !isShortPositionOpen)
{
ClosePosition();
Sell();
}
else if (_gannHighLowIndicator.Result.HasCrossedBelow(MarketSeries.Close, 1) && !isLongPositionOpen)
{
ClosePosition();
Buy();
}
}
/// <summary>
/// Close the existing position
/// </summary>
private void ClosePosition()
{
if (_position == null)
return;
Trade.Close(_position);
_position = null;
}
/// <summary>
/// Send a Buy trade request
/// </summary>
private void Buy()
{
Request request = new MarketOrderRequest(TradeType.Buy, Volume)
{
Label = "Gann HiLo",
//SlippagePips = 1,
StopLossPips = StopLoss > 0 ? (int?)StopLoss : null,
TakeProfitPips = TakeProfit > 0 ? (int?)TakeProfit : null
};
Trade.Send(request);
}
/// <summary>
/// Send a Sell trade request
/// </summary>
private void Sell()
{
Request request = new MarketOrderRequest(TradeType.Sell, Volume)
{
Label = "Gann HiLo",
//SlippagePips = 1,
StopLossPips = StopLoss > 0 ? (int?)StopLoss : null,
TakeProfitPips = TakeProfit > 0 ? (int?)TakeProfit : null
};
Trade.Send(request);
}
protected override void OnPositionOpened(Position openedPosition)
{
_position = openedPosition;
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
/// <summary>
/// Trailing Stop
/// </summary>
private void Trail()
{
if (_position.TradeType == TradeType.Buy)
{
double distance = Symbol.Bid - _position.EntryPrice;
if (distance >= SLTrigger * Symbol.PipSize)
{
if (!_isTrigerred)
{
_isTrigerred = true;
Print("Trailing Stop Loss triggered...");
}
double newStopLossPrice = Math.Round(Symbol.Bid - TrailingStop * Symbol.PipSize, Symbol.Digits);
if (_position.StopLoss == null || newStopLossPrice > _position.StopLoss)
{
Trade.ModifyPosition(_position, newStopLossPrice, _position.TakeProfit);
}
}
}
else
{
double distance = _position.EntryPrice - Symbol.Ask;
if (distance >= SLTrigger * Symbol.PipSize)
{
if (!_isTrigerred)
{
_isTrigerred = true;
Print("Trailing Stop Loss triggered...");
}
double newStopLossPrice = Math.Round(Symbol.Ask + TrailingStop * Symbol.PipSize, Symbol.Digits);
if (_position.StopLoss == null || newStopLossPrice < _position.StopLoss)
{
Trade.ModifyPosition(_position, newStopLossPrice, _position.TakeProfit);
}
}
}
}
}
}
amusleh
26 Apr 2022, 09:05
Hi,
The indicator you are using on your cBot is not a cTrader built-in indicator, so I don't know how it works.
I added two more instance of it on your indicator and changed the entry/exit logic to also consider the crossing between them.
Here is the code:
I'm not sure that's what you are looking to add, but you can now change the entry/exit logic based on your needs.
@amusleh