How can i make a Stop Jump to recognize all position (active position even by other bots or manuals opened positions).
How can i make a Stop Jump to recognize all position (active position even by other bots or manuals opened positions).
26 Apr 2020, 13:17
Hi, i would like to make a cbot that only has a Stop Jump. I have it already but what i dont know how to do is making the cbot to detect all position opened (by all position i mean like positions that were open by any other bot or any "active" position even if it was opened manual) so the bot can proceed to place the stop jump.
All i know how to do is to make the bot control the positions that were opened by the same bot using the label, but i dont know how to make it control all open positions in general.
I put the bot to open 2 position in back test but what i want is for the bot to be able to control any position that is active these are the codes that i have. I will really appreciate some help.
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 StopJump : Robot
{
[Parameter("Bot Name", DefaultValue = "StopJumpBot", Group = "General")]
public string label { get; set; }
[Parameter("Stop Jump Trigger", DefaultValue = 5, Group = "General")]
public int StopJumpTrigger { get; set; }
[Parameter("Stop Jump Pips", DefaultValue = 2, Group = "General")]
public int StopJumpPips { get; set; }
protected override void OnStart()
{
if (IsBacktesting)
{
ExecuteMarketOrder(TradeType.Buy, Symbol.Name, 2000, label, 30, 0);
ExecuteMarketOrder(TradeType.Sell, Symbol.Name, 2000, label, 30, 0);
}
}
protected override void OnTick()
{
SetStopJump();
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
private void SetStopJump()
{
var sellPositions = Positions.FindAll(label, SymbolName, TradeType.Sell);
foreach (Position position in sellPositions)
{
double distance = position.EntryPrice - Symbol.Ask;
if (distance < StopJumpTrigger * Symbol.PipSize)
continue;
double newStopLossPrice = position.EntryPrice - StopJumpPips * Symbol.PipSize;
if (position.StopLoss > newStopLossPrice)
{
ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
var buyPositions = Positions.FindAll(label, SymbolName, TradeType.Buy);
foreach (Position position in buyPositions)
{
double distance = Symbol.Bid - position.EntryPrice;
if (distance < StopJumpTrigger * Symbol.PipSize)
continue;
double newStopLossPrice = position.EntryPrice + StopJumpPips * Symbol.PipSize;
if (position.StopLoss < newStopLossPrice)
{
ModifyPosition(position, newStopLossPrice, position.TakeProfit);
}
}
}
}
}
Replies
yeril123@hotmail.com
27 Apr 2020, 22:58
RE:
PanagiotisCharalampous said:
Hi there,
If you need to access all positions, then you just need to stop filtering them by label. Just use the Positions collection directly.
Best Regards,
Panagiotis
How can i implement that? when i put to find the selling position for example for ""var sellPositions = Positions.FindAll(SymbolName, TradeType.Sell);"" if i dont put the label before the "SymbolName" i get an error telling me that it requires a label. thanks for helping me.
@yeril123@hotmail.com
PanagiotisCharalampous
28 Apr 2020, 08:19
Hi,
See below
var sellPositions = Positions.Where(x => x.TradeType == TradeType.Sell);
Best Regards,
Panagiotis
@PanagiotisCharalampous
yeril123@hotmail.com
28 Apr 2020, 20:06
RE:
PanagiotisCharalampous said:
Hi,
See below
var sellPositions = Positions.Where(x => x.TradeType == TradeType.Sell);
Best Regards,
Panagiotis
Thanks a lot i really appreaciate it!!
@yeril123@hotmail.com
PanagiotisCharalampous
27 Apr 2020, 09:44
Hi there,
If you need to access all positions, then you just need to stop filtering them by label. Just use the Positions collection directly.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous