HOW TO DISPLAY A VALUE ON CHART

Created at 01 Nov 2023, 02:44
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!
SA

sarvann24

Joined 25.10.2023

HOW TO DISPLAY A VALUE ON CHART
01 Nov 2023, 02:44


How to display a value on  chart, below is an example in that i want the value of “ var DIFF” displayed on chart

// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    This cBot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk.
//
//    The "Sample RSI cBot" will create a buy order when the Relative Strength Index indicator crosses the  level 30, 
//    and a Sell order when the RSI indicator crosses the level 70. The order is closed be either a Stop Loss, defined in 
//    the "Stop Loss" parameter, or by the opposite RSI crossing signal (buy orders close when RSI crosses the 70 level 
//    and sell orders are closed when RSI crosses the 30 level). 
//
//    The cBot can generate only one Buy or Sell order at any given time.
//
// -------------------------------------------------------------------------------------------------

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleRSIcBot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Periods", DefaultValue = 14)]
        public int Periods { get; set; }

        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        private RelativeStrengthIndex rsi;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
        }

        protected override void OnTick()
        {
            var DIFF = Bars.HighPrices.Last(6)-Bars.LowPrices.Last(6);
            
                if (rsi.Result.LastValue < 30)
                {
                    Close(TradeType.Sell);
                    Open(TradeType.Buy);
                }
                else if (rsi.Result.LastValue > 70)
                {
                    Close(TradeType.Buy);
                    Open(TradeType.Sell);
                }
            }
   

           private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("RSI", SymbolName, tradeType))
                ClosePosition(position);
                
          
        }

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("RSI", SymbolName, tradeType);
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
            
         

            if (position == null)
                ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "RSI",0,0);
               
        }
    }
}

 

 


@sarvann24
Replies

PanagiotisChar
01 Nov 2023, 07:03 ( Updated at: 01 Nov 2023, 07:04 )

Hi there,

You can try using the DrawText method


@PanagiotisChar

sarvann24
01 Nov 2023, 07:24 ( Updated at: 01 Nov 2023, 10:30 )

RE: HOW TO DISPLAY A VALUE ON CHART

PanagiotisChar said: 

Hi there,

You can try using the DrawText method

I don't know even basic of this coding,i am copying here and there and trying to build a cbot,

 I tried different ways with DrawText but getting error.

 So could be please tell me where exactly and what exact code has to be inserted in the above example cbot


@sarvann24

PanagiotisCharalampous
01 Nov 2023, 10:35

Here is an example

        protected override void OnTick()
        {
            var DIFF = Bars.HighPrices.Last(6)-Bars.LowPrices.Last(6);
            
            Chart.DrawText("Diff", DIFF.ToString(),  Bars.OpenTimes.Last(0), Symbol.Bid, Color.Red);
               
                if (rsi.Result.LastValue < 30)
                {
                    Close(TradeType.Sell);
                    Open(TradeType.Buy);
                }
                else if (rsi.Result.LastValue > 70)
                {
                    Close(TradeType.Buy);
                    Open(TradeType.Sell);
                }
            }

@PanagiotisCharalampous

PanagiotisCharalampous
01 Nov 2023, 10:35

Here is an example

        protected override void OnTick()
        {
            var DIFF = Bars.HighPrices.Last(6)-Bars.LowPrices.Last(6);
            
            Chart.DrawText("Diff", DIFF.ToString(),  Bars.OpenTimes.Last(0), Symbol.Bid, Color.Red);
               
                if (rsi.Result.LastValue < 30)
                {
                    Close(TradeType.Sell);
                    Open(TradeType.Buy);
                }
                else if (rsi.Result.LastValue > 70)
                {
                    Close(TradeType.Buy);
                    Open(TradeType.Sell);
                }
            }

@PanagiotisCharalampous

PanagiotisCharalampous
01 Nov 2023, 10:35

Here is an example

        protected override void OnTick()
        {
            var DIFF = Bars.HighPrices.Last(6)-Bars.LowPrices.Last(6);
            
            Chart.DrawText("Diff", DIFF.ToString(),  Bars.OpenTimes.Last(0), Symbol.Bid, Color.Red);
               
                if (rsi.Result.LastValue < 30)
                {
                    Close(TradeType.Sell);
                    Open(TradeType.Buy);
                }
                else if (rsi.Result.LastValue > 70)
                {
                    Close(TradeType.Buy);
                    Open(TradeType.Sell);
                }
            }

