понедельник, 2 января 2012 г.

Скрипт Camarilla_gars (WLD5).

using System;
using System.Collections.Generic;
using System.Text;
using WealthLab;
using WealthLab.Indicators;
using System.Drawing;



namespace WealthLabCompile
{
    class Camarilla_gars : WealthScript
    {
        double H5, H4, H3, L3, L4, L5;
        bool  bInDeal;
        bool  bLong;
        StrategyParameter parammult3;
        StrategyParameter parammult4;
        StrategyParameter parammult5;
        /////////////////////////////////////////////////////////
        // ПОЛЬЗОВАТЕЛЬСКИЕ УСТАНОВКИ ПРАВИЛ ТОРГОВЛИ
        /////////////////////////////////////////////////////////
       
        int    PARAM_START_HOUR = 11; // С какого часа начинать торговлю
        int    PARAM_STOP_HOUR  = 23; // До какого часа можно входить в сделки
       
        public Camarilla_gars()
        {
            H5 = H4 = H3 = 100000000.0;

            L3 = L4 = L5 = 0;
            bInDeal = false;
            parammult3 = CreateParameter("mult3", 24, 25, 35, 1);
            parammult4 = CreateParameter("mult4", 48, 45, 55, 1);
            parammult5 = CreateParameter("mult5", 310, 310, 999, 10);
        }

        public void DrawLevels(int StartBar, int StopBar)
        {
            DrawLine(PricePane, StartBar, H5, StopBar, H5, Color.Green,  WealthLab.LineStyle.Solid, 3);
            DrawLine(PricePane, StartBar, H4, StopBar, H4, Color.Green,      WealthLab.LineStyle.Solid, 2);
            DrawLine(PricePane, StartBar, H3, StopBar, H3, Color.Green, WealthLab.LineStyle.Solid, 1);
            DrawLine(PricePane, StartBar, L3, StopBar, L3, Color.Red, WealthLab.LineStyle.Solid, 1);
            DrawLine(PricePane, StartBar, L4, StopBar, L4, Color.Red,       WealthLab.LineStyle.Solid, 2);
            DrawLine(PricePane, StartBar, L5, StopBar, L5, Color.Red,   WealthLab.LineStyle.Solid, 3);
        }

        //***************************************************************************

        public bool CheckLong(int Bar, double Price)
        {
            if ((Close[Bar] > Price) && (Low[Bar] < Price)) return true;
            return false;
        }
       
