Price MA Relation by Popov
50709 downloads / 5504 views / Created: 27.06.2015




Average Rating: 5
Indicator Description
This indicator compares a bar price with a Moving Average.
See this post for examples Moving Average strategy
See this post for examples Moving Average strategy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
//==============================================================
// 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.Custom
{
public class PriceMARelation : Indicator
{
public PriceMARelation()
{
IndicatorName = "Price MA Relation";
PossibleSlots = SlotTypes.OpenFilter | SlotTypes.CloseFilter;
IndicatorAuthor = "Miroslav Popov";
IndicatorVersion = "1.0";
IndicatorDescription = "Compares a bar price with a Moving Average";
}
public override void Initialize(SlotTypes slotType)
{
SlotType = slotType;
// The ComboBox parameters
IndParam.ListParam[0].Caption = "Logic";
IndParam.ListParam[0].ItemList = new[]
{
"Bar price is higher than MA",
"Bar price is lower than MA"
};
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 = "Releation between a bar price and a MA";
IndParam.ListParam[1].Caption = "MA base price";
IndParam.ListParam[1].ItemList = new[] {"Close"};
IndParam.ListParam[1].Index = 0;
IndParam.ListParam[1].Text = "Close";
IndParam.ListParam[1].Enabled = true;
IndParam.ListParam[1].ToolTip = "Base price of the MA";
IndParam.ListParam[2].Caption = "MA method";
IndParam.ListParam[2].ItemList = Enum.GetNames(typeof (MAMethod));
IndParam.ListParam[2].Index = (int) MAMethod.Simple;
IndParam.ListParam[2].Text = IndParam.ListParam[2].ItemList[IndParam.ListParam[2].Index];
IndParam.ListParam[2].Enabled = true;
IndParam.ListParam[2].ToolTip = "Method for calculating the MA";
IndParam.ListParam[3].Caption = "Bar reference price";
IndParam.ListParam[3].ItemList = Enum.GetNames(typeof (BasePrice));
IndParam.ListParam[3].Index = (int) BasePrice.Close;
IndParam.ListParam[3].Text = IndParam.ListParam[3].ItemList[IndParam.ListParam[3].Index];
IndParam.ListParam[3].Enabled = true;
IndParam.ListParam[3].ToolTip = "Bar reference price.";
// The NumericUpDown parameters
IndParam.NumParam[0].Caption = "MA period";
IndParam.NumParam[0].Value = 13;
IndParam.NumParam[0].Min = 1;
IndParam.NumParam[0].Max = 200;
IndParam.NumParam[0].Enabled = true;
IndParam.NumParam[0].ToolTip = "Bars used for calculating the MA";
// 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 maBasePrice = (BasePrice) IndParam.ListParam[1].Index;
var maMethod = (MAMethod) IndParam.ListParam[2].Index;
var maPeriod = (int) IndParam.NumParam[0].Value;
var previous = IndParam.CheckParam[0].Checked ? 1 : 0;
var barBasePriceLong = (BasePrice) IndParam.ListParam[3].Index;
var barBasePriceShort = barBasePriceLong;
if (barBasePriceLong == BasePrice.High)
barBasePriceShort = BasePrice.Low;
if (barBasePriceLong == BasePrice.Low)
barBasePriceShort = BasePrice.High;
var ma = MovingAverage(maPeriod, 0, maMethod, Price(maBasePrice));
var barLong = Price(barBasePriceLong);
var barShort = Price(barBasePriceShort);
var firstBar = maPeriod + previous + 1;
// Initializing components
Component = new IndicatorComp[3];
Component[0] = new IndicatorComp
{
CompName = "Moving Average",
ChartColor = Color.DarkViolet,
DataType = IndComponentType.IndicatorValue,
ChartType = IndChartType.Line,
FirstBar = firstBar,
Value = ma
};
Component[1] = new IndicatorComp
{
ChartType = IndChartType.NoChart,
FirstBar = firstBar,
Value = new double[Bars]
};
Component[2] = new IndicatorComp
{
ChartType = IndChartType.NoChart,
FirstBar = firstBar,
Value = new double[Bars]
};
// Sets the Component's type
if (SlotType == SlotTypes.OpenFilter)
{
Component[1].DataType = IndComponentType.AllowOpenLong;
Component[1].CompName = "Is long entry allowed";
Component[2].DataType = IndComponentType.AllowOpenShort;
Component[2].CompName = "Is short entry allowed";
}
else if (SlotType == SlotTypes.CloseFilter)
{
Component[1].DataType = IndComponentType.ForceCloseLong;
Component[1].CompName = "Close out long position";
Component[2].DataType = IndComponentType.ForceCloseShort;
Component[2].CompName = "Close out short position";
}
// Sets the components signals
if (IndParam.ListParam[0].Text == "Bar price is higher than MA")
{
var sigma = Sigma();
for (int bar = firstBar; bar < Bars; bar++)
{
if (barLong[bar - previous] > ma[bar - previous] + sigma)
Component[1].Value[bar] = 1;
if (barShort[bar - previous] < ma[bar - previous] - sigma)
Component[2].Value[bar] = 1;
}
}
else if (IndParam.ListParam[0].Text == "Bar price is lower than MA")
{
var sigma = Sigma();
for (int bar = firstBar + previous; bar < Bars; bar++)
{
if (barLong[bar - previous] < ma[bar - previous] - sigma)
Component[1].Value[bar] = 1;
if (barShort[bar - previous] > ma[bar - previous] + sigma)
Component[2].Value[bar] = 1;
}
}
}
public override void SetDescription()
{
var maPeriod = (int) IndParam.NumParam[0].Value;
var maMethod = (MAMethod)IndParam.ListParam[2].Index;
var previous = IndParam.CheckParam[0].Checked ? "*" : String.Empty;
var maText = String.Format("MA{0}({1}, {2})", previous, maMethod, maPeriod);
var barBasePriceLong = (BasePrice) IndParam.ListParam[3].Index;
var barBasePriceShort = barBasePriceLong;
if (barBasePriceLong == BasePrice.High)
barBasePriceShort = BasePrice.Low;
if (barBasePriceLong == BasePrice.Low)
barBasePriceShort = BasePrice.High;
var barTextLong = String.Format("Bar {0}", barBasePriceLong);
var barTextShort = String.Format("Bar {0}", barBasePriceShort);
switch (IndParam.ListParam[0].Text)
{
case "Bar price is higher than MA":
EntryFilterLongDescription = barTextLong + " is higher than " + maText;
EntryFilterShortDescription = barTextShort + " is lower than " + maText;
ExitFilterLongDescription = barTextLong + " is higher than " + maText;
ExitFilterShortDescription = barTextShort + " is lower than " + maText;
break;
case "Bar price is lower than MA":
EntryFilterLongDescription = barTextLong + " is lower than " + maText;
EntryFilterShortDescription = barTextShort + " is higher than " + maText;
ExitFilterLongDescription = barTextLong + " is lower than " + maText;
ExitFilterShortDescription = barTextShort + " is higher than " + maText;
break;
}
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134//+--------------------------------------------------------------------+ //| Copyright: (C) 2014 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) 2014 Forex Software Ltd." #property link "http://forexsb.com" #property version "2.00" #property strict #include <Forexsb.com/Indicator.mqh> #include <Forexsb.com/Enumerations.mqh> //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class PriceMARelation : public Indicator { public: PriceMARelation(SlotTypes slotType) { SlotType=slotType; IndicatorName="Price MA Relation"; WarningMessage = ""; IsAllowLTF = true; ExecTime = ExecutionTime_DuringTheBar; IsSeparateChart = false; IsDiscreteValues = false; IsDeafultGroupAll = false; } virtual void Calculate(DataSet &dataSet); }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void PriceMARelation::Calculate(DataSet &dataSet) { Data=GetPointer(dataSet); // Reading the parameters BasePrice maBasePrice=(BasePrice) ListParam[1].Index; MAMethod maMethod=(MAMethod) ListParam[2].Index; int maPeriod = (int) NumParam[0].Value; int previous = CheckParam[0].Checked ? 1 : 0; BasePrice barBasePriceLong=(BasePrice) ListParam[3].Index; BasePrice barBasePriceShort=barBasePriceLong; if(barBasePriceLong==BasePrice_High) barBasePriceShort=BasePrice_Low; if(barBasePriceLong==BasePrice_Low) barBasePriceShort=BasePrice_High; double basePrc[]; Price(maBasePrice,basePrc); double ma[]; MovingAverage(maPeriod,0,maMethod,basePrc,ma); double barLong[]; Price(barBasePriceLong,barLong); double barShort[]; Price(barBasePriceShort,barShort); int firstBar=maPeriod+previous+1; // Saving the components ArrayResize(Component[0].Value,Data.Bars); Component[0].CompName = "Moving Average"; Component[0].DataType = IndComponentType_IndicatorValue; Component[0].FirstBar = firstBar; ArrayCopy(Component[0].Value,ma); ArrayResize(Component[1].Value,Data.Bars); ArrayInitialize(Component[1].Value,0); Component[1].FirstBar=firstBar; ArrayResize(Component[2].Value,Data.Bars); ArrayInitialize(Component[2].Value,0); Component[2].FirstBar=firstBar; // Sets the Component's type if(SlotType==SlotTypes_OpenFilter) { Component[1].DataType = IndComponentType_AllowOpenLong; Component[1].CompName = "Is long entry allowed"; Component[2].DataType = IndComponentType_AllowOpenShort; Component[2].CompName = "Is short entry allowed"; } else if(SlotType==SlotTypes_CloseFilter) { Component[1].DataType = IndComponentType_ForceCloseLong; Component[1].CompName = "Close out long position"; Component[2].DataType = IndComponentType_ForceCloseShort; Component[2].CompName = "Close out short position"; } // Sets the components signals if(ListParam[0].Text=="Bar price is higher than MA") { for(int bar=firstBar; bar<Data.Bars; bar++) { double sigma=Sigma(); if(barLong[bar-previous]>ma[bar-previous]+sigma) Component[1].Value[bar]=1; if(barShort[bar-previous]<ma[bar-previous]-sigma) Component[2].Value[bar]=1; } } else if(ListParam[0].Text=="Bar price is lower than MA") { for(int bar=firstBar+previous; bar<Data.Bars; bar++) { double sigma=Sigma(); if(barLong[bar-previous]<ma[bar-previous]-sigma) Component[1].Value[bar]=1; if(barShort[bar-previous]>ma[bar-previous]+sigma) Component[2].Value[bar]=1; } } } //+------------------------------------------------------------------+
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 - 2025, Forex Software Ltd.;
Copyright © 2006 - 2025, Forex Software Ltd.;