@PanagiotisCharalampous

sarvann24
01 Nov 2023, 10:56

RE: HOW TO DISPLAY A VALUE ON CHART

PanagiotisCharalampous said: 

Here is an example

        protected override void OnTick()        {            var DIFF = Bars.HighPrices.Last(6)-Bars.LowPrices.Last(6);                        Chart.DrawText("Diff", DIFF.ToString(),  Bars.OpenTimes.Last(0), Symbol.Bid, Color.Red);                               if (rsi.Result.LastValue < 30)                {                    Close(TradeType.Sell);                    Open(TradeType.Buy);                }                else if (rsi.Result.LastValue > 70)                {                    Close(TradeType.Buy);                    Open(TradeType.Sell);                }            }

Many thanks :)


@sarvann24

sarvann24
01 Nov 2023, 11:20

RE: HOW TO DISPLAY A VALUE ON CHART

PanagiotisCharalampous said: 

Here is an example

        protected override void OnTick()        {            var DIFF = Bars.HighPrices.Last(6)-Bars.LowPrices.Last(6);                        Chart.DrawText("Diff", DIFF.ToString(),  Bars.OpenTimes.Last(0), Symbol.Bid, Color.Red);                               if (rsi.Result.LastValue < 30)                {                    Close(TradeType.Sell);                    Open(TradeType.Buy);                }                else if (rsi.Result.LastValue > 70)                {                    Close(TradeType.Buy);                    Open(TradeType.Sell);                }            }

HI Panagiotis,

can the displayed text  be moved to top left?


@sarvann24

PanagiotisCharalampous
02 Nov 2023, 06:53

RE: RE: HOW TO DISPLAY A VALUE ON CHART

sarvann24 said: 

PanagiotisCharalampous said: 

Here is an example

        protected override void OnTick()        {            var DIFF = Bars.HighPrices.Last(6)-Bars.LowPrices.Last(6);                        Chart.DrawText("Diff", DIFF.ToString(),  Bars.OpenTimes.Last(0), Symbol.Bid, Color.Red);                               if (rsi.Result.LastValue < 30)                {                    Close(TradeType.Sell);                    Open(TradeType.Buy);                }                else if (rsi.Result.LastValue > 70)                {                    Close(TradeType.Buy);                    Open(TradeType.Sell);                }            }

HI Panagiotis,

can the displayed text  be moved to top left?

Here you go

        protected override void OnTick()
        {
            var DIFF = Bars.HighPrices.Last(6)-Bars.LowPrices.Last(6);
            
            Chart.DrawStaticText("Diff", DIFF.ToString(),  VerticalAlignment.Top, HorizontalAlignment.Left, Color.Red);
               
                if (rsi.Result.LastValue < 30)
                {
                    Close(TradeType.Sell);
                    Open(TradeType.Buy);
                }
                else if (rsi.Result.LastValue > 70)
                {
                    Close(TradeType.Buy);
                    Open(TradeType.Sell);
                }
            }

@PanagiotisCharalampous

sarvann24
02 Nov 2023, 07:09

RE: RE: RE: HOW TO DISPLAY A VALUE ON CHART

PanagiotisCharalampous said: 

sarvann24 said: 

PanagiotisCharalampous said: 

Here is an example

        protected override void OnTick()        {            var DIFF = Bars.HighPrices.Last(6)-Bars.LowPrices.Last(6);                        Chart.DrawText("Diff", DIFF.ToString(),  Bars.OpenTimes.Last(0), Symbol.Bid, Color.Red);                               if (rsi.Result.LastValue < 30)                {                    Close(TradeType.Sell);                    Open(TradeType.Buy);                }                else if (rsi.Result.LastValue > 70)                {                    Close(TradeType.Buy);                    Open(TradeType.Sell);                }            }

HI Panagiotis,

can the displayed text  be moved to top left?

Here you go

        protected override void OnTick()        {            var DIFF = Bars.HighPrices.Last(6)-Bars.LowPrices.Last(6);                        Chart.DrawStaticText("Diff", DIFF.ToString(),  VerticalAlignment.Top, HorizontalAlignment.Left, Color.Red);                               if (rsi.Result.LastValue < 30)                {                    Close(TradeType.Sell);                    Open(TradeType.Buy);                }                else if (rsi.Result.LastValue > 70)                {                    Close(TradeType.Buy);                    Open(TradeType.Sell);                }            }