        public bool CheckShort(int Bar, double Price)
        {
            if ((Close[Bar] < Price) && (High[Bar] > Price)) return true;
            return false;
        }
       
       
        protected override void Execute()
        {
            int    nDayStartBar = 0;
            int    Bar;
           
            double fProfitPrice     = 0.0;  // Где стоит тейк-профит
            double fEnterPrice      = 0.0;  // Цена, равная уроню Камарильи, на котором был вход
            double fStopPrice       = 0.0;  // Где стоит стоп-лосс
           
            double fMinPrice = 10000000000.0;
            double fMaxPrice = 0;
           
            for( Bar = 2; Bar < Bars.Count; Bar++)
            {
                //----------------------------------------------------------------------------
                // При смене дня меняем значения уровней Camarilla
                //----------------------------------------------------------------------------
               
                if ((Bar != 0) && (Date[Bar].Date != Date[Bar-1].Date))
                {
                    double c, h, l;
                    c = Close[Bar-1];
                    h = fMaxPrice;
                    l = fMinPrice;
                   
                    double mult3 = parammult3.Value/100;
                    double mult4 = parammult4.Value/100;
                    double mult5 = parammult5.Value/100;
                   
                    H5 = c + (h-l) * mult5;   
                    H4 = c + (h-l) * mult4;    //0.55
                    H3 = c + (h-l) * mult3;    //0.275
                    L3 = c - (h-l) * mult3;
                    L4 = c - (h-l) * mult4;
                    L5 = c - (h-l) * mult5;
   
                    nDayStartBar = Bar;
                    fMaxPrice = High[Bar];
                    fMinPrice = Low[Bar];
                }
                DrawLevels(nDayStartBar, Bar);
                if (fMinPrice > Low[Bar])  fMinPrice = Low[Bar];
                if (fMaxPrice < High[Bar]) fMaxPrice = High[Bar];
               
                //*****************************************************************************
                // Закрытие позиции
                //*****************************************************************************
               
                if (bInDeal)
                {
                    //----------------------------------------------------------------------------
                    // Закрываем LONG
                    //----------------------------------------------------------------------------
                   
                    if (bLong)
                    {
                        // Stop-loss
                        if (Close[Bar] < fStopPrice && Close[Bar-1] >= fStopPrice)
                        {
                            SellAtMarket(Bar+1, LastPosition, "stop-loss");
                            bInDeal = false;
                        }
                       
                        // Take-profit
                        if (bInDeal && Close[Bar] > fProfitPrice && Close[Bar-1] <= fProfitPrice)
                        {
                            SellAtMarket(Bar+1, LastPosition, "profit");
                            bInDeal = false;
                        }
                    }
                   
                    //----------------------------------------------------------------------------
                    // Закрываем SHORT
                    //----------------------------------------------------------------------------
                   
                    if (!bLong)
                    {
                        // Stop-loss
                        if (Close[Bar] > fStopPrice && Close[Bar-1] <= fStopPrice)
                        {
                            CoverAtMarket(Bar+1, LastPosition, "stop-loss");
                            bInDeal = false;
                        }
                       
                        // Take-profit
                        if (bInDeal && Close[Bar] < fProfitPrice && Close[Bar-1] >= fProfitPrice)
                        {
                            CoverAtMarket(Bar+1, LastPosition, "profit");
                            bInDeal = false;
                        }
                    }
                }
                //****************************************************************************
                // В конце дня принудтельно закрываем позицию
                //****************************************************************************
                   
               
                if (bInDeal && (Date[Bar].TimeOfDay.Hours >= 23) && (Date[Bar].TimeOfDay.Minutes >= 30))

                {
                    if (bLong)  SellAtMarket (Bar+1, LastPosition, "23:40");
                    else        CoverAtMarket(Bar+1, LastPosition, "23:40");
                    bInDeal = false;
                }
               
                //****************************************************************************
                // Открытие позиции
                //****************************************************************************
                   
                if (!bInDeal
                    && L3 != 0.0
                    && (Date[Bar].TimeOfDay.Hours >= PARAM_START_HOUR) && (Date[Bar].TimeOfDay.Minutes >= 00)
                    && (Date[Bar].TimeOfDay.Hours <  PARAM_STOP_HOUR) && (Date[Bar].TimeOfDay.Minutes <= 59)
                    )
                {
                    //------------------------------------------------------------------------
                    // Проверка на вход в лонг
                    //------------------------------------------------------------------------
                       
                    if (!bInDeal && Close[Bar] > H4 && Close[Bar-1] <= H4)
                    {
                        BuyAtMarket(Bar+1, "H4 long");                       
                        fEnterPrice   = H4;
                        fProfitPrice  = H5;
                        fStopPrice    = H3;
                        bInDeal   = true;
                        bLong     = true;
                    }
   
                    if (!bInDeal && Close[Bar] > L3 && Close[Bar-1] <= L3)
                    {
                        BuyAtMarket(Bar+1, "L3 long");                       
                        fEnterPrice   = L3;
                        fProfitPrice  = H3;
                        fStopPrice    = L4;
                        bInDeal   = true;
                        bLong     = true;
                    }
                       
                    //------------------------------------------------------------------------
                    // Проверка на вход в шорт
                    //------------------------------------------------------------------------
                       
                    if (!bInDeal && Close[Bar] < L4 && Close[Bar-1] >= L4)
                    {
                        ShortAtMarket(Bar+1, "L4 short");
                        fEnterPrice   = L4;
                        fProfitPrice  = L5;
                        fStopPrice    = L3;
                        bInDeal   = true;
                        bLong     = false;
                    }
   
                    if (!bInDeal && Close[Bar] < H3 && Close[Bar-1] >= H3)
                    {
                        ShortAtMarket(Bar+1, "H3 short");
                        fEnterPrice   = H3;
                        fProfitPrice  = L3;
                        fStopPrice    = H4;
                        bInDeal   = true;
                        bLong     = false;
                    }
                }
                if (bLong&&bInDeal) SetBarColor(Bar, Color.Green);
                if (!bLong&&bInDeal) SetBarColor(Bar, Color.Red);               
            }
        }
    }
}

Комментариев нет:

Отправить комментарий

Здесь Вы можете оставить свой комментарий