How to put Stoploss and takeprofit setting on this code
How to put Stoploss and takeprofit setting on this code
10 Mar 2020, 16:12
I get some code form internet. I interesting to put stoploss and takeprofit paramiter in to this code but i can not do. please help me and so sorry for my bad English.
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class MacdBot : Robot
{
private MacdHistogram _macd;
private Position _position;
[Parameter(DefaultValue = 10000, MinValue = 0)]
public int Volume { get; set; }
[Parameter("Period", DefaultValue = 9)]
public int Period { get; set; }
[Parameter("Long Cycle", DefaultValue = 26)]
public int LongCycle { get; set; }
[Parameter("Short Cycle", DefaultValue = 12)]
public int ShortCycle { get; set; }
protected override void OnStart()
{
_macd = Indicators.MacdHistogram(LongCycle, ShortCycle, Period);
}
protected override void OnBar()
{
if (Trade.IsExecuting)
return;
bool isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
bool isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;
if (_macd.Histogram.LastValue > 0.0 && _macd.Signal.IsRising() && !isLongPositionOpen)
{
ClosePosition();
Buy();
}
if (_macd.Histogram.LastValue < 0.0 && _macd.Signal.IsFalling() && !isShortPositionOpen)
{
ClosePosition();
Sell();
}
}
private void ClosePosition()
{
if (_position != null)
{
Trade.Close(_position);
_position = null;
}
}
private void Buy()
{
Trade.CreateBuyMarketOrder(Symbol, Volume);
}
private void Sell()
{
Trade.CreateSellMarketOrder(Symbol, Volume);
}
protected override void OnPositionOpened(Position openedPosition)
{
_position = openedPosition;
}
}
}
Replies
maxxx2532@gmail.com
10 Mar 2020, 17:19
RE: Thank you.
PanagiotisCharalampous said:
Hi there,
See below
using cAlgo.API; using cAlgo.API.Indicators; namespace cAlgo.Robots { [Robot(AccessRights = AccessRights.None)] public class MacdBot : Robot { private MacdHistogram _macd; private Position _position; [Parameter(DefaultValue = 10000, MinValue = 0)] public int Volume { get; set; } [Parameter("Period", DefaultValue = 9)] public int Period { get; set; } [Parameter("Long Cycle", DefaultValue = 26)] public int LongCycle { get; set; } [Parameter("Short Cycle", DefaultValue = 12)] public int ShortCycle { get; set; } [Parameter("Stop Loss", DefaultValue = 10)] public double StopLoss { get; set; } [Parameter("Stop Loss", DefaultValue = 10)] public double TakeProfit { get; set; } protected override void OnStart() { _macd = Indicators.MacdHistogram(LongCycle, ShortCycle, Period); Positions.Opened += OnPositionsOpened; } void OnPositionsOpened(PositionOpenedEventArgs obj) { _position = obj.Position; } protected override void OnBar() { bool isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy; bool isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell; if (_macd.Histogram.LastValue > 0.0 && _macd.Signal.IsRising() && !isLongPositionOpen) { ClosePosition(); Buy(); } if (_macd.Histogram.LastValue < 0.0 && _macd.Signal.IsFalling() && !isShortPositionOpen) { ClosePosition(); Sell(); } } private void ClosePosition() { if (_position != null) { _position.Close(); _position = null; } } private void Buy() { ExecuteMarketOrder(TradeType.Buy, Symbol.Name, Volume, "", StopLoss, TakeProfit); } private void Sell() { ExecuteMarketOrder(TradeType.Sell, Symbol.Name, Volume, "", StopLoss, TakeProfit); } } }
Best Regards,
Panagiotis
@maxxx2532@gmail.com
PanagiotisCharalampous
10 Mar 2020, 16:18
Hi there,
See below
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous