how to add Trailing stop loss? Sample RSI cBot
how to add Trailing stop loss? Sample RSI cBot
22 Apr 2019, 22:54
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class wisdom101 : Robot
{
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("Periods", DefaultValue = 14)]
public int Periods { get; set; }
[Parameter("Stop Loss (pips)", DefaultValue = 10, MinValue = 10)]
public int StopLoss { get; set; }
[Parameter("Volume", DefaultValue = 80, MinValue = 80)]
public int Volume { get; set; }
private RelativeStrengthIndex rsi;
protected override void OnStart()
{
rsi = Indicators.RelativeStrengthIndex(Source, Periods);
}
protected override void OnTick()
{
if (rsi.Result.LastValue <= 30)
{
Close(TradeType.Sell);
Open(TradeType.Buy);
}
else if (rsi.Result.LastValue >= 70)
{
Close(TradeType.Buy);
Open(TradeType.Sell);
}
}
private void Close(TradeType tradeType)
{
foreach (var position in Positions.FindAll("BTCWISDOMV2", Symbol, tradeType))
ClosePosition(position);
}
private void Open(TradeType tradeType)
{
var position = Positions.Find("BTCWISDOMV2", Symbol, tradeType);
if (position == null)
ExecuteMarketOrder(tradeType, Symbol, Volume, "BTCWISDOMV2", StopLoss, null);
}
protected override void OnTick()
{
if (position == null)
return;
// Trailing
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);
}
}
}
}
}
}
It is giving the error below:
Error CS0111: Type 'cAlgo.Robots.wisdom101' already defines a member called 'OnTick' with the same parameter types
Replies
gkmngzrbtctrdr
23 Apr 2019, 13:15
Hi Panagiotis,
What can write there instead of OnTick() ?
Best Regards,
Jacob
@gkmngzrbtctrdr
PanagiotisCharalampous
23 Apr 2019, 14:18
Hi JAcob,
It seems that you are not familiar with programming and that you just copied and pasted code from another cBot expecting it to work. I am not sure what are you trying to do. Can you please explain better what are you trying to do and where did you find the code you pasted?
Best Regards,
Panagiotis
@PanagiotisCharalampous
btcwisdomv2
23 Apr 2019, 14:29
Hi Panagiotis,
I found the traling stop loss part from forum pages. First I found from another forum page adding stop loss to sample RSI cbot. Then I tried it and actually it seems this bot will be much better with trailing stop loss. Then search forum pages and found adding stop loss for another bot and tried to add this to sample rsi cbot. Also watch a video from clickalgo education on youtube. As I am not very familiar with programming I tried to do it by myself. Just want to add trailing stop loss to sample rsi cbot. In position opening cbot will have a stop loss then as the price changes stop loss will be tralining.
If you can help me on this issue appreciate it.
Regards,
Jacob
@btcwisdomv2
PanagiotisCharalampous
23 Apr 2019, 14:48
Hi Jacob,
If you just want to add a trailing stop loss to the Sample RSI cBot, you don't need to get into all this trouble. You just need to change the line below
ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "SampleRSI");
to
ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "SampleRSI", stopLoss, null, "", true);
Note that you will need to define the stopLoss parameter somehow either from within the code or pass it as a parameter.
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
23 Apr 2019, 12:00
Hi btcwisdomv2,
Thanks for posting in our forum. This happens because you are defining OnTick() method twice.
Best Regards,
Panagiotis
@PanagiotisCharalampous