How would I add a trailing stop to this algorithm
19 Jul 2019, 16:56
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot]
public class BARS_3 : Robot
{
[Parameter("Vol", DefaultValue = 10000) ] public int Vol { get; set; }
[Parameter("SL", DefaultValue = 100) ] public int SL { get; set; }
[Parameter("TP", DefaultValue = 100) ] public int TP { get; set; }
private DataSeries Price { get; set; }
private Position op_pos;
protected override void OnStart()
{
// Put your initialization logic here
}
private void verify_3bars_and_trade()
{
int bars=MarketSeries.Close.Count-1;
int[] b_or_m=new int[3];
Print("{0},{1},{2},{3},{4}",MarketSeries.High[bars-1],MarketSeries.High[bars-2],MarketSeries.High[bars-3],MarketSeries.Close.Count,MarketSeries.High[1]);
if (MarketSeries.Close[bars-1]>MarketSeries.Open[bars-1]) b_or_m[0]=1;
else b_or_m[0]=2;
if (MarketSeries.Close[bars-2]>MarketSeries.Open[bars-2]) b_or_m[1]=1;
else b_or_m[1]=2;
if (MarketSeries.Close[bars-3]>MarketSeries.Open[bars-3]) b_or_m[2]=1;
else b_or_m[2]=2;
Print("{0},{1},{2}",b_or_m[0],b_or_m[1],b_or_m[2]);
TradeType op=TradeType.Buy;
int b_or_s=0;
if ((b_or_m[0]==1)&&(b_or_m[1]==1)&&(b_or_m[2]==1))
{
op= TradeType.Buy;
b_or_s=1;
}
if ((b_or_m[0]==2)&&(b_or_m[1]==2)&&(b_or_m[2]==2))
{
op= TradeType.Sell;
b_or_s=2;
}
if(b_or_s!=0)
{
Trade.CreateMarketOrder(op, Symbol, Vol);
Print("{0} {1} with vol={2}",Symbol.Code,op,Vol);
}
}
protected override void OnTick()
{
}
protected override void OnBar()
{
if (op_pos == null) verify_3bars_and_trade();
}
protected override void OnPositionOpened(Position pos)
{
double SL_pr =0;
double TP_pr =0;
op_pos=pos;
if(pos.TradeType==TradeType.Sell)
{
SL_pr = pos.EntryPrice + SL * Symbol.PipSize;
TP_pr = pos.EntryPrice - TP * Symbol.PipSize;
}
if(pos.TradeType==TradeType.Buy)
{
SL_pr = pos.EntryPrice - SL * Symbol.PipSize;
TP_pr = pos.EntryPrice + TP * Symbol.PipSize;
}
Trade.ModifyPosition(pos, SL_pr, TP_pr);
Print("Position {0} modify, SL={1}, TP={2}",pos.Id,SL_pr,TP_pr);
}
protected override void OnPositionClosed(Position pos)
{
op_pos=null;
}
protected override void OnError(Error error)
{
Print("Errot={0}",error.Code);
}
protected override void OnStop()
{
Print("Robot 3BARS by VovkaSOL stopped by user");
}
}
}
How would I add a trailing stop to this algorithm?
Thanks
Replies
tradermatrix
21 Jul 2019, 15:43
I have this code updated with the new references....
like that i think it's ok
good luck
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot()]
public class TreeBarsTraDerMaTriX : Robot
{
[Parameter("3 BARS TraDerMaTriX n°", DefaultValue = "3")]
public string InstanceName { get; set; }
[Parameter("Volume", DefaultValue = 10000)]
public double volume { get; set; }
[Parameter("SL", DefaultValue = 270)]
public int SL { get; set; }
[Parameter("TP", DefaultValue = 280)]
public int TP { get; set; }
[Parameter("Use Trail", DefaultValue = true)]
public bool UseTrail { get; set; }
[Parameter("Trigger (pips)", DefaultValue = 15)]
public int TrailingStopTrigger { get; set; }
[Parameter("Trailing (pips)", DefaultValue = 6)]
public int TrailingStopStep { get; set; }
protected override void OnStart()
{
}
protected override void OnTick()
{
TRAIL();
}
protected override void OnBar()
{
play();
}
private void play()
{
int bars = MarketSeries.Close.Count - 1;
int[] b_or_m = new int[3];
Print("{0},{1},{2},{3},{4}", MarketSeries.High[bars - 1], MarketSeries.High[bars - 2], MarketSeries.High[bars - 3], MarketSeries.Close.Count, MarketSeries.High[1]);
if (MarketSeries.Close[bars - 1] > MarketSeries.Open[bars - 1])
b_or_m[0] = 1;
else
b_or_m[0] = 2;
if (MarketSeries.Close[bars - 2] > MarketSeries.Open[bars - 2])
b_or_m[1] = 1;
else
b_or_m[1] = 2;
if (MarketSeries.Close[bars - 3] > MarketSeries.Open[bars - 3])
b_or_m[2] = 1;
else
b_or_m[2] = 2;
Print("{0},{1},{2}", b_or_m[0], b_or_m[1], b_or_m[2]);
var cBotPositions = Positions.FindAll(InstanceName);
if (cBotPositions.Length >= 1)
return;
if ((b_or_m[0] == 1) && (b_or_m[1] == 1) && (b_or_m[2] == 1))
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, InstanceName, SL, TP);
}
else if ((b_or_m[0] == 2) && (b_or_m[1] == 2) && (b_or_m[2] == 2))
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, InstanceName, SL, TP);
}
}
protected override void OnError(Error error)
{
Print("Errot={0}", error.Code);
}
protected override void OnStop()
{
Print("Robot 3BARS by TraDerMaTriX stopped by user");
}
private void TRAIL()
{
if (UseTrail)
{
var sellPositions = Positions.FindAll(InstanceName, 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(InstanceName, 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);
}
}
}
}
}
}
@tradermatrix

alexsanramon
20 Jul 2019, 10:38
[Parameter("Trailing Stop Trigger (pips)", DefaultValue = 20)] public int TrailingStopTrigger { get; set; } [Parameter("Trailing Stop Step (pips)", DefaultValue = 0)] public int TrailingStopStep { get; set; } protected override void OnTick() { 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); } } }Trailing Stop should be place on tick.
@alexsanramon