C# Issue: Get Average Spread Stored in List

Created at 12 Jan 2015
deklin's avatar

deklin

Joined 31.12.2014

Status

Open


Budget

1.00 USD


Payment Method

Direct Payment

Job Description

I need help with a simple C# issue.  I want to get the average spread stored by Pair in an associated list.

This works exactly the way I want it to, but only for one symbol:

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

namespace cAlgo.Robots
{
    [Robot()]
    public class Robot : Robot
    {
        List SpreadList = new List();
        protected override void OnStart()
        {
        }

        protected override void OnTick()
        {

            SpreadList.Add(Symbol.Spread);
            if (SpreadList.Count() >= 5)
            {

                SpreadList.RemoveAt(0);

                if (Symbol.Spread < SpreadList.Average())
                    Print("Current Spread: " + (Symbol.Spread / Symbol.PipSize) + "  | Average Spread: " + (SpreadList.Average() / Symbol.PipSize) + " | Status: Better Than Average");
                else
                    Print("Current Spread: " + (Symbol.Spread / Symbol.PipSize) + "  | Average Spread: " + (SpreadList.Average() / Symbol.PipSize) + " | Status: Worse Than Average");
            }
            else
                Print("More Data Needed...");


        }

        protected override void OnStop()
        {
        }

    }
}

 

This does not work:

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Internals;
using System.Collections.Generic;

namespace cAlgo.Robots
{
    [Robot()]
    public class RobotExample : Robot
    {
        string[] pPairs = new string[] 
        {
            "EURUSD",
            "GBPUSD",
            "USDJPY",
            "AUDUSD",
            "EURJPY"
        };

        List> SpreadList = new List>();

        protected override void OnStart()
        {
        }

        protected override void OnTick()
        {

            foreach (var p in pPairs)
            {

                Symbol sSymbol = MarketData.GetSymbol(p);
                SpreadList.Insert(0, new KeyValuePair(p, sSymbol.Spread));

                if ((SpreadList.Count() / pPairs.Count()) >= 5)
                {
                    SpreadList.RemoveAt(0);
                 //   Print(sSymbol.Code + " | Current Spread: " + (sSymbol.Spread / sSymbol.PipSize) + " | Average Spread: " +  SpreadList.FindAll(sSymbol.Code).Average() / sSymbol.PipSize );
                }
                else Print("More Data Needed...");
            }
        }

        protected override void OnStop()
        {
        }


    }
}

The problem is with this:

SpreadList.FindAll(sSymbol.Code).Average()

Comments
Log in to add a comment.
alexmost113's avatar
alexmost113 · 9 years ago

let me implement it for you

bachapk's avatar
bachapk · 9 years ago

Please contact bachapk@gmail.com