badvolumn
badvolumn
03 Sep 2024, 08:40
I manually opened a 0.01 lot position in EUR/USD and USD/CHF without any issues, but when I started the cBot to open the same lot size, it gave an error.
Replies
chenshy27
03 Sep 2024, 10:55
RE: badvolumn
PanagiotisCharalampous said:
Hi there,
Please share your cBot code so that we can understand what it is doing.
Best regards,
Panagiotis
using cAlgo.API;
using cAlgo.API.Internals;
using System.Linq;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class GridTradingBot : Robot
{
private const double EurUsdTakeProfit = 2.5; // EUR/USD 盈利2.5美金时平仓
private const double UsdChfTakeProfit = 2.27; // USD/CHF 盈利2.27美金时平仓
private const double EurUsdLossThreshold = 2.5; // EUR/USD 亏损2.5美金时加仓
private const double UsdChfLossThreshold = 2.27; // USD/CHF 亏损2.27美金时加仓
private const double LotSize = 0.01; // 初始手数
private const double MaxLotSize = 0.1; // 最大手数
private Symbol _eurUsd;
private Symbol _usdChf;
protected override void OnStart()
{
// 获取符号信息
_eurUsd = Symbols.GetSymbol("EURUSD");
_usdChf = Symbols.GetSymbol("USDCHF");
// 启动时开仓 EUR/USD 和 USD/CHF 多单
OpenPosition(_eurUsd, TradeType.Buy);
OpenPosition(_usdChf, TradeType.Buy);
}
protected override void OnTick()
{
ManagePositions(_eurUsd, EurUsdTakeProfit, EurUsdLossThreshold);
ManagePositions(_usdChf, UsdChfTakeProfit, UsdChfLossThreshold);
}
private void OpenPosition(Symbol symbol, TradeType tradeType, double volume = LotSize)
{
ExecuteMarketOrder(tradeType, symbol.Name, volume, "GridTradingBot");
}
private void ManagePositions(Symbol symbol, double takeProfit, double lossThreshold)
{
var positions = Positions.FindAll("GridTradingBot", symbol.Name);
foreach (var position in positions)
{
// 如果单个仓位的利润达到设定的盈利目标,平仓并重新开仓
if (position.NetProfit >= takeProfit)
{
ClosePosition(position);
OpenPosition(symbol, position.TradeType, LotSize);
}
}
// 如果总亏损超过设定的亏损阈值且手数未达到上限,加仓
double totalVolume = positions.Sum(p => p.VolumeInUnits);
double totalProfit = positions.Sum(p => p.NetProfit);
if (totalProfit <= -lossThreshold && totalVolume < MaxLotSize * 100000)
{
OpenPosition(symbol, TradeType.Buy, LotSize);
}
}
}
}
@chenshy27
chenshy27
03 Sep 2024, 10:56
RE: badvolumn
PanagiotisCharalampous said:
Hi there,
Please share your cBot code so that we can understand what it is doing.
Best regards,
Panagiotis
using cAlgo.API;
using cAlgo.API.Internals;
using System.Linq;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class GridTradingBot : Robot
{
private const double EurUsdTakeProfit = 2.5; // EUR/USD 盈利2.5美金时平仓
private const double UsdChfTakeProfit = 2.27; // USD/CHF 盈利2.27美金时平仓
private const double EurUsdLossThreshold = 2.5; // EUR/USD 亏损2.5美金时加仓
private const double UsdChfLossThreshold = 2.27; // USD/CHF 亏损2.27美金时加仓
private const double LotSize = 0.01; // 初始手数
private const double MaxLotSize = 0.1; // 最大手数
private Symbol _eurUsd;
private Symbol _usdChf;
protected override void OnStart()
{
// 获取符号信息
_eurUsd = Symbols.GetSymbol("EURUSD");
_usdChf = Symbols.GetSymbol("USDCHF");
// 启动时开仓 EUR/USD 和 USD/CHF 多单
OpenPosition(_eurUsd, TradeType.Buy);
OpenPosition(_usdChf, TradeType.Buy);
}
protected override void OnTick()
{
ManagePositions(_eurUsd, EurUsdTakeProfit, EurUsdLossThreshold);
ManagePositions(_usdChf, UsdChfTakeProfit, UsdChfLossThreshold);
}
private void OpenPosition(Symbol symbol, TradeType tradeType, double volume = LotSize)
{
ExecuteMarketOrder(tradeType, symbol.Name, volume, "GridTradingBot");
}
private void ManagePositions(Symbol symbol, double takeProfit, double lossThreshold)
{
var positions = Positions.FindAll("GridTradingBot", symbol.Name);
foreach (var position in positions)
{
// 如果单个仓位的利润达到设定的盈利目标,平仓并重新开仓
if (position.NetProfit >= takeProfit)
{
ClosePosition(position);
OpenPosition(symbol, position.TradeType, LotSize);
}
}
// 如果总亏损超过设定的亏损阈值且手数未达到上限,加仓
double totalVolume = positions.Sum(p => p.VolumeInUnits);
double totalProfit = positions.Sum(p => p.NetProfit);
if (totalProfit <= -lossThreshold && totalVolume < MaxLotSize * 100000)
{
OpenPosition(symbol, TradeType.Buy, LotSize);
}
}
}
}
@chenshy27
PanagiotisCharalampous
03 Sep 2024, 11:48
RE: RE: badvolumn
chenshy27 said:
PanagiotisCharalampous said:
Hi there,
Please share your cBot code so that we can understand what it is doing.
Best regards,
Panagiotis
using cAlgo.API;
using cAlgo.API.Internals;
using System.Linq;namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class GridTradingBot : Robot
{
private const double EurUsdTakeProfit = 2.5; // EUR/USD 盈利2.5美金时平仓
private const double UsdChfTakeProfit = 2.27; // USD/CHF 盈利2.27美金时平仓
private const double EurUsdLossThreshold = 2.5; // EUR/USD 亏损2.5美金时加仓
private const double UsdChfLossThreshold = 2.27; // USD/CHF 亏损2.27美金时加仓
private const double LotSize = 0.01; // 初始手数
private const double MaxLotSize = 0.1; // 最大手数private Symbol _eurUsd;
private Symbol _usdChf;protected override void OnStart()
{
// 获取符号信息
_eurUsd = Symbols.GetSymbol("EURUSD");
_usdChf = Symbols.GetSymbol("USDCHF");// 启动时开仓 EUR/USD 和 USD/CHF 多单
OpenPosition(_eurUsd, TradeType.Buy);
OpenPosition(_usdChf, TradeType.Buy);
}protected override void OnTick()
{
ManagePositions(_eurUsd, EurUsdTakeProfit, EurUsdLossThreshold);
ManagePositions(_usdChf, UsdChfTakeProfit, UsdChfLossThreshold);
}private void OpenPosition(Symbol symbol, TradeType tradeType, double volume = LotSize)
{
ExecuteMarketOrder(tradeType, symbol.Name, volume, "GridTradingBot");
}private void ManagePositions(Symbol symbol, double takeProfit, double lossThreshold)
{
var positions = Positions.FindAll("GridTradingBot", symbol.Name);foreach (var position in positions)
{
// 如果单个仓位的利润达到设定的盈利目标,平仓并重新开仓
if (position.NetProfit >= takeProfit)
{
ClosePosition(position);
OpenPosition(symbol, position.TradeType, LotSize);
}
}// 如果总亏损超过设定的亏损阈值且手数未达到上限,加仓
double totalVolume = positions.Sum(p => p.VolumeInUnits);
double totalProfit = positions.Sum(p => p.NetProfit);if (totalProfit <= -lossThreshold && totalVolume < MaxLotSize * 100000)
{
OpenPosition(symbol, TradeType.Buy, LotSize);
}
}
}
}
The volume in your order method should be in units not in lots. Use QuantityToVolume() method to convert your lots to units
@PanagiotisCharalampous
PanagiotisCharalampous
03 Sep 2024, 08:58
Hi there,
Please share your cBot code so that we can understand what it is doing.
Best regards,
Panagiotis
@PanagiotisCharalampous