robot with two independent stop Trailling
robot with two independent stop Trailling
21 May 2013, 13:40
hello
My robot uses 2 indicators
So: two different volumes can + stoploss and take profit (separately adjustable).
the trailing stop is common and limit my field of action.
j generally uses this Trailling stop:
private void SetTrailingStop()
{
foreach (var position in Account.Positions)
{
if (position.TradeType == TradeType.Sell)
{
double distance = position.EntryPrice - Symbol.Ask;
if (distance >= Trigger * Symbol.PipSize)
{
double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
{
Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
else
{
double distance = Symbol.Bid - position.EntryPrice;
if (distance >= Trigger * Symbol.PipSize)
{
double newStopLossPrice = Symbol.Bid - TrailingStop * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
{
Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}}}}}
so I would like if possible create a Trailling stop for each volume.
the robot would start like this:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Requests;
using cAlgo.Indicators;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot("TRADERMATRIX")]
public class BREAKMACHINE : Robot
{
///////////////////////////////////////////////////////////////////////////////////
[Parameter(DefaultValue = "BREAK1")] //changer le n° lors de la duplication
public string LabelName { get; set;}
///////////////////////////////////////////////////////////////////////////////////
[Parameter(DefaultValue = 80000)]
public int Volume1 { get; set; }
[Parameter("TakeProfit1", DefaultValue = 180)]
public int TakeProfit1 { get; set; }
[Parameter("StopLoss1 (pips)", DefaultValue = 36)]
public int StopLoss1 { get; set; }
[Parameter("Trailing Stop1 (pips)", DefaultValue = 10)]
public int TrailingStop1 { get; set; }
[Parameter("Trigger1 (pips)", DefaultValue = 80)]
public int Trigger1 { get; set; }
///////////////////////////////////////////////////////////////////////////////////////
[Parameter]
public DataSeries Source { get; set; }
[Parameter("Band Height", DefaultValue = 10.0, MinValue = 0)]
public double BandHeight { get; set; }
[Parameter("Bollinger Bands Deviations", DefaultValue = 2)]
public int Deviations { get; set; }
[Parameter("Bollinger Bands Periods", DefaultValue = 20)]
public int Periods { get; set; }
[Parameter("Bollinger Bands MA Type")]
public MovingAverageType MAType { get; set; }
[Parameter("Consolidation Periods", DefaultValue = 1)]
public int ConsolidationPeriods { get; set; }
[Parameter("Volume2", DefaultValue = 50000, MinValue = 0)]
public int Volume2 { get; set; }
[Parameter("Max Volume2", DefaultValue = 1)] //maximum number of trade open
public double MaxVolume2 { get; set; }
[Parameter("StopLoss2", DefaultValue =30)]
public int StopLoss2 { get; set; }
[Parameter("TakeProfit2", DefaultValue = 40)]
public int TakeProfit2 { get; set; }
[Parameter("Trailing Stop2 (pips)", DefaultValue = 10)]
public int TrailingStop2 { get; set; }
[Parameter("Trigger2 (pips)", DefaultValue = 40)]
public int Trigger2 { get; set; }
////////////////////////////////////////////////////////////////////////////////////////
[Parameter("Protectionbalance", DefaultValue = 45000)] //the robot closes positions and stop (the balance amount)
public double ProtectionBalance { get; set; }
/////////////////////////////////////////////////////////////////////////////////////////
private BollingerBands bollingerBands;
private int consolidation;
private int lastTrendBarIndex;
private Position position;
//////////////////////////////////////////////////////////////////////////////////////////
protected override void OnStart()
{
bollingerBands = Indicators.BollingerBands(Source, Periods, Deviations, MAType);
}
protected override void OnTick()
cordialement
Replies
cAlgo_Fanatic
22 May 2013, 15:35
You can modify the code in Sample Trailing Robot and check each position's volume and modify the stop loss accordingly.
Use variables such as trigger and trailingStop which will be set to the corresponding input parameter - Trigger1/Trigger2 and TrailingStop1/TrailingStop2 according to the Volume of the position.
The code below is a modification of the Sample Trailing Stop Robot in the cAlgo platform:
foreach (var position in Account.Positions) { double trigger = position.Volume == Volume1 ? Trigger1 : Trigger2; double trailingStop = position.Volume == Volume1 ? TrailingStop1 : TrailingStop2; { if (position.TradeType == TradeType.Buy) { double distance = Symbol.Bid - position.EntryPrice; if (distance >= trigger*Symbol.PipSize) { if (!_isTrigerred) { _isTrigerred = true; Print("Trailing Stop Loss triggered..."); } double newStopLossPrice = Math.Round(Symbol.Bid - trailingStop*Symbol.PipSize, Symbol.Digits); if (position.StopLoss == null || newStopLossPrice > position.StopLoss) { Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit); } } } else { //...
Where Volume1, Volume2, Trigger1, Trigger2,TrailingStop1, and TrailingStop2 are input parameters.
@cAlgo_Fanatic
tradermatrix
26 May 2013, 15:27
hello
thank you very much
but I have not managed to install two Trailling stop.
malgres several formulas.
there is always a Trailling stop which takes precedence over the other.
My robot is presented as follows:
[Parameter(DefaultValue = 100000)]
public int Volume1 { get; set; }
[Parameter("TakeProfit1", DefaultValue = 200)]
public int TakeProfit1 { get; set; }
[Parameter("StopLoss1 (pips)", DefaultValue = 36)]
public int StopLoss1 { get; set; }
[Parameter("Trailing Stop1 (pips)", DefaultValue = 10)]
public int TrailingStop1 { get; set; }
[Parameter("Trigger1 (pips)", DefaultValue = 80)]
public int Trigger1 { get; set; }
//////////////////////////////////////////////////////////////////////////////////////////////////
[Parameter("Volume2", DefaultValue = 50000, MinValue = 0)]
public int Volume2 { get; set; }
[Parameter("StopLoss2", DefaultValue =30)]
public int StopLoss2 { get; set; }
[Parameter("TakeProfit2", DefaultValue = 180)]
public int TakeProfit2 { get; set; }
[Parameter("Trailing Stop2 (pips)", DefaultValue = 20)]
public int TrailingStop2 { get; set; }
[Parameter("Trigger2 (pips)", DefaultValue = 40)]
public int Trigger2 { get; set; }
I think that I did not understand the implementation of the second Trailling stop.
there is something that m escapes.
can you say more
cordially
{
foreach (var position in Account.Positions)
{
double trigger = position.Volume == Volume1 ? Trigger1 : Trigger2;
double trailingStop = position.Volume == Volume1 ? TrailingStop1 : TrailingStop2;
{
if (position.TradeType == TradeType.Buy)
{
double distance = Symbol.Bid - position.EntryPrice;
if (distance >= trigger*Symbol.PipSize)
{
if (!_isTrigerred)
{
_isTrigerred = true;
Print("Trailing Stop Loss triggered...");
}
double newStopLossPrice = Math.Round(Symbol.Bid - trailingStop*Symbol.PipSize, Symbol.Digits);
if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
{
Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
else
{
double Trigger = position.Volume == Volume1 ? Trigger1 : Trigger2;
double TrailingStop = position.Volume == Volume1 ? TrailingStop1 : TrailingStop2;
{
if (position.TradeType == TradeType.Buy)
{
double distance = Symbol.Ask - position.EntryPrice;
if (distance >= trigger*Symbol.PipSize)
{
if (!_isTrigerred)
{
_isTrigerred = true;
Print("Trailing Stop Loss triggered...");
}
double newStopLossPrice = Math.Round(Symbol.Ask - trailingStop*Symbol.PipSize, Symbol.Digits);
if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
{
Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}}
}}}}
}}
}
@tradermatrix
cAlgo_Fanatic
27 May 2013, 12:22
Correct, you would need to differentiate between the trigger for one and the other, therefore two different boolean variables for isTriggered.
Like:
if (distance >= trigger*Symbol.PipSize) { if (!_isTrigerred1 && trigger == Trigger1) { _isTrigerred1 = true; Print("Trailing Stop Loss triggered..."); } else if (!_isTrigerred2 && trigger == Trigger2) { _isTrigerred2 = true; Print("Trailing Stop Loss triggered..."); }
isTriggered1 and isTriggered2 would be fields (declared in the outer scope) initialized to false.
@cAlgo_Fanatic
tradermatrix
22 May 2013, 11:56
hello
is it possible to adjust two Trailling stop on the same robot?
1 for each volume.
volume1(100000)= trigger1: 80 pips / traillingstop1: 20 pips
Volume2(50000)= trigger2: 40 pips / traillingstop2: 10 pips
cordially
@tradermatrix