Swingarm ATR free

by mak.razeeb in category Trend at 14/01/2023
Description

//+------------------------------------------------------------------+
//|                                      Indicator: Swingarm ATR.mq4 |
//|                                       Created with EABuilder.com |
//|                                        https://www.eabuilder.com |
//+------------------------------------------------------------------+
#property copyright "Created with EABuilder.com"
#property link      "https://www.eabuilder.com"
#property version   "1.00"
#property description ""

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 1

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

//--- indicator buffers
double Buffer1[];

double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | Swingarm ATR @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(1);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexArrow(0, 241);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      if(#———————————–
      input trailType = {default modified, unmodified};
      input ATRPeriod = 28;
      input ATRFactor = 5;
      input firstTrade = {default long, short};
      input averageType = AverageType.WILDERS;
      
      input fib1Level = 61.8;
      input fib2Level = 78.6;
      input fib3Level = 88.6;
      
      Assert(ATRFactor > 0, “‘atr factor’ must be positive: ” + ATRFactor);
      
      def HiLo = Min(high – low, 1.5 * Average(high – low, ATRPeriod));
      def HRef = if low <= high[1]
      then high – close[1]
      else (high – close[1]) – 0.5 * (low – high[1]);
      def LRef = if high >= low[1]
      then close[1] – low
      else (close[1] – low) – 0.5 * (low[1] – high);
      
      def trueRange;
      switch (trailType) {
      case modified:
      trueRange = Max(HiLo, Max(HRef, LRef));
      case unmodified:
      trueRange = TrueRange(high, close, low);
      }
      def loss = ATRFactor * MovingAverage(averageType, trueRange, ATRPeriod);
      
      def state = {default init, long, short};
      def trail;
      switch (state[1]) {
      case init:
      if (!IsNaN(loss)) {
      switch (firstTrade) {
      case long:
      state = state.long;
      trail = close – loss;
      case short:
      state = state.short;
      trail = close + loss;
      }
      } else {
      state = state.init;
      trail = Double.NaN;
      }
      case long:
      if (close > trail[1]) {
      state = state.long;
      trail = Max(trail[1], close – loss);
      } else {
      state = state.short;
      trail = close + loss;
      }
      case short:
      if (close < trail[1]) {
      state = state.short;
      trail = Min(trail[1], close + loss);
      } else {
      state = state.long;
      trail = close – loss;
      }
      }
      
      def BuySignal = Crosses(state == state.long, 0, CrossingDirection.ABOVE);
      def SellSignal = Crosses(state == state.short, 0, CrossingDirection.ABOVE);
      
      def ex = if BuySignal then high else if SellSignal then low else if state == state.long then Max(ex[1], high) else if state == state.short then Min(ex[1], low) else ex[1];
      
      plot TrailingStop = trail;
      
      TrailingStop.SetPaintingStrategy(PaintingStrategy.POINTS);
      TrailingStop.DefineColor(“Long”, Color.GREEN);
      TrailingStop.DefineColor(“Short”, Color.RED);
      TrailingStop.AssignValueColor(if state == state.long
      then TrailingStop.Color(“Long”)
      else Tra //Custom Code
      )
        {
         Buffer1[i] = Low[i]; //Set indicator value at Candlestick Low
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
// -------------------------------------------------------------------------------
//							Average True Range (ATR)
// based on indicator 'True Range' and overlayed by simple Moving Average (SMA)
// -------------------------------------------------------------------------------

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{	
    [Indicator("Average True Range", IsOverlay = false, ScalePrecision = 5, AccessRights = AccessRights.None)]
    public class AverageTrueRange : Indicator
    {
    	
        [Parameter(DefaultValue = 14)]
        public int Periods { get; set; }

        [Output("Average True Range", Color = Colors.Blue)]
        public IndicatorDataSeries ATR { get; set; }
		
		private IndicatorDataSeries tr;		
		private TrueRange tri;
		private MovingAverage TRMA;
		
        protected override void Initialize()
        {
            // Initialize and create nested indicators
            tr = CreateDataSeries();
            tri = Indicators.TrueRange();
            TRMA = Indicators.MovingAverage(tr,Periods, MovingAverageType.Simple);            
                        
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index            
            tr[index] = tri.Result[index];            
            ATR[index] = TRMA.Result[index];        
            
        }
    }
}
Comments

mfejza - January 15, 2023 @ 00:09

get SwingArm ATR Trend Indicator: https://ctrader.com/algos/indicators/show/3229

0