Topics
Replies
riskontradealgo
12 Jul 2024, 07:39
( Updated at: 12 Jul 2024, 09:53 )
RE: RE: RE: RE: Crash on MacOs
Hey there Panagiotis Charalampous , Code is here.
using cAlgo.API;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class PositionListerBot : Robot
{
private ComboBox posComboBox;
private Button _closeButton;
private int _buyCount;
private int _sellCount;
private int _posCount;
protected override void OnStart()
{
// Create and configure the ComboBox
InitializeComboBox();
// Create and configure the Button
_closeButton = new Button
{
Text = "Close Selected",
Top = 40,
Left = 10,
Width = 200,
Height = 30,
};
_closeButton.Click += OnCloseButtonClick;
var _stackPanel = new StackPanel()
{
Width = 200,
Orientation = Orientation.Vertical,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top
};
_stackPanel.AddChild(posComboBox);
_stackPanel.AddChild(_closeButton);
Chart.AddControl(_stackPanel);
// Subscribe to position events
Positions.Opened += OnPositionOpened;
Positions.Closed += OnPositionClosed;
}
private void OnPositionOpened(PositionOpenedEventArgs obj)
{
string itemText = $"{obj.Position.Id}";
posComboBox.RemoveItem(itemText);
UpdateComboBox();
}
private void OnPositionClosed(PositionClosedEventArgs obj)
{
string itemText = $"{obj.Position.Id}";
posComboBox.RemoveItem(itemText);
UpdateComboBox();
}
private void InitializeComboBox()
{
posComboBox = new ComboBox
{
Top = 10,
Left = 10,
Width = 200,
Height = 20,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
BackgroundColor = Color.Transparent
};
posComboBox.SelectedItemChanged += PosComboBox_SelectedItemChanged;
UpdateComboBox();
}
private void UpdateComboBox()
{
string[] itemsToRemove = { "ALL", "NO TRADE", "LONG TRADES", "SHORT TRADES" };
foreach (var item in itemsToRemove)
{
posComboBox.RemoveItem(item);
}
foreach (var position in Positions)
{
if (position.SymbolName == Symbol.Name)
{
string itemText = $"{position.Id}";
posComboBox.RemoveItem(itemText);
}
}
_buyCount = 0;
_sellCount = 0;
_posCount = 0;
foreach (var position in Positions)
{
if (position.SymbolName == Symbol.Name)
{
string itemText = $"{position.Id}";
_posCount++;
}
}
if (_posCount == 0)
{
posComboBox.AddItem("NO TRADE");
posComboBox.SelectedItem = "NO TRADE";
}
else
{
posComboBox.AddItem("ALL");
posComboBox.SelectedItem = "ALL";
}
foreach (var position in Positions)
{
if (position.SymbolName == Symbol.Name)
{
if (position.TradeType == TradeType.Buy) _buyCount++; else _sellCount++;
string itemText = $"{position.Id}";
posComboBox.AddItem(itemText);
}
}
if (_buyCount > 0)
{
posComboBox.AddItem("LONG TRADES");
}
if (_sellCount > 0)
{
posComboBox.AddItem("SHORT TRADES");
}
}
private void OnCloseButtonClick(ButtonClickEventArgs obj)
{
string selectedItem = posComboBox.SelectedItem;
if(string.IsNullOrWhiteSpace(selectedItem)) return;
if (selectedItem == "ALL")
{
// Close all positions
foreach (var position in Positions)
{
ClosePositionAsync(position);
}
}
else if (selectedItem == "LONG TRADES")
{
// Print("CLOSE LONG TRADES");
foreach (var position in Positions)
{
if (position.TradeType == TradeType.Buy && position.SymbolName == Symbol.Name)
ClosePositionAsync(position);
}
}
else if (selectedItem == "SHORT TRADES")
{
// Print("CLOSE SHORT TRADES");
foreach (var position in Positions)
{
if (position.TradeType == TradeType.Sell && position.SymbolName == Symbol.Name)
ClosePositionAsync(position);
}
}
else if (selectedItem != "NO TRADE" && selectedItem != "SHORT TRADES" && selectedItem != "LONG TRADES" && selectedItem != "ALL")
{
// Extract the position Id from the selected item text
var positionId = selectedItem;
foreach (var position in Positions)
{
if (position.Id.ToString() == positionId)
{
ClosePositionAsync(position);
}
}
Positions.Closed += OnPositionClosed;
}
}
private void PosComboBox_SelectedItemChanged(ComboBoxSelectedItemChangedEventArgs obj)
{
Print("Selected Index : " + posComboBox.SelectedIndex);
Print("Selected Item : " + posComboBox.SelectedItem);
}
protected override void OnStop()
{
// Unsubscribe from position events
Positions.Opened -= OnPositionOpened;
Positions.Closed -= OnPositionClosed;
// Unsubscribe from button click event
_closeButton.Click -= OnCloseButtonClick;
}
}
}
@riskontradealgo
riskontradealgo
11 Jul 2024, 15:20
( Updated at: 12 Jul 2024, 07:19 )
RE: RE: Crash on MacOs
After MacOs 5.0.1 Update, crashing issue is solved.
But I am facing some other problems right now.
As you can see from 2 videos below, same code, on same pair but getting different outcomes when I print results of combobox.SelectedIndex and combobox.SelectedItem
Windows
MacOs
@riskontradealgo
riskontradealgo
11 Jul 2024, 11:40
( Updated at: 11 Jul 2024, 14:29 )
RE: Crash on MacOs
PanagiotisCharalampous said:
Hi there,
Please share your tool's code so that we can reproduce.
Best regards,
Panagiotis
This problem happens because of the combobox, when I comment what is in the function, bot does not crash.
private void UpdateComboBox()
{
string[] itemsToRemove = { "ALL", "NO TRADE", "LONG TRADES", "SHORT TRADES" };
foreach (var item in itemsToRemove)
{
posComboBox.RemoveItem(item);
}
foreach (var position in Positions)
{
if (position.SymbolName == Symbol.Name)
{
string itemText = $"{position.Id}";
posComboBox.RemoveItem(itemText);
}
}
_buyCount = 0;
_sellCount = 0;
_posCount = 0;
foreach (var position in Positions)
{
if (position.SymbolName == Symbol.Name)
{
string itemText = $"{position.Id}";
_posCount++;
}
}
if (_posCount == 0)
{
posComboBox.AddItem("NO TRADE");
posComboBox.SelectedItem = "NO TRADE";
}
else
{
posComboBox.AddItem("ALL");
posComboBox.SelectedItem = "ALL";
}
foreach (var position in Positions)
{
if (position.SymbolName == Symbol.Name)
{
if (position.TradeType == TradeType.Buy) _buyCount++; else _sellCount++;
string itemText = $"{position.Id}";
posComboBox.AddItem(itemText);
}
}
if (_buyCount > 0)
{
posComboBox.AddItem("LONG TRADES");
}
if (_sellCount > 0)
{
posComboBox.AddItem("SHORT TRADES");
}
}
@riskontradealgo
riskontradealgo
12 Jul 2024, 12:54
RE: RE: RE: RE: RE: Crash on MacOs
Have you guys checked?
@riskontradealgo