System

The System category contains various miscellaneous methods that apply to the overall Wealth-Lab system.

BuyAtMarket

Abort

void Abort();

Causes the Strategy to immediately cease execution.


Example

protected override void Execute(){
    if ( Bars.Count < 1000 )
    {
        Abort();
    }
}
WealthScript

ClearDebug

void ClearDebug();

Clears all Debug Window messages.

Remarks


Example

protected override void Execute(){
    for(int i = 0; i < 1; i++)
    {
        PrintDebug( "Test string" );
    }
    ClearDebug();
}
BuyAtMarket

ClearGloblals

void ClearGlobals();

Completely clears any objects that were stored in the Global Object Pool (GOP) via calls to SetGloblal.

Remarks


Example

protected override void Execute(){
    // Run example for SetGlobal first
    DataSeries average = (DataSeries) GetGlobal("average");
    if( average.Count > 0 )
        PrintDebug( GetGlobal("average").ToString() + " found in GOP; BarCount = " + average.Count.ToString() );
    ClearGlobals();
    if ( GetGlobal("average").ToString() == "" )
        PrintDebug ( "GOP was cleared" ); // null
}
    
BuyAtMarket

CreateParameter Method

StrategyParameter CreateParameter(string name, double value, double start, double stop, double step);

Used in a Strategy class constructor to create a StrategyParameter type. The specified name appears next to the slider in the Data Panel to identify the parameter, value is the initial default value for the Strategy Parameter, and step controls the increments between the start and stop minimum and maximum bounds of the parameter.

Remarks

  • Strategy Parameters are optional.
  • Details about incorporating Strategy Parameters can be found in the WealthScript Language Guide.
  • Known issue: Non-white space character cannot be typed if included in CreateParameter after ampersand. After including an ampersand as part of CreateParameter description (e.g. "L&S"), you will not be able to type the character after the ampserand (i.e. "S"), whitespace excluded, in that Strategy Window after compiling.
    • Workaround: Don't use an ampersand for the parameter's string name. If you must, just leave a space after it.

Example

protected override void Execute(){
    /*     See pre-built Strategies such as the "Glitch Index" 
        and "Moving Average Crossover", or the ShortAtClose example 
        in the QuickRef.*/
}
BuyAtMarket

FlushDebug

void FlushDebug(string message);

Forces any debug messages that have been generated during the Strategy execution (by calling PrintDebug) to be displayed in the Debug Window immediately.  Normally, all debug messages are displayed after the Strategy completes its execution.


Example

protected override void Execute(){
    PrintDebug( "Now You See Him" );
    FlushDebug( );
    System.Windows.Forms.MessageBox.Show( "Try commenting FlushDebug", "Message from WL5");
    // The debug string will not be seen until the messagebox is closed
}
BuyAtMarket

GetChartBitmap

Bitmap GetChartBitmap(int width, int height);

Renders an image of the chart, including plotted indicators and manually drawn objects, as a Bitmap of the specified width and height.  Use Bitmap.Save method to save the image to a file of a particular image type.


Example

protected override void Execute(){
    // Captures screen image in a PNG file under the WL installation directory
    Bitmap bm = GetChartBitmap( 500, 300 );
    bm.Save( Bars.Symbol + ".png", System.Drawing.Imaging.ImageFormat.Png );
    //For System.Drawing.Bitmap details, refer to MSDN:
    //http://msdn2.microsoft.com/en-us/library/system.drawing.bitmap_members.aspx
}
BuyAtMarket

GetGlobal

object GetGlobal(string key);

The Global Object Pool (GOP) is a global storage area that Strategies can place objects into (SetGlobal) and at some point in the future read objects from (GetGlobal).  Objects remain in the GOP throughout the lifetime of the Wealth-Lab application, and can be shared among Strategies that operate in any context (Strategy Window, Strategy Explorer, etc.)

Each object in the GOP has a unique string key associated with it.  GetGlobal returns the object in the GOP with the specified key.  If the object was not found, the method returns null.

Remarks

  • You will need to cast the resulting object to the type you are expecting before being able to work with it.

Example

protected override void Execute(){
    // You should run the SetGlobal example before executing this
    // Getting entire series from the global storage is also convenient
    // But first we cast the object into DataSeries
    DataSeries average = (DataSeries) GetGlobal("average");
    ChartPane averagePane = CreatePane( 75, true, false );
    PlotSeries( averagePane, average, Color.Black, WealthLab.LineStyle.Solid, 1 );
}
BuyAtMarket

GetTradingLoopStartBar Property

int GetTradingLoopStartBar( int startBar )

Returns the larger of two parameters:

  • the passed startBar value, or
  • the largest value of the StrategyParameters that have "period" in their Name property.

Especially when optimizing Strategies that use indicators with multiple periods, employ GetTradingLoopStartBar as the initial bar index for the trading loop to prevent runtime errors in the script or creating trades before all indicators are valid.


Example

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;

namespace WealthLab.Strategies
{
    public class ChannelBreakoutExample : WealthScript
    {
        private StrategyParameter p1;
        private StrategyParameter p2;

        public ChannelBreakoutExample()
        {
            p1 = CreateParameter("Period High",20,2,200,20);
            p2 = CreateParameter("Period Low",40,2,200,20);
        }

        protected override void Execute()
        {
            Highest h = Highest.Series(High, p1.ValueInt);
            Lowest l = Lowest.Series(Low, p2.ValueInt);
            
            PlotSeries(PricePane, h >> 1, Color.Red, LineStyle.Solid, 1);
            PlotSeries(PricePane, l >> 1, Color.Green, LineStyle.Solid, 1);             

            for(int bar = GetTradingLoopStartBar(1); bar < Bars.Count; bar++)
            {
                if (IsLastPositionActive)
                {
                    SellAtStop(bar + 1, LastPosition, Lowest.Series(Low, p2.ValueInt)[bar]);
                }
                else
                {
                    BuyAtStop(bar + 1, Highest.Series(High, p1.ValueInt)[bar]);
                }
            }
        }
    }
} 
BuyAtMarket

