Topics
Replies

soliman.mehisen
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.mehisen

soliman.mehisen
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.mehisen