using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using Community.Components;namespace WealthLab.Strategies { /* Show portfolio equity */ public class OnTheFly : WealthScript { protected override void Execute() { Utility u = new Utility(this); string firstRun = ""; string results = ""; SetGlobal( firstRun, Bars.Symbol ); if( (string)GetGlobal( firstRun ) == DataSetSymbols[0] ) { RemoveGlobal( results + "_PerformanceResults" ); /* Option 1: run a strategy on-the-fly (see the "Donor" class below) */ SystemPerformance sp = u.runDonor( new Donor() ); SetGlobal( results + "_PerformanceResults", sp.Results ); } /* Retrieve and plot the portfolio equity from Wealth-Lab's global memory */ SystemResults sr = (SystemResults)GetGlobal( results + "_PerformanceResults" ); DataSeries globalEquity = sr.EquityCurve; globalEquity.Description = "Donor system Portfolio Equity"; globalEquity = Synchronize( globalEquity ); ChartPane gEqPane = CreatePane( 40, false, true ); PlotSeries( gEqPane, globalEquity, Color.DarkGreen, LineStyle.Histogram, 2 ); HideVolume(); } } // Your optional "donor" strategy public class Donor : WealthScript { protected override void Execute() { for(int bar = 21; bar < Bars.Count; bar++) { if (IsLastPositionActive) { if ( Close[bar] < Close[bar-20] ) SellAtMarket( bar+1, LastPosition ); } else { if ( Close[bar] > Close[bar-20] ) if( BuyAtMarket( bar+1 ) != null ) LastPosition.Priority = Close[bar]; } } } } }
// Invoke an instance of the helper class from Community.Components, passing it a WealthScript object as "this" Utility u = new Utility(this);// Use this trick to execute some code only once; see this Knowledge Base if( (string)GetGlobal( firstRun ) == DataSetSymbols[0] ) // Make Wealth-Lab run your strategy on-the-fly SystemPerformance sp = u.runDonor( new Donor() ); // Store in global memory SetGlobal( results + "_PerformanceResults", sp.Results );// Retrieve and plot the portfolio equity from Wealth-Lab's global memory SystemResults sr = (SystemResults)GetGlobal( results + "_PerformanceResults" ); DataSeries globalEquity = sr.EquityCurve;
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using Community.Components;namespace WealthLab.Strategies { /* Show portfolio equity */ public class FromDiskFile : WealthScript { protected override void Execute() { /* Specify a Strategy: */ string myStrategyName = "Neo Master"; Utility u = new Utility(this); string firstRun = ""; string results = ""; SetGlobal( firstRun, Bars.Symbol ); if( (string)GetGlobal( firstRun ) == DataSetSymbols[0] ) { RemoveGlobal( results + "_PerformanceResults" ); /* Option 2: Load an existing Strategy from its XML file on your disk */ WealthScript myStrategy = u.LoadStrategyFromDisk( myStrategyName ); SystemPerformance sp = u.runDonor( myStrategy ); SetGlobal( results + "_PerformanceResults", sp.Results ); } /* Retrieve and plot the portfolio equity from Wealth-Lab's global memory */ SystemResults sr = (SystemResults)GetGlobal( results + "_PerformanceResults" ); DataSeries globalEquity = sr.EquityCurve; globalEquity.Description = myStrategyName + " Portfolio Equity"; globalEquity = Synchronize( globalEquity ); ChartPane gEqPane = CreatePane( 40, false, true ); PlotSeries( gEqPane, globalEquity, Color.DarkGreen, LineStyle.Histogram, 2 ); /* Build drawdown series */ double drawdownPct = 0, newHigh = 0; var DrawdownSeriesPct = new DataSeries(Bars,"Drawdown %"); for(int bar = 0; bar < Bars.Count; bar++) { if (globalEquity[bar] > newHigh) newHigh = globalEquity[bar]; if (globalEquity[bar] < newHigh) drawdownPct = (100 * (globalEquity[bar] / newHigh - 1)); DrawdownSeriesPct[bar] = drawdownPct; } ChartPane gDDPane = CreatePane( 40, false, true ); PlotSeries( gDDPane, DrawdownSeriesPct, Color.Red, LineStyle.Histogram, 2 ); HideVolume(); } } }
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using Community.Components;namespace WealthLab.Strategies { /* Interacting with portfolio level equity */ public class InteractingWithPortfolioEquity : WealthScript { protected override void Execute() { // Create an instance of the Utility class from Community.Components, pass WealthScript as "this" Utility u = new Utility(this); // Save current symbol in Wealth-Lab's global memory string firstRun = ""; string results = ""; SetGlobal( firstRun, Bars.Symbol ); #region User-configurable: specify Strategy name string myStrategyName = "Neo Master"; //string myStrategyName = "Bandwagon Trade"; #endregion User-configurable: specify Strategy name /* Here we kick in: first symbol detected */ if( (string)GetGlobal( firstRun ) == DataSetSymbols[0] ) { RemoveGlobal( results + "_PerformanceResults" ); #region User-configurable: Load Donor strategy // Loading an existing strategy from disk: WealthScript myStrategy = u.LoadStrategyFromDisk( myStrategyName ); SystemPerformance sp = u.runDonor( myStrategy ); // As an alternative, you can load a strategy on-the-fly (see the "Donor" class below): //SystemPerformance sp = u.runDonor( new Donor() ); #endregion User-configurable: Load Donor strategy SetGlobal( results + "_PerformanceResults", sp.Results ); } /* Simulation already performed, now run using Wealth-Lab's global memory */ // Retrieve system results from the GOP and plot portfolio equity SystemResults sr = (SystemResults)GetGlobal( results + "_PerformanceResults" ); DataSeries globalEquity = sr.EquityCurve; globalEquity.Description = "Portfolio Equity"; globalEquity = Synchronize( globalEquity ); ChartPane gEqPane = CreatePane( 40, false, true ); PlotSeries( gEqPane, globalEquity, Color.DarkGreen, LineStyle.Histogram, 2 ); HideVolume(); /* Make sure to start your trading loop on a bar after which the portfolio equity curve produces valid values! */ for(int bar = Bars.FirstActualBar; bar < Bars.Count; bar++) { foreach (Position p in sr.Positions ) { if(( p.EntryDate == Bars.Date[bar] ) & ( p.Bars.Symbol == Bars.Symbol )) { #region User-configurable: Interacting with Portfolio Equity /* Your equity interaction rule goes here: */ if( globalEquity[bar] > globalEquity[bar-100] ) { Position pos = u.entry( p, bar, p.EntrySignal, p.BasisPrice ); if( pos != null ) { pos.Priority = p.Priority; pos.EntrySignal = p.EntrySignal; //pos.Equals(p); } } #endregion User-configurable: Interacting with Portfolio Equity /* Alternative: u.entry( p, bar, p.EntrySignal, p.EntryPrice ); if( LastPosition != null ) LastPosition.Priority = p.Priority;*/ } if( ActivePositions.Count > 0 ) { for( int pos = ActivePositions.Count - 1; pos > -1 ; pos-- ) { Position ap = ActivePositions[pos]; if(( p.ExitBar == bar ) & ( p.Bars.Symbol == Bars.Symbol )) //if( p.EntryBar == ap.EntryBar ) //if( p.EntryPrice == ap.EntryPrice ) if( p.Priority == ap.Priority ) u.exit( p, ap, bar, p.ExitSignal, p.ExitPrice ); } } } } } } #region User-configurable: your "Donor" strategy // This strategy is optional public class Donor : WealthScript { protected override void Execute() { for(int bar = 101; bar < Bars.Count; bar++) { if (IsLastPositionActive) { if ( Close[bar] < Close[bar-20] ) SellAtMarket( bar+1, LastPosition ); } else { if ( Close[bar] > Close[bar-20] ) if( BuyAtMarket( bar+1 ) != null ) LastPosition.Priority = Close[bar]; } } } } #endregion User-configurable: your "Donor" strategy }