Bot only triggers initial trade // Trail does not trigger
Created at 27 Mar 2024, 09:45
Bot only triggers initial trade // Trail does not trigger
27 Mar 2024, 09:45
I am coding a (relatively) simple bot.
Use on Renko Charts
Open trader after two bars close same color
Trail Stop
Stop should close trade just as another is opened in opposite direction (i.e. long gets closed just as short criteria is met)
Trail stop does not trigger, and the bot only opens the initial position, and no others.
Please help me correct these two issues, thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class RenkoTrader : Robot
{
[Parameter("Initial Quantity (Lots)", Group = "Volume", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
public double InitialQuantity { get; set; }
[Parameter("Include Trailing Stop", DefaultValue = true)]
public bool IncludeTrailingStop { get; set; }
[Parameter("Trailing Stop Trigger (pips)", DefaultValue = 0)]
public int TrailingStopTrigger { get; set; }
[Parameter("Trailing Stop Step (pips)", DefaultValue = 5)]
public int TrailingStopStep { get; set; }
private Position position = null;
protected override void OnStart()
{
}
protected override void OnBarClosed()
{
var PositionCount = Positions.Where(p => p.Label == InstanceId).Count();
// HANDLE PRICE UPDATES HERE
if (Bars.Last(2).Close > Bars.Last(2).Open && Bars.Last(1).Close > Bars.Last(1).Open && Bars.Last(0).Close > Bars.Last(0).Open)
{
if (PositionCount == 0 && position == null)
position = ExecuteMarketOrder(TradeType.Buy, SymbolName, 10, InstanceId).Position;
if (Bars.Last(2).Close < Bars.Last(2).Open && Bars.Last(1).Close < Bars.Last(1).Open && Bars.Last(0).Close < Bars.Last(0).Open)
{
if (PositionCount == 0 && position == null)
position = ExecuteMarketOrder(TradeType.Sell, SymbolName, 10, InstanceId).Position;
if (IncludeTrailingStop)
{
SetTrailingStop();
}
//ADD THIS TO MANAGE POSITION SECTION
/// <summary>
///When the profit in pips is above or equal to the trigger the stoploss will start trailing the spot price.
///TrailingStop defines the number of pips the stoploss trails the spot price by.
/// if Trigger is 0 the trailing stop will begin immediately
/// </summary>
void SetTrailingStop()
{
var sellPositions = Positions.FindAll(InstanceId, SymbolName, 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(InstanceId, SymbolName, 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);
}
}
}
}
}
}
}
}
joeanspach634
30 Sep 2024, 09:25 ( Updated at: 07 Oct 2024, 19:27 )
Hello @ slope game, I think you need to manage your positions correctly and ensure that the trailing stop is set for each position individually. Adjust the trailing stop logic to trail the stop loss correctly.
Here is the modified version of your code with corrections and improvements:
@joeanspach634