Draw trend lines from open positions to current close price (live deal map)

Created at 21 Dec 2021, 04:16
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
TA

taytayt4

Joined 21.12.2021

Draw trend lines from open positions to current close price (live deal map)
21 Dec 2021, 04:16


Hello all,

I am trying to draw trend lines with x1 and y1 being an open position, and I am trying to make x2 and y2 the close of the current bar.

If you want more context to the application: Sometimes I have positions open and lose them on the chart window when I am using small timeframe charts, So I am looking to make my own “Live” deal map.

I tried to look into how trader make their deal map to get insight and cant find it anywhere. Here is an idea of what I am after:

 

for each position in Cbot.positions

DrawTrendLine(Position#, position.opentime, position.price, current.time, current.price (I use the close?), Color(i could care less))

I am out Mobile posting on my phone, cuz when I am home i am scouring for code. I will try to reply what I have when i get home! 

Past Searches: I am trying to look up how ctrader makes historical deal map code with C# and i cant find. I’m hoping i can just change the code and make it a live deal map

 

I have tried referencing the Positions category, the Position category, and for all of these I match up the datetime parameters, or the double parameters, but I seem to keep running into problems.

 

I feel like with the for each command i can set up multiple lines, but I cannot seem to use the for each position.open to get the open time. Then I cannot assign first y to the position.open price, and for some reason I cant assign close to x2 and y2. Thus making a live deal map indicator basically.


@taytayt4
Replies

taytayt4
21 Dec 2021, 06:34

RE: Here we go, here is the code

taytayt4 said:

Hello all,

I am trying to draw trend lines with x1 and y1 being an open position, and I am trying to make x2 and y2 the close of the current bar.

If you want more context to the application: Sometimes I have positions open and lose them on the chart window when I am using small timeframe charts, So I am looking to make my own “Live” deal map.

I tried to look into how trader make their deal map to get insight and cant find it anywhere. Here is an idea of what I am after:

 

for each position in Cbot.positions

DrawTrendLine(Position#, position.opentime, position.price, current.time, current.price (I use the close?), Color(i could care less))

I am out Mobile posting on my phone, cuz when I am home i am scouring for code. I will try to reply what I have when i get home! 

Past Searches: I am trying to look up how ctrader makes historical deal map code with C# and i cant find. I’m hoping i can just change the code and make it a live deal map

 

I have tried referencing the Positions category, the Position category, and for all of these I match up the datetime parameters, or the double parameters, but I seem to keep running into problems.

 

I feel like with the for each command i can set up multiple lines, but I cannot seem to use the for each position.open to get the open time. Then I cannot assign first y to the position.open price, and for some reason I cant assign close to x2 and y2. Thus making a live deal map indicator basically.

So above was more of a synopsis, to dive into the code, here is what I am trying to fix:

 

using cAlgo.API;
using System;
using System.Linq;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HotkeysTool : Robot
    {

        protected override void OnStart()
        {

        }


        private void DrawLiveDealMap()
        {

            var botPositions = Positions.FindAll(Label, SymbolName);

            foreach (var position in botPositions)
            {
                Chart.DrawTrendLine("trendLine", LastResult.Position.Entrytime, LastResult.Position.EntryPrice, Bar.OpenTime, Bar.Close, Color.Aqua);
            }
        }

 
    }
}

You can tell I am on the right path, but I get errors left and right. Maybe I am misinterpreting the structure of C#... Nonetheless, I wil feverishly look until i can help my eyes draw a live trendline of an open position from WHEN IT WAS OPENED to CURRENT CLOSE PRICE (basically a live deal map).

 

Thank you


@taytayt4

amusleh
21 Dec 2021, 09:40

Hi,

Try this:

using cAlgo.API;
using System;
using System.Linq;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        protected override void OnStart()
        {
            foreach (var trade in History)
            {
                if (trade.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) continue;

                DrawDealpMap(trade);
            }

            Positions.Closed += Positions_Closed;
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            if (obj.Position.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) return;

            var trade = History.FirstOrDefault(iTrade => iTrade.PositionId == obj.Position.Id);

            if (trade != null)
            {
                DrawDealpMap(trade);
            }
        }

        private void DrawDealpMap(HistoricalTrade trade)
        {
            var color = trade.TradeType == TradeType.Buy ? Color.Green : Color.Red;

            Chart.DrawTrendLine(trade.PositionId.ToString(), trade.EntryTime, trade.EntryPrice, trade.ClosingTime, trade.ClosingPrice, color);
        }
    }
}

 


@amusleh

taytayt4
21 Dec 2021, 20:45

Brilliant Historical Deal Map!

Dear amusleh,

Thank you so much for your reply.

