Hi,

I have been working on .Net SDK. A have sent a post before 

http://finance.groups.yahoo.com/group/amibroker/message/142119

but I got only 3 replies.

If anyone is interested please reply to me directly. (If there is not enough 
interest I will not make it available. I do it, bacause I do intraday 
autotrading.)

Here are two sample code fragments. The first is an indicator class with three 
indicator panels. The third function demos how to pass AFL variables between 
.Net and AFL.

The second code fragment starting with the [ABMethod] attribute is a real plug 
in function that is directly accessable from AFL. (But this is for more 
advanced developers.) There are three parameters (array, string and float) 
passed from AFL to the .Net function.

Regards,

NetSDKforAB

using System;
using System.Diagnostics;
using AmiBroker;
using AmiBroker.PlugIn;
using YUtils;

namespace SamplePlugIn
{
    unsafe public class Samples : IndicatorBase
    {
        /// <summary>
        /// This function demos how to create a simple 
        /// "average crossing" indicator in .Net.
        /// ATArray objects (Open, High, etc.) hold time series data. 
        /// AFxxx classes have static members to execute AFL functions.
        /// </summary>
        public void Sample1()
        {
            // calculate typical price from bar price data
            ATArray MyTypicalPrice = (High + Low + 2 * Close) / 4;

            // calculate slow average of typical price
            ATArray SlowMa = AFAvg.Ma(MyTypicalPrice, 20);

            // plotting average typical price
            AFGraph.Plot(SlowMa, "Ma20", Color.Black, Style.Thick);

            // calculate fast average of close price
            ATArray FastEma = AFAvg.Ema(Close, 5);

            // plotting fast average close price with alternating color
            AFGraph.Plot(FastEma, "Ema5",
                AFTools.Iif(SlowMa < FastEma, (float)Color.DarkGreen, 
(float)Color.Brown),
                Style.Line);

            // calculate the difference of the two averages
            ATArray Diff = (FastEma - SlowMa) / SlowMa * 100;

            // plotting difference as a histogram
            AFGraph.Plot(Diff, "Diff", Color.Red, Style.Histogram | 
Style.OwnScale, -0.2f, 0.2f);
        }

        /// <summary>
        /// Demos how to do fast loops
        /// Because of compiled code and integer index arithmetric 
        /// loops are 2-10 times faster than AFL script loops
        /// </summary>
        public void Sample2()
        {
            try
            {
                int maPeriod = 20;
                float tempSum;
                
                // declaration of result array
                ATArray MyMa;

                // allocation of memory for result array
                MyMa = new ATArray();

                // set null values to the beginning of the array
                for (int i = 0; i < maPeriod - 1 && i < MyMa.Length; i++)
                    MyMa[i] = ATFloat.Null;

                // calculate the average for each bar
                for (int i = maPeriod - 1; i < MyMa.Length; i++)
                {
                    // clear temporary result
                    tempSum = 0.0f;

                    // inner loop to sum bar data
                    for (int j = 0; j < maPeriod; j++)
                        tempSum = tempSum + Close[i - j];

                    // saving calculated value to array
                    MyMa[i] = tempSum / maPeriod;
                }

                // plot the calculated average
                AFGraph.Plot(MyMa, "MyMa", Color.Black, Style.Line);
            }
            catch (Exception e)
            {
                YException.Show("Error while executing indicator.", e);
            }
        }

        /// <summary>
        /// Reading and writing AFL global variables
        /// </summary>
        public void Sample3()
        {
            try
            {
                // defining AFL variable object
                ATAfl EmaPeriodAfl = new ATAfl("Period");

                // reading value from AFL variable
                // AFL variable MUST be initialized with a float value
                // uninitialized variable or other data types will cause 
exception
                float emaPeriod = EmaPeriodAfl.GetFloat();

                // do the calculation of the average
                ATArray MyEma = AFAvg.Ema(Close, emaPeriod);
                
                // plot the average
                AFGraph.Plot(MyEma, "MyEma", Color.Black, Style.Line);

                // defining AFL variable "MyEma" to store result
                ATAfl EmaAfl = new ATAfl("MyEma");

                // setting calculated array value to AFL variable
                // following AFL scripts can use this calculated array
                EmaAfl.Set(MyEma);
            }
            catch (Exception e)
            {
                YException.Show("Error while executing indicator.", e);
            }
        }
    }
}


         [ABMethod]
        [ABParameter(0, Type = ABParameterType.Array, Decription = "color")]
        [ABParameter(1, Type = ABParameterType.String, Decription = "plotName")]
        [ABParameter(2, Type = ABParameterType.Float, Decription = "level")]
        unsafe public static ATVar XDebugRibbon(int argNum, ATVar* argList)
        {
            YDebug.DebugRibbon(argList[0].GetArray(), argList[1].GetString(), 
(int)argList[2].GetFloat());
            return ATVar.CreateNone();
        }

Reply via email to