Hi there. My TSL.-bot used to work perfectly on all open positions. But since i updated ctrader to version 4.2.18 it stops interactinig with the open positions.
I thought it might be because of the framework. I even re-built the source code with .NET 6.0. but it still doesn't work. It just doesn't trigger a trailing stop loss.
There are just this 2 warnings.
Severity Code Description Project File Line Suppression State
Warning CS0618 'Positions.FindAll(string, Symbol, TradeType)' is obsolete: 'Parameter 'Symbol symbol' was replaced with 'string symbolName'. More details here https://ctrader.com/forum/announcements/15847' tsl amk (2) C:\Users\PC\Documents\cAlgo\Sources\Robots\tsl amk (2)\tsl amk (2)\tsl amk (2).cs 41 Active
Severity Code Description Project File Line Suppression State
Warning CS0618 'Positions.FindAll(string, Symbol, TradeType)' is obsolete: 'Parameter 'Symbol symbol' was replaced with 'string symbolName'. More details here https://ctrader.com/forum/announcements/15847' tsl amk (2) C:\Users\PC\Documents\cAlgo\Sources\Robots\tsl amk (2)\tsl amk (2)\tsl amk (2).cs 58 Active
But i read from several sources that one can ignore them, isn´t that right?!? Can someone holp to correct the code ?!?
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 my_tsl : Robot
{
[Parameter("Instance Name", DefaultValue = "")]
public string InstanceName { get; set; }
[Parameter("Include Trailing Stop", DefaultValue = true)]
public bool IncludeTrailingStop { get; set; }
[Parameter("Trailing Stop Trigger (pips)", DefaultValue = 2)]
public double TrailingStopTrigger { get; set; }
[Parameter("Trailing Stop Step (pips)", DefaultValue = 1)]
public double TrailingStopStep { get; set; }
protected override void OnStart()
{
// Put your initialization logic here
}
protected override void OnTick()
{
if (IncludeTrailingStop)
{
SetTrailingStop();
}
// Put your core logic here
}
private void SetTrailingStop()
{
var sellPositions = Positions.FindAll(InstanceName, Symbol, TradeType.Sell);
foreach (Position position in sellPositions)
{
double distance = position.EntryPrice - Symbol.Ask;
if (distance < TrailingStopTrigger * Symbol.PipSize)
continue;
double newStopLossPrice = Symbol.Ask + TrailingStopStep * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
{
ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
var buyPositions = Positions.FindAll(InstanceName, Symbol, TradeType.Buy);
foreach (Position position in buyPositions)
{
double distance = Symbol.Bid - position.EntryPrice;
if (distance < TrailingStopTrigger * Symbol.PipSize)
continue;
double newStopLossPrice = Symbol.Bid - TrailingStopStep * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
{
ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}