Alert Object

The Alert object represents a trade that needs to be placed on the following bar.

Remarks

BuyAtMarket

Account Property

string Account

Returns an Account string, which contains the account of generated alert.

BuyAtMarket

AlertDate Property

DateTime AlertDate

Returns an AlertDate structure, which contains the date of generated alert.


Example

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

namespace WealthLab.Strategies
{
    public class MyStrategy : WealthScript
    {
        // Writes trades into CSV file
        public void AlertsToFile()
        {        
            StreamWriter atf;
            
            string path = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.Personal),
                "Alerts(" + StrategyName + ").csv" );
            
            if( Alerts.Count > 0 )
            { 
                string s = ",";
                atf = new StreamWriter( path, true );
                for( int n = 0; n < Alerts.Count; n++ )
                {
                    WealthLab.Alert a = Alerts[n];
                    atf.Write( a.Bars.Symbol + s 
                        + a.AlertDate.ToShortDateString() + s
                        + a.AlertDate.ToShortTimeString() + s
                        + a.AlertType + s                        
                        + a.Price
                        + "\r\n" );  // etc.
                }
                atf.Close();
            }            
        }
        
        protected override void Execute()
        {
            // Alert generating code - Example
            for(int bar = 3; bar < Bars.Count; bar++)
            {
                if (!IsLastPositionActive)
                {
                    // Two consecutive lower closes
                    if( ( Bars.Close[bar] < Bars.Close[bar-1] ) &
                        ( Bars.Close[bar-1] < Bars.Close[bar-2] ) )
                        BuyAtStop( bar+1, Close[bar]*1.03 );
                }
                if (IsLastPositionActive)
                {
                    SellAtLimit( bar+1, LastPosition, Close[bar]*1.10 );
                }
            }
            AlertsToFile();
        }
    }
}
BuyAtMarket

AlertType Property

TradeType AlertType

Returns a TradeType enumerated list, which contains the type of generated alert. Possible values are:


Example

protected override void Execute(){
    // Alert generating code
    for(int bar = 3; bar < Bars.Count; bar++)
    {
        if (IsLastPositionActive)
            SellAtLimit( bar+1, LastPosition, Close[bar]*1.01 );
        else
            BuyAtLimit( bar+1, Close[bar]*0.97 );
    }

    // Show the alert type
    if( Alerts.Count > 0 )
    {
        for( int i = 0; i < Alerts.Count; i++ )
        {
            WealthLab.Alert a = Alerts[i];
                PrintDebug( "Alert " + ( i+1 ) + " type: " + a.AlertType );
        }
    }
}
BuyAtMarket

BarInterval Property

int BarInterval

Returns the intraday bar interval of the generated alert. For example, BarInterval will return 5 for 5-minute bars.

Remarks


Example

protected override void Execute(){
    // Alert generating code
    for(int bar = 1; bar < Bars.Count; bar++)
    {
        if (!IsLastPositionActive)
        {
                BuyAtMarket( bar+1 );
        }
        if (IsLastPositionActive)
        {
            SellAtMarket( bar+1, LastPosition );
        }
    }
    
    // Returns the bar interval of an alert
    if( Alerts.Count > 0 )
    {
        for( int i = 0; i < Alerts.Count; i++ )
        {
            WealthLab.Alert a = Alerts[i];
            if( a.BarInterval > 0 )
            PrintDebug( "BarInterval: " + a.BarInterval + " tick, second or minute " ); else
            PrintDebug( "BarInterval is Daily or greater" );
        }
    }
}
BuyAtMarket

BasisPrice Property

double BasisPrice

Returns a BasisPrice number, which contains the basis price of generated alert which is going to establish a position.


Example

protected override void Execute(){
    // Alert generating code
    for(int bar = 3; bar < Bars.Count; bar++)
    {
        if (IsLastPositionActive)
            SellAtLimit( bar+1, LastPosition, Close[bar]*1.01 );
         else
            BuyAtLimit( bar+1, Close[bar]*0.97 );
    }
    
    // Show the basis price of an alert
    if( Alerts.Count > 0 )
    { 
        for( int i = 0; i < Alerts.Count; i++ )
        {
            WealthLab.Alert a = Alerts[i];
            if( ( a.AlertType != TradeType.Sell ) & 
                ( a.AlertType != TradeType.Cover ) )
            PrintDebug( "Alert " + ( i+1 ) + " basis price: " + a.BasisPrice );
        }
    }
}
BuyAtMarket