Thanks


@sarvann24

sarvann24
10 Nov 2023, 06:14

RE: HOW TO DISPLAY A VALUE ON CHART

PanagiotisCharalampous said: 

Here is an example

        protected override void OnTick()        {            var DIFF = Bars.HighPrices.Last(6)-Bars.LowPrices.Last(6);                        Chart.DrawText("Diff", DIFF.ToString(),  Bars.OpenTimes.Last(0), Symbol.Bid, Color.Red);                               if (rsi.Result.LastValue < 30)                {                    Close(TradeType.Sell);                    Open(TradeType.Buy);                }                else if (rsi.Result.LastValue > 70)                {                    Close(TradeType.Buy);                    Open(TradeType.Sell);                }            }

HI, Can we set rules to make only one trade per bar ONTICK( ),

Only reversing the old postion .


@sarvann24

PanagiotisCharalampous
10 Nov 2023, 07:12

RE: RE: HOW TO DISPLAY A VALUE ON CHART

sarvann24 said: 

PanagiotisCharalampous said: 

Here is an example

        protected override void OnTick()        {            var DIFF = Bars.HighPrices.Last(6)-Bars.LowPrices.Last(6);                        Chart.DrawText("Diff", DIFF.ToString(),  Bars.OpenTimes.Last(0), Symbol.Bid, Color.Red);                               if (rsi.Result.LastValue < 30)                {                    Close(TradeType.Sell);                    Open(TradeType.Buy);                }                else if (rsi.Result.LastValue > 70)                {                    Close(TradeType.Buy);                    Open(TradeType.Sell);                }            }

HI, Can we set rules to make only one trade per bar ONTICK( ),

Only reversing the old postion .

Hi there,

Yes, use a flag after you place a trade and reset it on each bar.


@PanagiotisCharalampous

sarvann24
10 Nov 2023, 09:55

RE: RE: RE: HOW TO DISPLAY A VALUE ON CHART

PanagiotisCharalampous said: 

sarvann24 said: 

PanagiotisCharalampous said: 

Here is an example

        protected override void OnTick()        {            var DIFF = Bars.HighPrices.Last(6)-Bars.LowPrices.Last(6);                        Chart.DrawText("Diff", DIFF.ToString(),  Bars.OpenTimes.Last(0), Symbol.Bid, Color.Red);                               if (rsi.Result.LastValue < 30)                {                    Close(TradeType.Sell);                    Open(TradeType.Buy);                }                else if (rsi.Result.LastValue > 70)                {                    Close(TradeType.Buy);                    Open(TradeType.Sell);                }            }

HI, Can we set rules to make only one trade per bar ONTICK( ),

Only reversing the old postion .

Hi there,

Yes, use a flag after you place a trade and reset it on each bar.

I am a learner, dont know how to do it,

 much appreciated if you provide me the code for this RSI example


@sarvann24

PanagiotisCharalampous
10 Nov 2023, 12:13

RE: RE: RE: RE: HOW TO DISPLAY A VALUE ON CHART

sarvann24 said: 

PanagiotisCharalampous said: 

sarvann24 said: 

PanagiotisCharalampous said: 

Here is an example

        protected override void OnTick()        {            var DIFF = Bars.HighPrices.Last(6)-Bars.LowPrices.Last(6);                        Chart.DrawText("Diff", DIFF.ToString(),  Bars.OpenTimes.Last(0), Symbol.Bid, Color.Red);                               if (rsi.Result.LastValue < 30)                {                    Close(TradeType.Sell);                    Open(TradeType.Buy);                }                else if (rsi.Result.LastValue > 70)                {                    Close(TradeType.Buy);                    Open(TradeType.Sell);                }            }

HI, Can we set rules to make only one trade per bar ONTICK( ),

Only reversing the old postion .

Hi there,

Yes, use a flag after you place a trade and reset it on each bar.

I am a learner, dont know how to do it,

 much appreciated if you provide me the code for this RSI example

Hi there,

Unfortunately I cannot do custom work for free. Give it a try yourself and come back with questions. If don't know how to program better assign the job to a professional.

Best regards,


@PanagiotisCharalampous