Converting single symbol cBot to multi symbol
Converting single symbol cBot to multi symbol
01 Apr 2021, 10:24
I have developed a cBot which handles a single symbol and uses properties/events such as Bars.LastBar, Symbol.TickSize, OnTick, etc. which refers to the current chart single symbol, Now I want to extend my cBot so it will run the same way only on several symbols, without running it on several charts. How can I accomplish that?
Thanks
Replies
eynt
25 Oct 2021, 06:33
RE: RE: RE:
Hello
I have failed converting my code from a single to multi symbol. Perhaps you can attach a code sample of how you converted one of cTrader cBot samples (such as Sample Aligator, Sample Bears Power etc.) from single to multi symbol. An important note: The code must support running on a back test as well the same way (meaning one chart will run several symbols).
Thanks
@eynt
amusleh
25 Oct 2021, 09:42
RE: RE: RE: RE:
yuval.ein said:
Hello
I have failed converting my code from a single to multi symbol. Perhaps you can attach a code sample of how you converted one of cTrader cBot samples (such as Sample Aligator, Sample Bears Power etc.) from single to multi symbol. An important note: The code must support running on a back test as well the same way (meaning one chart will run several symbols).
Thanks
Hi,
Here is the sample martingale cBot that works on two symbols, you must provide the symbol names:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SampleMartingalecBot : Robot
{
[Parameter("Initial Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double InitialQuantity { get; set; }
[Parameter("Stop Loss", Group = "Protection", DefaultValue = 40)]
public int StopLoss { get; set; }
[Parameter("Take Profit", Group = "Protection", DefaultValue = 40)]
public int TakeProfit { get; set; }
[Parameter("First Symbol", Group = "Symbols")]
public string FirstSymbolName { get; set; }
[Parameter("Second Symbol", Group = "Symbols")]
public string SecondSymbolName { get; set; }
private Random random = new Random();
protected override void OnStart()
{
Positions.Closed += OnPositionsClosed;
var firstSymbol = Symbols.GetSymbol(FirstSymbolName);
ExecuteOrder(InitialQuantity, firstSymbol, GetRandomTradeType());
var secondSymbol = Symbols.GetSymbol(SecondSymbolName);
ExecuteOrder(InitialQuantity, secondSymbol, GetRandomTradeType());
}
private void ExecuteOrder(double quantity, Symbol symbol, TradeType tradeType)
{
var volumeInUnits = symbol.QuantityToVolumeInUnits(quantity);
var result = ExecuteMarketOrder(tradeType, symbol.Name, volumeInUnits, "Martingale", StopLoss, TakeProfit);
if (result.Error == ErrorCode.NoMoney)
Stop();
}
private void OnPositionsClosed(PositionClosedEventArgs args)
{
Print("Closed");
var position = args.Position;
if (position.Label != "Martingale") return;
var symbol = Symbols.GetSymbol(args.Position.SymbolName);
if (position.GrossProfit > 0)
{
ExecuteOrder(InitialQuantity, symbol, GetRandomTradeType());
}
else
{
ExecuteOrder(position.Quantity * 2, symbol, position.TradeType);
}
}
private TradeType GetRandomTradeType()
{
return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
}
}
}
@amusleh
firemyst
26 Oct 2021, 05:45
RE: RE: RE: RE: RE: RE:
yuval.ein said:
What if I want to use OnTick for more than one symbol?
Thanks
All you have to do is subscribe to the Tick event for each symbol you want.
This thread talks about it for indicators, but it's the same for cBots:
Example: Bars.Tick += YourOnTickEventMethod;
@firemyst
eynt
28 Oct 2021, 10:04
RE: RE: RE: RE: RE: RE: RE:
My original single symbol cBot has dozens of private members (strings, integers, etc.). All those members were designed for a single symbol. Now that there are many symbols, each of them should have a different value per symbol. Is there an easy straight forward way to convert all the members from single to multi symbol?
Thanks
@eynt
firemyst
28 Oct 2021, 10:21
RE: RE: RE: RE: RE: RE: RE: RE:
yuval.ein said:
My original single symbol cBot has dozens of private members (strings, integers, etc.). All those members were designed for a single symbol. Now that there are many symbols, each of them should have a different value per symbol. Is there an easy straight forward way to convert all the members from single to multi symbol?
Thanks
Yes.
Add [] onto the end of them to declare them as arrays.
int[]
double[]
etc etc
[0] is the first symbol's value, [1] is the second symbol's, so on and so forth. Hopefully after that, you won't have to make too many other changes to your code depending on how it's written.
@firemyst
amusleh
01 Apr 2021, 10:42
Hi,
This sample indicator might help you:
Please read the API references for more info regarding symbols and bars.
@amusleh