This was super helpful, and now I'm trying to find out which of your API to insert here. It seems some of them are not referencible (obviously, I used words to indicate what I am after here). 

  1. I have replaced Historical with the word "Live" but my only guess is I have to use Positions or Position Library. I've tried both but seem to get an error
  2. for the draw trendline, I am trying gto fetch current price.

You can start to see that as price moves, the trendline moves so that when It looks like my deals are complete I can exit my positions. Does this help?

Here is the code:

using cAlgo.API;
using System;
using System.Linq;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        protected override void OnStart()
        {
            foreach (var trade in Live)
            {
                if (trade.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false)
                    continue;

                DrawDealpMap(trade);
            }

            Positions.Opened += Positions_Opened;
        }

        private void Positions_Opened(PositionOpenedEventArgs obj)
        {
            if (obj.Position.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false)
                return;

            var trade = Live.FirstOrDefault(iTrade => iTrade.PositionId == obj.Position.Id);

            if (trade != null)
            {
                DrawDealpMap(trade);
            }
        }

        private void DrawDealpMap(Position trade)
        {
            var color = trade.TradeType == TradeType.Buy ? Color.Green : Color.Red;

            Chart.DrawTrendLine(trade.PositionId.ToString(), trade.EntryTime, trade.EntryPrice, Robot.Time, double Close{ get; }, color);
        }
    }
}

Please let me know anything I can look up/study/reference here. Or if my substitutions are unclear. You helped me figure out how to plot historical deals (same as Activating the "Deal Map" feature), now I want to replace some variables to make it a live deal map.

 

Cheers (:


@taytayt4

taytayt4
21 Dec 2021, 20:46

RE:

amusleh said:

Hi,

Try this:

using cAlgo.API;
using System;
using System.Linq;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        protected override void OnStart()
        {
            foreach (var trade in History)
            {
                if (trade.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) continue;

                DrawDealpMap(trade);
            }

            Positions.Closed += Positions_Closed;
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            if (obj.Position.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) return;

            var trade = History.FirstOrDefault(iTrade => iTrade.PositionId == obj.Position.Id);

            if (trade != null)
            {
                DrawDealpMap(trade);
            }
        }

        private void DrawDealpMap(HistoricalTrade trade)
        {
            var color = trade.TradeType == TradeType.Buy ? Color.Green : Color.Red;

            Chart.DrawTrendLine(trade.PositionId.ToString(), trade.EntryTime, trade.EntryPrice, trade.ClosingTime, trade.ClosingPrice, color);
        }
    }
}

 

(My Message is above but i dont know how u get pinged if I reply, thanks)


@taytayt4

taytayt4
21 Dec 2021, 22:05 ( Updated at: 21 Dec 2023, 09:22 )

I will replace historical data with live data best i can - next i try position or positions library. And current close price i will look for. Might have to use marketseries

Here is a picture of what I’m after. I will add position/positions library instead of historical and see what error messages i get when i get hom . here is a picture that explains some instant ideas of mine

 

amusleh said:

Hi,

Try this:

using cAlgo.API;
using System;
using System.Linq;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        protected override void OnStart()
        {
            foreach (var trade in History)
            {
                if (trade.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) continue;

                DrawDealpMap(trade);
            }

            Positions.Closed += Positions_Closed;
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            if (obj.Position.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) return;

            var trade = History.FirstOrDefault(iTrade => iTrade.PositionId == obj.Position.Id);

            if (trade != null)
            {
                DrawDealpMap(trade);
            }
        }

        private void DrawDealpMap(HistoricalTrade trade)
        {
            var color = trade.TradeType == TradeType.Buy ? Color.Green : Color.Red;

            Chart.DrawTrendLine(trade.PositionId.ToString(), trade.EntryTime, trade.EntryPrice, trade.ClosingTime, trade.ClosingPrice, color);
        }
    }
}

 

 


@taytayt4

taytayt4
21 Dec 2021, 22:05 ( Updated at: 21 Dec 2023, 09:22 )

I will replace historical data with live data best i can - next i try position or positions library. And current close price i will look for. Might have to use marketseries

Here is a picture of what I’m after. I will add position/positions library instead of historical and see what error messages i get when i get hom . here is a picture that explains some instant ideas of mine

 

amusleh said:

Hi,

Try this:

using cAlgo.API;
using System;
using System.Linq;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        protected override void OnStart()
        {
            foreach (var trade in History)
            {
                if (trade.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) continue;

                DrawDealpMap(trade);
            }

            Positions.Closed += Positions_Closed;
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            if (obj.Position.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) return;

            var trade = History.FirstOrDefault(iTrade => iTrade.PositionId == obj.Position.Id);

            if (trade != null)
            {
                DrawDealpMap(trade);
            }
        }

        private void DrawDealpMap(HistoricalTrade trade)
        {
            var color = trade.TradeType == TradeType.Buy ? Color.Green : Color.Red;

            Chart.DrawTrendLine(trade.PositionId.ToString(), trade.EntryTime, trade.EntryPrice, trade.ClosingTime, trade.ClosingPrice, color);
        }
    }
}

 

 


