Thursday, April 26, 2018

basic script

using System;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
using Community;

namespace Channels
{
    class StrategyBasic : WealthScript
    {
        //Strategy Parameters - GLobal
        StrategyParameter _periodFast;
        StrategyParameter _periodSlow;

        //Strategy Parameters - Constructor
        public StrategyBasic()
        {
            _periodFast = CreateParameter("Period Fast", 15, 0, 100, 1);
            _periodSlow = CreateParameter("Period SLow", 180, 0, 100, 1);
        }

        //Strategy Variables
        bool enterLong;
        bool enterShort;
        bool exitLong;
        bool exitShort;
        bool abovePricePane;
        bool displayGrid;

        //The Main Method
        protected override void Execute()
        {
            //Technical Settings
            ClearDebug();
            int firstValidValue = 0;

            //Bringing in the Parameters
            int periodFast = _periodFast.ValueInt;
            int periodSlow = _periodSlow.ValueInt;

            abovePricePane = false;
            displayGrid = true;

            DataSeries macd = MACD.Series(Close);
            PlotSeries(pane2, m                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
            // Create a pane
            ChartPane myCustomPane1 = CreatePane(40, abovePricePane, displayGrid);
            DrawLabel(myCustomPane1, "myCustomPane1", Color.Black);


            //Indicators
            #region Indicators

            DataSeries vwap = Community.Indicators.VWAP.Series(Bars);
            PlotSeries(PricePane, vwap, Color.Purple, LineStyle.Solid, 2);
            DataSeries emaFast = EMA.Series(Close, periodFast, EMACalculation.Modern);
            firstValidValue = Math.Max(firstValidValue, periodFast * 3);
            PlotSeries(PricePane, emaFast, Color.Blue, LineStyle.Solid, 2);

            DataSeries smaSlow = SMA.Series(Close, periodSlow);
            //DataSeries emaSlow = EMA.Series(Close, periodSlow, EMACalculation.Modern);
            firstValidValue = Math.Max(firstValidValue, periodSlow * 3);
            PlotSeries(PricePane, smaSlow, Color.Green, LineStyle.Solid, 2);

            #endregion

            for (int bar = firstValidValue; bar < Bars.Count; bar++)
            {
                //the rules
                enterLong = CrossOver(bar, emaFast, smaSlow);
                enterShort = CrossUnder(bar, emaFast, smaSlow);

                exitLong = Bars.Close[bar] < emaFast[bar];
                exitShort = Bars.Close[bar] > emaFast[bar];

                //if we have a position
                if (ActivePositions.Count > 0)
                {
                    //if the open position is LONG
                    if (LastActivePosition.PositionType == PositionType.Long)
                    {
                        if (exitLong)
                        {
                            ExitAtMarket(bar + 1, LastActivePosition, "Long Stop");
                        }
                    }

                    //if the open position is SHORT
                    else if (LastActivePosition.PositionType == PositionType.Short)
                    {
                        if (exitShort)
                        {
                            ExitAtMarket(bar + 1, LastActivePosition, "Short Stop");
                        }
                    }
                }

                //if we don't have the position
                else if (ActivePositions.Count == 0)
                {
                    if (enterLong)
                    {
                        BuyAtMarket(bar + 1, "Long");
                    }
                    if (enterShort)
                    {
                        ShortAtMarket(bar + 1, "Short");
                    }
                }
            }

        }
    }
}

No comments:

Post a Comment