Help Please
Help Please
10 Oct 2017, 21:18
How to access different positions by index from Positions[] (position array). Pleaswe provide some sample codes.
Replies
PanagiotisCharalampous
11 Oct 2017, 09:33
Hi anjupeguforex@gmail.com,
You can access positions in many ways. Some of them are the following
1) Using an index. The number in the brackets indicates the index of the position you want to get
var position = Positions[4];
2) Using a label. When creating positions you can assign a label and then find them using the following function
var position = Positions.Find(label);
3) Using a label on multiple positions. If you use the same label on may positions, you can get them as follows
var positions = Positions.FindAll(label);
Let me know if this is what you are looking for.
Best Regards,
Panagiotis
@PanagiotisCharalampous
anjupeguforex@gmail.com
12 Oct 2017, 06:49
Using an index making IndexOutOfRangeException Error
Using Index makeing IndexOutOfRangeException: Index was outside the bounds of the array.
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Internals; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class FXGODREWORKING : Robot { [Parameter("Position Volume", DefaultValue = 1000, MinValue = 1000, Step = 5000)] public int InitialVolume { get; set; } [Parameter("Buy ", DefaultValue = true)] public bool buy { get; set; } [Parameter("Stop Loss", DefaultValue = 40)] public int StopLoss { get; set; } [Parameter("Take Profit", DefaultValue = 150)] public int TakeProfit { get; set; } [Parameter("New Position Add", DefaultValue = 150)] public int NewAdd { get; set; } private const string mylabel = "POS-INDEX-SAMPLE"; protected override void OnStart() { Positions.Closed += OnPositionsClosed1; // Positions.Closed += OnPositionsClosed2; Positions.Opened += PositionsOnOpened1; var position = Positions.Find(mylabel, Symbol); if (position == null) { if (buy == true) ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume, mylabel, StopLoss, TakeProfit); else ExecuteMarketOrder(TradeType.Sell, Symbol, InitialVolume, mylabel, StopLoss, TakeProfit); } } //////////////////////////////////////////////////////////////////////////////////// /* I have Some Other uses so Please help me making it Correct by using Position Index, */ ///////////////////////////////////////////////////////////////////////////////////////// private void PositionsOnOpened1(PositionOpenedEventArgs args) { var position = args.Position; Position[] poz = Positions.FindAll(mylabel); foreach (var order in PendingOrders) { if (order.Label == position.Label) { if (order != null) CancelPendingOrder(order); } } if (Positions.Count == 1) { // one position exit it place One buy and One Sell Pending Order PlaceStopOrder(TradeType.Buy, Symbol, InitialVolume, position.EntryPrice + NewAdd * Symbol.PipSize, mylabel, NewAdd, TakeProfit); PlaceStopOrder(TradeType.Sell, Symbol, InitialVolume, position.EntryPrice - NewAdd * Symbol.PipSize, mylabel, NewAdd, TakeProfit); } if (poz[1] != null) { // If poz[1] or more position exist // placing pending order by looking last and 2nd last existing position if (poz[0].TradeType == TradeType.Buy && poz[1].TradeType == TradeType.Buy) { PlaceStopOrder(TradeType.Buy, Symbol, InitialVolume, position.EntryPrice + NewAdd * Symbol.PipSize, mylabel, NewAdd, TakeProfit); } else if (poz[0].TradeType == TradeType.Sell && poz[1].TradeType == TradeType.Sell) { PlaceStopOrder(TradeType.Buy, Symbol, InitialVolume, position.EntryPrice + NewAdd * Symbol.PipSize, mylabel, NewAdd, TakeProfit); } } } //////////////////////////////////////////////// private void OnPositionsClosed1(PositionClosedEventArgs args) { Print("Closed"); var position = args.Position; Position[] pos = Positions.FindAll(mylabel, Symbol); if (pos[0].GrossProfit < 0) { // If last position make loss by Sl it take a new reverse position if (pos[0].TradeType == TradeType.Buy) ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume, mylabel, NewAdd, TakeProfit); else ExecuteMarketOrder(TradeType.Sell, Symbol, InitialVolume, mylabel, NewAdd, TakeProfit); } if (pos[1] != null) { // If last position make loss modity position pos[1] Stoploss to Its Entry Price if (pos[0].TradeType != pos[1].TradeType) ModifyPosition(pos[1], pos[1].EntryPrice, pos[1].TakeProfit); } } } }
@anjupeguforex@gmail.com
anjupeguforex@gmail.com
12 Oct 2017, 07:23
Same issue Again
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Internals; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class FXGODREWORKING : Robot { [Parameter("Position Volume", DefaultValue = 1000, MinValue = 1000, Step = 5000)] public int InitialVolume { get; set; } [Parameter("Buy ", DefaultValue = true)] public bool buy { get; set; } [Parameter("Stop Loss", DefaultValue = 20)] public int StopLoss { get; set; } [Parameter("Take Profit", DefaultValue = 150)] public int TakeProfit { get; set; } [Parameter("New Position Add", DefaultValue = 20)] public int NewAdd { get; set; } private const string mylabel = "POS-INDEX-SAMPLE"; protected override void OnStart() { Positions.Closed += OnPositionsClosed1; // Positions.Closed += OnPositionsClosed2; Positions.Opened += PositionsOnOpened1; var position = Positions.Find(mylabel, Symbol); if (position == null) { if (buy == true) ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume, mylabel, StopLoss, TakeProfit); else ExecuteMarketOrder(TradeType.Sell, Symbol, InitialVolume, mylabel, StopLoss, TakeProfit); } } //////////////////////////////////////////////////////////////////////////////////// /* I have Some Other uses so Please help me making it Correct by using Position Index, */ ///////////////////////////////////////////////////////////////////////////////////////// private void PositionsOnOpened1(PositionOpenedEventArgs args) { var position = args.Position; Position[] poz = Positions.FindAll(mylabel); var poz1 = poz[0]; var poz2 = poz[1]; foreach (var order in PendingOrders) { if (order.Label == position.Label) { if (order != null) CancelPendingOrder(order); } } if (Positions.Count == 1) { // one position exit it place One buy and One Sell Pending Order PlaceStopOrder(TradeType.Buy, Symbol, InitialVolume, position.EntryPrice + NewAdd * Symbol.PipSize, mylabel, NewAdd, TakeProfit); PlaceStopOrder(TradeType.Sell,Symbol, InitialVolume, position.EntryPrice - NewAdd * Symbol.PipSize, mylabel, NewAdd, TakeProfit); } if (poz2 != null) { // If poz2 or more position exist // placing pending order by looking last and 2nd last existing position if (poz1.TradeType == TradeType.Buy && poz2.TradeType == TradeType.Buy) { PlaceStopOrder(TradeType.Buy, Symbol, InitialVolume, position.EntryPrice + NewAdd * Symbol.PipSize, mylabel, NewAdd, TakeProfit); } else if (poz1.TradeType == TradeType.Sell && poz2.TradeType == TradeType.Sell) { PlaceStopOrder(TradeType.Buy, Symbol, InitialVolume, position.EntryPrice + NewAdd * Symbol.PipSize, mylabel, NewAdd, TakeProfit); } } } //////////////////////////////////////////////// private void OnPositionsClosed1(PositionClosedEventArgs args) { Print("Closed"); var position = args.Position; Position[] pos = Positions.FindAll(mylabel, Symbol); var pos1 = pos[0]; var pos2 = pos[1]; if (pos1.GrossProfit < 0) { // If last position make loss by Sl it take a new reverse position if (pos1.TradeType == TradeType.Buy) ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume, mylabel, NewAdd, TakeProfit); else ExecuteMarketOrder(TradeType.Sell, Symbol, InitialVolume, mylabel, NewAdd, TakeProfit); } if (pos2 != null) { // // If last position make loss modity position pos2 Stoploss to Its Entry Price if (pos1.TradeType != pos2.TradeType) ModifyPosition(pos2, pos2.EntryPrice, pos2.TakeProfit); } } } }
@anjupeguforex@gmail.com
PanagiotisCharalampous
12 Oct 2017, 09:23
Hi anjupeguforex@gmail.com,
You get this exception because you are trying to access an array item that does not exist. Before accessing a position in Positions using index You should always check that the index is within the range of the array. I propose to change checks like the following
if (poz[1] != null)
to
if (poz.Length > 1)
Best Regards,
Panagiotis
@PanagiotisCharalampous
anjupeguforex@gmail.com
12 Oct 2017, 13:12
RE:
Panagiotis Charalampous said:
Hi anjupeguforex@gmail.com,
You get this exception because you are trying to access an array item that does not exist. Before accessing a position in Positions using index You should always check that the index is within the range of the array. I propose to change checks like the following
if (poz[1] != null)to
if (poz.Length > 1)Best Regards,
Panagiotis
Now all are working.Thank You very much.
@anjupeguforex@gmail.com
... Deleted by UFO ...