OrderType Property

OrderType OrderType

Returns an OrderType enumerated list, which contains the order type of generated alert. Possible values are:

  • AtClose
  • Limit
  • Market
  • Stop

Example

protected override void Execute(){
    // Alert generating code
    
    Random rnd = new Random();
    
    for(int bar = 1; bar < Bars.Count; bar++)
    {
        if (IsLastPositionActive)
        {
            // Exit trade after 3 days
            if ( bar+1 - LastPosition.EntryBar >= 3 )
                SellAtMarket( bar+1, LastPosition, "Time-based" );
        }
        else
        {
            // Random factor
            if ( rnd.Next(0,10) < 3 )
                BuyAtStop( bar+1, High[bar]*1.03, "Buy strength" ); else
                BuyAtLimit( bar+1, Low[bar]*0.97, "Buy weakness" );
        }
    }
    
    // Show the alert order type
    if( Alerts.Count > 0 )
    { 
        for( int i = 0; i < Alerts.Count; i++ )
        {
            WealthLab.Alert a = Alerts[i];
            PrintDebug( "Alert " + ( i+1 ) + " order type:  " + a.OrderType );
        }
    }
}
BuyAtMarket

Position Property

Position Position

Contains the Position object that corresponds to a Sell or Cover alert.

BuyAtMarket

PositionType Property

PositionType PositionType

Returns a PositionType enumerated list, which contains the position type of generated alert. Possible values are:

  • Long
  • Short

Example

protected override void Execute(){
    // Alert generating code
    
    Random rnd = new Random();
    
    for(int bar = 1; bar < Bars.Count; bar++)
    {
        if (IsLastPositionActive)
        {
            // Exit trade after 3 days
            if ( bar+1 - LastPosition.EntryBar >= 3 )
                ExitAtMarket( bar+1, LastPosition, "3 days" );
        }
        else
        {
            if ( rnd.Next(0,1) == 1 )
                BuyAtMarket( bar+1 ); else
                ShortAtMarket( bar+1 );
        }
    }
    
    // Show the alert position type
    if( Alerts.Count > 0 )
    { 
        for( int i = 0; i < Alerts.Count; i++ )
        {
            WealthLab.Alert a = Alerts[i];
                PrintDebug( "Alert " + ( i+1 ) + " is to go " + a.PositionType );
        }
    }
}
BuyAtMarket

Price Property

double Price

Returns a Price number, which contains the price of generated alert, except for Market and AtClose orders which return 0.


Example

protected override void Execute(){
    // Alert generating code
    for(int bar = 4; bar < Bars.Count; bar++)
    {
        if (!IsLastPositionActive)
        {
            // Three consecutive lower closes
            if( ( Bars.Close[bar] < Bars.Close[bar-1] ) &
                ( Bars.Close[bar-1] < Bars.Close[bar-2] ) &
                ( Bars.Close[bar-2] < Bars.Close[bar-3] ) )  
                BuyAtStop( bar+1, Close[bar]*1.03 );

            if( Close[bar] > Close[bar-3] )
                BuyAtMarket( bar+1 );
        }
        if (IsLastPositionActive)
        {
            SellAtLimit( bar+1, LastPosition, Close[bar]*1.01 );
        }
    }
    
    if( Alerts.Count > 0 )
    { 
        for( int i = 0; i < Alerts.Count; i++ )
        {
            WealthLab.Alert a = Alerts[i];
            // Show the alert price if it's limit/stop order
                if( ( a.OrderType != OrderType.Market ) & 
                    ( a.OrderType != OrderType.AtClose ) )
                PrintDebug( "Alert " + ( i+1 ) + " has a " + a.OrderType + " price of " + a.Price ); else
                PrintDebug( "Alert " + ( i+1 ) + " is a AtMarket/AtClose order; price N/A" );
        }
    }
}
BuyAtMarket

Scale Property

BarScale Scale

Returns a BarScale enumerated list, which contains the bar scale of generated alert. Possible values are:

  • Daily
  • Minute
  • Monthly
  • Quarterly
  • Second
  • Tick
  • Weekly
  • Yearly

