Description
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 TrailingStopLossSample : Robot
{
[Parameter("VolumeInLots", DefaultValue = 1)]
public double VolumeInLots { get; set; }
[Parameter("Buy")]
public bool Buy { get; set; }
[Parameter("Stop Loss", DefaultValue = 5)]
public double StopLoss { get; set; }
[Parameter("Trigger When Gaining", DefaultValue = 1)]
public double TriggerWhenGaining { get; set; }
[Parameter("Trailing Stop Loss Distance", DefaultValue = 1)]
public double TrailingStopLossDistance { get; set; }
[Parameter("Step (pips)", DefaultValue = 5)]
public double Step { get; set; }
private double _highestGain;
private bool _isTrailing;
protected override void OnStart()
{
}
protected override void OnTick()
{
// Schaut nach ob Positionen offen sind =)
if (Positions.Count == 0)
{
//Execute a market order based on the direction parameter
ExecuteMarketOrder(Buy ? TradeType.Buy : TradeType.Sell, Symbol, VolumeInLots * 100000, "SampleTrailing", StopLoss, null);
//Set the position's highest gain in pips
_highestGain = Positions[0].Pips;
}
{
var position = Positions.Find("SampleTrailing");
if (position == null)
{
Stop();
return;
}
//If the trigger is reached, the robot starts trailing
if (position.Pips >= TriggerWhenGaining)
{
//Based on the position's direction, we calculate the new stop loss price and we modify the position
if (position.TradeType == TradeType.Buy)
{
var newSLprice = Symbol.Ask - (Symbol.PipSize * TrailingStopLossDistance);
if (newSLprice > position.StopLoss)
{
ModifyPosition(position, newSLprice, null);
}
}
else
{
var newSLprice = Symbol.Bid + (Symbol.PipSize * TrailingStopLossDistance);
if (newSLprice < position.StopLoss)
{
ModifyPosition(position, newSLprice, null);
}
}
TriggerWhenGaining += Step;
}
}
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
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 TrailingStopLossSample : Robot
{
[Parameter("VolumeInLots", DefaultValue = 1)]
public double VolumeInLots { get; set; }
[Parameter("Buy")]
public bool Buy { get; set; }
[Parameter("Stop Loss", DefaultValue = 5)]
public double StopLoss { get; set; }
[Parameter("Trigger When Gaining", DefaultValue = 1)]
public double TriggerWhenGaining { get; set; }
[Parameter("Trailing Stop Loss Distance", DefaultValue = 1)]
public double TrailingStopLossDistance { get; set; }
[Parameter("Step (pips)", DefaultValue = 5)]
public double Step { get; set; }
private double _highestGain;
private bool _isTrailing;
protected override void OnStart()
{
}
protected override void OnTick()
{
// Schaut nach ob Positionen offen sind =)
if (Positions.Count == 0)
{
//Execute a market order based on the direction parameter
ExecuteMarketOrder(Buy ? TradeType.Buy : TradeType.Sell, Symbol, VolumeInLots * 100000, "SampleTrailing", StopLoss, null);
//Set the position's highest gain in pips
_highestGain = Positions[0].Pips;
}
{
var position = Positions.Find("SampleTrailing");
if (position == null)
{
Stop();
return;
}
//If the trigger is reached, the robot starts trailing
if (position.Pips >= TriggerWhenGaining)
{
//Based on the position's direction, we calculate the new stop loss price and we modify the position
if (position.TradeType == TradeType.Buy)
{
var newSLprice = Symbol.Ask - (Symbol.PipSize * TrailingStopLossDistance);
if (newSLprice > position.StopLoss)
{
ModifyPosition(position, newSLprice, null);
}
}
else
{
var newSLprice = Symbol.Bid + (Symbol.PipSize * TrailingStopLossDistance);
if (newSLprice < position.StopLoss)
{
ModifyPosition(position, newSLprice, null);
}
}
TriggerWhenGaining += Step;
}
}
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
AL
al-n
Joined on 03.01.2021
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: following.algo
- Rating: 0
- Installs: 1078
- Modified: 03/08/2022 15:48
Warning! Running cBots downloaded from this section may lead to financial losses. Use them at your own risk.
Note that publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section, please use the Copyright Infringement Notification form to submit a claim.
Comments
Log in to add a comment.
LA
I do have the same problem and same question. looking for the solution and order panel should be more user friendly and option to synchronize the Order Panel.
OS
Stainless steel spring manufacturers and suppliers necessitate a unique design based on mechanical and strength calculations.
Web development, also known as website development, refers to the tasks involved in the creation, construction, and maintenance of websites and web applications that are accessible via a browser. I'm familiar with development because I work at a logoistic agency that also creates websites.