Replies

TakeProfit
16 Jun 2020, 16:28

RE:

PanagiotisCharalampous said:

Hi TakeProfit.

See an example below on how you can subscribe to ticks and bar changes from Bar collections coming from other symbols and timeframes

using System;
using System.Linq;
using System.Text;
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 MultisymbolBacktesting : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        public Symbol[] MySymbols;

        protected override void OnStart()
        {
            // We get a symbol array for the following for symbols
            MySymbols = Symbols.GetSymbols("EURUSD", "GBPUSD", "USDJPY", "USDCHF");
            // We subscribe to the tick event for each symbol
            foreach (var symbol in MySymbols)
            {
                symbol.Tick += Symbol_Tick;
            }
            // We subscribe to the bar event for each symbol for the current timeframe
            foreach (var symbol in MySymbols)
            {
                var bars = MarketData.GetBars(TimeFrame, symbol.Name);
                bars.BarOpened += Bars_BarOpened;
            }
        }

        private void Bars_BarOpened(BarOpenedEventArgs obj)
        {
            // When a bar opens we check the previous bar. If the bar is bullish, we send a buy order, else we send a sell order.
            if (obj.Bars.Last(1).Close > obj.Bars.Last(1).Open)
            {
                ExecuteMarketOrder(TradeType.Buy, obj.Bars.SymbolName, 1000, "", 2, 2);
            }
            else
            {
                ExecuteMarketOrder(TradeType.Sell, obj.Bars.SymbolName, 1000, "", 2, 2);
            }
        }

        private void Symbol_Tick(SymbolTickEventArgs obj)
        {
            // On each symbol tick, we print the symbol's bid/ask prices on the chart
            var sb = new StringBuilder();
            foreach (var symbol in MySymbols)
            {
                var textLine = string.Format("{0} {1} {2}", symbol.Name, symbol.Bid, symbol.Ask);
                sb.AppendLine(textLine);
            }
            Chart.DrawStaticText("symbols", sb.ToString(), VerticalAlignment.Top, HorizontalAlignment.Left, Chart.ColorSettings.ForegroundColor);
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 

Best Regards,

Panagiotis 

Join us on Telegram

 

Hi Panagiotis,

thank you for your quick reply!

 

The example helped a lot.

I just implemented and tested it and it works exactly as I wanted it to!

 

Best regards


@TakeProfit

TakeProfit
11 Feb 2019, 18:12

Hi Panagiotis,

Thank you very much, it works, I went with the following addition to also filter for Symbol.Code.

  1. var OpenPositions = Positions.Where(x => x.SymbolCode == Symbol.Code && x.Label == "Long" || x.Label == "Short");
    foreach (Position position in OpenPositions)
    {...}

@TakeProfit

TakeProfit
24 Jan 2019, 13:13 ( Updated at: 21 Dec 2023, 09:21 )

Hi Panagiotis,

The clean installation did fix the issue. 

Although I didn't removed the 2.0 folder or reinstalled the .NET Framework, it still worked. Maybe this helps finding the root of the problem for a fix in the future.

I saved all folders before reinstallation and I am looking for the settings of the instances from the Algorithms in cAlgo.

After clean installation all those instances (see red boxes in picture) with the setups I was working on were gone. 

Best Regards


@TakeProfit