How do I access ALL the positions I have for a symbol while it's open?
How do I access ALL the positions I have for a symbol while it's open?
17 Apr 2020, 04:32
Hi everyone:
In the screen capture, I had one position of EURAUD, but the position was modified 6 times before it was closed.
I want the information (entry/exit time and price) of each modification of the position while it was opened.
How do I get all this information?
Eg, entry price of 1k at 9:12am; entry price of another 1k at 9:13am; another entry price of 1k at 9:13am again; first 1k entry price at 9:15am; second 1k entry at 9:15am bar; third 1k entry at 9:15am bar.
Positions.Where(x => x.Label == String.Empty && x.SymbolName == _s.Name).OrderByDescending(x => x.EntryTime).First().EntryPrice
//and
Positions.Where(x => x.Label == String.Empty && x.SymbolName == _s.Name).OrderByDescending(x => x.EntryTime).First().EntryTime
Only gives me the overall amalgamated information, not each modification. I was hoping it would give me the latest position modification information.
Similarly:
HistoricalTrade ht = History.FindLast(String.Empty, Symbol.Name)
only provides the overall information, not each modification.
@Spotware / @Panagiotis?
Thank you.
Replies
firemyst
21 Apr 2020, 11:23
RE:
PanagiotisCharalampous said:
Hi firemyst,
Deal information is available through History. Each entry represents a deal.
Best Regards,
Panagiotis
@Panagiotis:
See my original post. I tried that:
Similarly:
HistoricalTrade ht = History.FindLast(String.Empty, Symbol.Name)
only provides the overall information, not each modification.
That only brings back the overall trade, it doesn't appear to bring back every entry price/time along the way. The labels on my trades are empty.
Then I tried:
HistoricalTrade[] ht = History.FindAll(String.Empty, _s.Name, _p.TradeType);
but that doesn't seem to supply historical information on a trade until the overall position closes.
I want all the deal/trade information while the positions are still opened.
Am I missing something?
Thank you.
@firemyst
PanagiotisCharalampous
21 Apr 2020, 11:46
Hi firemyst,
Indeed History provides the information of closed deals only and you need to filter by position id. Deals associated with open positions are not available in History.
Best Regards,
Panagiotis
@PanagiotisCharalampous
firemyst
21 Apr 2020, 12:01
RE:
PanagiotisCharalampous said:
Hi firemyst,
Indeed History provides the information of closed deals only and you need to filter by position id. Deals associated with open positions are not available in History.
Best Regards,
Panagiotis
Great! Good to know.
So my question is how am I able to get all the "Deal" information while the position is open?
I would like to draw lines on the chart and do other calculations based on those with my bots.
Thank you,
@firemyst
PanagiotisCharalampous
21 Apr 2020, 12:19
Hi firemyst,
I was wrong above. You can actually get the deal information even for open positions. See below an example on how to get all deals of an open position on modification.
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.CentralAsiaStandardTime, AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
protected override void OnStart()
{
Positions.Modified += Positions_Modified;
}
private void Positions_Modified(PositionModifiedEventArgs obj)
{
foreach (var trade in History.Where(x => x.PositionId == obj.Position.Id))
{
Print("Entry price: " + trade.EntryPrice);
Print("Closing price: " + trade.ClosingPrice);
}
}
protected override void OnTick()
{
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
Best Regards,
Panagiotis
@PanagiotisCharalampous
firemyst
21 Apr 2020, 12:29
RE:
PanagiotisCharalampous said:
Hi firemyst,
I was wrong above. You can actually get the deal information even for open positions. See below an example on how to get all deals of an open position on modification.
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.CentralAsiaStandardTime, AccessRights = AccessRights.None)] public class NewcBot : Robot { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } protected override void OnStart() { Positions.Modified += Positions_Modified; } private void Positions_Modified(PositionModifiedEventArgs obj) { foreach (var trade in History.Where(x => x.PositionId == obj.Position.Id)) { Print("Entry price: " + trade.EntryPrice); Print("Closing price: " + trade.ClosingPrice); } } protected override void OnTick() { } protected override void OnStop() { // Put your deinitialization logic here } } }
Best Regards,
Panagiotis
Oh that's awesome! Thank you @Panagiotis!
Thanks for the code sample!
I'll give it a try and see what I can do. Assume no news is good news. :-)
@firemyst
firemyst
21 Apr 2020, 14:00
RE:
PanagiotisCharalampous said:
Hi firemyst,
I was wrong above. You can actually get the deal information even for open positions. See below an example on how to get all deals of an open position on modification.
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.CentralAsiaStandardTime, AccessRights = AccessRights.None)] public class NewcBot : Robot { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } protected override void OnStart() { Positions.Modified += Positions_Modified; } private void Positions_Modified(PositionModifiedEventArgs obj) { foreach (var trade in History.Where(x => x.PositionId == obj.Position.Id)) { Print("Entry price: " + trade.EntryPrice); Print("Closing price: " + trade.ClosingPrice); } } protected override void OnTick() { } protected override void OnStop() { // Put your deinitialization logic here } } }
Best Regards,
Panagiotis
Bad news @Panagiotis, this doesn't appear to work.
Here's my code:
private void Positions_Modified(PositionModifiedEventArgs args)
{
Print("args.Position.SymbolName " + args.Position.SymbolName);
Print("_p.SymbolName " + _p.SymbolName);
Print("args.Position.Id " + args.Position.Id);
Print("_p.Id " + _p.Id);
foreach (var trade in History.Where(x => x.PositionId == args.Position.Id))
{
Print("Entry price: " + trade.EntryPrice);
Print("Closing price: " + trade.ClosingPrice);
Print("Entry time: " + trade.EntryTime);
Print("Modified time: " + trade.ClosingTime);
Print("Volume: " + trade.VolumeInUnits);
Print("--------------------------------");
}
}
And the logged output in descending order:
Notice it doesn't print anything within the foreach loop even though the Position ID's match as shown:
21/04/2020 18:55:01.971 | _p.Id 40057512
21/04/2020 18:55:01.971 | args.Position.Id 40057512
21/04/2020 18:55:01.971 | _p.SymbolName AUDUSD
21/04/2020 18:55:01.971 | args.Position.SymbolName AUDUSD
When I put a breakpoint on the opening curly brace of the method within Visual Studio, it skips over the foreach loop.
So again it's not going to work because the foreach loop is querying the History object, which won't have the "deals" at least until the overall position is closed.
Any other ideas on how to get the "Deal Information" while the position is still open?
Thank you.
@firemyst
PanagiotisCharalampous
21 Apr 2020, 14:59
Hi firemyst,
It seems to work fine for me. This is what I get when I close partially a position
Please note that the History contains only closing deals. Hence if for example you are adding volume to a position, hence creating an opening deal. this will not be recorded. It will be recorded only when the deal closes.
Best Regards,
Panagiotis
@PanagiotisCharalampous
firemyst
21 Apr 2020, 16:30
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi firemyst,
It seems to work fine for me. This is what I get when I close partially a position
Please note that the History contains only closing deals. Hence if for example you are adding volume to a position, hence creating an opening deal. this will not be recorded. It will be recorded only when the deal closes.
Best Regards,
Panagiotis
Hi @Panagiotis:
The opening details of the deal is exactly what I want -- the entry price and time where I've increased a position size.
Why is there nothing in the API to give us access to those? Are there any plans to include that in future releases of the API?
Thank you.
@firemyst
PanagiotisCharalampous
21 Apr 2020, 16:43
Hi firemyst,
As far as I can remember, nobody ever requested this information. No, we do not have plans to add this at the moment.
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
21 Apr 2020, 09:20
Hi firemyst,
Deal information is available through History. Each entry represents a deal.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous