ModifyPosition Take Profit.
ModifyPosition Take Profit.
03 Jun 2022, 11:04
Hello, I would like the code to modify the Take-Profit 'IN PIPS!' when something is triggered. In the below example when a new position is opened it should modify the Take-Profit and Stop-Loss to 3 pips. At the moment when this runs it closes the position and ends the program, or it makes the Stop-Loss and Take-Profit go to weird levels that I don't expect. I think I am doing something wrong.
Many Thanks.
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 ModifyStopLossTakeProfitExample : Robot
{
private String label = "ModTP";
private bool variable = false;
protected override void OnTick()
{
var position = Positions.Find(label, SymbolName, TradeType.Buy);
if (position == null)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.QuantityToVolumeInUnits(1), label, 2, 2, "Comment", false);
variable = true;
}
if (variable == true)
{
ModifyPosition(position, 3, 3);
// I have also tried:
// position.ModifyTakeProfitPips(3);
// position.ModifyStopLossPips(3);
}
}
}
}
Replies
dave_gryp
11 Jun 2022, 19:01
RE:
amusleh said:
Hi,
Try this:
using System; using cAlgo.API; using cAlgo.API.Internals; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class ModifyStopLossTakeProfitExample : Robot { private String label = "ModTP"; private bool variable = false; protected override void OnTick() { var position = Positions.Find(label, SymbolName, TradeType.Buy); if (position == null) { ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.QuantityToVolumeInUnits(1), label, 2, 2, "Comment", false); variable = true; } if (variable == true) { double stopLoss, takeProfit; if (position.TradeType == TradeType.Buy) { stopLoss = position.EntryPrice - (3 * Symbol.PipSize); takeProfit = position.EntryPrice + (3 * Symbol.PipSize); } else { stopLoss = position.EntryPrice + (3 * Symbol.PipSize); takeProfit = position.EntryPrice - (3 * Symbol.PipSize); } ModifyPosition(position, stopLoss, takeProfit); } } } }
If your position is closed immediately after modification then it means the amount of stop loss or take profit you set was very small.
Many Thanks Amusleh. That worked! I am getting used to the modify position now. The new problem that I have is I am getting this error:
08/04/2019 02:36:00.000 | → Modifying position PID2 (SL: null, TP: 1.12196) FAILED with error "TechnicalError", Position PID2
I have 2 positions open a buy and a sell. The position that has the error has been previously modified. I would just like to know if there is some more in depth reference material that I can use to trouble shoot this.
This is the Modify code that is causing my error:
spread = Symbol.Ask - Symbol.Bid;
halfSpread = (spread / 2);
if (Positions.Find(Short).Pips < 0)
{
shortPriceLine = Symbol.Ask - (Math.Abs(Positions.Find(Short).Pips) * pipsFactor /*0.0001*/);
double longShortDistance = shortPriceLine - Positions.Find(Long).EntryPrice;
double centralPriceLine = (longShortDistance / 2) + Positions.Find(Long).EntryPrice;
Positions.Find(Short).ModifyTakeProfitPrice(centralPriceLine + halfSpread);
Positions.Find(Long).ModifyStopLossPrice(centralPriceLine - halfSpread);
Many Thanks again.
@dave_gryp
amusleh
06 Jun 2022, 08:14
Hi,
Try this:
If your position is closed immediately after modification then it means the amount of stop loss or take profit you set was very small.
@amusleh