Creating a cBot that compounds the investment amount
18 Sep 2018, 21:14
Im trying to code a cBot that insead of having a parameter that simply allows me to set the amount of units that i wish to buy i would like the code to get the account ballance and the equity. however having limmited coding expericance, especiallly when coding a cBot, i dont know how to get this data from cTrader. I assume that it is stored in an API. I though maybe loaing the API "using System;" I would find it. If anyone could tell me how to access account data such as this it would be much appricated.
Also as the API's that cAlgo has added to c# are very specific and is there a guide anywhere to learn how to code cBots?
Thank you,
Will
Replies
Jonkey
26 Sep 2018, 14:07
Hope this helps, its a good starting point for that sort of a robot...
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 NewcBot : Robot
{
// ######################################################################################################## PARAMETERS ##########
[Parameter("Vol as %", DefaultValue = false)]
public bool volPercentBool { get; set; }
[Parameter("Vol %", DefaultValue = 1, MinValue = 1, Step = 1)]
public int volPercent { get; set; }
[Parameter("Vol Qty", DefaultValue = 50000, MinValue = 1000, Step = 1000)]
public int volQty { get; set; }
[Parameter("StopLoss Pips", DefaultValue = 10.0, Step = 0.1)]
public double slPips { get; set; }
[Parameter("Allowable Slippage", DefaultValue = 0.5, MinValue = 0.5, Step = 0.1)]
public double marketRangePips { get; set; }
// ########################################################################################################### VARIABLES ##########
private int volume;
// ################################################################################################## CALCULATE VOLUME ##########
private int CalculateVolume(double stopLossPips)
{
int result;
switch (volPercentBool)
{
case true:
double costPerPip = (double)((int)(Symbol.PipValue * 10000000)) / 100;
double posSizeForRisk = (Account.Balance * volPercent / 100) / (stopLossPips * costPerPip);
double posSizeToVol = (Math.Round(posSizeForRisk, 2) * 100000);
Print("costperppip = {0}, posSizeFoprRisk = {1}, posSizeLotsToVol = {2}", costPerPip, posSizeForRisk, posSizeToVol);
result = (int)Symbol.NormalizeVolumeInUnits(posSizeToVol, RoundingMode.ToNearest);
result = result > 150000 ? 150000 : result;
Print("{0}% of Account Balance used for Volume! Volume equals {1}", volPercent, result);
break;
default:
result = volQty;
Print("Volume Quantity Used! Volume equals {0}", result);
break;
}
return result;
}
// ########################################################################################################### ON START ##########
protected override void OnStart()
{
// Put your initialization logic here
volume = CalculateVolume(slPips);
}
protected override void OnTick()
{
// Put your core logic here
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
@Jonkey

PanagiotisCharalampous
19 Sep 2018, 11:09
Hi Will,
Thanks for posting in our forum. You can get balance and equity information from Account.Balance and Account.Equity properties.
Best Regards,
Panagiotis
@PanagiotisCharalampous