//------------------------------------------------------------------ #property copyright "Ionone 2021" #property version "1.00" #property description "Elder-ray index" //------------------------------------------------------------------ #property indicator_separate_window #property indicator_buffers 4 #property indicator_plots 1 #property indicator_label1 "Elder Index" #property indicator_type1 DRAW_COLOR_LINE #property indicator_color1 clrBlue,clrRed,clrGreen #property indicator_width1 2 #property indicator_style1 STYLE_SOLID // //--- input parameters // input bool showbull = false; input bool Trend = true; input int PeriodEMA = 13; // //--- indicator buffers // double bullishPower[],bearishPower[]; double val[],valc[]; int handleEMA; //------------------------------------------------------------------ // Custom indicator initialization function //------------------------------------------------------------------ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,val,INDICATOR_DATA); SetIndexBuffer(1,valc,INDICATOR_COLOR_INDEX); SetIndexBuffer(2,bullishPower,INDICATOR_CALCULATIONS); SetIndexBuffer(3,bearishPower,INDICATOR_CALCULATIONS); //--- indicator short name assignment IndicatorSetString(INDICATOR_SHORTNAME,"Elder-ray Index 01"); handleEMA = iMA(NULL,0,PeriodEMA,00,MODE_EMA,PRICE_CLOSE); return (INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator de-initialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ double GetIndi(int i, int rates_total, int bufnum, int handle) { double Buffer[1]; Buffer[0] = 0; int retries = 5; while (retries >= 0 && rates_total-1-i >= 0 && rates_total-1-i < rates_total && CopyBuffer(handle,bufnum,rates_total-1-i,1,Buffer) <= 0) { retries--; } return Buffer[0]; } /*study("Elder-ray Index") //1. input for show bullish power/bearish power showbull = input(title="show bullish(checked)", type=input.bool, defval=false) Trend = input(title="trend for long term(long)", type=input.bool, defval=true) //2. Get 13 EMA line_ema13 = ema(close, 13) //3. Get bullish power/bearish power bullishpower = high - line_ema13 bearishpower = low - line_ema13 //4. Get the slope for bullish power/bearish power slopebullishp = bullishpower - bullishpower[1] slopebearishp = bearishpower - bearishpower[1] // 5. indicator rule // if showbull is true, show bullish power, if showbull is false, show bearish power // if Trend is bull, buy when bearish power is getting back from the lowest but still under zero // if Trend is bear, short when bullish power is going down from the highest but still up zero // now only draw line but not histgram // condition to buy during Trend is bull Longcondition = (Trend and (bearishpower < 0) and (slopebearishp > 0)) // condition to buy during Trend is bear Shortcondition = ((not Trend) and (bullishpower > 0) and (slopebullishp < 0)) //mark long conditio with red, and short condition with green plot(showbull?bullishpower:na, title="bullish power", color=Shortcondition?color.green:color.blue, linewidth=2) plot(showbull?na:bearishpower, title="bearish power", color=Longcondition?color.red:color.blue, linewidth=2)*/ 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 i=(prev_calculated>0?prev_calculated-1:0); for (; i 0) slopebullishp = bullishPower[i] - bullishPower[i-1]; double slopebearishp; if (i > 0) slopebearishp = bearishPower[i] - bearishPower[i-1] ; bool Longcondition = (Trend && (bearishPower[i] < 0) && (slopebearishp > 0)); bool Shortcondition = ((!Trend) && (bullishPower[i] > 0) && (slopebullishp < 0)) ; /* plot(showbull?bullishpower:na, title="bullish power", color=Shortcondition?color.green:color.blue, linewidth=2) plot(showbull?na:bearishpower, title="bearish power", color=Longcondition?color.red:color.blue, linewidth=2)*/ if (showbull) { val[i] = bullishPower[i]; if (Shortcondition) valc[i] = 2; else valc[i] = 0; } else { val[i] = bearishPower[i]; if (Longcondition) valc[i] = 1; else valc[i] = 0; } } return(rates_total); } //+------------------------------------------------------------------+ //| Custom functions | //+------------------------------------------------------------------+