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( ( Lowbar-1 == Lowest.Series( Low, low )bar-1 ) & ( arsibar-1 == Lowest.Series( arsi, low )bar-1 ) & TurnUp( bar, Low ) & TurnUp( bar, arsi ) ) { trough = true; troughBar = bar-1; } } if( trough == true ) { if( ( Lowbar != Lowest.Series( Low, low )bar ) & ( arsibar == Lowest.Series( arsi, low )bar ) ) trough = false; } /* 2nd trough: price low not confirmed by the indicator */ if( trough == true ) { if( ( Lowbar-1 == Lowest.Series( Low, low )bar-1 ) & ( Lowbar-1 <= LowtroughBar ) & ( arsibar-1 != Lowest.Series( arsi, low )bar-1 ) & ( arsibar-1 > arsitroughBar ) & 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, LowtroughBar, bar-1, Lowbar-1, Color.Blue, solid, 2 ); DrawLine( arsiPane, troughBar, arsitroughBar, bar-1, arsibar-1, Color.Red, solid, 2 ); } } } else { /* Exit after N days */ Position p = LastPosition; if ( bar+1 - p.EntryBar >= days ) SellAtMarket( bar+1, p, "Timed" ); } } } } }