trailing stop
trailing stop
11 Jan 2013, 14:58
hello
to help implement a trailing stop to offer two separate robots:
1 SampleBuy trailing
2, SampleSell trailing
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot("Sample Buy Trailing Robot")]
public class SampleBuyTrailing : Robot
{
[Parameter(DefaultValue = 10000)]
public int Volume { get; set; }
[Parameter("Stop Loss (pips)", DefaultValue = 50)]
public int StopLoss { get; set; }
[Parameter("Take Profit (pips)", DefaultValue = 50)]
public int TakeProfit { get; set; }
[Parameter("Trigger (pips)", DefaultValue = 20)]
public int Trigger { get; set; }
[Parameter("Trailing Stop (pips)", DefaultValue = 10)]
public int TrailingStop { get; set; }
private Position position;
protected override void OnStart()
{
Trade.CreateBuyMarketOrder(Symbol, Volume);
}
protected override void OnPositionOpened(Position openedPosition)
{
position = openedPosition;
double? stopLossPrice = null;
double? takeProfitSize = null;
if (StopLoss != 0)
stopLossPrice = position.EntryPrice - StopLoss * Symbol.PipSize;
if (TakeProfit != 0)
takeProfitSize = position.EntryPrice + TakeProfit * Symbol.PipSize;
Trade.ModifyPosition(openedPosition, stopLossPrice, takeProfitSize);
}
protected override void OnTick()
{
if (position == null) return;
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);
}
}
}
protected override void OnPositionClosed(Position closedPosition)
{
Stop();
}
}
}
///////////////////////////////////////////////////////////////////
SampleSell trailling
protected override void OnPositionOpened(Position openedPosition)
{
position = openedPosition;
double? stopLossPrice = null;
double? takeProfitSize = null;
if (StopLoss != 0)
stopLossPrice = position.EntryPrice + StopLoss * Symbol.PipSize;
if (TakeProfit != 0)
takeProfitSize = position.EntryPrice - TakeProfit * Symbol.PipSize;
Trade.ModifyPosition(openedPosition, stopLossPrice, takeProfitSize);
}
protected override void OnTick()
{
if (position == null) return;
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);
}
}
}
how to combine the two solutions?
because I have a problem with my robot works only trailing Buy
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot("Sample Buy Trailing Robot")]
public class SampleBuyTrailing : Robot
{
[Parameter(DefaultValue = 10000)]
public int Volume { get; set; }
[Parameter("Stop Loss (pips)", DefaultValue = 50)]
public int StopLoss { get; set; }
[Parameter("Take Profit (pips)", DefaultValue = 50)]
public int TakeProfit { get; set; }
[Parameter("Trigger (pips)", DefaultValue = 20)]
public int Trigger { get; set; }
[Parameter("Trailing Stop (pips)", DefaultValue = 10)]
public int TrailingStop { get; set; }
private Position position;
protected override void OnStart()
{
Trade.CreateBuyMarketOrder(Symbol, Volume);
Trade.CreateSellMarketOrder(Symbol,Volume);
}
protected override void OnPositionOpened(Position openedPosition)
.......................
thank you very much
cordially
Replies
tradermatrix
11 Jan 2013, 18:53
thank you
but it does not work
the robot refuse:
{Sell trailing stop block}
else
{Buy trailing stop block
but it is possible that I developed bad the rest.
cordially
@tradermatrix
phamvanthanh
23 Jan 2013, 06:02
RE:
Please post your code so that we can identify the problem for you.
Hi,
is your problem fixed. Now I am having same problem. It trails only shortposition My trailing code as below.
private void TrailingStop(int _Strigger)
{
if (position == null) return;
if ( position.TradeType == TradeType.Sell)
{
var distance = position.EntryPrice - Symbol.Ask;
if (distance > Strigger* Symbol.PipSize)
{
double newStopLossPrice = Symbol.Ask + Trailing * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
{
Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
else
{
var distance = Symbol.Bid - position.EntryPrice;
if (distance > Strigger*Symbol.PipSize)
{
double newStopLossPrice = Symbol.Bid - Trailing * Symbol.PipSize;
if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
{
Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
}
@phamvanthanh
admin
23 Jan 2013, 11:07
It looks like the problem may be in the rest of the code.
If you have two positions or more open simultaneously then you need to use a list to store multiple positions. See list sample. Using one global field position will work only with one position open at a time.
Alternatively if your code has only two positions opened at a time, you may use two global fields one for sell and one for buy. Then you will need to check the TradeType in the OnPositionOpened and OnPositionClosed methods to assign the correct position object (from the parameter) to the corresponding global position field.
protected override void OnPositionOpened(Position openedPosition) { if (openedPosition.TradeType == TradeType.Buy) _buyPosition = openedPosition; else _sellPosition = openedPosition; } protected override void OnPositionClosed(Position closedPosition) { if (closedPosition.TradeType == TradeType.Buy) _buyPosition = null; else _sellPosition = null; }
You would also have to adjust the rest of the code to replace the one field position using the same logic.
Let us know if you require additional help.
@admin
tradermatrix
23 Jan 2013, 19:24
RE:
It looks like the problem may be in the rest of the code.
If you have two positions or more open simultaneously then you need to use a list to store multiple positions. See list sample. Using one global field position will work only with one position open at a time.
Alternatively if your code has only two positions opened at a time, you may use two global fields one for sell and one for buy. Then you will need to check the TradeType in the OnPositionOpened and OnPositionClosed methods to assign the correct position object (from the parameter) to the corresponding global position field.
protected override void OnPositionOpened(Position openedPosition) { if (openedPosition.TradeType == TradeType.Buy) _buyPosition = openedPosition; else _sellPosition = openedPosition; } protected override void OnPositionClosed(Position closedPosition) { if (closedPosition.TradeType == TradeType.Buy) _buyPosition = null; else _sellPosition = null; }You would also have to adjust the rest of the code to replace the one field position using the same logic.
Let us know if you require additional help.
merci phamvanthanh et admin
I managed to create a good trailing stop on my robots.
guiding me with:
/forum/calgo-reference-samples/56
and also this very simple example of use (add robots):
/algos/show/134
thank you for your help ...
cordially.
@tradermatrix
phamvanthanh
11 Jan 2013, 17:42
RE:
I think you should try below:
if (position == null) return;
if (position != null && position.TradeType == TradeType.Sell)
{ Sell trailing stop block}
else
{ Buy trailing stop block}
@phamvanthanh