Topics
Replies
7023423
15 Aug 2013, 02:21
RE:
cAlgo_Fanatic said:
Could you give an example of what you need?
Hi cAlgo Fanatic,
I want to close all my multiple open positions all together at the same time when my total unrealized P&L meets my profit or stop loss targets.
For example, when I open or have 100k EUR/USD buy, 100k GBP/USD buy, and 100K USD/JPY sell now, I want to close all of my open positions when unrealized P&L from all three positions become either $300 or -$300.
Below is my coding, but below coding is only for the profit target and also when I setup for $300 for example, my net profit after the closing becomes $300 minus one side commissions.
It will be great if you can add the function for the stop loss target and solve the commission issue.
Thank you so much for your help.
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Requests;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC)]
public class AAC : Robot
{
protected override void OnStart()
{
// Put your initialization logic here
}
protected override void OnTick()
{
var netProfit = 0.0;
foreach (var openedPosition in Account.Positions)
{
netProfit += openedPosition.NetProfit;
}
if(netProfit >= 300.0)
{
foreach (var openedPosition in Account.Positions)
{
Trade.Close(openedPosition);
}
}
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
@7023423
7023423
14 Aug 2013, 12:12
RE:
cAlgo_Fanatic said:
You need to change the code in the foreach loop to this:
netProfit += openedPosition.NetProfit + openedPosition.Commissions;
Hi cAlgo Fanatic,
Thank you for the reply and I changed the code
netProfit += openedPosition.NetProfit; --> netProfit += openedPosition.NetProfit + openedPosition.Commissions;
but then result was even worse than before. If I setup to close all positions when my unrealized P&L equal or greater than $100, my actual net profit after the closing all my positions, it becomes $100 minus my full commission(round trip commission),
Is it because commission is negative number?
It will be great if you can help me again.
Thank you.
@7023423
7023423
09 Aug 2013, 19:02
RE: RE: RE:
cAlgo_Fanatic said:
Hi cAlgo Fanatic,I saw this conversation and it works good, but due to the commission which will be applied after the close, final netProfit amount becomes less than what I set up.
So how can I solve this problem by applying the commission on the exit?
Thank you for your help in advance.
Please clarify your question. The net profit is equal to the gross profit minus the commission.
If you like to close the positions on the event that the robot stops you can add the code to the OnStop() method.
I mean for example if(netProfit >= 30.0) if I setup like this, robot close all opened position when total sum of unrealized P&L equal or greater than $30.
so I expected to have $30 net profit, but because of the commission when robot close the position, my actual net profit becomes $30 minus one side commission(closing).
I think unrealized P&L does already deduct the commission when I open the position(one side), but not including the upcoming commission from the closing the trades.
Thank you,
@7023423
7023423
09 Aug 2013, 08:41
RE:
cAlgo_Fanatic said:
Yes, given that the currency of your account is in USD. You can calculate the profit of all positions of the account as well as close all positions of the account:
var netProfit = 0.0; foreach (var openedPosition in Account.Positions) { netProfit += openedPosition.NetProfit; } if(netProfit >= 30.0) { foreach (var openedPosition in Account.Positions) { Trade.Close(openedPosition); } }
Hi cAlgo Fanatic,
I saw this conversation and it works good, but due to the commission which will be applied after the close, final netProfit amount becomes less than what I set up.
So how can I solve this problem by applying the commission on the exit?
Thank you for your help in advance.
@7023423
7023423
22 Aug 2013, 12:04 ( Updated at: 21 Dec 2023, 09:20 )
RE:
cAlgo_Fanatic said:
Hi cAlgo Support,
Thank you for the reply. This one works perfect.
Just one more question (or request), what if I want to set up different amount for the profit side and stop loss side?
I simply added one more if phrase....if (NetProfit <= -200) but didn't work.
Please help me.
@7023423