@taytayt4

taytayt4
21 Dec 2021, 22:05 ( Updated at: 21 Dec 2023, 09:22 )

I will replace historical data with live data best i can - next i try position or positions library. And current close price i will look for. Might have to use marketseries

Here is a picture of what I’m after. I will add position/positions library instead of historical and see what error messages i get when i get hom . here is a picture that explains some instant ideas of mine

 

amusleh said:

Hi,

Try this:

using cAlgo.API;
using System;
using System.Linq;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        protected override void OnStart()
        {
            foreach (var trade in History)
            {
                if (trade.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) continue;

                DrawDealpMap(trade);
            }

            Positions.Closed += Positions_Closed;
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            if (obj.Position.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) return;

            var trade = History.FirstOrDefault(iTrade => iTrade.PositionId == obj.Position.Id);

            if (trade != null)
            {
                DrawDealpMap(trade);
            }
        }

        private void DrawDealpMap(HistoricalTrade trade)
        {
            var color = trade.TradeType == TradeType.Buy ? Color.Green : Color.Red;

            Chart.DrawTrendLine(trade.PositionId.ToString(), trade.EntryTime, trade.EntryPrice, trade.ClosingTime, trade.ClosingPrice, color);
        }
    }
}

 

 


@taytayt4

amusleh
22 Dec 2021, 09:20

Hi,

The deal map is for trades, not for positions.

But if you want to have something like deal map for positions you can try this:

using cAlgo.API;
using System;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        protected override void OnStart()
        {
            UpdateDealMaps();

            Positions.Opened += Positions_Opened;
            Positions.Closed += Positions_Closed;
        }

        protected override void OnTick()
        {
            UpdateDealMaps();
        }

        private void Positions_Opened(PositionOpenedEventArgs obj)
        {
            if (obj.Position.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) return;

            DrawDealpMap(obj.Position);
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            if (obj.Position.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) return;

            // Remove the position deal map when it got closed

            Chart.RemoveObject(obj.Position.Id.ToString());
        }

        private void UpdateDealMaps()
        {
            foreach (var position in Positions)
            {
                if (position.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) continue;

                DrawDealpMap(position);
            }
        }

        private void DrawDealpMap(Position position)
        {
            var color = position.TradeType == TradeType.Buy ? Color.Green : Color.Red;

            Chart.DrawTrendLine(position.Id.ToString(), position.EntryTime, position.EntryPrice, Server.Time, Symbol.Bid, color);
        }
    }
}

It draws a deal map line when a position is opened and it keeps changing it based on price change until the position got closed, after that it removes the line.


@amusleh

taytayt4
23 Dec 2021, 07:58

RE:

amusleh said:

Hi,

The deal map is for trades, not for positions.

But if you want to have something like deal map for positions you can try this:

using cAlgo.API;
using System;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        protected override void OnStart()
        {
            UpdateDealMaps();

            Positions.Opened += Positions_Opened;
            Positions.Closed += Positions_Closed;
        }

        protected override void OnTick()
        {
            UpdateDealMaps();
        }

        private void Positions_Opened(PositionOpenedEventArgs obj)
        {
            if (obj.Position.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) return;

            DrawDealpMap(obj.Position);
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            if (obj.Position.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) return;

            // Remove the position deal map when it got closed

            Chart.RemoveObject(obj.Position.Id.ToString());
        }

        private void UpdateDealMaps()
        {
            foreach (var position in Positions)
            {
                if (position.SymbolName.Equals(SymbolName, StringComparison.OrdinalIgnoreCase) == false) continue;

                DrawDealpMap(position);
            }
        }

        private void DrawDealpMap(Position position)
        {
            var color = position.TradeType == TradeType.Buy ? Color.Green : Color.Red;

            Chart.DrawTrendLine(position.Id.ToString(), position.EntryTime, position.EntryPrice, Server.Time, Symbol.Bid, color);
        }
    }
}

It draws a deal map line when a position is opened and it keeps changing it based on price change until the position got closed, after that it removes the line.

Hi amusleh,

This is exactly what I was trying to learn, thank you so much. You wrote it so beautifully, I see how this will also not drag on extra cpu because of the compactness of the code. I sincerely appreciate it. I am new to this api and language, but everyone knows ctrader software is the best - it's the hardest language for me to learn. Happy Holidays and New Year. I am now going to read why this updates without needing OnTick().


@taytayt4

taytayt4
23 Dec 2021, 08:12

RE: RE:

oh wait it does use OnTick() :thumbsup:

 


@taytayt4

taytayt4
23 Dec 2021, 08:12

RE: RE:

oh wait it does use OnTick() :thumbsup:

 


@taytayt4