ctrader sample bot multiple instances
ctrader sample bot multiple instances
22 Feb 2022, 09:43
cTrader has inbuilt cBot 'Sample RSI cBot'. I also added SL TP to code.
If I run multiple instances of this cBot (same symbol, different timeframes), it shows same Label 'SampleRSI' for all positions opened through different cBot instances.
This creates problem because bot can't differentiate which open position need to be closed.
How can I add new parameter 'Instance_Name' to cBot? It will allow me to write different values in Instance_Name parameter for each cBot instance, to differentiate open positions.
I am not expert in coding, so please update it in the below code. Thanks
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SampleRSImodifiedwoLabel : Robot
{
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Source", Group = "RSI")]
public DataSeries Source { get; set; }
[Parameter("Periods", Group = "RSI", DefaultValue = 14)]
public int Periods { get; set; }
[Parameter("Lower RSI Level", Group = "RSI", DefaultValue = 30, MinValue = 0, MaxValue = 50)]
public int LRL { get; set; }
[Parameter("Upper RSI Level", Group = "RSI", DefaultValue = 70, MinValue = 50, MaxValue = 100)]
public int URL { get; set; }
[Parameter("Take Profit in pips", Group = "TP SL", DefaultValue = 100)]
public int TP { get; set; }
[Parameter("Stop Loss in pips", Group = "TP SL", DefaultValue = 100)]
public int SL { get; set; }
private RelativeStrengthIndex rsi;
protected override void OnStart()
{
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
}
protected override void OnTick()
{
if (rsi.Result.LastValue < LRL)
{
Close(TradeType.Sell);
Open(TradeType.Buy);
}
else if (rsi.Result.LastValue > URL)
{
Close(TradeType.Buy);
Open(TradeType.Sell);
}
}
private void Close(TradeType tradeType)
{
foreach (var position in Positions.FindAll("SampleRSI", SymbolName, tradeType))
ClosePosition(position);
}
private void Open(TradeType tradeType)
{
var position = Positions.Find("SampleRSI", SymbolName, tradeType);
var volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
if (position == null)
ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "SampleRSI", SL, TP);
}
}
}
amusleh
23 Feb 2022, 09:00
Hi,
Try this:
@amusleh