Recent Swing High Low by Hyoru

43268 downloads / 4041 views / Created: 23.05.2013
 Average Rating: 0

Indicator Description

Hi all,
First I want to thank Popov and all the people involved in the development of this great software.
I'm not an expert programmer but I'm capable of creating my own MT4 indicators and now I'm starting to create indicators for FSB.
My first indicator open positions when the most recent swing high or low is reached, at the moment it only gives FSB and opening and closing point for the position.
My logic for a swing high is a candle which high is above the closest two on both sides (inverse logic applies for a swing low). So you have to wait for at least 2 candles before getting a confirmation that you have a valid swing high/low.
I need some help to solve a problem, when a swing is confirmed, the tester goes back to the swing candle and opens a position there (so far I can avoid the problem by adding 1 pip to the vertical shift but could be better without the issue).
That's it, I'll appreciate all the suggestions/complains you have.

Link to forum: Recent Swing High Low

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 System; using System.Drawing; using ForexStrategyBuilder.Infrastructure.Entities; using ForexStrategyBuilder.Infrastructure.Enums; using ForexStrategyBuilder.Infrastructure.Interfaces; namespace ForexStrategyBuilder.Indicators.Store { public class Recent_Swing_High_Low : Indicator { public Recent_Swing_High_Low() { IndicatorName = "Recent Swing High Low"; PossibleSlots = SlotTypes.Open | SlotTypes.Close; IndicatorAuthor = "Hyoru"; IndicatorVersion = "2.0"; IndicatorDescription = "A custom indicator for FSB and FST."; } public override void Initialize(SlotTypes slotType) { SlotType = slotType; // The ComboBox parameters IndParam.ListParam[0].Caption = "Logic"; if (slotType == SlotTypes.Open) IndParam.ListParam[0].ItemList = new string[] { "Enter long at the most recent swing high", "Enter long at the most recent swing low" }; else if (slotType == SlotTypes.Close) IndParam.ListParam[0].ItemList = new string[] { "Exit long at the most recent swing high", "Exit long at the most recent swing low" }; 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 indicator."; IndParam.ListParam[1].Caption = "Base price"; IndParam.ListParam[1].ItemList = new string[] { "High and Low" }; 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 = "Used price from the indicator."; // The NumericUpDown parameters IndParam.NumParam[0].Caption = "Vertical shift"; IndParam.NumParam[0].Value = 0; IndParam.NumParam[0].Min = -200; IndParam.NumParam[0].Max = +200; IndParam.NumParam[0].Enabled = true; IndParam.NumParam[0].ToolTip = "A vertical shift above the swing high and below the swing low price."; } public override void Calculate(IDataSet dataSet) { DataSet = dataSet; double dShift = IndParam.NumParam[0].Value * Point; int iFirstBar = 7; // Calculation double[] adHighPrice = new double[Bars]; double[] adLowPrice = new double[Bars]; for (int iBar = iFirstBar; iBar < Bars; iBar++) { // Check if current high is a swing high if (High[iBar - 3] >= High[iBar - 4] && High[iBar - 3] > High[iBar - 5] && // Check 2 candles to the left High[iBar - 3] >= High[iBar - 2] && High[iBar - 3] > High[iBar - 1]) // Check 2 candles to the right { adHighPrice[iBar] = High[iBar - 3]; } else { adHighPrice[iBar] = adHighPrice[iBar - 1]; } // Check if current low is a swing low if (Low[iBar - 3] <= Low[iBar - 4] && Low[iBar - 3] < Low[iBar - 5] && // Check 2 candles to the left Low[iBar - 3] <= Low[iBar - 2] && Low[iBar - 3] < Low[iBar - 1]) // Check 2 candles to the right { adLowPrice[iBar] = Low[iBar - 3]; } else { adLowPrice[iBar] = adLowPrice[iBar - 1]; } } // Shifting the price double[] adUpperBand = new double[Bars]; double[] adLowerBand = new double[Bars]; for (int iBar = 1; iBar < Bars; iBar++) { adUpperBand[iBar] = adHighPrice[iBar] + dShift; adLowerBand[iBar] = adLowPrice[iBar] - dShift; } // Saving the components Component = new IndicatorComp[4]; Component[0] = new IndicatorComp(); Component[0].CompName = "Swing High"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Level; Component[0].ChartColor = Color.DarkGreen; Component[0].FirstBar = iFirstBar; Component[0].Value = adHighPrice; Component[1] = new IndicatorComp(); Component[1].CompName = "Swing Low"; Component[1].DataType = IndComponentType.IndicatorValue; Component[1].ChartType = IndChartType.Level; Component[1].ChartColor = Color.DarkRed; Component[1].FirstBar = iFirstBar; Component[1].Value = adLowPrice; Component[2] = new IndicatorComp(); Component[2].ChartType = IndChartType.NoChart; Component[2].FirstBar = iFirstBar; Component[2].Value = new double[Bars]; Component[3] = new IndicatorComp(); Component[3].ChartType = IndChartType.NoChart; Component[3].FirstBar = iFirstBar; Component[3].Value = new double[Bars]; // Sets the Component's type if (SlotType == SlotTypes.Open) { Component[2].CompName = "Long position entry price"; Component[2].DataType = IndComponentType.OpenLongPrice; Component[3].CompName = "Short position entry price"; Component[3].DataType = IndComponentType.OpenShortPrice; } else if (SlotType == SlotTypes.Close) { Component[2].CompName = "Long position closing price"; Component[2].DataType = IndComponentType.CloseLongPrice; Component[3].CompName = "Short position closing price"; Component[3].DataType = IndComponentType.CloseShortPrice; } switch (IndParam.ListParam[0].Text) { case "Enter long at the most recent swing high": case "Exit long at the most recent swing high": Component[2].Value = adUpperBand; Component[3].Value = adLowerBand; break; case "Enter long at the most recent swing low": case "Exit long at the most recent swing low": Component[2].Value = adLowerBand; Component[3].Value = adUpperBand; break; } } public override void SetDescription() { int iShift = (int)IndParam.NumParam[0].Value; string sUpperTrade = null; string sLowerTrade = null; if (iShift > 0) { sUpperTrade = iShift + " pips above the "; sLowerTrade = iShift + " pips below the "; } else if (iShift == 0) { if (IndParam.ListParam[0].Text == "Enter long at the most recent swing high" || IndParam.ListParam[0].Text == "Enter long at the most recent swing low" || IndParam.ListParam[0].Text == "Exit long at the most recent swing high" || IndParam.ListParam[0].Text == "Exit long at the most recent swing low") { sUpperTrade = "at the "; sLowerTrade = "at the "; } } else { sUpperTrade = -iShift + " pips below the "; sLowerTrade = -iShift + " pips above the "; } switch (IndParam.ListParam[0].Text) { case "Enter long at the most recent swing high": EntryPointLongDescription = sUpperTrade + "most recent swing high"; EntryPointShortDescription = sLowerTrade + "most recent swing low"; break; case "Enter long at the most recent swing low": EntryPointLongDescription = sLowerTrade + "most recent swing low"; EntryPointShortDescription = sUpperTrade + "most recent swing high"; break; case "Exit long at the most recent swing high": ExitPointLongDescription = sUpperTrade + "most recent swing high"; ExitPointShortDescription = sLowerTrade + "most recent swing low"; break; case "Exit long at the most recent swing low": ExitPointLongDescription = sLowerTrade + "most recent swing low"; ExitPointShortDescription = sUpperTrade + "most recent swing high"; break; default: break; } return; } public override string ToString() { return IndicatorName +" (" + IndParam.NumParam[0].Value.ToString() + ")"; // Shift } } }
//+--------------------------------------------------------------------+ //| Copyright: (C) 2014, Miroslav Popov - All rights reserved! | //| 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 | //| another applications without a permission. 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 2014, Miroslav Popov" #property link "http://forexsb.com" #property version "1.00" #property strict #include <Forexsb.com/Indicator.mqh> #include <Forexsb.com/Enumerations.mqh> //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class RecentSwingHighLow : public Indicator { public: RecentSwingHighLow(SlotTypes slotType) { SlotType=slotType; IndicatorName="Recent Swing High Low"; WarningMessage = ""; IsAllowLTF = true; ExecTime = ExecutionTime_DuringTheBar; IsSeparateChart = false; IsDiscreteValues = false; IsDeafultGroupAll = false; } virtual void Calculate(DataSet &dataSet); }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void RecentSwingHighLow::Calculate(DataSet &dataSet) { Data=GetPointer(dataSet); double dShift = NumParam[0].Value * Data.Point; int iFirstBar = 7; // Calculation double adHighPrice[]; ArrayResize(adHighPrice,Data.Bars); ArrayInitialize(adHighPrice,0); double adLowPrice[]; ArrayResize(adLowPrice,Data.Bars); ArrayInitialize(adLowPrice,0); for(int iBar=iFirstBar; iBar<Data.Bars; iBar++) { // Check if current high is a swing high if(Data.High[iBar - 3]>= Data.High[iBar - 4] && Data.High[iBar - 3]> Data.High[iBar - 5] && // Check 2 candles to the left Data.High[iBar - 3] >= Data.High[iBar - 2] && Data.High[iBar - 3] > Data.High[iBar - 1]) // Check 2 candles to the right { adHighPrice[iBar]=Data.High[iBar-3]; } else { adHighPrice[iBar]=adHighPrice[iBar-1]; } // Check if current low is a swing low if(Data.Low[iBar - 3]<= Data.Low[iBar - 4] && Data.Low[iBar - 3]< Data.Low[iBar - 5] && // Check 2 candles to the left Data.Low[iBar - 3] <= Data.Low[iBar - 2] && Data.Low[iBar - 3] < Data.Low[iBar - 1]) // Check 2 candles to the right { adLowPrice[iBar]=Data.Low[iBar-3]; } else { adLowPrice[iBar]=adLowPrice[iBar-1]; } } // Shifting the price double adUpperBand[]; ArrayResize(adUpperBand, Data.Bars); ArrayInitialize(adUpperBand,0); double adLowerBand[]; ArrayResize(adLowerBand, Data.Bars); ArrayInitialize(adLowerBand,0); for(int iBar=1; iBar<Data.Bars; iBar++) { adUpperBand[iBar] = adHighPrice[iBar] + dShift; adLowerBand[iBar] = adLowPrice[iBar] - dShift; } // Saving the components ArrayResize(Component[0].Value,Data.Bars); Component[0].CompName = "Swing Data.High"; Component[0].DataType = IndComponentType_IndicatorValue; Component[0].FirstBar = iFirstBar; ArrayCopy(Component[0].Value,adHighPrice); ArrayResize(Component[1].Value,Data.Bars); Component[1].CompName = "Swing Low"; Component[1].DataType = IndComponentType_IndicatorValue; Component[1].FirstBar = iFirstBar; ArrayCopy(Component[1].Value,adLowPrice); ArrayResize(Component[2].Value,Data.Bars); Component[2].FirstBar=iFirstBar; ArrayResize(Component[3].Value,Data.Bars); Component[3].FirstBar=iFirstBar; // Sets the Component's type if(SlotType==SlotTypes_Open) { Component[2].CompName = "Long position entry price"; Component[2].DataType = IndComponentType_OpenLongPrice; Component[3].CompName = "Short position entry price"; Component[3].DataType = IndComponentType_OpenShortPrice; } else if(SlotType==SlotTypes_Close) { Component[2].CompName = "Long position closing price"; Component[2].DataType = IndComponentType_CloseLongPrice; Component[3].CompName = "Short position closing price"; Component[3].DataType = IndComponentType_CloseShortPrice; } if(ListParam[0].Text=="Enter long at the most recent swing high" || ListParam[0].Text=="Exit long at the most recent swing high") { ArrayCopy(Component[2].Value, adUpperBand); ArrayCopy(Component[3].Value, adLowerBand); } else if(ListParam[0].Text=="Enter long at the most recent swing low" || ListParam[0].Text=="Exit long at the most recent swing low") { ArrayCopy(Component[2].Value, adLowerBand); ArrayCopy(Component[3].Value, adUpperBand); } } //+------------------------------------------------------------------+
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 - 2024, Forex Software Ltd.;