Closing each opened trade after x amount of time
Closing each opened trade after x amount of time
09 Apr 2020, 20:28
Hi everyone,I have made this simple bot a while ago with help of ctrader community and sample bots because I dont have any knowledge in coding it was really hard for me but good thing is i am learing in this way,now i have one question wich I can't solve by my self so i really need your help,I need the bot to close each trade after for example 15 min from the time it has been opened
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 Cowabungastratey : Robot
{
[Parameter("Instance Name", DefaultValue = "001")]
public string InstanceName { get; set; }
[Parameter("Lot Size", DefaultValue = 0.1)]
public double LotSize { get; set; }
[Parameter("Source FastEma")]
public DataSeries SourceFastEma { get; set; }
[Parameter("Source SlowEma")]
public DataSeries SourceSlowEma { get; set; }
[Parameter("Period FastEma", DefaultValue = 5, MinValue = 1, MaxValue = 100)]
public int PeriodFastEma { get; set; }
[Parameter("Period SloeEma", DefaultValue = 10, MinValue = 5, MaxValue = 100)]
public int PeriodSlowEma { get; set; }
[Parameter("ATR Value", DefaultValue = 0.00095)]
public double AtrValue { get; set; }
[Parameter("Calculate OnBar", DefaultValue = false)]
public bool CalculateOnBar { get; set; }
private ExponentialMovingAverage FastEma;
private ExponentialMovingAverage SlowEma;
private MarketSeries h4;
private ExponentialMovingAverage ema1;
private ExponentialMovingAverage ema2;
private AverageTrueRange atr;
protected override void OnStart()
{
FastEma = Indicators.ExponentialMovingAverage(SourceFastEma, PeriodFastEma);
SlowEma = Indicators.ExponentialMovingAverage(SourceSlowEma, PeriodSlowEma);
h4 = MarketData.GetSeries(TimeFrame.Hour4);
ema1 = Indicators.ExponentialMovingAverage(h4.High, 12);
ema2 = Indicators.ExponentialMovingAverage(h4.High, 9);
atr = Indicators.AverageTrueRange(MarketSeries, 14, MovingAverageType.Exponential);
// Put your initialization logic here
}
protected override void OnTick()
{
if (CalculateOnBar)
{
return;
}
ManagePostions();
}
protected override void OnBar()
{
if (!CalculateOnBar)
{
return;
}
ManagePostions();
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
private void ManagePostions()
{
if (SlowEma.Result.LastValue < FastEma.Result.LastValue)
{
if (atr.Result.LastValue > AtrValue)
if (ema2.Result.LastValue > ema1.Result.LastValue)
if (!IsPositionOpenByType(TradeType.Buy))
{
OpenPosition(TradeType.Buy);
}
ClosePosition(TradeType.Sell);
}
if (SlowEma.Result.LastValue > FastEma.Result.LastValue)
{
if (atr.Result.LastValue > AtrValue)
if (ema2.Result.LastValue < ema1.Result.LastValue)
if (!IsPositionOpenByType(TradeType.Sell))
{
OpenPosition(TradeType.Sell);
}
ClosePosition(TradeType.Buy);
}
}
private void OpenPosition(TradeType type)
{
double Volume = Symbol.QuantityToVolumeInUnits(LotSize);
ExecuteMarketOrder(type, this.SymbolName, Volume, InstanceName, null, null);
}
private void ClosePosition(TradeType type)
{
var P = Positions.Find(InstanceName, this.SymbolName, type);
if (P != null)
{
ClosePosition(P);
}
}
private bool IsPositionOpenByType(TradeType type)
{
var P = Positions.FindAll(InstanceName, SymbolName, type);
if (P.Count() >= 1)
{
return true;
}
return false;
}
}
}
Replies
alisalehicontact
10 Apr 2020, 15:08
RE:
PanagiotisCharalampous said:
Hi alisalehicontact,
You can use Position.EntryTime property and compare it with Server.Time to decide if a position with be closed or not.
Best Regards,
Panagiotis
Thanks a lot for your reply It helped me to better understand the logic but unfortunately i couldn't make it work,theas are the thing i did:
1- I added a parameter to be able to control the amount of time I want the trade to continue
[Parameter("Max time position open(minute)", DefaultValue = 15, MinValue = 5)]
public int MaxTimeOpen { get; set; }
2-then as you suggested i added the Position.EntryTime property and server time to where the bot opens my positions and I also add posOpenTimeMinute to be able to comare
private void OpenPosition(TradeType type)
{
double Volume = Symbol.QuantityToVolumeInUnits(LotSize);
ExecuteMarketOrder(type, this.SymbolName, Volume, InstanceName, null, null);
Print(LastResult.Position.EntryTime.Minute);
Print(Server.Time.Minute)
int posOpenTimeMinute = position.EntryTime.Minute + MaxTimeOpen;
3-I tried to add this logic to where bot closes my postions:
if (Server.Time.Minute > posOpenTimeMinute)
{
ClosePosition(TradeType.Buy);
but I am getting this error: Error CS1513: } expected wich I cant figure out where is the problem
here is the whole code again after adding these new things:
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 Cowabungastratey : Robot
{
[Parameter("Instance Name", DefaultValue = "001")]
public string InstanceName { get; set; }
[Parameter("Lot Size", DefaultValue = 0.1)]
public double LotSize { get; set; }
[Parameter("Max time position open(minute)", DefaultValue = 15, MinValue = 5)]
public int MaxTimeOpen { get; set; }
[Parameter("Source FastEma")]
public DataSeries SourceFastEma { get; set; }
[Parameter("Source SlowEma")]
public DataSeries SourceSlowEma { get; set; }
[Parameter("Period FastEma", DefaultValue = 5, MinValue = 1, MaxValue = 100)]
public int PeriodFastEma { get; set; }
[Parameter("Period SloeEma", DefaultValue = 10, MinValue = 5, MaxValue = 100)]
public int PeriodSlowEma { get; set; }
[Parameter("ATR Value", DefaultValue = 0.00095)]
public double AtrValue { get; set; }
[Parameter("Calculate OnBar", DefaultValue = false)]
public bool CalculateOnBar { get; set; }
private ExponentialMovingAverage FastEma;
private ExponentialMovingAverage SlowEma;
private MarketSeries h4;
private ExponentialMovingAverage ema1;
private ExponentialMovingAverage ema2;
private AverageTrueRange atr;
protected override void OnStart()
{
FastEma = Indicators.ExponentialMovingAverage(SourceFastEma, PeriodFastEma);
SlowEma = Indicators.ExponentialMovingAverage(SourceSlowEma, PeriodSlowEma);
h4 = MarketData.GetSeries(TimeFrame.Hour4);
ema1 = Indicators.ExponentialMovingAverage(h4.High, 12);
ema2 = Indicators.ExponentialMovingAverage(h4.High, 9);
atr = Indicators.AverageTrueRange(MarketSeries, 14, MovingAverageType.Exponential);
// Put your initialization logic here
}
protected override void OnTick()
{
if (CalculateOnBar)
{
return;
}
ManagePostions();
}
protected override void OnBar()
{
if (!CalculateOnBar)
{
return;
}
ManagePostions();
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
private void ManagePostions()
{
if (SlowEma.Result.LastValue < FastEma.Result.LastValue)
{
if (atr.Result.LastValue > AtrValue)
if (ema2.Result.LastValue > ema1.Result.LastValue)
if (!IsPositionOpenByType(TradeType.Buy))
{
OpenPosition(TradeType.Buy);
}
if (Server.Time.Minute > posOpenTimeMinute)
{
ClosePosition(TradeType.Sell);
}
if (SlowEma.Result.LastValue > FastEma.Result.LastValue)
{
if (atr.Result.LastValue > AtrValue)
if (ema2.Result.LastValue < ema1.Result.LastValue)
if (!IsPositionOpenByType(TradeType.Sell))
{
OpenPosition(TradeType.Sell);
}
if (Server.Time.Minute > posOpenTimeMinute)
{
ClosePosition(TradeType.Buy);
}
}
private void OpenPosition(TradeType type)
{
double Volume = Symbol.QuantityToVolumeInUnits(LotSize);
ExecuteMarketOrder(type, this.SymbolName, Volume, InstanceName, null, null);
Print(LastResult.Position.EntryTime.Minute);
Print(Server.Time.Minute)
int posOpenTimeMinute = position.EntryTime.Minute + MaxTimeOpen;
}
private void ClosePosition(TradeType type)
{
var P = Positions.Find(InstanceName, this.SymbolName, type);
if (P != null)
{
ClosePosition(P);
}
}
private bool IsPositionOpenByType(TradeType type)
{
var P = Positions.FindAll(InstanceName, SymbolName, type);
if (P.Count() >= 1)
{
return true;
}
return false;
}
}
}
@alisalehicontact
PanagiotisCharalampous
10 Apr 2020, 07:37
Hi alisalehicontact,
You can use Position.EntryTime property and compare it with Server.Time to decide if a position with be closed or not.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous