Replies

freemangreat
19 Nov 2018, 17:35

Thanks for the reply Panagiotis.
Yes, I decided to cheat this limitation by creating a wait thread for event of closing.

But this only works when changing parameters / timeframes / symbols and not CloseEvent.

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

using System.Threading;
using System.Threading.Tasks;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TestWaitClose : Indicator
    {
        [Parameter(DefaultValue = 0)]
        public int RESET { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private static AutoResetEvent InitEvent;
        private string Status;
//......................................................        
        protected override void Initialize()
        {
            if(InitEvent != null)
                InitEvent.Set();    // send signal to previous instance of the indicator
            else
                InitEvent = new AutoResetEvent(false); // first start
            
            Status ="Init";
            
            startWaitResetHandler();
        }

        private async void startWaitResetHandler()
        {
            await Task.Run(() => InitEvent.WaitOne());
            
            // OnClose indicator operations ..
            Status ="DeInit";
            Console.Beep(1000, 200);
        }
//......................................................
        public override void Calculate(int index) 
        { 
            ChartObjects.DrawText("StatusInfo", Status, StaticPosition.Left);
        }
    }
}

 


@freemangreat

freemangreat
29 Oct 2018, 00:20

The result of the indicator will be in the Log. For the best filtering of candles see how to change the function checkCandle.

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TestCandle : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        bool bFirst =true;
        
        protected override void Initialize()
        {
            // Initialize and create nested indicators
        }

        public override void Calculate(int index)
        {
        CANDLE candle;
        
            if(IsLastBar && bFirst) 
            {
            int c =5; // check last 5 bars
            index -=(c - 1);
            
                while(c-- > 0)
                {
                    candle =checkCandle(index);
                    Print("Bar: {0} - Body: {1} PinUp: {2} PinDown: {3} DiffPins: {4}", 
                            index++, 
                            candle.fBody,
                            candle.fPinUp,
                            candle.fPinDown,
                            candle.fDiffPins);
                }
                
                bFirst =false;
            }
        }
//........................................................................
        public CANDLE checkCandle(int iCandle)
        {
        CANDLE candle = new CANDLE {};
        
            candle.fBody =(MarketSeries.Close[iCandle] - MarketSeries.Open[iCandle]);
            
            // if bull candle
            if(candle.fBody > 0) 
            {
                candle.fPinUp =(MarketSeries.High[iCandle] - MarketSeries.Close[iCandle]);
                candle.fPinDown =(MarketSeries.Open[iCandle] - MarketSeries.Low[iCandle]);
            }
            // if bear or 0
            else 
            {
                candle.fPinUp =(MarketSeries.High[iCandle] - MarketSeries.Open[iCandle]);
                candle.fPinDown =(MarketSeries.Close[iCandle] - MarketSeries.Low[iCandle]);
            }
            
            candle.fDiffPins =0;
            // compare pins
            if(candle.fPinUp >= Symbol.PipSize && candle.fPinDown >= Symbol.PipSize)
                if(candle.fPinUp > candle.fPinDown)
                    candle.fDiffPins =(candle.fPinUp / candle.fPinDown);
                else
                if(candle.fPinUp < candle.fPinDown)
                    candle.fDiffPins =(-candle.fPinDown / candle.fPinUp);        
                
            candle.fDiffPins *=100;
            
        return(candle);            
        }
//------------------------------------------------------------------------
        public struct CANDLE
        {
            public double fBody;    // +/0/- ; bullish - positive, bearish - negative
            public double fPinUp;   // +/0 ; allways positive or 0
            public double fPinDown; // +/0 ; allways positive or 0
            public double fDiffPins; // +/-(%); PinUp > PinDown (+) : PinUp < PinDown (-)
        } 
    }
}

 


@freemangreat

freemangreat
26 Oct 2018, 17:20

This needs to be altered for the POST request of the HTTP.

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.IO;
using System.Net;
// https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-send-data-using-the-webrequest-class

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.Internet)]
    public class TestWebRequest : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        protected override void Initialize()
        {
            // Create a request for the URL. 		
            WebRequest request = WebRequest.Create ("https://pastebin.com/raw/q3t5DY1i");
            // If required by the server, set the credentials.
            //request.Credentials = CredentialCache.DefaultCredentials;
            
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            // Display the status.
            Print(response.StatusDescription);
            
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Print(responseFromServer);
            
            // Cleanup the streams and the response.
            reader.Close ();
            dataStream.Close ();
            response.Close ();
        }

        public override void Calculate(int index){}
    }
}

 


@freemangreat

freemangreat
24 Oct 2018, 15:47

Thanks lec,

Code - is the best language of self-expression.


@freemangreat

freemangreat
23 Oct 2018, 06:09

You can try to save the tick history from the tick chart to a file (binary or text format). And then in its indicator to restore this data from the file and use for calculations.

But I have no ready solution.
https://ctrader.com/forum/calgo-reference-samples/54


@freemangreat

freemangreat
23 Oct 2018, 00:05

Because the last 1H bar is not closed and the EMA value on it is recalculated with each tick.

The correct value will be the last on the 12th closed bar (60 min / 5 min = 12 bars).

 


@freemangreat

freemangreat
22 Oct 2018, 14:15

Unfortunately, the data of tick timeframes are not available for calculation on the main timeframe.
https://ctrader.com/api/reference/timeframe


@freemangreat

freemangreat
22 Oct 2018, 04:27

genious idea)

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class UpNDown : Indicator
    {
//        [Parameter(DefaultValue = 0.0)]
//        public double Parameter { get; set; }
        
        [Output("UpTick",Color = Colors.Green, PlotType = PlotType.Histogram)]
        public IndicatorDataSeries UpTick { get; set; }

        [Output("DownTick", Color = Colors.Red, PlotType = PlotType.Histogram)]
        public IndicatorDataSeries DownTick { get; set; }
        
        private double TotalTick;
        private double fPrevTickValue =0;
        private int iLastBar;
        
        protected override void Initialize()
        {
            // Initialize and create nested indicators
        }

        public override void Calculate(int index)
        {
            if(IsLastBar) // not work on history
            {
                if(iLastBar != index) // new bar
                {
                    UpTick[index] =0;
                    DownTick[index] =0;
                    TotalTick =0;
                    iLastBar =index;
                }
                
                double fLastTickValue =MarketSeries.Close[index];
                
                if(fPrevTickValue != 0.0) // every tick if prev tick exist
                {
                    if(fLastTickValue > fPrevTickValue)
                        UpTick[index]++;
                    else
                        DownTick[index]--;
                        
                    TotalTick =(UpTick[index] - DownTick[index]);
                    ChartObjects.DrawText("info", "Total: " + TotalTick, StaticPosition.TopLeft);
                }
                
                fPrevTickValue =fLastTickValue;
            }
        }
    }
}

 


@freemangreat

freemangreat
21 Oct 2018, 21:46

I have given a universal code for calculate profit / loss of position for any currency pair to ---> any currency (not only to the account currency).
Calculate in the account currency is certainly easier.

In your code, you do not use the side of trade (Buy/Sell).


@freemangreat

freemangreat
20 Oct 2018, 22:23

What?)) See line of code #41. And specify any price StopLoss price.

If you have difficulty in programming such elementary things, perhaps you better order or buy ready-made code.


@freemangreat

