Sonic by Popov

2054 downloads / 843 views / Created: 07.12.2015
 Average Rating: 0

Indicator Description

The Sonic indicator detects bars with higher than the average Volume. There are two types of bars: Rising volume - bars with Volume > 1.5 of the average volume for the last 10 bars, Climax - bars with Volume > 2.0 of the average volume.

Comments

//============================================================== // Forex Strategy Builder // Copyright © Miroslav Popov. All rights reserved. //============================================================== // THIS CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, // EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE. //============================================================== using ForexStrategyBuilder.Infrastructure.Entities; using ForexStrategyBuilder.Infrastructure.Enums; using ForexStrategyBuilder.Infrastructure.Interfaces; namespace ForexStrategyBuilder.Indicators.Store { public class Sonic : Indicator { public Sonic() { IndicatorName = "Sonic"; PossibleSlots = SlotTypes.OpenFilter | SlotTypes.CloseFilter; SeparatedChart = true; IndicatorAuthor = "Miroslav Popov"; IndicatorVersion = "1.0"; IndicatorDescription = "Determines rising volume bars and climax bars."; } public override void Initialize(SlotTypes slotType) { SlotType = slotType; // The ComboBox parameters IndParam.ListParam[0].Caption = "Logic"; IndParam.ListParam[0].ItemList = new[] { SlotType == SlotTypes.OpenFilter ? "Bullish bar with rising volume" : "Bearish bar with climax volume" }; IndParam.ListParam[0].Index = 0; IndParam.ListParam[0].Text = IndParam.ListParam[0].ItemList[IndParam.ListParam[0].Index]; IndParam.ListParam[0].Enabled = true; IndParam.ListParam[0].ToolTip = "Logic of application of the oscillator."; IndParam.ListParam[1].Caption = "Base price"; IndParam.ListParam[1].ItemList = new[] {"Bars range"}; IndParam.ListParam[1].Index = 0; IndParam.ListParam[1].Text = IndParam.ListParam[1].ItemList[IndParam.ListParam[1].Index]; IndParam.ListParam[1].Enabled = true; IndParam.ListParam[1].ToolTip = "The base price of the indicator."; // The NumericUpDown parameters IndParam.NumParam[0].Caption = "Volume period"; IndParam.NumParam[0].Value = 10; IndParam.NumParam[0].Min = 1; IndParam.NumParam[0].Max = 200; IndParam.NumParam[0].Enabled = true; IndParam.NumParam[0].ToolTip = "The average volume period."; // The CheckBox parameters IndParam.CheckParam[0].Caption = "Use previous bar value"; IndParam.CheckParam[0].Enabled = true; IndParam.CheckParam[0].ToolTip = "Use the indicator value from the previous bar."; } public override void Calculate(IDataSet dataSet) { DataSet = dataSet; // Reading the parameters var volumePeriod = (int) IndParam.NumParam[0].Value; int previous = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int firstBar = volumePeriod + previous + 2; double[] averageVolume = new double[Bars]; for (int bar = firstBar; bar < Bars; bar++) { int sum = 0; for (int n = bar - 1; n >= bar - volumePeriod; n--) sum += Volume[n]; averageVolume[bar] = 1.0*sum/volumePeriod; } // Rising Volume int[] volumeType = new int[Bars]; for (int bar = firstBar; bar < Bars; bar++) if (Volume[bar] >= 1.5*averageVolume[bar]) volumeType[bar] = 2; // Climax Volume for (int bar = firstBar; bar < Bars; bar++) { double highVolRange = 0; double volumeRange = Volume[bar]*(High[bar] - Low[bar]); for (int n = bar - 1; n >= bar - volumePeriod; n--) { double volRange = Volume[n]*(High[n] - Low[n]); if (highVolRange < volRange) highVolRange = volRange; } if (volumeRange >= highVolRange || Volume[bar] >= 2.0*averageVolume[bar]) volumeType[bar] = 1; } // Bar type double[] buyZone = new double[Bars]; double[] sellZone = new double[Bars]; for (int bar = firstBar; bar < Bars; bar++) { if (SlotType == SlotTypes.OpenFilter && volumeType[bar] == 2) { // Enter on a rising volume bar buyZone[bar] = Close[bar] > Open[bar] ? 1 : 0; sellZone[bar] = Close[bar] < Open[bar] ? 1 : 0; } else if (SlotType == SlotTypes.CloseFilter && volumeType[bar] == 1) { // Exit on a climax bar buyZone[bar] = Close[bar] > Open[bar] ? 1 : 0; sellZone[bar] = Close[bar] < Open[bar] ? 1 : 0; } } // Visualization double[] histogram = new double[Bars]; for (int bar = firstBar; bar < Bars; bar++) { if (buyZone[bar] > Epsilon) histogram[bar] = 1; else if (sellZone[bar] > Epsilon) histogram[bar] = -1; } // Shift signal if it is necessary if (previous > 0) { for (int bar = Bars - 1; bar >= firstBar; bar--) { buyZone[bar] = buyZone[bar - 1]; sellZone[bar] = sellZone[bar - 1]; } } // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp { CompName = SlotType == SlotTypes.OpenFilter ? "Rising volume" : "Climax", DataType = IndComponentType.IndicatorValue, ChartType = IndChartType.Histogram, FirstBar = firstBar, Value = histogram }; Component[1] = new IndicatorComp { ChartType = IndChartType.NoChart, FirstBar = firstBar }; Component[2] = new IndicatorComp { ChartType = IndChartType.NoChart, FirstBar = firstBar }; // Sets the Component's type if (SlotType == SlotTypes.OpenFilter) { Component[1].DataType = IndComponentType.AllowOpenLong; Component[1].CompName = "Is long entry allowed"; Component[1].Value = buyZone; Component[2].DataType = IndComponentType.AllowOpenShort; Component[2].CompName = "Is short entry allowed"; Component[2].Value = sellZone; } else if (SlotType == SlotTypes.CloseFilter) { Component[1].DataType = IndComponentType.ForceCloseLong; Component[1].CompName = "Close out long position"; Component[1].Value = sellZone; Component[2].DataType = IndComponentType.ForceCloseShort; Component[2].CompName = "Close out short position"; Component[2].Value = buyZone; } } public override void SetDescription() { EntryFilterLongDescription = "there is a bullish bar with rising volume"; EntryFilterShortDescription = "there is a bearish bar with rising volume"; ExitFilterLongDescription = "there is a bearish climax bar"; ExitFilterShortDescription = "there is a bullish climax bar"; } } }
//+--------------------------------------------------------------------+ //| Copyright: (C) 2015 Forex Software Ltd. | //| Website: http://forexsb.com/ | //| Support: http://forexsb.com/forum/ | //| License: Proprietary under the following circumstances: | //| | //| This code is a part of Forex Strategy Builder. It is free for | //| use as an integral part of Forex Strategy Builder. | //| One can modify it in order to improve the code or to fit it for | //| personal use. This code or any part of it cannot be used in | //| other applications without a permission. | //| The contact information cannot be changed. | //| | //| NO LIABILITY FOR CONSEQUENTIAL DAMAGES | //| | //| In no event shall the author be liable for any damages whatsoever | //| (including, without limitation, incidental, direct, indirect and | //| consequential damages, damages for loss of business profits, | //| business interruption, loss of business information, or other | //| pecuniary loss) arising out of the use or inability to use this | //| product, even if advised of the possibility of such damages. | //+--------------------------------------------------------------------+ #property copyright "Copyright (C) 2015 Forex Software Ltd." #property link "http://forexsb.com" #property version "1.00" #property strict #include <Forexsb.com/Indicator.mqh> #include <Forexsb.com/Enumerations.mqh> #include <Forexsb.com/Indicators/CommodityChannelIndex.mqh> //## Requires CommodityChannelIndex.mqh class Sonic : public Indicator { public: Sonic(SlotTypes slotType) { SlotType = slotType; IndicatorName = "Sonic"; WarningMessage = ""; IsAllowLTF = true; ExecTime = ExecutionTime_DuringTheBar; IsSeparateChart = true; IsDiscreteValues = false; IsDefaultGroupAll = false; } virtual void Calculate(DataSet &dataSet); }; void Sonic::Calculate(DataSet &dataSet) { Data = GetPointer(dataSet); // Reading the parameters MAMethod maMethod=(MAMethod) ListParam[2].Index; int volumePeriod = (int) NumParam[0].Value; int previous = CheckParam[0].Checked ? 1 : 0; // Calculation int firstBar = volumePeriod + previous + 2; double averageVolume[]; ArrayResize(averageVolume, Data.Bars); ArrayInitialize(averageVolume, 0); for (int bar = firstBar; bar < Data.Bars; bar++) { int sum = 0; for (int n = bar - 1; n >= bar - volumePeriod; n--) sum += (int) Data.Volume[n]; averageVolume[bar] = 1.0*sum/volumePeriod; } // Rising Volume double volumeType[]; ArrayResize(volumeType, Data.Bars); ArrayInitialize(volumeType, 0); for (int bar = firstBar; bar < Data.Bars; bar++) if (Data.Volume[bar] >= 1.5*averageVolume[bar]) volumeType[bar] = 2; // Climax Volume for (int bar = firstBar; bar < Data.Bars; bar++) { double highVolRange = 0; double volumeRange = Data.Volume[bar]*(Data.High[bar] - Data.Low[bar]); for (int n = bar - 1; n >= bar - volumePeriod; n--) { double volRange = Data.Volume[n]*(Data.High[n] - Data.Low[n]); if (highVolRange < volRange) highVolRange = volRange; } if (volumeRange >= highVolRange || Data.Volume[bar] >= 2.0*averageVolume[bar]) volumeType[bar] = 1; } // Bar type double buyZone[]; ArrayResize(buyZone, Data.Bars); ArrayInitialize(buyZone, 0); double sellZone[]; ArrayResize(sellZone, Data.Bars); ArrayInitialize(sellZone, 0); for (int bar = firstBar; bar < Data.Bars; bar++) { if (SlotType == SlotTypes_OpenFilter && volumeType[bar] == 2) { // Enter on a rising volume bar buyZone[bar] = Data.Close[bar] > Data.Open[bar] ? 1 : 0; sellZone[bar] = Data.Close[bar] < Data.Open[bar] ? 1 : 0; } else if (SlotType == SlotTypes_CloseFilter && volumeType[bar] == 1) { // Exit on a climax bar buyZone[bar] = Data.Close[bar] > Data.Open[bar] ? 1 : 0; sellZone[bar] = Data.Close[bar] < Data.Open[bar] ? 1 : 0; } } // Shift signal if it is necessary if (previous > 0) { for (int bar = Data.Bars - 1; bar >= firstBar; bar--) { buyZone[bar] = buyZone[bar - 1]; sellZone[bar] = sellZone[bar - 1]; } } // Saving the components ArrayResize(Component[0].Value, Data.Bars); Component[0].FirstBar = firstBar; ArrayResize(Component[1].Value, Data.Bars); Component[1].FirstBar = firstBar; // Sets the Component's type if (SlotType == SlotTypes_OpenFilter) { Component[0].DataType = IndComponentType_AllowOpenLong; Component[0].CompName = "Is long entry allowed"; ArrayCopy(Component[0].Value, buyZone); Component[1].DataType = IndComponentType_AllowOpenShort; Component[1].CompName = "Is short entry allowed"; ArrayCopy(Component[1].Value, sellZone); } else if (SlotType == SlotTypes_CloseFilter) { Component[0].DataType = IndComponentType_ForceCloseLong; Component[0].CompName = "Close out long position"; ArrayCopy(Component[0].Value, sellZone); Component[1].DataType = IndComponentType_ForceCloseShort; Component[1].CompName = "Close out short position"; ArrayCopy(Component[1].Value, buyZone); } }
Risk warning: Forex, spread bets and CFD are leveraged products. They may not be suitable for you as they carry a high degree of risk to your capital and you can lose more than your initial investment. You should ensure you understand all of the risks.
Copyright © 2006 - 2017, Miroslav Popov; Created by Yavor Kirov