Example

protected override void Execute(){
    // Alert generating code
    for(int bar = 3; bar < Bars.Count; bar++)
    {
        if (!IsLastPositionActive)
        {
                BuyAtMarket( bar+1 );
        }
        if (IsLastPositionActive)
        {
            SellAtMarket( bar+1, LastPosition );
        }
    }
    
    // Returns alert bar scale
    if( Alerts.Count > 0 )
    {
        for( int i = 0; i < Alerts.Count; i++ )
        {
            WealthLab.Alert a = Alerts[i];
            PrintDebug( "Alert from strategy that executed on " + a.Scale + " scale ");
        }
    }
}
BuyAtMarket

Shares Property

double Shares

Returns a Shares number, which contains the number of shares for generated alert.

Remarks

  • In portfolio simulation mode, all trades are pre-executed using 1 share per Position, and then position sizing is applied after the fact.  So the Shares property will always return 1 while the Strategy is executing.

Example

protected override void Execute(){
    // Alert generating code
    for(int bar = 3; bar < Bars.Count; bar++)
    {
        if (!IsLastPositionActive)
        {
            // Three consecutive lower closes
            if( ( Bars.Close[bar] < Bars.Close[bar-1] ) &
                ( Bars.Close[bar-1] < Bars.Close[bar-2] ) &
                ( Bars.Close[bar-2] < Bars.Close[bar-3] ) )  
                BuyAtStop( bar+1, Close[bar]*1.03 );
        }
        if (IsLastPositionActive)
        {
            SellAtLimit( bar+1, LastPosition, Close[bar]*1.10 );
        }
    }
    
    // Show the number of shares in alert
    if( Alerts.Count > 0 )
    { 
        for( int i = 0; i < Alerts.Count; i++ )
        {
            WealthLab.Alert a = Alerts[i];
                PrintDebug( "Alert " + ( i+1 ) + " is for " + a.Shares + " shares " );
        }
    }
}
BuyAtMarket

SignalName Property

string SignalName

Returns a SignalName string, which contains the name of a signal which generated the alert.


Example

protected override void Execute(){
    // Alert generating code
    
    Random rnd = new Random();
    
    for(int bar = 1; bar < Bars.Count; bar++)
    {
        if (IsLastPositionActive)
        {
            // Exit trade after 3 days
            if ( bar+1 - LastPosition.EntryBar >= 3 )
                SellAtMarket( bar+1, LastPosition, "Time-based" );
        }
        else
        {
            // Random factor
            if ( rnd.Next(0,1) == 0 )
                BuyAtStop( bar+1, High[bar]*1.03, "Buy strength" ); else
                BuyAtLimit( bar+1, Low[bar]*0.97, "Buy weakness" );
        }
    }
    
    // Show the signal name of the alert
    if( Alerts.Count > 0 )
    { 
        for( int i = 0; i < Alerts.Count; i++ )
        {
            WealthLab.Alert a = Alerts[i];
            PrintDebug( "Alert " + ( i+1 ) + " was generated by:  " + a.SignalName + " signal" );
        }
    }
}
BuyAtMarket

Symbol Property

string Symbol

Returns a Symbol string, which contains the symbol name of generated alert.


Example

protected override void Execute(){
            // Alert generating code
            for(int bar = 3; bar < Bars.Count; bar++)
            {
                if (!IsLastPositionActive)
                {
                    // Three consecutive lower closes
                    if( ( Bars.Close[bar] < Bars.Close[bar-1] ) &
                        ( Bars.Close[bar-1] < Bars.Close[bar-2] ) &
                        ( Bars.Close[bar-2] < Bars.Close[bar-3] ) )  
                        BuyAtStop( bar+1, Close[bar]*1.03 );
                }
                if (IsLastPositionActive)
                {
                    SellAtLimit( bar+1, LastPosition, Close[bar]*1.10 );
                }
            }
            
            // Show the symbol name for an alert
            if( Alerts.Count > 0 )
            { 
                for( int i = 0; i < Alerts.Count; i++ )
                {
                    WealthLab.Alert a = Alerts[i];
                        PrintDebug( "Alert " + ( i+1 ) + " is for " + a.Symbol );
                }
            }
        }