 
    
            backtest Multiple Currencies
            
                 25 Mar 2024, 12:20
            
                    
Hello
I need a Help on this code “How to Backtest an indicator for Multi Currencies ” as you see this code for Getting ATR value for each Currency on the watchlist But that not working with me , Is there somebody help me on that?
Best Regards,
using System;
using System.Linq;
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 ATRcBot2 : Robot
    {
       
       [Parameter("Risk %", DefaultValue = 0.02)]
        public double RiskPct { get; set; }
        
         // Create Indicator Variables
        private AverageTrueRange atr;
       
        private Symbol[] TradeList;
        
        
        protected override void OnStart()     
        {
         
        
        
         //Loop through watchlists
            foreach(Watchlist w in Watchlists)
            if(w.Name == "JPY")
            TradeList =  Symbols.GetSymbols(w.SymbolNames.ToArray());
             
             
            
            foreach(Symbol s in TradeList)
            { 
            var bars = MarketData.GetBars(TimeFrame , s. Name);
            var atr = Indicators.AverageTrueRange(14, MovingAverageType.Exponential);
                bars.BarOpened += OnbarsBarOpened; 
             
            }
            
           void OnbarsBarOpened(BarOpenedEventArgs obj)
            {
           
            var SymbolName = obj.Bars.SymbolName;
            var prevclose = obj.Bars.ClosePrices.Last(1);
            
            var prevatr = (atr.Result.Last(1)/ Symbol.PipSize).ToString("F0"); 
             
            
            Print("SymbolName {0} ,prevclose {1},prevatr {2}", SymbolName ,prevclose,prevatr );
         
            }
            
            }
        protected override void OnBar()
        {
       
        }        
           
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}
 
Replies
                     Soliman.Mansour
                     27 Mar 2024, 01:45
                                            ( Updated at: 27 Mar 2024, 07:26 )
                                    
RE: backtest Multiple Currencies
PanagiotisCharalampous said:
Hi there,
What exactly is not working? What do you expect to happen and what happens instead?
Best regards,
Panagiotis
Hello
I expect to have the value of ATR for each Symbol on the WatchList (the values will be shown on Log ) But it gives an error as the code can't read the ATR Value for each Symbol.
Acually, I am Studying the Loop Function for a cBot or indicator (How can I let the cBot work for all Symbols on the watchlist at the same Time for backtesting purpose) and this is a sample (The ATR Indicator).
Best regards,
Soliman
@Soliman.Mansour
                     PanagiotisCharalampous
                     27 Mar 2024, 08:30
                                    
Your code has several amateur issues e.g.
            var atr = Indicators.AverageTrueRange(14, MovingAverageType.Exponential);You are initializing a global variable as a local one. Then when you try to access the global one inside OnbarsBarOpened, you get an exception. Fix your exceptions first and provide a workable cBot so that we can see the logic as well.
@PanagiotisCharalampous
                     Soliman.Mansour
                     28 Mar 2024, 03:52
                                    
RE: backtest Multiple Currencies
PanagiotisCharalampous said:
Your code has several amateur issues e.g.
var atr = Indicators.AverageTrueRange(14, MovingAverageType.Exponential);You are initializing a global variable as a local one. Then when you try to access the global one inside OnbarsBarOpened, you get an exception. Fix your exceptions first and provide a workable cBot so that we can see the logic as well.
Hello,
You're right I am amateur I'm not Programmer but I'm try to understand the logic of the code but I don't know the tools ,Thanks for your time and I appreciate your concern ,
Here's the code after I fix var Prevatr = (obj.Bars.atr.Result.Last(1)/ Symbol.PipSize).ToString("F0"); , But still can't give the ATR value for each Symbol

This is the error message

using System;
using System.Linq;
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 ATRcBot2 : Robot
    {
       
       [Parameter("Risk %", DefaultValue = 0.02)]
        public double RiskPct { get; set; }
        
          // Create Indicator Variables
        private AverageTrueRange atr;
        
       
        private Symbol[] TradeList;
       
        protected override void OnStart()     
        {
         // Load indicators on start up
            atr = Indicators.AverageTrueRange(14, MovingAverageType.Exponential);
            
         //Loop through watchlists
            foreach(Watchlist w in Watchlists)
            if(w.Name == "JPY")
            TradeList =  Symbols.GetSymbols(w.SymbolNames.ToArray());
             
                       
            foreach(Symbol s in TradeList)
            { 
            var bars = MarketData.GetBars(TimeFrame , s. Name);
            
             bars.BarOpened += OnbarsBarOpened; 
              
             }
           void OnbarsBarOpened(BarOpenedEventArgs obj)
            {
           
            var SymbolName = obj.Bars.SymbolName;
            var prevclose = obj.Bars.ClosePrices.Last(1);
            var prevhigh = obj.Bars.HighPrices.Last(1);
            var prevlow = obj.Bars.LowPrices.Last(1);
            var Prevatr = (obj.Bars.atr.Result.Last(1)/ Symbol.PipSize).ToString("F0"); 
            
            Print("SymbolName {0} ,prevclose {1},prevhigh {2} ,prevlow {3} ,Prevatr {4} ", SymbolName ,prevclose,prevhigh,prevlow,Prevatr);
            
            }
            
            }
        protected override void OnBar()
        {
       
       
        }        
           
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}
 
@Soliman.Mansour
                     PanagiotisCharalampous
                     28 Mar 2024, 07:39
                                    
Hi there,
The error is straight forward and it's not related to cTrader but general C# knowledge. The code you are writing does not make any sense. If you want to program cBots you need to learn C# first. As per the message, Bars does not contain any property called atr. I do not understand why you put that there.
Best regards.
Panagiotis
@PanagiotisCharalampous

PanagiotisCharalampous
26 Mar 2024, 06:44
Hi there,
What exactly is not working? What do you expect to happen and what happens instead?
Best regards,
Panagiotis
@PanagiotisCharalampous