How to get the bot to close my manual trades?
How to get the bot to close my manual trades?
15 Feb 2022, 18:32
Please help this is the code.
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Strat3 : Robot
{
[Parameter("Evaluation Time ", DefaultValue = 3600)]
public int EvaluationTime { get; set; }
[Parameter("Volume ", DefaultValue = 100, MinValue = 10, Step = 10)]
public double Volume { get; set; }
[Parameter()]
public DataSeries MaPrice { get; set; }
[Parameter("MaType", DefaultValue = MovingAverageType.Weighted)]
public MovingAverageType MaType { get; set; }
[Parameter("DPO Period", DefaultValue = 3)]
public int DPOPeriod { get; set; }
[Parameter("RVI Period", DefaultValue = 9)]
public int RVIPeriod { get; set; }
private Position OpenPosition;
private int pips = 2;
private double _Tenkan;
private double _DPrice;
private double _RVi;
private double _Signal;
private double _CurrentPrice;
private RelativeVigorIndex _RVI;
private IchimokuKinkoHyo _Ichi;
private DetrendedPriceOscillator _DPO;
protected override void OnStart()
{
Timer.Start(EvaluationTime);
_RVI = Indicators.GetIndicator<RelativeVigorIndex>(RVIPeriod, MaType);
_DPO = Indicators.DetrendedPriceOscillator(MaPrice, DPOPeriod, MaType);
_Ichi = Indicators.IchimokuKinkoHyo(5, 9, 26);
}
protected override void OnTimer()
{
_Tenkan = _Ichi.TenkanSen.LastValue;
_DPrice = _DPO.Result.LastValue;
_RVi = _RVI.Result.LastValue;
_Signal = _RVI.Signal.LastValue;
_CurrentPrice = MarketSeries.Close.LastValue;
if (OpenPosition == null)
{
if (_RVi > _Signal && _DPrice > 0 && _CurrentPrice < _Tenkan)
{
VolumeReset();
var Result = ExecuteMarketOrder(TradeType.Buy, Symbol, Volume);
OpenPosition = Result.Position;
}
else if (_Signal > _RVi && _DPrice < 0 && _CurrentPrice > _Tenkan)
{
VolumeReset();
var Result = ExecuteMarketOrder(TradeType.Sell, Symbol, Volume);
OpenPosition = Result.Position;
}
}
if (OpenPosition != null)
{
foreach (var position in Positions)
if (position.TradeType == TradeType.Buy)
if (_RVi < _Signal && _DPrice < 0)
{
CloseOpenPosition();
}
foreach (var position in Positions)
if (position.TradeType == TradeType.Sell)
if (_RVi > _Signal && _DPrice > 0)
{
CloseOpenPosition();
}
}
}
private void CloseOpenPosition()
{
ClosePosition(OpenPosition);
OpenPosition = null;
}
private void VolumeReset()
{
Volume = (Account.Balance / 0.7) / 5;
Volume = Symbol.NormalizeVolumeInUnits(Volume, RoundingMode.Down);
}
}
}
amusleh
16 Feb 2022, 10:03 ( Updated at: 16 Feb 2022, 10:04 )
Hi,
To close a position you just have to pass the position to ClosePosition method and it will close it.
Please check the references for example code:
Method ClosePosition | API reference | cTrader Community
cAlgo API Reference - Positions Interface (ctrader.com)
cAlgo API Reference - Position Interface (ctrader.com)
@amusleh