using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using TASCIndicators;namespace WealthLab.Strategies { /* ARSI divergence: Price sets a lowest low but the indicator fails to confirm the new low and turns up */ public class ARSI_Divergence : WealthScript { private StrategyParameter paramHighest; private StrategyParameter paramPeriod; private StrategyParameter paramExitDays; public ARSI_Divergence() { paramPeriod = CreateParameter("ARSI period", 14, 1, 100, 1); paramHighest = CreateParameter("Highest high of", 20, 5, 50, 1); paramExitDays = CreateParameter("Exit after", 20, 1, 50, 1); } protected override void Execute() { bool trough = false; int troughBar = -1; int low = paramHighest.ValueInt; int period = paramPeriod.ValueInt; int days = paramExitDays.ValueInt; ARSI arsi = ARSI.Series( Close, period ); Lowest indicatorLowest = Lowest.Series( arsi, low ); Lowest hLow = Lowest.Series( Low, low ); HideVolume(); LineStyle solid = LineStyle.Solid; ChartPane arsiPane = CreatePane( 50, false, true ); PlotSeries( arsiPane, arsi, Color.Green, solid, 2 ); PlotSeries( arsiPane, RSI.Series( Close, period ), Color.Red, solid, 1 ); for(int bar = 80; bar < Bars.Count; bar++) { if (!IsLastPositionActive) { /* 1st trough: both price and indicator */ if( trough == false ) { if( ( Low[bar-1] == Lowest.Series( Low, low )[bar-1] ) & ( arsi[bar-1] == Lowest.Series( arsi, low )[bar-1] ) & TurnUp( bar, Low ) & TurnUp( bar, arsi ) ) { trough = true; troughBar = bar-1; } } if( trough == true ) { if( ( Low[bar] != Lowest.Series( Low, low )[bar] ) & ( arsi[bar] == Lowest.Series( arsi, low )[bar] ) ) trough = false; } /* 2nd trough: price low not confirmed by the indicator */ if( trough == true ) { if( ( Low[bar-1] == Lowest.Series( Low, low )[bar-1] ) & ( Low[bar-1] <= Low[troughBar] ) & ( arsi[bar-1] != Lowest.Series( arsi, low )[bar-1] ) & ( arsi[bar-1] > arsi[troughBar] ) & TurnUp( bar, Low ) & TurnUp( bar, arsi ) ) { trough = false; BuyAtMarket( bar+1 ); /* Highlight divergence */ for (int b = troughBar; b <= bar; b++) SetPaneBackgroundColor( arsiPane, b, Color.FromArgb( 30, Color.LightGreen ) ); DrawLine( PricePane, troughBar, Low[troughBar], bar-1, Low[bar-1], Color.Blue, solid, 2 ); DrawLine( arsiPane, troughBar, arsi[troughBar], bar-1, arsi[bar-1], Color.Red, solid, 2 ); } } } else { /* Exit after N days */ Position p = LastPosition; if ( bar+1 - p.EntryBar >= days ) SellAtMarket( bar+1, p, "Timed" ); } } } } }