NinjaScript > Language Reference > Strategy >

TradeCollection

Print this Topic Previous pageReturn to chapter overviewNext page

Definition

A collection of Trade objects. You can access a trade object by providing an index value. Trades are indexed sequentially meaning the oldest trade taken in a strategy will be at an index value of zero. The most recent trade taken will be at an index value of the total trades in the collection minus 1.

 

Methods and Properties

Count

An int value representing the number of trades in the collection

GetTrades()

Gets a TradeCollection object representing a specified position

LosingTrades

Gets a TradeCollection object of losing trades

TradesPerformance

Gets a TradesPerformance object

WinningTrades

Gets a TradeCollection object of winning trades

 

 

Examples

EXAMPLE 1
protected override void OnBarUpdate()
{
    // Accesses the first/last trade in the strategy (oldest trade is at index 0)
    // and prints out the profit as a percentage to the output window

    if (Performance.AllTrades.Count > 1)

    {

         Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
        Trade firstTrade = Performance.AllTrades[0];

 
        Print("The last trade profit is " + lastTrade.ProfitPercent);
        Print("The first trade profit is " + firstTrade.ProfitPercent);

    }
}

 

EXAMPLE 2
protected override void OnBarUpdate()
{
    // Once the strategy has executed 20 trades loop through the losing trades
    // collection and print out the PnL on only long trades
    if (Performance.AllTrades.Count == 20)
    {
        Print("There are " + Performance.AllTrades.LosingTrades.Count + " losing trades.");
        foreach (Trade myTrade in Performance.AllTrades.LosingTrades)
        {
              if (myTrade.Entry.MarketPosition == MarketPosition.Long)
                  Print(myTrade.ProfitCurrency * myTrade.Quantity);
        }
    }
}