How to look up and compare stats of trade history in cbot

Created at 25 Mar 2022, 11:09
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!
BA

bazisfree

Joined 17.01.2022

How to look up and compare stats of trade history in cbot
25 Mar 2022, 11:09


Hi,

I am trying to find (and understand) a way to get the values of the last two completed trades. That is:

a. were they losers?

b. was one short and one long?

c. if yes, then what was the opening and closing price of each trade?

d. and, what was the opening and closing time of each trade?

The reason for this is I want to see:

1. If all of these prices are within 20 pips from lowest to highest

2. If all of these entries and exits were within 30 periods from last exit to second last entry (Ie. 2 fully completed trades)

I am trading on only one symbol and this code will be in one cbot working in one instance, in one time frame.

I've tried to find a solution that I could understand on the forum and with google but haven't, so thought I'd ask if anyone could help me with a bit of sample code please? I'm obviously new to programming but have a baseline understanding. Thanks in advance.

Oh, I might add...
I want to set a condition that adjusts the entry positions size (if the above condition exists) for 60 periods since last exit (that set this condition). I should be able to work this part out, but any help is appreciated! Thanks.

I've now written some code to add here that will help explain what I'm trying to do and my level of understanding so far...

private bool ScanForTightChannel()
        {
            bool tightChannel = false;
            int tightChannelLookBack = 30;
            int channelHeightPips = 20;

            var lastCompletedTrade = History.FindLast("5M Trade");
            //var secondLastCompletedTrade = History.FindLast("5M Trade")(1);  don't know how to do this.

            var directionLCT = lastCompletedTrade.TradeType;
            double entryLCT = lastCompletedTrade.EntryPrice;
            double exitLCT = lastCompletedTrade.ClosingPrice;
            DateTime entryTimeLCT = lastCompletedTrade.EntryTime;
            DateTime exitTimeLCT = lastCompletedTrade.ClosingTime;

            var directionSecondLCT = secondLastCompletedTrade.TradeType;
            double entrySecondLCT = secondLastCompletedTrade.EntryPrice;
            double exitSecondLCT = secondLastCompletedTrade.ClosingPrice;
            DateTime entryTimeSecondLCT = secondLastCompletedTrade.EntryTime;
            DateTime exitTimeSecondLCT = secondLastCompletedTrade.ClosingTime;

            if (directionLCT != directionSecondLCT)
            {
                double findMaxLCT = Math.Max(entryLCT, exitLCT);
                double findMaxSecondLCT = Math.Max(entrySecondLCT, exitSecondLCT);
                double findMax = Math.Max(findMaxLCT, findMaxSecondLCT);
                double findMinLCT = Math.Min(entryLCT, exitLCT);
                double findMinSecondLCT = Math.Min(entrySecondLCT, exitSecondLCT);
                double findMin = Math.Min (findMinLCT, findMinSecondLCT);
                
                if ((findMax - findMin) < channelHeightPips)
                {
                    int timeForTwoTrades;
                    // do something ? to find if time from entry of second last to exit of last was within 30 periods
                    
                    if (timeForTwoTrades < tightChannelLookBack)
                    {
                        tightChannel = true;
                    }
                }

            }

      return tightChannel;

      }


            //////////////////// the stuff below here is all stuff I got from the forum with google searches, but I don't understand how to adapt and use any of it yet.  ////////////////////////////
            foreach (HistoricalTrade trade in History)
            {
                if (trade.Label == "5M Trade")
                {
                    Print("EntryTime: {0}, ClosingTime: {1}, EntryPrice {2}, ClosingPrice {3}, Profit: {4}", trade.EntryTime, trade.ClosingTime, trade.EntryPrice, trade.ClosingPrice, trade.NetProfit);
                }
            }
            // BarOpenedEventArgs
            // BarClosedEventArgs
            //   var resultEntryPrice = History.Where(trade => trade.OrderByDescending(trade => trade.EntryTime).Take(2).Get(trade => trade.EntryPrice));
        

        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;
        }


@bazisfree
Replies

bazisfree
11 Apr 2022, 08:55

How to look up and compare stats of trade history in cbot

Why is no one answering this? I am new to forums and this was my first post.

It was a reasonable question was it not? I'm not asking or expecting someone to write a program for me, I'm just needing some example code so that I can solve some problems and improve my trading algorithm.

I do not know if this tagging works but I'll try.

@PanagiotisCharalampous 

@amusleh

Cheers.


@bazisfree

amusleh
11 Apr 2022, 11:59

Hi,

You should open Automate related thread under automate section of forum not under cTrader desktop.

Regarding your question, you can use History, it contains all your account historical trades with all of their data, example:

using System.Linq;
using cAlgo.API;

namespace NewcBot
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        protected override void OnStart()
        {
            var lastTwoTrades = History.OrderByDescending(trade => trade.ClosingTime).Take(2).ToArray();

            foreach (var trade in lastTwoTrades)
            {
                Print("{0} | {1} | {2} | {3} | {4} | {5} | {6} | {7}", trade.SymbolName, trade.TradeType, trade.VolumeInUnits, trade.EntryPrice, trade.ClosingPrice, trade.Pips, trade.NetProfit, trade.Commissions); ;
            }
        }
    }
}

 


@amusleh

bazisfree
12 Apr 2022, 10:57

RE: Thank you!

Hi Amusleh,

Thanks for that. I have used this to skip what I want and get the values for the last, second last etc. I am not finished coding all that I want but I think I should get there.

I'll make sure to post in the right place next time :)

Cheers,

Baz

 

amusleh said:

Hi,

You should open Automate related thread under automate section of forum not under cTrader desktop.

Regarding your question, you can use History, it contains all your account historical trades with all of their data, example:

using System.Linq;
using cAlgo.API;

namespace NewcBot
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        protected override void OnStart()
        {
            var lastTwoTrades = History.OrderByDescending(trade => trade.ClosingTime).Take(2).ToArray();

            foreach (var trade in lastTwoTrades)
            {
                Print("{0} | {1} | {2} | {3} | {4} | {5} | {6} | {7}", trade.SymbolName, trade.TradeType, trade.VolumeInUnits, trade.EntryPrice, trade.ClosingPrice, trade.Pips, trade.NetProfit, trade.Commissions); ;
            }
        }
    }
}

 

 


@bazisfree