freemangreat
20 Oct 2018, 17:49

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class LossProfitToCurrency : Indicator
    {
        [Parameter(DefaultValue = 0)]   // Price for open position 
        public double PositionOpenPrice { get; set; }
        
        [Parameter(DefaultValue = 1.0)] // Open trade in Lots (Buy for example)
        public double PositionLots { get; set; }
        
        [Parameter(DefaultValue = 10)] // Stoploss in standard points (Pts) (for 4 or 2 digits after point)
        public int StoplossLevel { get; set; }
//...............................................................................
        private string sBaseCurrency;
        private string sCounterCurrency;
        // Any valid code major out currency USD EUR GBP JPY CHF AUD CAD ... etc
        private string sOutCurrency ="EUR";
        
        private double fPtsSize =0.0001; // Standard point/pip size (default 4 digits after point)
        private double fStopLossPrice;
        
        Symbol TransPair;       // trans pair for cross currencies
        int iTransPairType =0;
//...............................................................................      
        protected override void Initialize()
        {
            // Initialize and create nested indicators
            if(PositionOpenPrice <= 0.0)
                PositionOpenPrice =(Symbol.Bid);

            if(Symbol.Digits == 2 || Symbol.Digits == 3) // correct for 2 or 3 digits pairs
                fPtsSize =0.01;
            // set stoploss price
            fStopLossPrice =(PositionOpenPrice - StoplossLevel * fPtsSize); // for buy side
            
            // disassembling a pair of current chart into constituent currencies
            sBaseCurrency =Symbol.Code.Substring(0, 3);
            sCounterCurrency =Symbol.Code.Substring(3, 3);
            
            // try find transpair if need..
            if((sOutCurrency != sBaseCurrency) && (sOutCurrency != sCounterCurrency))
            {
                TransPair  =MarketData.GetSymbol(sCounterCurrency + sOutCurrency);
                
                if(TransPair != null)
                    iTransPairType =1; // fwd pair
                else
                {
                    TransPair  =MarketData.GetSymbol(sOutCurrency + sCounterCurrency);
                    
                    if(TransPair != null)
                        iTransPairType =(-1); // bwd pair
                }
            }
        //...... draw lines for Position and Stoploss prices
            ChartObjects.DrawHorizontalLine("BuyPosition", PositionOpenPrice, Colors.RoyalBlue);
            ChartObjects.DrawHorizontalLine("StopLoss", fStopLossPrice, Colors.Red);
        }
//---------------------------------------------------------------------------------------------
        public override void Calculate(int index)
        {
            if(!IsLastBar)
                return;
                
        string sOutText;
        double fLossProfit =(fStopLossPrice - PositionOpenPrice); // buy side
        double fLossProfitOutCurrency =0;
        double fCurrentPairPrice =Symbol.Bid; // actual price
        double fPositionVolume =(PositionLots * Symbol.LotSize);
//...........................................................................                 
        // Calculate Loss/Profit in currency
        // where OUT is out currency

            // fwd pair xxxOUT
            if(sCounterCurrency == sOutCurrency)
                fLossProfitOutCurrency =(fPositionVolume * fLossProfit);
            else
            // bwd pair OUTxxx
            if(sBaseCurrency == sOutCurrency)
                fLossProfitOutCurrency =(fPositionVolume * fLossProfit) / fCurrentPairPrice;
            else
            // cross pair xxxxxx
            if(iTransPairType != 0)
            {
            double fTransPairPrice =TransPair.Bid;

                // transpair is fwd pair xxxOUT
                if(iTransPairType > 0)
                    fLossProfitOutCurrency =(fPositionVolume * fLossProfit) * fTransPairPrice;
                // transpair is bwd pair OUTxxx
                else
                    fLossProfitOutCurrency =(fPositionVolume * fLossProfit) / fTransPairPrice;
            }
            else
            {
                ChartObjects.DrawText("Params", "Not valid out currency", StaticPosition.TopLeft);
                return;
            }
//...........................................................................            
            fLossProfitOutCurrency =Math.Round(fLossProfitOutCurrency, 5);
            
            sOutText ="Base currency: " + sBaseCurrency + 
                      "\nCounter currency: " + sCounterCurrency +
                      "\nLoss/Profit: " + fLossProfitOutCurrency + " " + sOutCurrency;

            ChartObjects.DrawText("Params", sOutText, StaticPosition.TopLeft);
        
        } // Calculate
    } // class
} // namespace

 


@freemangreat

freemangreat
19 Oct 2018, 15:05

Yes, ChartObjects works fine, but will there be the Chart / ChartArea interface available?
For example, to get BarsTotal, is only with Chart

and ChartArea had many methods for handling mouse events.


@freemangreat

freemangreat
19 Oct 2018, 14:25

Hi Panagiotis,

Version 3.0.65534.35441


@freemangreat

freemangreat
18 Oct 2018, 02:29

Maybe PendingOrders?


@freemangreat

freemangreat
17 Oct 2018, 15:59

Count	Property	The total number of open positions.

limit order is not an open position.

Are you sure the label is defined?

Print("Positions {0}: {1}", label, Positions.FindAll(label));

 


@freemangreat

freemangreat
16 Oct 2018, 20:25

RE:

freemangreat said:

Hi everyone.
Is there an opportunity to declare constants in cAlgo?

#define PI 3.1415926535897932384626433832795

 

In C# there is no preprocessor directive #define.

private const double PI=3.1415926535897932384626433832795;

 


@freemangreat