Cbot Stopped
Cbot Stopped
24 Jun 2020, 10:42
Dear All,I had download the following code from Ctrader website and try it in my demo account.
using System;
using cAlgo.API;
using cAlgo;
using cAlgo.Lib;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class PayBackII : Robot
{
[Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
public int InitialVolume { get; set; }
[Parameter("Stop Loss", DefaultValue = 57)]
public int StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 150)]
public int TakeProfit { get; set; }
const long microVolume = 1000;
const string botLabel = "PB-";
protected override void OnStart()
{
Positions.Opened += OnPositionOpened;
Positions.Closed += OnPositionClosed;
relanceOrders();
}
private void relanceOrders()
{
manageOpen(TradeType.Buy, InitialVolume);
manageOpen(TradeType.Sell, InitialVolume);
}
private void manageOpen(TradeType tradeType, long volume, string prefixLabel = botLabel)
{
int nVolumePartition = 10, part1 = 5, part2 = 3;
long nVol = (long)Math.Floor((double)(volume / (microVolume * nVolumePartition)));
long partialVolume = nVol * microVolume;
var result1 = ExecuteMarketOrder(tradeType, Symbol, partialVolume * part1, prefixLabel + tradeType.ToString() + "-1");
var result2 = ExecuteMarketOrder(tradeType, Symbol, partialVolume * part2, prefixLabel + tradeType.ToString() + "-2");
var result3 = ExecuteMarketOrder(tradeType, Symbol, volume - (part1 + part2) * partialVolume, prefixLabel + tradeType.ToString() + "-3");
}
private void manageClose()
{
foreach (var position in Positions)
{
if (position.TakeProfit.HasValue)
{
string labelType = position.Label.Substring(position.Label.Length - 1, 1);
double potentialGainPips = ((position.TradeType == TradeType.Buy) ? 1 : -1) * (position.TakeProfit.Value - position.EntryPrice) / Symbol.PipSize;
double potentialLosePips = ((position.TradeType == TradeType.Buy) ? 1 : -1) * (position.StopLoss.Value - position.EntryPrice) / Symbol.PipSize;
double percentGain = position.Pips / potentialGainPips;
double percentLose = -position.Pips / potentialLosePips;
if ((percentGain >= 0.43) && (labelType == "3"))
ClosePosition(position);
if ((percentGain >= 0.76) && (labelType == "2"))
ClosePosition(position);
if ((percentLose <= -0.33) && (labelType == "1"))
ClosePosition(position);
if ((percentLose <= -0.66) && (labelType == "2"))
ClosePosition(position);
}
}
}
protected void OnPositionOpened(PositionOpenedEventArgs args)
{
var position = args.Position;
ModifyPosition(position, position.pipsToStopLoss(Symbol, StopLoss), position.pipsToTakeProfit(Symbol, TakeProfit));
}
protected void OnPositionClosed(PositionClosedEventArgs args)
{
Position position = args.Position;
if (position.Pips < 0)
manageOpen(position.inverseTradeType(), position.Volume, botLabel + "Mart-");
if (Positions.Count == 0)
relanceOrders();
}
protected override void OnTick()
{
manageClose();
}
protected override void OnError(Error error)
{
if (error.Code != ErrorCode.BadVolume)
{
Print("erreur : " + error.Code);
Stop();
}
}
}
}
When I do the backtest it was working fine, but When I run the same in a demo account, the positions are opened but immediately the Bot is stopped with following error. Could you please help to fix it?
24/06/2020 07:26:31.220 | cBot "PayBack II" was stopped for AUDUSD, m15.
24/06/2020 07:26:31.188 | Crashed in OnTick with InvalidOperationException: Nullable object must have a value.
24/06/2020 07:26:31.172 | → Modifying position PID127983417 (SL: 0.69791, TP: 0.67721) SUCCEEDED, Position PID127983417
24/06/2020 07:26:30.922 | Modifying position PID127983417 (SL: 0.69791, TP: 0.67721)
24/06/2020 07:26:30.922 | → Modifying position PID127983415 (SL: 0.69792, TP: 0.67722) SUCCEEDED, Position PID127983415
24/06/2020 07:26:30.672 | Modifying position PID127983415 (SL: 0.69792, TP: 0.67722)
24/06/2020 07:26:30.672 | → Modifying position PID127983414 (SL: 0.69792, TP: 0.67722) SUCCEEDED, Position PID127983414
24/06/2020 07:26:30.438 | Modifying position PID127983414 (SL: 0.69792, TP: 0.67722)
24/06/2020 07:26:30.438 | → Modifying position PID127983412 (SL: 0.68654, TP: 0.70724) SUCCEEDED, Position PID127983412
24/06/2020 07:26:30.188 | Modifying position PID127983412 (SL: 0.68654, TP: 0.70724)
24/06/2020 07:26:30.188 | → Modifying position PID127983408 (SL: 0.68653, TP: 0.70723) SUCCEEDED, Position PID127983408
24/06/2020 07:26:29.751 | Modifying position PID127983408 (SL: 0.68653, TP: 0.70723)
24/06/2020 07:26:29.751 | → Modifying position PID127983404 (SL: 0.68656, TP: 0.70726) SUCCEEDED, Position PID127983404
24/06/2020 07:26:29.501 | Modifying position PID127983404 (SL: 0.68656, TP: 0.70726)
24/06/2020 07:26:29.485 | → Executing Market Order to Sell 2000 AUDUSD SUCCEEDED, Position PID127983417
24/06/2020 07:26:29.047 | Executing Market Order to Sell 2000 AUDUSD
24/06/2020 07:26:29.047 | → Executing Market Order to Sell 3000 AUDUSD SUCCEEDED, Position PID127983415
24/06/2020 07:26:28.594 | Executing Market Order to Sell 3000 AUDUSD
24/06/2020 07:26:28.594 | → Executing Market Order to Sell 5000 AUDUSD SUCCEEDED, Position PID127983414
24/06/2020 07:26:28.047 | Executing Market Order to Sell 5000 AUDUSD
24/06/2020 07:26:28.047 | → Executing Market Order to Buy 2000 AUDUSD SUCCEEDED, Position PID127983412
24/06/2020 07:26:27.516 | Executing Market Order to Buy 2000 AUDUSD
24/06/2020 07:26:27.516 | → Executing Market Order to Buy 3000 AUDUSD SUCCEEDED, Position PID127983408
24/06/2020 07:26:27.016 | Executing Market Order to Buy 3000 AUDUSD
24/06/2020 07:26:27.016 | → Executing Market Order to Buy 5000 AUDUSD SUCCEEDED, Position PID127983404
24/06/2020 07:26:26.563 | Executing Market Order to Buy 5000 AUDUSD
24/06/2020 07:26:26.563 | cBot "PayBack II" was started successfully for AUDUSD, m15.
Thanks and Regards
R. Vadivelan
Replies
velu130486
25 Jun 2020, 09:02
RE:
Hi Panagiotis,
Actually I had downloaded the bot from this forum
https://ctrader.com/algos/cbots/show/505
But I got the error for missing Calgo.lib. Similar discussions are already done in the forum and I found the solution as posted earlier by forum members.
https://calgobots.codeplex.com/
I had downloaded the library files from the link and now I able to build the code, but not able to crack the reason for this error. If you could support for this error this will help us for the previous post also.
Thanks and Regards
R. Vadivelan
PanagiotisCharalampous said:
Hi Vadivelan,
I cannot even build this cBot. It uses cAlgo.Lib which I am not even aware of. Did you try contacting the developer?
Best Regards,
Panagiotis
velu130486
03 Jul 2020, 12:11
( Updated at: 21 Dec 2023, 09:22 )
RE: RE:Cbot Strategy
Hi Panagiotis,
I had downloaded some source files from the below link and I found some files named strategies. Could you please advice how to link these strategies to the Cbot. I had already tried to save strategy file as Indicator/Cbot but not succeed. Could you please support what I have to do the strategy file to link into Cbot.
using cAlgo;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Lib;
namespace cAlgo.Strategies
{
public class ArgunesStrategy : Strategy
{
ExponentialMovingAverage ema5; // Close
SimpleMovingAverage sma8; // Open
ExponentialMovingAverage ema21; // Close
ExponentialMovingAverage ema55; // Close
public ArgunesStrategy(Robot robot) : base(robot)
{
Initialize();
}
protected override void Initialize()
{
ema5 = Robot.Indicators.ExponentialMovingAverage(Robot.MarketSeries.Close, 5);
sma8 = Robot.Indicators.SimpleMovingAverage(Robot.MarketSeries.Open, 8);
ema21 = Robot.Indicators.ExponentialMovingAverage(Robot.MarketSeries.Close, 21);
ema55 = Robot.Indicators.ExponentialMovingAverage(Robot.MarketSeries.Close, 55);
}
public override TradeType? signal()
{
if (!Robot.existBuyPositions() && (ema21.Result.LastValue > ema55.Result.LastValue) && (ema5.Result.HasCrossedAbove(sma8.Result, 0)))
{
Robot.closeAllSellPositions();
return TradeType.Buy;
}
if (!Robot.existSellPositions() && (ema21.Result.LastValue < ema55.Result.LastValue) && (ema5.Result.HasCrossedBelow(sma8.Result, 0)))
{
Robot.closeAllBuyPositions();
return TradeType.Sell;
}
return null;
}
}
}
using System;
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Lib;
using cAlgo.Strategies;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Argunes : Robot
{
#region cBot Parameters
[Parameter("Volume", DefaultValue = 100000, MinValue = 0)]
public int InitialVolume { get; set; }
[Parameter("Stop Loss", DefaultValue = 150)]
public int StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 1000)]
public int TakeProfit { get; set; }
#endregion
#region cBot variables
OrderParams initialOP;
List<Strategy> strategies;
#endregion
protected override void OnStart()
{
base.OnStart();
double slippage = 2; // maximum slippage in point, if order execution imposes a higher slippage, the order is not executed.
string botPrefix = "Argunes"; // order prefix passed by the bot
string positionComment = string.Format("{0}-{1} {2}", botPrefix, Symbol.Code, TimeFrame); ; // order label passed by the bot
initialOP = new OrderParams(null, Symbol, InitialVolume, this.botName(), StopLoss, TakeProfit, slippage, positionComment, null, new List<double>() { 5, 3, 2 });
strategies = new List<Strategy>();
strategies.Add(new ArgunesStrategy(this));
}
protected override void OnTick()
{
}
protected override void OnBar()
{
base.OnBar();
controlRobot();
}
protected override void OnStop()
{
base.OnStop();
this.closeAllPositions();
}
private void controlRobot()
{
TradeType? tradeType = this.signal(strategies);
if (tradeType.HasValue)
{
initialOP.TradeType = tradeType.Value;
initialOP.Volume = InitialVolume;
this.splitAndExecuteOrder(initialOP);
}
}
}
velu130486 said:
Hi Panagiotis,
Actually I had downloaded the bot from this forum
https://ctrader.com/algos/cbots/show/505
But I got the error for missing Calgo.lib. Similar discussions are already done in the forum and I found the solution as posted earlier by forum members.
https://calgobots.codeplex.com/
I had downloaded the library files from the link and now I able to build the code, but not able to crack the reason for this error. If you could support for this error this will help us for the previous post also.
Thanks and Regards
R. Vadivelan
PanagiotisCharalampous said:
Hi Vadivelan,
I cannot even build this cBot. It uses cAlgo.Lib which I am not even aware of. Did you try contacting the developer?
Best Regards,
Panagiotis
PanagiotisCharalampous
03 Jul 2020, 12:18
Hi Vadivelan,
You can read how to create cBots/Indicators here. However these strategies seem to use custom libraries which you will need to add as references. I do not know where to find them so it would be better to contact the cBot creators for more detailed support.
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
24 Jun 2020, 14:27
Hi Vadivelan,
I cannot even build this cBot. It uses cAlgo.Lib which I am not even aware of. Did you try contacting the developer?
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous