Get All Open Positons - CTrader 4.2.2
Get All Open Positons - CTrader 4.2.2
23 May 2022, 00:26
Hello Ahmad,
CTrader Version 4.2.2
CBot Version .Net6 / NetFrameWork 4.7.2
I want to get all the open positions and close them. Using the example provided https://spotware.github.io/automate-api-docs/guides/trading/#close-position have changed the code not to place orders. But to manually place order.
Scenario:-
Positions are opened manually or by same cBot or different cBot on the same symbol on different timeframe.
Attached is modified code of the example. Cannot find all the open positions.
The only positions details which is provided is only by the same cBot placed orders.
Regards
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.FullAccess)]
public class NewcBot3 : Robot
{
protected override void OnStart()
{
Print("Started Bot...");
// ExecuteMarketOrder(TradeType.Buy, SymbolName, 0.01, "");
// ExecuteMarketOrder(TradeType.Buy, SymbolName, 0.03, "");
}
protected override void OnBar()
{
Print("onBar Executed on.. " + Chart.TimeFrame);
var positions = Positions.FindAll("", SymbolName);
Print("positions Length=" + positions.Length);
foreach (var position in positions)
{
Print("Position Entry Time=" + position.EntryTime);
// if (position.VolumeInUnits >= 0.02)
// {
ClosePosition(position);
// }
}
}
}
}
Replies
m4trader4
23 May 2022, 19:53
( Updated at: 21 Dec 2023, 09:22 )
RE:
Ahmad,
Findall was the statement i used earlier it was working fine. Using the above code, if there is buy and sell position only one position is getting closed. Not all the positions are getting closed.
@m4trader4
amusleh
24 May 2022, 10:12
Hi,
I just tested this on M1 chart:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.FullAccess)]
public class NewcBot3 : Robot
{
protected override void OnStart()
{
Print("Started Bot...");
var firstResult = ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin);
if (!firstResult.IsSuccessful)
{
Print("First Order not placed successfully");
}
var secondResult = ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin);
if (!secondResult.IsSuccessful)
{
Print("Second Order not placed successfully");
}
}
protected override void OnBar()
{
Print("OnBar Executed on.. " + Chart.TimeFrame);
Print("Positions Length=" + Positions.Count);
foreach (var position in Positions)
{
if (position.SymbolName != SymbolName) continue;
Print("Position Entry Time=" + position.EntryTime);
ClosePosition(position);
}
}
}
}
All positions were closed without any issue.
@amusleh
m4trader4
24 May 2022, 10:53
RE:
Ahmad,
Open 1 sell and 1 buy position manually from Gui. Start cBot check for the 1M bar completion for results. Please check gif in the telegram for the scenario.
Positions.FindAll("")
Positions.FindAll("",symbolname) all these were working and now its not working. Pasting working code
var positionsCBS = Positions.FindAll("", Symbolname);
//Print(arg2 + " " + "Yes message processed");
foreach (var psnCBS in positionsCBS)
{
// Print("BuySell Positon= " + psnCBS);
if (psnCBS.Comment == "" || psnCBS.Comment.Contains("EP:") || psnCBS.Comment.Contains("Market"))
{
ClosePositionAsync(psnCBS);
}
}
Regards
Ahmed
@m4trader4
amusleh
24 May 2022, 12:23
RE: RE:
m4trader4 said:
Ahmad,
Open 1 sell and 1 buy position manually from Gui. Start cBot check for the 1M bar completion for results. Please check gif in the telegram for the scenario.
Positions.FindAll("")
Positions.FindAll("",symbolname) all these were working and now its not working. Pasting working code
var positionsCBS = Positions.FindAll("", Symbolname); //Print(arg2 + " " + "Yes message processed"); foreach (var psnCBS in positionsCBS) { // Print("BuySell Positon= " + psnCBS); if (psnCBS.Comment == "" || psnCBS.Comment.Contains("EP:") || psnCBS.Comment.Contains("Market")) { ClosePositionAsync(psnCBS); } }
Regards
Ahmed
Hi,
Try this:
using System;
using System.Linq;
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.FullAccess)]
public class NewcBot3 : Robot
{
protected override void OnStart()
{
//Print("Started Bot...");
//var firstResult = ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin);
//if (!firstResult.IsSuccessful)
//{
// Print("First Order not placed successfully");
//}
//var secondResult = ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin);
//if (!secondResult.IsSuccessful)
//{
// Print("Second Order not placed successfully");
//}
}
protected override void OnBar()
{
var symbolPositions = Positions.Where(position => position.SymbolName.Equals(SymbolName, StringComparison.Ordinal)).ToArray();
foreach (var position in symbolPositions)
{
ClosePosition(position);
}
}
}
}
@amusleh
m4trader4
25 May 2022, 06:28
RE: RE: RE:
Dear Ahmad
The above code is working.
Could you please provide statements to use for the following as previous used statements are not working. I dont understand why?
- Find and Close ALL symbols with trade type BUY and trade type SELL positions whether opened by same cBot or any other cBot or Manually
ClosePositionAsync(positions BUYand SELL)
- Find and Close ALL symbol with trade type BUY positions whether opened by same cBot or any other cBot or Manually
ClosePositionAsync(positions BUY)
- Find and Close ALL symbol with trade type SELL positions whether opened by same cBot or any other cBot or Manually
ClosePositionAsync(positions SELL)
- Find and Close SPECIFIC symbols with trade type BUY and trade type SELL positions whether opened by same cBot or any other cBot or Manually
ClosePositionAsync(positions BUYand SELL)
- Find and Close SPECIFIC symbol with trade type BUY positions whether opened by same cBot or any other cBot or Manually
ClosePositionAsync(positions BUY)
- Find and Close SPECIFIC symbols with trade type SELL positions whether opened by same cBot or any other cBot or Manually
ClosePositionAsync(positions SELL)
- Find and Cancel ALL symbols with trade type BUY and trade type SELL pending orders whether opened by same cBot or any other cBot or Manually
CancelPendingOrdersAsync(positions BUYand SELL)
- Find and Cancel ALL symbols with trade type BUY pending orders whether opened by same cBot or any other cBot or Manually
CancelPendingOrdersAsync(positions BUY)
- Find and Cancel ALL symbols with trade type SELL positions whether opened by same cBot or any other cBot or Manually
CancelPendingOrdersAsync(positions SELL)
- Find and Cancel SPECIFIC symbol with trade type BUY and trade type SELL pending orders whether opened by same cBot or any other cBot or Manually
CancelPendingOrdersAsync(positions BUYand SELL)
- Find and Cancel SPECIFIC symbols with trade type BUY pending orders whether opened by same cBot or any other cBot or Manually
CancelPendingOrdersAsync(positions BUY)
- Find and Cancel SPECIFIC symbols with trade type SELL positions whether opened by same cBot or any other cBot or Manually
CancelPendingOrdersAsync(positions SELL)
@m4trader4
amusleh
25 May 2022, 09:00
Hi,
Try this:
using System;
using System.Linq;
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
[Parameter("Trade Type", DefaultValue = TradeType.Buy)]
public TradeType TradeType { get; set; }
[Parameter("Symbol Name", DefaultValue = "EURUSD")]
public string SelectedSymbolName { get; set; }
protected override void OnStart()
{
CloseAllPositionsAsync(TradeType, SelectedSymbolName);
CancelAllOrdersAsync(TradeType, SelectedSymbolName);
}
private void CloseAllPositionsAsync(TradeType tradeType, string symbolName, Action<TradeResult> callback = null)
{
var positionsCopy = Positions.ToArray();
foreach (var position in positionsCopy)
{
if (position.TradeType == tradeType && position.SymbolName.Equals(symbolName, StringComparison.Ordinal))
{
ClosePositionAsync(position, callback);
}
}
}
private void CancelAllOrdersAsync(TradeType tradeType, string symbolName, Action<TradeResult> callback = null)
{
var ordersCopy = PendingOrders.ToArray();
foreach (var order in ordersCopy)
{
if (order.TradeType == tradeType && order.SymbolName.Equals(symbolName, StringComparison.Ordinal))
{
CancelPendingOrderAsync(order, callback);
}
}
}
}
}
@amusleh
amusleh
23 May 2022, 09:07
Hi,
You don't have to use FindAll method of Positions, you use it only when you want to filter the positions based on a label.
Change your code to:
@amusleh