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 ) == DataSetSymbols0 ) { 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 ( Closebar < Closebar-20 ) SellAtMarket( bar+1, LastPosition ); } else { if ( Closebar > Closebar-20 ) if( BuyAtMarket( bar+1 ) != null ) LastPosition.Priority = Closebar; } } } } }
// 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 ) == DataSetSymbols0 ) // 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 ) == DataSetSymbols0 ) { 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 (globalEquitybar > newHigh) newHigh = globalEquitybar; if (globalEquitybar < newHigh) drawdownPct = (100 * (globalEquitybar / newHigh - 1)); DrawdownSeriesPctbar = 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 ) == DataSetSymbols0 ) { 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.Datebar ) & ( p.Bars.Symbol == Bars.Symbol )) { #region User-configurable: Interacting with Portfolio Equity /* Your equity interaction rule goes here: */ if( globalEquitybar > globalEquitybar-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 = ActivePositionspos; 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 ( Closebar < Closebar-20 ) SellAtMarket( bar+1, LastPosition ); } else { if ( Closebar > Closebar-20 ) if( BuyAtMarket( bar+1 ) != null ) LastPosition.Priority = Closebar; } } } } #endregion User-configurable: your "Donor" strategy }