Determining the maximum gain of a position since running the bot
Determining the maximum gain of a position since running the bot
30 Jul 2019, 16:27
Hi,
I am trying to determine the maximum pips gained by the positions in the current chart. I am able to read the contents of a position into a list and an array. But, I cant figure out how to compare the values. When I try to print the number of pips gained, it shows the current pips. Any suggestions on how to store the values so that I can compare with the current pip gained after every tick?
Thank you.
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; using System.Collections.Generic; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class Positions_Max_Gain : Robot { public string stringPositionList; public List<Position> positionList = new List<Position>(); Position[] array; protected override void OnStart() { Print("Starting bot to record position size..."); foreach (var position in Positions) { if (position.SymbolName == Chart.SymbolName) { positionList.Add(position); Print("Position added to list: " + position.Id); } } Print("PositionList count: " + positionList.Count); array = positionList.ToArray(); } protected override void OnTick() { // To do: add code to store max gain in Pips for each position since running this bot foreach (Position e in positionList) { Print(e.Id + " has " + e.Pips + " pips"); } Print(array.Length); Print(array[0]); foreach (var position in Positions) { // comparison logic goes here } } protected override void OnStop() { } } }
Replies
therealnakedtrader
30 Jul 2019, 16:42
RE:
Panagiotis Charalampous said:
Hi therealnakedtrader,
Try the example below
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.UTC, AccessRights = AccessRights.None)] public class NewcBot : Robot { double _maxPips; protected override void OnStart() { // Put your initialization logic here } protected override void OnTick() { if (Positions.Count > 0) { _maxPips = Math.Max(_maxPips, Positions.Max(x => x.Pips)); } Print("Max Pips: " + _maxPips); } protected override void OnStop() { // Put your deinitialization logic here } } }Best Regards,
Panagiotis
Thanks, Panagiotis. This seems to work for all positions as a whole. I am trying to determine the max pips gained by each position. But, I think I can find a way to make this work now. Thanks again.
@therealnakedtrader
PanagiotisCharalampous
30 Jul 2019, 16:57
Hi therealnakedtrader,
Then you can consider this
using System; using System.Collections.Generic; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewcBot : Robot { Dictionary<Position, double> _positions; protected override void OnStart() { _positions = new Dictionary<Position, double>(); Positions.Opened += Positions_Opened; foreach (var position in Positions) _positions.Add(position, 0); } private void Positions_Opened(PositionOpenedEventArgs obj) { _positions.Add(obj.Position, 0); } protected override void OnTick() { foreach (var position in Positions) { _positions[position] = Math.Max(_positions[position], position.Pips); } } protected override void OnStop() { // Put your deinitialization logic here } } }
Best Regards,
Panagiotis
@PanagiotisCharalampous
therealnakedtrader
30 Jul 2019, 17:08
RE:
Panagiotis Charalampous said:
Hi therealnakedtrader,
Then you can consider this
using System; using System.Collections.Generic; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewcBot : Robot { Dictionary<Position, double> _positions; protected override void OnStart() { _positions = new Dictionary<Position, double>(); Positions.Opened += Positions_Opened; foreach (var position in Positions) _positions.Add(position, 0); } private void Positions_Opened(PositionOpenedEventArgs obj) { _positions.Add(obj.Position, 0); } protected override void OnTick() { foreach (var position in Positions) { _positions[position] = Math.Max(_positions[position], position.Pips); } } protected override void OnStop() { // Put your deinitialization logic here } } }Best Regards,
Panagiotis
Yep! This one worked just right. Thanks heaps, Panagiotis.
@therealnakedtrader
PanagiotisCharalampous
30 Jul 2019, 16:34
Hi therealnakedtrader,
Try the example below
Best Regards,
Panagiotis
@PanagiotisCharalampous