Hello everyone,
I hope someone can help me solve an issue with my cBot.
First of all, I want to mention that I’m trying to learn how to create cBots, so for now, I’ve used a very simple code and tried to add a logic for creating pending orders:
When the conditions for buy and sell are met, the cBot should first ensure that there are no other open positions. If there are none, it should proceed to place pending orders at a fixed distance, configurable from the control panel.
Once a Buy order is triggered, the code should then cancel all pending Buy orders. Same thing for Sell orders. This option is also configurable from the panel.
Unfortunately, when I run the cBot on the cloud, the pending orders are not placed when the openBuy or openSell conditions are met.
Additionally, I receive this error message in the log, which I don’t understand:
Error | Crashed in OnBar with TypeLoadException: Could not load type 'cAlgo.API.ProtectionType' from assembly 'cAlgo.API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3499da3018340880'.
I also want to specify that ProtectionType is a parameter I am forced to configure; otherwise, the system tells me that the code for pending orders is obsolete.
I’ve tried searching online for a possible solution but haven’t been able to find anything.
I hope someone can help me resolve this issue.
Thank you in advance, and I’m including the code of my cBot below:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SampleRSIcBot : Robot
{
// POSITION PARAMETERS
private double _volumeInUnits;
[Parameter("Volume (Lots)", DefaultValue = 0.1, Group = "Trade", Step = 0.1)]
public double VolumeInLots { get; set; }
[Parameter("Distance in pips for Limit", DefaultValue = 4, Group = "Trade", Step = 1)]
public double PipsDistance { get; set; }
[Parameter("Take Profit (Pips)", DefaultValue = 20, Group = "Trade", Step = 1)]
public double TakeProfitInPips { get; set; }
[Parameter("Stop Loss (Pips)", DefaultValue = 9, Group = "Trade", Step = 1)]
public double StopLossInPips { get; set; }
[Parameter("Enable Cancel Pending Orders", DefaultValue = true, Group = "Trade")]
public bool EnableCancelPendingOrders { get; set; }
// RSI Parameters
[Parameter("Source", Group = "RSI")]
public DataSeries Source { get; set; }
[Parameter("Periods", Group = "RSI", DefaultValue = 14)]
public int Periods { get; set; }
[Parameter("OB", Group = "RSI", DefaultValue = 70, MinValue = 0, MaxValue = 100)]
public int OBLevel { get; set; }
[Parameter("OS", Group = "RSI", DefaultValue = 30, MinValue = 0, MaxValue = 100)]
public int OSLevel { get; set; }
private RelativeStrengthIndex rsi;
protected override void OnStart()
{
// Convert volume from lots to units
_volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
}
protected override void OnBar()
{
bool openBuy = rsi.Result.LastValue < OSLevel;
Print("OpenBuy: " + openBuy);
bool openSell = rsi.Result.LastValue > OBLevel;
Print("OpenSell: " + openSell);
string _buyLabel = "WiCh-Buy";
string _sellLabel = "WiCh-Sell";
// Check if there are open Buy positions
bool _hasOpenBuyPosition = Positions.Any(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Buy && p.Label == _buyLabel);
// Check if there are open Sell positions
bool _hasOpenSellPosition = Positions.Any(p => p.SymbolName == SymbolName && p.TradeType == TradeType.Sell && p.Label == _sellLabel);
// Open pending Buy order
if (openBuy && !_hasOpenBuyPosition)
{
try
{
PlaceLimitOrder(TradeType.Buy, SymbolName, _volumeInUnits, Symbol.Ask - PipsDistance * Symbol.PipSize, _buyLabel, StopLossInPips, TakeProfitInPips, ProtectionType.Relative);
Print("Buy Limit Order Placed");
}
catch (Exception ex)
{
Print("Error placing Buy order: " + ex.Message);
}
}
// Open pending Sell order
if (openSell && !_hasOpenSellPosition)
{
try
{
PlaceLimitOrder(TradeType.Sell, SymbolName, _volumeInUnits, Symbol.Bid + PipsDistance * Symbol.PipSize, _sellLabel, StopLossInPips, TakeProfitInPips, ProtectionType.Relative);
Print("Sell Limit Order Placed");
}
catch (Exception ex)
{
Print("Error placing Sell order: " + ex.Message);
}
}
// Cancel pending orders when one is executed
if (EnableCancelPendingOrders)
{
var _openBuyPositions = Positions.FindAll(_buyLabel);
var _openSellPositions = Positions.FindAll(_sellLabel);
// If there is at least one open Buy position, cancel all pending Buy orders
if (_openBuyPositions.Length > 0)
{
var _buyPendingOrders = PendingOrders.Where(o => o.Label == _buyLabel && o.TradeType == TradeType.Buy).ToList();
foreach (var order in _buyPendingOrders)
{
CancelPendingOrder(order);
Print("Pending Buy order canceled: {0}", order.Id);
}
}
// If there is at least one open Sell position, cancel all pending Sell orders
if (_openSellPositions.Length > 0)
{
var _sellPendingOrders = PendingOrders.Where(o => o.Label == _sellLabel && o.TradeType == TradeType.Sell).ToList();
foreach (var order in _sellPendingOrders)
{
CancelPendingOrder(order);
Print("Pending Sell order canceled: {0}", order.Id);
}
}
}
}
}
}