#include "sierrachart.h"

SCDLLName("DelDivatBar1") 

SCSFExport scsf_my_DelDivatBar1(SCStudyGraphRef sc)
{
    
	 SCSubgraphRef TopSignal = sc.Subgraph[1];
	 SCSubgraphRef BottomSignal = sc.Subgraph[2];
	 
	  
	  SCInputRef SoundAlert = sc.Input[1];
	  SCInputRef BarsBack = sc.Input[2];
   
	// Section 1 - Set the configuration variables and defaults
	
	// This section only runs once upon adding an instance of the study to a chart, and when a chartbook is opened and an instance of this study is on a chart in a chartbook.  It is run once for each instance in this case.
	if (sc.SetDefaults)  
	{
		sc.GraphName = "DelDivatBar1";
		
		sc.StudyDescription = "DelDivatBar1";
		
		sc.AutoLoop = 1;  // true
		sc.FreeDLL = 1;
		
		
		sc.GraphRegion = 0;
		
		TopSignal.Name =  "TopSignal";
		BottomSignal.Name = "BottomSignal";
		
		TopSignal.DrawStyle = DRAWSTYLE_ARROWDOWN;
		BottomSignal.DrawStyle = DRAWSTYLE_ARROWUP;
		
		
		
		SoundAlert.Name = "SoundAlert";
		SoundAlert.SetInt(0);
		SoundAlert.SetIntLimits(0, 1);
		SoundAlert.SetDescription("SoundAlert");
		
		BarsBack.Name = "Bars_Back_Filter";
		BarsBack.SetInt(5);
		
	
		
				
		// Must return before doing any data processing if sc.SetDefaults is set
		return;
	}
	
	
	// Section 2 - Do data processing
	// Everything below here is run on every chart update or when the chart is
	// reloaded.
	
 	
	int Ask = sc.AskVolume[sc.Index-1];
	int Bid = sc.BidVolume[sc.Index-1];  	
	int Delta = Ask-Bid;
	
	float BarOC = sc.Close[sc.Index-1]-sc.Open[sc.Index-1];
	int& TopCondition = sc.PersistVars->i1;
	int& BotCondition = sc.PersistVars->i2;
	TopCondition = 0; BotCondition = 0;
    
	
	int sound_alert = SoundAlert.GetInt();
	int Brsbck = BarsBack.GetInt();
	
	if( BarOC > 0 && Delta < 0)
	  {
	   for (int i = 1; i < Brsbck+1; i++)
	       {
		    if(sc.High[sc.Index-1]<sc.High[sc.Index-i-1]){TopCondition = 1; break;}
		   }
	  if(TopCondition == 0)
	  {
	  TopSignal[sc.Index-1] =  sc.High[sc.Index-1]+sc.TickSize;
	  if(sound_alert == 1)sc.SetAlert(7);
	  }
	  }
	  
	if(BarOC < 0 && Delta > 0)
	  {
	   for (int i = 1; i < Brsbck+1; i++)
	       {
		    if(sc.Low[sc.Index-1]>sc.Low[sc.Index-i-1]){BotCondition = 1; break;}
		   }
	  if(BotCondition == 0)
	  {	   
	   BottomSignal[sc.Index-1] = sc.Low[sc.Index-1]-sc.TickSize;
	    if(sound_alert == 1)sc.SetAlert(8);
	  }
	  }
	  
	}