IsStreaming Property

bool IsStreaming

Returns a bool value indicating whether the Strategy is executing on a streaming data source or a static data source.  Wealth-Lab executes Strategies on streaming data sources each time a new bar of data is completely formed for the current chart time scale.


Example

protected override void Execute(){
    // For example, use IsStreaming to disable IsLastBarOfDay logic

    for(int bar = 20; bar < Bars.Count; bar++)
    {
        bool LastBar = Bars.IsLastBarOfDay(bar);
        
        if (IsLastPositionActive)
        {
            if ( LastBar & !IsStreaming )
            {
                SellAtClose( bar, LastPosition, "EOD" );
            }
        }
        else
        {
            // plain vanilla entry rule
            BuyAtStop( bar+1, Highest.Series( High, 20 )[bar] );
        }
    }
}
BuyAtMarket

PrintDebug

void PrintDebug(string message);
void PrintDebug(object message);
void PrintDebug(Object[] messages);

Prints the string specified by message to the application Debug Window, and displays the Debug Window if it is currently not visible.  For performance reasons, Wealth-Lab caches all of the printed debug strings internally and finally displays them in the Debug Window after the Strategy finishes executing.  To force the debug messages to appear during a Strategy execution, call FlushDebug.


Example

protected override void Execute(){
    for(int bar = 60; bar < Bars.Count; bar++)
    {
        // Print the bars where there were SMA crossovers
        if ( CrossOver( bar, SMA.Series( Close, 20 ), SMA.Series( Close, 60 ) ) )
        {
            PrintDebug( bar );
        }
    }
}
BuyAtMarket

PrintStatusBar

void PrintStatusBar(string message);

Displays the string specified in message to the main status bar.  Caution: printing too many times to the status bar, for example printing during each bar of data in the Strategy main loop, can result in a significant slow down of your Strategy execution speed.


Example

protected override void Execute(){
//Execution progress in status bar

    for(int bar = 20; bar < Bars.Count; bar++)
        PrintStatusBar("Processing " + ( bar * 100 / Bars.Count ) + "% complete");
}
BuyAtMarket

RemoveGlobal

void RemoveGlobal(string key);
void RemoveGlobal(object value);

Removes an object from the Global Object Pool (GOP) by either key or value.

Remarks

  • See GetGlobal and SetGlobal for a description of the Global Object Pool.

Example

protected override void Execute(){
    // Run example for SetGlobal first
    PrintDebug ( GetGlobal("average").ToString() );
    RemoveGlobal( "average" );
    PrintDebug ( GetGlobal("average").ToString() ); // null
}
BuyAtMarket

SetGlobal

void SetGlobal(string key, object value);

The Global Object Pool (GOP) is a global storage area that Strategies can place objects into (SetGlobal) and at some point in the future read object from (GetGlobal).  Objects remain in the GOP throughout the lifetime of the Wealth-Lab application, and can be shared among Strategies that operate in any context (Strategy Window, Strategy Explorer, etc.)

Each object in the GOP has a unique string key associated with it.  SetGlobal places an object (value) into the GOP, using the specified key.  This will overwrite any existing object that was placed using the same key.


Example

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;

namespace WealthLab.Strategies
{
    public class MyStrategy : WealthScript
    {
        // Put series in the global storage
        void SetGlobalSeries( string sName, DataSeries series )
        {
            SetGlobal( sName, series );
        }

        protected override void Execute()
        {
            SetGlobalSeries( "average", ((High + Low)/2) );
        }
    }
}
BuyAtMarket

StrategyName Property

string StrategyName

Returns the name of the Strategy that is currently being executed.


Example

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;

namespace WealthLab.Strategies
{
    public class AlertsToFile : WealthScript
    {
        // Collects generated alerts and writes them into file
        void WriteAlerts()
        {
            StreamWriter alertFile;
            string str;
            
            if( Alerts.Count > 0 )
            {
                // Open output file
                alertFile = new StreamWriter( "Alerts.txt", true );
                // Strategy Name
                alertFile.Write( "Alert for strategy name: " + StrategyName + "\r\n" );

                for( int i = 0; i < Alerts.Count; i++ )
                {
                    WealthLab.Alert a = Alerts[i];
                    
                    str = ( "AlertDate: " + a.AlertDate ) + "\r\n" +
                        ( "AlertType: " + a.AlertType ) + "\r\n" +
                        ( "OrderType: " + a.OrderType ) + "\r\n" +
                        ( "PositionType: " + a.PositionType ) + "\r\n" +
                        ( "Price: " + a.Price ) + "\r\n" +
                        ( "Symbol: " + a.Symbol ) + "\r\n" +
                        ( "Shares: " + a.Shares ) + "\r\n" +
                        ( "SignalName: " + a.SignalName ) + "\r\n" + "\r\n";

                    // Creates the file containing alerts under WLP installation folder
                    alertFile.Write( str );
                }
                alertFile.Close();
            }                    
        }
        
        protected override void Execute()
        {
            for(int bar = 40; bar < Bars.Count; bar++)
            {
                if (IsLastPositionActive)
                    SellAtStop( bar+1, LastPosition, Lowest.Series( Bars.Low, 20 )[bar], "Exit" );
                else
                    BuyAtStop( bar+1, Highest.Series( Bars.High, 40 )[bar], "Entry" );
            }
            
            WriteAlerts();
        }
    }
}