using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class TradingTheAussie : WealthScript { private DataSeries SecBol( DataSeries ds, int period ) { DataSeries sd = 2 * StdDev.Series(ds, period, StdDevCalculation.Population); return 1 + ( ( ds - SMA.Series(ds, period) + sd ) / ( sd + sd + 0.0001 ) ); } protected override void Execute() { string sampleSym = "YBA"; // out-of-sample; use "YTC" for in-sample const double pct = 0.007; int per = 30; bool synch = false; DataSeries xau = GetExternalSymbol("XAU", synch).Close; //sec2 DataSeries crb = GetExternalSymbol("CRB", synch).Close; //sec3 DataSeries yba = 100 - GetExternalSymbol(sampleSym, synch).Close; //sec5 DataSeries ej = GetExternalSymbol("EURJPY", synch).Close; //sec6 // initialize the initial values to account that yba data starts at 4/23/2008 int stBar = DateTimeToBar(new DateTime(2003, 4, 23), false); for(int bar = 0; bar < stBar; bar++) ybabar = ybastBar; // Based calculations from raw data and then synchronize DataSeries ybaSMA = Synchronize( SMA.Series(yba, 40) ); DataSeries xauROC = Synchronize( ROC.Series(xau, 1) ); DataSeries crbROC = Synchronize( ROC.Series(crb, 1) ); DataSeries ejROC = Synchronize( ROC.Series(ej, 2) ); ej = Synchronize( ej ); yba = Synchronize( yba ); DataSeries sec1BOL = SecBol( Close, per ); DataSeries sec2BOL = Synchronize( SecBol( xau, per ) ); DataSeries sec3BOL = Synchronize( SecBol( crb, per ) ); DataSeries DIV2 = 100 * (sec2BOL - sec1BOL)/sec1BOL; DataSeries DIV3 = 100 * (sec3BOL - sec1BOL)/sec1BOL; DataSeries DIV1 = DIV2 * 1d; DataSeries DIV1m = DIV3 * 1d; DIV1.Description = "DIV1(max)"; DIV1m.Description = "DIV1(min)"; for(int bar = 0; bar < Bars.Count; bar++) { DIV1bar = Math.Max( DIV3bar, DIV2bar ); DIV1mbar = Math.Min( DIV3bar, DIV2bar ); } DataSeries macd = MACD.Series(Close); DataSeries ema = EMA.Series(macd, 9, EMACalculation.Modern); DataSeries HH4 = Highest.Series(High, 4); DataSeries LL4 = Lowest.Series(Low, 4); /* Plotting */ ChartPane macdPane = CreatePane( 40, true, true ); ChartPane divPane = CreatePane( 40, true, true ); ChartPane yldPane = CreatePane( 40, true, true ); PlotSeries(yldPane, yba, Color.Fuchsia, LineStyle.Solid, 1); PlotSeries(yldPane, ybaSMA, Color.Black, LineStyle.Solid, 1); PlotSeries(macdPane, macd - ema, Color.Black, LineStyle.Histogram, 1); PlotSeries(macdPane, macd, Color.Red, LineStyle.Solid, 1); PlotSeries(macdPane, ema, Color.Green, LineStyle.Solid, 1); PlotSeries(divPane, DIV1, Color.Red, LineStyle.Solid, 1); PlotSeries(divPane, DIV1m, Color.Gray, LineStyle.Solid, 1); PlotSeries(PricePane, SMA.Series(Close, 50), Color.Blue, LineStyle.Solid, 1); PlotSeries(PricePane, SMA.Series(Close, 15), Color.Black, LineStyle.Solid, 1); HideVolume(); for(int bar = 100; bar < Bars.Count; bar++) { double C = Closebar; double C1 = Closebar - 1; // Buy conditions - broken down to verify which is the triggering condition bool buy1 = Highest.Series(DIV1, 3)bar > 10 && DIV1bar < DIV1bar - 1 && ROC.Series(Close, 2)bar > 0 && C > (1 + pct) * LL4bar && ybaSMAbar > ybaSMAbar - 1; bool buy2 = DIV1bar > 40 && DIV2bar + DIV3bar > 80 && DIV1bar < DIV1bar - 1 && C > C1 && C > (1 + pct) * LL4bar; bool buy3 = CrossOver(bar, SMA.Series(Close, 15), SMA.Series(Close, 50)) && ybaSMAbar > ybaSMAbar - 1; // Short Conditions bool sht1 = Lowest.Series(DIV1, 3)bar < -10 && DIV1bar > DIV1bar - 1 && ROC.Series(Close, 2)bar < 0 && C < (1 - pct) * HH4bar && ybaSMAbar < ybaSMAbar - 1; bool sht2 = DIV1bar < -20 && DIV2bar + DIV3bar < -40 && DIV1bar > DIV1bar - 1 && C < C1 && C < (1 - pct) * HH4bar; bool sht3 = CrossOver(bar, SMA.Series(Close, 50), SMA.Series(Close, 15)) && ybaSMAbar < ybaSMAbar - 1; // Sell Conditions bool s1 = CrossOver(bar, ema, macd) && Highest.Series(macd, 5)bar > Highest.Series(macd, 50)bar - 5; bool s2 = DIV1mbar < -30 && ( xauROCbar < -0.5 || crbROCbar < -0.5 ) && C < (1 - pct) * HH4bar; bool s3 = DIV1mbar < 0 && ejROCbar < -1 && C < (1 - pct) * HH4bar && SMA.Series(ej, 40)bar < SMA.Series(ej, 40)bar - 1; // Cover Conditions bool c1 = CrossOver(bar, macd, ema) && Lowest.Series(macd, 5)bar < Lowest.Series(macd, 50)bar - 5; bool c2 = DIV1bar > 30 && ( xauROCbar > 0.5 || crbROCbar > 0.5 ) && C > (1 + pct) * HH4bar; // should it be LL4 as in Buy? if( IsLastPositionActive ) { Position p = LastPosition; if( p.PositionType == PositionType.Long ) { if ( sht1 || sht2 || sht3 ) { SellAtClose(bar, p, "R:" + sht1 + "," + sht2 + "," + sht3 ); ShortAtClose(bar, "reverse"); } else if ( s1 || s2 || s3 ) SellAtClose(bar, p, "X:" + s1 + "," + s2 + "," + s3); } else if ( buy1 || buy2 || buy3 ) { CoverAtClose(bar, p, "R:" + buy1 + "," + buy2 + "," + buy3 ); BuyAtClose(bar, "reverse"); } else if ( c1 || c2 ) CoverAtClose(bar, p, "X:" + c1 + "," + c2 ); } else if( buy1 || buy2 || buy3 ) BuyAtClose(bar, buy1 + "," + buy2 + "," + buy3 ); else if ( sht1 || sht2 || sht3 ) ShortAtClose(bar, sht1 + "," + sht2 + "," + sht3); } } } }