# Strategy Programming

### Account <a href="#account" id="account"></a>

#### Description <a href="#description" id="description"></a>

Account is an object containing information about the account with which the current strategy is working.

The individual properties are:

* **Account.AccountConnection** Name for the broker connection used (the name assigned under the account connection submenu)
* **Account.AccountType** Type of account (live account, simulated account etc.)
* **Account.Broker** Name/definition for the broker
* **Account.BuyingPower** The current account equity in consideration of the leverage provided by the broker (IB leverages your account equity by a factor of 4, meaning that with 10000€ your buying power is equal to 40000€)
* **Account.CashValue** Amount (double)
* **Account.Currency** Currency in which the account is held
* **Account.IsDemo** True, if the account is a demo account
* **Account.Name** Name of the account (should be identical to Account.AccountConnection)
* **Account.RealizedProfitLoss** Realized profits and losses (double)

#### Example <a href="#example" id="example"></a>

```csharp
Print("AccountConnection " + Account.AccountConnection);
Print("AccountType " + Account.AccountType);
Print("Broker " + Account.Broker);
Print("BuyingPower " + Account.BuyingPower);
Print("CashValue " + Account.CashValue);
Print("Currency " + Account.Currency);
Print("InstrumentTypes " + Account.InstrumentTypes);
Print("IsDemo " + Account.AccountType == AccountType.Simulate);
Print("Name " + Account.Name);
Print("RealizedProfitLoss " + Account.RealizedProfitLoss);
```

### BarsCountFromTradeClose() <a href="#barscountfromtradeclose" id="barscountfromtradeclose"></a>

#### Description <a href="#description_1" id="description_1"></a>

The property "BarsCountFromTradeClose" outputs the number of bars that have occurred since the last exit from the market.

#### Usage <a href="#usage" id="usage"></a>

```csharp
BarsCountFromTradeClose()
BarsCountFromTradeClose(string strategyName)
```

For multi-bar strategies

```csharp
BarsCountFromTradeClose(int multibarSeriesIndex, string strategyName, int exitsAgo)
```

#### Parameter <a href="#parameter" id="parameter"></a>

|                     |                                                                                                                                                                                                                                                                                                                    |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| strategyName        | The Strategy name (string) that has been used to clearly label the exit within the exit method.                                                                                                                                                                                                                    |
| multibarSeriesIndex | For \*[*MultiBars* ](https://script.agenatrader.com/handling-bars-and-instruments/multibars#multibars)strategies. Index of the data series for which the exit order has been executed. See [*ProcessingBarSeriesIndex*](https://script.agenatrader.com/handling-bars-and-instruments/bars#processingbarindexlast). |
| exitsAgo            | Number of exits that have occurred in the past. A zero indicates the number of bars that have formed after the last exit.                                                                                                                                                                                          |

#### Example <a href="#example_1" id="example_1"></a>

```csharp
Print("The last exit was " + BarsCountFromTradeClose() + " bars ago.");
```

### BarsCountFromTradeOpen() <a href="#barscountfromtradeopen" id="barscountfromtradeopen"></a>

#### Description <a href="#description_2" id="description_2"></a>

The property "BarsCountFromTradeOpen" returns the number of bars that have occurred since the last entry into the market.

#### Usage <a href="#usage_1" id="usage_1"></a>

```csharp
BarsCountFromTradeOpen()
BarsCountFromTradeOpen(string strategyName)
```

For multi-bar strategies

```csharp
BarsCountFromTradeOpen(int multibarSeriesIndex, string strategyName, int entriesAgo)
```

#### Parameter <a href="#parameter_1" id="parameter_1"></a>

|                     |                                                                                                                                                                                                                                                                                                                    |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| strategyName        | The strategy name (string) that has been used to clearly label the entry within an entry method.                                                                                                                                                                                                                   |
| multibarSeriesIndex | For \*[*MultiBars* ](https://script.agenatrader.com/handling-bars-and-instruments/multibars#multibars)strategies. Index of the data series for which the exit order has been executed. See [*ProcessingBarSeriesIndex*](https://script.agenatrader.com/handling-bars-and-instruments/bars#processingbarindexlast). |
| entriesAgo          | Number of entries in the past. A zero indicates the number of bars that have formed after the last entry.                                                                                                                                                                                                          |

#### Example <a href="#example_2" id="example_2"></a>

```csharp
Print("The last entry was " + BarsCountFromTradeOpen() + " bars ago.");
```

### CancelAllOrders() <a href="#cancelallorders" id="cancelallorders"></a>

#### Description <a href="#description_3" id="description_3"></a>

CancelAllOrders deletes all oders (cancel) managed by the strategy. A cancel request is sent to the broker. Whether an or there is really deleted, can not be guaranteed. It may happen that an order has received a partial execution before it is deleted. Therefore we recommend that you check the status of the order with [*OnOrderChanged()*](https://script.agenatrader.com/events#onorderchanged).

#### Usage <a href="#usage_2" id="usage_2"></a>

```csharp
CancelAllOrders()
```

#### Parameter <a href="#parameter_2" id="parameter_2"></a>

None

#### Example <a href="#example_3" id="example_3"></a>

```csharp
protected override void OnCalculate()
{
   if (BarsCountFromTradeOpen() >= 30)
       CancelAllOrders();
}
```

### Order.CancelOrder() <a href="#ordercancel" id="ordercancel"></a>

#### Description <a href="#description_4" id="description_4"></a>

Cancel order deletes an order.

A cancel request is sent to the broker. There is no guarantee that the order will actually be deleted there. It may occur that the order receives a partial execution before it is deleted. Therefore we recommend that you check the status of the order with [*OnOrderChanged()*](https://script.agenatrader.com/events#onorderchanged).

#### Usage <a href="#usage_3" id="usage_3"></a>

```csharp
Order.CancelOrder()
```

#### Parameter <a href="#parameter_3" id="parameter_3"></a>

An order object of the type "IOrder"

#### Example <a href="#example_4" id="example_4"></a>

```csharp
private IOrder entryOrder = null;
private int barNumber = 0;
protected override void OnCalculate()
{
    // Place an entry stop at the high of the current bar
    if (entryOrder == null)
    {
         entryOrder=  SubmitOrder(new StrategyOrderParameters
                {
                    Direction = OrderDirection.Buy,
                    Type = OrderType.Stop,
                    StopPrice = High[0],
                    Quantity = 100,
                    SignalName = "strategyName",
                });
        
        barNumber = ProcessingBarIndex;
    }
    // Delete the order after 3 bars
    if (Position.PositionType == PositionType.Flat &&
    ProcessingBarIndex > barNumber + 3)
        entryOrder.CancelOrder();
}
```

### CreateIfDoneGroup() <a href="#createifdonegroup" id="createifdonegroup"></a>

#### Description <a href="#description_5" id="description_5"></a>

If two orders are linked to one another via a CreateIfDoneGroup, it means that if the one order has been executed, the second linked order is activated.

#### Usage <a href="#usage_4" id="usage_4"></a>

```csharp
CreateIfDoneGroup(IEnumerable<IOrder> orders)
```

#### Parameter <a href="#parameter_4" id="parameter_4"></a>

An order object of type IOrder as a list

#### Example <a href="#example_5" id="example_5"></a>

```csharp
private IOrder oopenlong = null;
private IOrder osubmitbuy = null;


protected override void OnInit()
{
   IsAutoConfirmOrder = false;
}


protected override void OnCalculate()
{

 oopenlong =  SubmitOrder(new StrategyOrderParameters
                {
                    Direction = OrderDirection.Buy,
                    Type = OrderType.Market,
                    Quantity = DefaultOrderQuantity,
                    SignalName = "strategyName",
                });

osubmitbuy =  SubmitOrder(new StrategyOrderParameters
                {
                    Direction = OrderDirection.Sell,
                    Type = OrderType.Stop,
                    Quantity = DefaultOrderQuantity,
            StopPrice = Close[0] * 1.1,
                    SignalName = "strategyName",
                });

   CreateIfDoneGroup(new List<IOrder> { oopenlong, osubmitbuy });

   oopenlong.ConfirmOrder();
}
```

### CreateOCOGroup() <a href="#createocogroup" id="createocogroup"></a>

#### Description <a href="#description_6" id="description_6"></a>

If two orders are linked via a CreateOCOGroup, it means that once the one order has been executed, the second linked order is deleted.

#### Usage <a href="#usage_5" id="usage_5"></a>

```csharp
CreateOCOGroup(IEnumerable<IOrder> orders)
```

#### Parameter <a href="#parameter_5" id="parameter_5"></a>

An order object of type IOrder as a list

#### Example <a href="#example_6" id="example_6"></a>

```csharp
private IOrder oopenlong = null;
private IOrder oEnterShort = null;


protected override void OnInit()
{
   IsAutoConfirmOrder = false;
}


protected override void OnCalculate()
{

oopenlong =  SubmitOrder(new StrategyOrderParameters
                {
                    Direction = OrderDirection.Buy,
                    Type = OrderType.Stop,
                    Quantity = DefaultOrderQuantity,
            StopPrice = Close[0] * 1.1,
                    SignalName = "strategyName",
                });


oEnterShort =  SubmitOrder(new StrategyOrderParameters
                {
                    Direction = OrderDirection.Sell,
                    Type = OrderType.Stop,
                    Quantity = DefaultOrderQuantity,
            StopPrice = Close[0] * -1.1,
                    SignalName = "strategyName",
                });



   CreateOCOGroup(new List<IOrder> { oopenlong, oEnterShort });

   oopenlong.ConfirmOrder();
   oEnterShort.ConfirmOrder();
}
```

### CreateOROGroup() <a href="#createorogroup" id="createorogroup"></a>

#### Description <a href="#description_7" id="description_7"></a>

If two orders are linked via a CreateOROGroup, it means that once the one order has been executed, the order size of the second order is reduced by the order volume of the first order.

#### Usage <a href="#usage_6" id="usage_6"></a>

```csharp
CreateOROGroup(IEnumerable<IOrder> orders)
```

#### Parameter <a href="#parameter_6" id="parameter_6"></a>

An order object of type IOrder as a list

#### Example <a href="#example_7" id="example_7"></a>

```csharp
private IOrder oStopLong = null;
private IOrder oLimitLong = null;


protected override void OnInit()
{
   IsAutoConfirmOrder = false;
}


protected override void OnCalculate()
{

   oStopLong =  SubmitOrder(new StrategyOrderParameters
                {
                    Direction = OrderDirection.Buy,
                    Type = OrderType.Stop,
                    Quantity = DefaultOrderQuantity,
            StopPrice = Close[0] * -1.1,
                    SignalName = "strategyName",
                });




   oLimitLong =  SubmitOrder(new StrategyOrderParameters
                {
                    Direction = OrderDirection.Buy,
                    Type = OrderType.Limit,
                    Quantity = DefaultOrderQuantity*0.5,
            Price = Close[0] * 1.1,
                    SignalName = "strategyName",
                });

   CreateOROGroup(new List<IOrder> { oLimitLong, oStopLong });
}
```

### DataSeriesConfigurable <a href="#dataseriesconfigurable" id="dataseriesconfigurable"></a>

### DefaultOrderQuantity <a href="#defaultorderquantity" id="defaultorderquantity"></a>

#### Description <a href="#description_8" id="description_8"></a>

Change order changes an order.

Default quantity defines the amount to be used in a strategy. Default quantity is set within the [*OnInit()*](https://script.agenatrader.com/keywords#oninit) method.

#### Usage <a href="#usage_7" id="usage_7"></a>

```csharp
ReplaceOrder(IOrder iOrder, int quantity, double limitPrice, double stopPrice)
```

#### Parameter <a href="#parameter_7" id="parameter_7"></a>

An int value containing the amount (stocks, contracts etc.)

#### Example <a href="#example_8" id="example_8"></a>

```csharp
protected override void OnInit()
{
DefaultOrderQuantity = 100;
}
```

### ExcludeTradeHistoryInBacktest <a href="#excludetradehistoryinbacktest" id="excludetradehistoryinbacktest"></a>

### CloseLongTrade () <a href="#closelongtrade" id="closelongtrade"></a>

#### Description <a href="#description_9" id="description_9"></a>

CloseLongTrade creates a sell order for closing a long position (sell).

See: [SubmitOrder()](#submitorder), [CloseShortTrade()](#closeshorttrade)

#### Usage <a href="#usage_8" id="usage_8"></a>

See [*StrategyOrderParameters*](#strategyorderparameters)

#### Parameter <a href="#parameter_8" id="parameter_8"></a>

See [*StrategyOrderParameters*](#strategyorderparameters)

#### Return Value <a href="#return-value" id="return-value"></a>

An order object of the type "IOrder"

#### Example <a href="#example_9" id="example_9"></a>

```csharp
var order = CloseLongTrade(new StrategyOrderParameters
{
    Type = OrderType.Market
});
```

### ExitOnClose <a href="#exitonclose" id="exitonclose"></a>

### ExitOnCloseSeconds <a href="#exitoncloseseconds" id="exitoncloseseconds"></a>

### CloseShortTrade() <a href="#closeshorttrade" id="closeshorttrade"></a>

#### Description <a href="#description_10" id="description_10"></a>

CloseShortTradecreates a buy-to-cover order for closing a short position (buy).

See: [SubmitOrder()](#submitorder), [*CloseLongTrade()*](#closelongtrade)

#### Usage <a href="#usage_9" id="usage_9"></a>

See [*StrategyOrderParameters*](#strategyorderparameters)

#### Parameter <a href="#parameter_9" id="parameter_9"></a>

See [*StrategyOrderParameters*](#strategyorderparameters)

#### Return Value <a href="#return-value_1" id="return-value_1"></a>

An order object of the type "IOrder"

#### Example <a href="#example_10" id="example_10"></a>

```csharp
var order = CloseShortTrade(new StrategyOrderParameters
{
    Type = OrderType.Stop,
    Quantity = quantity,
    StopPrice = price
});
```

### Account.GetValue() <a href="#accountgetvalue" id="accountgetvalue"></a>

#### Description <a href="#description_11" id="description_11"></a>

Get account value outputs information regarding the account for which the current strategy is being carried out.

See [*GetProfitLoss()*.](#getprofitloss)

#### Usage <a href="#usage_10" id="usage_10"></a>

```csharp
Account.GetValue(AccountItem accountItem)
```

#### Parameter <a href="#parameter_10" id="parameter_10"></a>

Possible values for account item are:

AccountItem.BuyingPower

AccountItem.CashValue

AccountItem.RealizedProfitLoss

#### Return Value <a href="#return-value_2" id="return-value_2"></a>

A double value for the account item for historical bars, a zero (0) is returned

#### Example <a href="#example_11" id="example_11"></a>

```csharp
Print("The current account cash value is " + Account.GetValue(AccountItem.CashValue));
Print("The current account cash value with the leverage provided by the broker is " + Account.GetValue(AccountItem.BuyingPower));
Print("The current P/L already realized is " + Account.GetValue(AccountItem.RealizedProfitLoss));
```

### GetEntries() <a href="#getentries" id="getentries"></a>

#### Description <a href="#description_12" id="description_12"></a>

This DataSeries is used in conditions and indicates multiple entry prices for entry orders

#### Usage <a href="#usage_11" id="usage_11"></a>

Overload in scripted condition for short and long signal indication

#### Parameter <a href="#parameter_11" id="parameter_11"></a>

None

#### Return Value <a href="#return-value_3" id="return-value_3"></a>

int

#### Example <a href="#example_12" id="example_12"></a>

```csharp
public class MyTestEntry : UserScriptedCondition
    {

        double _percentage = 100;

        protected override void Initialize()
        {
            IsEntry = true;
            IsStop = false;
            IsTarget= false;
            Add(new OutputDescriptor(Color.FromKnownColor(KnownColor.Black), "Occurred"));
            Add(new OutputDescriptor(Color.FromArgb(255, 118, 222, 90), "Entry1"));
            Add(new OutputDescriptor(Color.FromArgb(255, 118, 222, 90), "Entry2"));
            Add(new OutputDescriptor(Color.FromArgb(255, 118, 222, 90), "Entry3"));
            Overlay = true;
            CalculateOnBarClose = true;
        }

        protected override void OnBarUpdate()
        {

            Calculate();

        }

        public override void Recalculate()
        {
            Calculate();
        }

        private void Calculate ()
        {

            if (TradeDirection == PositionType.Long)
            {
                Entry1.Set(Close[0] + 0.5);
                Entry2.Set(Close[0] + 1);
                Entry3.Set(Close[0] + 1.5);
            }
            else
            {
                Entry1.Set(Close[0] - 0.5);
                Entry2.Set(Close[0] - 1);
                Entry3.Set(Close[0] - 1.5);
            }
        }

        #region Properties

        [Browsable(false)]
        [XmlIgnore()]
        public DataSeries Occurred
        {
            get { return Values[0]; }
        }

        [Browsable(false)]
        [XmlIgnore()]
        public DataSeries Entry1
        {
            get { return Values[1]; }
        }

        [Browsable(false)]
        [XmlIgnore()]
        public DataSeries Entry2
        {
            get { return Values[2]; }
        }

        [Browsable(false)]
        [XmlIgnore()]
        public DataSeries Entry3
        {
            get { return Values[3]; }
        }

        public override IList<DataSeries> GetEntrys()
        {
            return new[] { Entry1, Entry2, Entry3 };
```

### GetProfitLoss() <a href="#getprofitloss" id="getprofitloss"></a>

#### Description <a href="#description_13" id="description_13"></a>

Get profit loss outputs the currently unrealized profit or loss for a running position.

See [*Account.GetValue()*.](#accountgetvalue)

#### Usage <a href="#usage_12" id="usage_12"></a>

```csharp
GetProfitLoss(int pLType);
```

#### Parameter <a href="#parameter_12" id="parameter_12"></a>

Potential values for the P/L type are:

0 – Amount: P/L as a currency amount

1 – Percent: P/L in percent

2 – Risk: P/L in Van Tharp R-multiples [*www.vantharp.com*](http://www.vantharp.com/tharp-concepts/risk-and-r-multiples.asp)

3 – P/L in ticks

#### Return Value <a href="#return-value_4" id="return-value_4"></a>

A double value for the unrealized profit or loss

#### Example <a href="#example_13" id="example_13"></a>

```csharp
Print("The current risk for the strategy " + this.Name + " is " + GetProfitLoss(1) + " " + Instrument.Currency);
Print("This equals "+ string.Format( "{0:F1} R.", GetProfitLoss(3)));
```

### GetProfitLossAmount() <a href="#getprofitlossamount" id="getprofitlossamount"></a>

#### Description <a href="#description_14" id="description_14"></a>

GetProfitLossAmount () provides the current unrealized gain or loss of a current position as the currency amount.

See [*Account.GetValue()*.](#accountgetvalue)

#### Usage <a href="#usage_13" id="usage_13"></a>

```csharp
GetProfitLossAmount(double profitLoss);
```

#### Parameter <a href="#parameter_13" id="parameter_13"></a>

Double

#### Return Value <a href="#return-value_5" id="return-value_5"></a>

A double value for the unrealized profit or loss

#### Example <a href="#example_14" id="example_14"></a>

```csharp
Print("the current P&L " + this.Name + " is " + GetProfitLossAmount(Position.OpenProfitLoss) + " " + Instrument.Currency);
```

### GetProfitLossRisk() <a href="#getprofitlossrisk" id="getprofitlossrisk"></a>

#### Description <a href="#description_15" id="description_15"></a>

GetProfitLossRisk () returns the current unrealized gain or loss of a current position in R-multiples.

See [*Account.GetValue()*.](#accountgetvalue)

#### Usage <a href="#usage_14" id="usage_14"></a>

```csharp
GetProfitLossRisk();
```

#### Parameter <a href="#parameter_14" id="parameter_14"></a>

None

#### Return Value <a href="#return-value_6" id="return-value_6"></a>

A double value for the R-Multiple

#### Example <a href="#example_15" id="example_15"></a>

```csharp
Print("the current P&L " + this.Name + " is " + string.Format( "{0:F1} R.", GetProfitLossRisk()));
```

### GetScriptedCondition() <a href="#getscriptedcondition" id="getscriptedcondition"></a>

#### Description <a href="#description_16" id="description_16"></a>

This method allows user to communicate between scripts.

### IsAutoConfirmOrder <a href="#isautoconfirmorder" id="isautoconfirmorder"></a>

#### Description <a href="#description_17" id="description_17"></a>

IsAutoConfirmOrder determines whether orders are activated automatically. IsAutoConfirmOrder is specified in the [*OnInit()* ](https://script.agenatrader.com/keywords#oninit)method.

If IsAutoConfirmOrder = true, then orders are automatically activated (default). If IsAutoConfirmOrder is assigned the value false, the corresponding order must be activated with order.

#### Parameter <a href="#parameter_15" id="parameter_15"></a>

Bool value

#### Example <a href="#example_16" id="example_16"></a>

```csharp
protected override void OnInit()
{
   IsAutoConfirmOrder = false;
}
```

### Order <a href="#order" id="order"></a>

#### Description <a href="#description_18" id="description_18"></a>

IOrder is an object that contains information about an order that is currently managed by a strategy.

The individual properties are:

* Action **One of four possible positions in the market:**
  * OrderDirection.Buy
  * OrderDirection.Sell
* **AveragePrice** **The average purchase or selling price of a position.For positions without partial executions, this corresponds to the entry price.**
* **FilledQuantity** For partial versions
* **LimitPrice**
* **Name**    The unique SignalName (maybe mistake SignalName)
* **OrderId** The unique OrderId
* **OrderMode** One of three possible positions in the market:
  * OrderMode.Direct
  * OrderMode.Dynamic
  * OrderMode.Synthetic
* **OrderState** The current status of the order can be queried (see *OnOrderExecution* and *OnOrderChanged*)
  * OrderState.Accepted
  * OrderState.Cancelled
  * OrderState.CancelRejected
  * OrderState.FilledQuantity
  * OrderState.PartFilled
  * OrderState.PendingCancel
  * OrderState.PendingReplace
  * OrderState.PendingSubmit
  * OrderState.Rejected
  * OrderState.ReplaceRejected
  * OrderState.Unknown
  * OrderState.Working
* **OrderType** Possible order types:
  * OrderType.Limit
  * OrderType.Market
  * OrderType.Stop
  * OrderType.StopLimit
* **Quantity** The quantity to be ordered
* **StopPrice**
* **Timestamp** Time stamp
* **TimeFrame** The TimeFrame, which is valid for the order.
* **TimeFrame**

Possible Methods:

* **order Order.Cancel()** Delete the Order
* **order.ConfirmOrder()** Confirm the order. This method have to be executed if IsAutoConfirmOrder is set to false and you want to run the order automatically. This is, for example, the case when an OCO or IfDone fabrication is to be produced.

### Performance <a href="#performance" id="performance"></a>

#### Description <a href="#description_19" id="description_19"></a>

Performance is an object containing information regarding all trades that have been generated by a strategy.

The trades are sorted into multiple lists. With the help of these lists it is easier to create a performance evaluation.

See Performance Characteristics.

The individual lists are:

* **Performance.AllTrades** A [*Trade*](#trade) collection object containing all trades generated by a strategy.
* **Performance.LongTrades** A [*Trade*](#trade) collection object containing all long trades generated by a strategy.
* **Performance.ShortTrades** A [*Trade*](#trade) collection object containing all short trades generated by a strategy.
* **Performance.WinningTrades** A [*Trade*](#trade) collection object containing all profitable trades generated by a strategy.
* **Performance.LosingTrades** A [*Trade*](#trade) collection object containing all loss trades generated by a strategy.

#### Example <a href="#example_17" id="example_17"></a>

```csharp
// When exiting a strategy, create a performance evaluation
protected override void OnDispose()
{
Print("Performance evaluation of the strategy : " + this.Name);
Print("----------------------------------------------------");
Print("Amount of all trades: " + Performance.AllTrades.Count);
Print("Amount of winning trades: " + Performance.WinningTrades.Count);
Print("Amount of all loss trades: " + Performance.LosingTrades.Count);
Print("Amount of all long trades: " + Performance.LongTrades.Count);
Print("Amount of short trades: " + Performance.ShortTrades.Count);
Print("Result: " + Account.RealizedProfitLoss + " " + Account.Currency);
}
```

### Position <a href="#position" id="position"></a>

#### Description <a href="#description_20" id="description_20"></a>

Position is an object containing information regarding the position currently being managed by a strategy.

The individual properties are:

* **Position.AvgPrice** The average buy or sell price of a position. For positions without partial executions, this is equal to the entry price.
* **Position.CreatedDateTime** Date and time at which the position was opened.
* **Position.Instrument** The trading instrument in which the position exists. See *Instruments*.
* **Position.PositionType** One of three possible positions in the market:
  * PositionType.Flat
  * PositionType.Long
  * PositionType.Short
* **Position.OpenProfitLoss** The currently not yet realized profit or loss. See [*GetProfitLoss()*.](#getprofitloss)
* **Position.ProfitCurrency** Profit (or loss) displayed as a currency amount.
* **Position.ProfitPercent** Profit (or loss) displayed in percent.
* **Position.ProfitPoints** Profit (or loss) displayed in points or pips.
* **Position.Quantity** Amount of stocks, contracts, CFDs etc. within a position.

#### Example <a href="#example_18" id="example_18"></a>

```csharp
if (Position.PositionType != PositionType.Flat)
{
Print("Average price " + Position.AvgPrice);
Print("Opening time " + Position.CreatedDateTime);
Print("Instrument " + Position.Instrument);
Print("Current positioning " + Position.PositionType);
Print("Unrealized P/L " + Position.OpenProfitLoss);
Print("P/L (currency) " + Position.ProfitCurrency);
Print("P/L (in percent) " + Position.ProfitPercent);
Print("P/L (in points) " + Position.ProfitPoints);
Print("Pieces " + Position.Quantity);
}
```

### PositionType <a href="#positiontype" id="positiontype"></a>

See ***Position.PositionType*****.**

### TraceOrders <a href="#traceorders" id="traceorders"></a>

#### Description <a href="#description_21" id="description_21"></a>

The trace orders property is especially useful for keeping track of orders generated by strategies. It also provides an overview of which orders were generated by which strategies. Trace orders can be specified with the [*OnInit()*](https://script.agenatrader.com/keywords#oninit) method.

When TraceOrders is activated, each order will display the following values in the output window:

* Instrument
* Time frame
* Action
* Type
* Limit price
* Stop price
* Quantity
* Name

This information is useful when creating and debugging strategies.

#### Usage <a href="#usage_15" id="usage_15"></a>

TraceOrders

#### Parameter <a href="#parameter_16" id="parameter_16"></a>

none

#### Return Value <a href="#return-value_7" id="return-value_7"></a>

**true** Tracing is currently switched on **false** Tracing is switched off

#### Example <a href="#example_19" id="example_19"></a>

```csharp
protected override void OnInit()
{
ClearTraceWindow();
TraceOrders = true;
}
```

### Quantity <a href="#quantity" id="quantity"></a>

See ***Position.Quantity***, ***Position.PositionType*****.**

### ReplaceOrder() <a href="#replaceorder" id="replaceorder"></a>

#### Description <a href="#description_22" id="description_22"></a>

Change order, as the name suggests, changes an order.

#### Usage <a href="#usage_16" id="usage_16"></a>

```csharp
ReplaceOrder(IOrder iOrder, int quantity, double limitPrice, double stopPrice)
```

#### Parameter <a href="#parameter_17" id="parameter_17"></a>

|            |                                          |
| ---------- | ---------------------------------------- |
| iOrder     | An order object of the type "IOrder"     |
| quantity   | Number of units to be ordered            |
| limitPrice | Limit price. Set this to 0 if not needed |
| stopPrice  | Stop price. Set this to 0 if not needed  |

#### Example <a href="#example_20" id="example_20"></a>

```csharp
private IOrder stopOrder = null;
protected override void OnCalculate()
{
// If the position is profiting by 10 ticks then set the stop to break-even
if (stopOrder != null
    && Close[0] >= Position.AvgPrice + (10 * TickSize)
        && stopOrder.StopPrice < Position.AvgPrice)
ReplaceOrder(stopOrder, stopOrder.Quantity, stopOrder.LimitPrice, Position.AvgPrice);
}
```

### SetUpProfitTarget() <a href="#setupprofittarget" id="setupprofittarget"></a>

#### Description <a href="#description_23" id="description_23"></a>

Set profit target immediately creates a "take profit" order after an entry order is generated. The order is sent directly to the broker and becomes active immediately. If the profit target is static, you can also define SetUpProfitTarget() with the OnInit() method.

See [*SetUpStopLoss()*](#setupstoploss), [*SetUpTrailStop()*](#setuptrailstop).

#### Usage <a href="#usage_17" id="usage_17"></a>

```csharp
SetUpProfitTarget(double currency)
SetUpProfitTarget(CalculationMode mode, double value)
SetUpProfitTarget(string fromEntry signal, CalculationMode mode, double value)
```

#### Parameter <a href="#parameter_18" id="parameter_18"></a>

|                  |                                                                                                                                                                         |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| currency         | Sets the profit target in a currency, for example 500€.                                                                                                                 |
| mode             | Potential values can be: CalculationMode.Percent (display in percent); CalculationMode.Price (display as price value); CalculationMode.Ticks (display in ticks or pips) |
| value            | The distance between entry price and profit target. This is dependent upon the „mode" but generally refers to a monetary value, a percentage or a value in ticks.       |
| fromEntry signal | The name of the entry signal for which the profit target is to be generated. The amount is taken from the entry order referenced.                                       |

#### Example <a href="#example_21" id="example_21"></a>

```csharp
protected override void OnInit()
{
// Creates a Target Order 20 ticks above the market
SetUpProfitTarget(CalculationMode.Ticks, 20);
}
```

### SetUpStopLoss() <a href="#setupstoploss" id="setupstoploss"></a>

#### Description <a href="#description_24" id="description_24"></a>

Set stop loss creates a stop loss order after an entry order is placed. The order is sent directly to the broker and becomes effective immediately.

If the stop loss is static, then SetUpStopLoss() can be defined with the OnInit() method.

See [*SetUpProfitTarget()*](#setupprofittarget), [*SetUpTrailStop()*](#setuptrailstop).

#### Usage <a href="#usage_18" id="usage_18"></a>

```csharp
SetUpStopLoss(double currency)
SetUpStopLoss(double currency, bool simulated)
SetUpStopLoss(CalculationMode mode, double value)
SetUpStopLoss(string fromEntry signal, CalculationMode mode, double value, bool simulated)
```

#### Parameter <a href="#parameter_19" id="parameter_19"></a>

|                  |                                                                                                                                                                                                             |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| currency         | The difference between the stop loss and the entry price (=risk) in a currency, such as 500€                                                                                                                |
| mode             | Potential values can be: CalculationMode.Percent (display in percent); CalculationMode.Price (display as price value); CalculationMode.Ticks (display in ticks or pips)                                     |
| simulated        | When set to "true," the stop order does not go live (as a market order) until the price has „touched" it for the first time (meaning that it is executed just as it would be under real market conditions). |
| value            | The distance between stop price and profit target. This is dependent upon the „mode" but generally refers to a monetary value, a percentage or a value in ticks.                                            |
| fromEntry signal | The name of the entry signal for which the stop order is to be generated. The amount is taken from the entry order referenced.                                                                              |

#### Example <a href="#example_22" id="example_22"></a>

```csharp
protected override void OnInit()
{
// Sets profitTarget 15 Ticks above the market
SetUpStopLoss("MACDEntry", CalculationMode.Ticks, 15, true);
}
```

### SetUpTrailStop() <a href="#setuptrailstop" id="setuptrailstop"></a>

#### Description <a href="#description_25" id="description_25"></a>

Set trail stop creates a trail stop order after an entry order is generated. Its purpose is to protect you from losses, and after reaching break-even, to protect your gains.

The order is sent directly to the broker and becomes effective immediately.

If the stop loss price and the offset value are static, you can define SetUpTrailStop() with the OnInit() method.

If you use SetUpTrailStop() within the[ *OnCalculate()*](https://script.agenatrader.com/events#oncalculate) method, you must make sure that the parameters are readjusted to the initial value, otherwise the most recently used settings will be used for the new position.

**Functionality:**

Assuming that you have SetUpTrailStop(CalculationMode.Ticks, 30) selected:

In a long position, the stop will be 30 ticks from the previously reached high. If the market makes a new high, the stop will be adjusted. However, the stop will no longer be moved downwards.

In a short position, this behavior starts with the most recent low.

**Tips:**

It is not possible to use SetUpStopLoss and SetUpTrailStop for the same position at the same time within one strategy. The SetUpStopLoss() method will always have precedence over the other methods.

However, it is possible to use both variants parallel to each other in the same strategy if they are referencing different entry signals.

Partial executions of a single order will cause a separate trading stop for each partial position.

If a SetUpProfitTarget() is used in addition to a SetUpTrailStop(), then both orders will be automatically linked to form an OCO order.

It is always a stop market order that is generated, and not a stop limit order.

If a position is closed by a different exit order within the strategy, then the TrailingStopOrder is automatically deleted.

See [*SetUpStopLoss()*](#setupstoploss), [*SetUpTrailStop()*](#setuptrailstop).

#### Usage <a href="#usage_19" id="usage_19"></a>

```csharp
SetUpTrailStop(double currency)
SetUpTrailStop(double currency, bool simulated)
SetUpTrailStop(CalculationMode mode, double value)
SetUpTrailStop(string fromEntry signal, CalculationMode mode, double value, bool simulated)
```

#### Parameter <a href="#parameter_20" id="parameter_20"></a>

|                  |                                                                                                                                                                                                             |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| currency         | The distance between the stop loss and the entry price                                                                                                                                                      |
| mode             | Possible values are: CalculationMode.Percent; CalculationMode.Ticks                                                                                                                                         |
| simulated        | When set to "true," the stop order does not go live (as a market order) until the price has „touched" it for the first time (meaning that it is executed just as it would be under real market conditions). |
| value            | The distance between stop price and profit target. This is dependent upon the „mode" but generally refers to a monetary value, a percentage or a value in ticks.                                            |
| fromEntry signal | The name of the entry signal for which the stop order is to be generated. The amount is taken from the entry order referenced.                                                                              |

#### Example <a href="#example_23" id="example_23"></a>

```csharp
protected override void OnInit()
{
// Sets a trailing at the low of the last candle
    SetUpTrailStop(CalculationMode.Price, Low[0]);
}
```

### StrategyOrderParameters <a href="#strategyorderparameters" id="strategyorderparameters"></a>

### Description <a href="#description_26" id="description_26"></a>

This class aggregates all properties needed to submit the order.

See [*SubmitOrder()*](#submitorder), [*CloseLongTrade()*](#closelongtrade), [*CloseShortTrade()*](#closeshorttrade).

#### Usage <a href="#usage_20" id="usage_20"></a>

```csharp
public class StrategyOrderParameters
    {
        public OrderDirection Direction { get; set; }
        public OrderMode Mode { get; set; } = OrderMode.Direct;
        public OrderType Type { get; set; }
        public bool LiveUntilCancelled { get; set; }
        public int Quantity { get; set; }
        public double Price { get; set; }
        public double StopPrice { get; set; }
        public string SignalName { get; set; } = String.Empty;
        public IInstrument Instrument { get; set; }
        public ITimeFrame TimeFrame { get; set; }
        public string FromEntrySignal { get; set; } = String.Empty;
    }
```

#### Parameter <a href="#parameter_21" id="parameter_21"></a>

|                    |                                                                                                                                                                                                   |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| OrderDirection     | Possible values are: **orderDirection.Buy** (Buy order for a long entry); **orderDirection.Sell** (Sell order for closing a long position)                                                        |
| OrderMode          | One of three possible positions in the market: Direct, Dynamic, Synthetic                                                                                                                         |
| OrderType          | Possible values: OrderType.Limit, OrderType.Market, OrderType.Stop, OrderType.StopLimit                                                                                                           |
| LiveUntilCancelled | The order will not be deleted at the end of the bar, but will remain active until removed with [*Order.Cancel*](#ordercancel) or until it reaches its expiry (see [*TimeInForce*](#timeinforce)). |
| Quantity           | Amount                                                                                                                                                                                            |
| Price              | Limit value. Inputting a 0 makes this parameter irrelevant                                                                                                                                        |
| StopPrice          | Stop value. Inputting a 0 makes this parameter irrelevant                                                                                                                                         |
| SignalName         | An unambiguous signal name (string)                                                                                                                                                               |
| Instrument         | The trading instrument in which the position exists.                                                                                                                                              |
| TimeFrame          | The TimeFrame, which is valid for the order.                                                                                                                                                      |
| FromEntrySignal    | The name of the attached entry signal                                                                                                                                                             |

### SubmitOrder() <a href="#submitorder" id="submitorder"></a>

#### Description <a href="#description_27" id="description_27"></a>

Submit order creates a user-defined order. For this order, no stop or limit order is placed in the market. All AgenaTrader control mechanisms are switched off for this order type. The user is responsible for managing the various stop and target orders, including partial executions.

See [*OnOrderChanged()*](https://script.agenatrader.com/events#onorderchanged), [*OnOrderExecution()*.](https://script.agenatrader.com/events#onorderexecution)

#### Usage <a href="#usage_21" id="usage_21"></a>

See [*StrategyOrderParameters*](#strategyorderparameters)

#### Parameter <a href="#parameter_22" id="parameter_22"></a>

See [*StrategyOrderParameters*](#strategyorderparameters)

#### Return Value <a href="#return-value_8" id="return-value_8"></a>

an order object of the type "IOrder"

#### Example <a href="#example_24" id="example_24"></a>

```csharp
// Limit Long order
Submit Limit Buy
var order = SubmitOrder(new StrategyOrderParameters
                {
                    Direction = OrderDirection.Buy,
                    Type = OrderType.Limit,
                    Mode = orderMode,
                    Price = limitPrice,
                    Quantity = quantity,
                    SignalName = entryName,
                    Instrument = Instrument,
                    TimeFrame = TimeFrame,
                    LiveUntilCancelled = true
                });

// Short Market order
Submit Sell Market
var order = SubmitOrder(new StrategyOrderParameters
            {
                Direction = OrderDirection.Sell,
                Type = OrderType.Market,
                Mode = ordermode,
                Quantity = quantity,
                SignalName = entryName,
                Instrument = Instrument,
                TimeFrame = TimeFrame
            });
```

### TimeInForce <a href="#timeinforce" id="timeinforce"></a>

#### Description <a href="#description_28" id="description_28"></a>

The time in force property determines how long an order is valid for. The validity period is dependent upon which values are accepted by a broker.

For orders submitted with `LiveUntilCancelled = true`, the `TimeInForce` setting is always ignored.\
`TimeInForce` will only be applied when `LiveUntilCancelled` is set to `false`.

TimeInForce is specified with the [*OnInit()*](https://script.agenatrader.com/keywords#oninit) method.

Permitted values are: TimeInForce.day TimeInForce.loc TimeInForce.gtc (GTC = good till canceled) TimeInForce.gtd

**Default:** TimeInForce.GTC

#### Usage <a href="#usage_22" id="usage_22"></a>

**TimeInForce**

#### Example <a href="#example_25" id="example_25"></a>

```csharp
protected override void OnInit()
{
TimeInForce = TimeInForce.Day;
}
```

### Trade <a href="#trade" id="trade"></a>

#### Description <a href="#description_29" id="description_29"></a>

Trade is an object containing information about trades that have been executed by a strategy or are currently running.

The individual properties are:

* **Trade.AvgPrice** Average entry price
* **Trade.ClosedProfitLoss** Profit or loss already realized
* **Trade.Commission** Commissions
* **Trade.CreatedDateTime** Time at which the trade was created
* **Trade.EntryReason** Description of the entry signal For strategies: signal entry name
* **Trade.ExitDateTime** Time at which the trade was closed
* **Trade.ExitPrice** Exit price
* **Trade.ExitReason** Description of the exit signal For strategies: name of the strategy
* **Trade.Instrument** Description of the trading instrument
* **Trade.PositionType** Positioning within the market
  * PositionType.Flat
  * PositionType.Long
  * PositionType.Short
* **Trade.OpenProfitLoss** Unrealized profit/loss of a running position
* **Trade.ProfitCurrency** Profit or loss in the currency that the account is held in
* **Trade.ProfitLoss** Profit or loss
* **Trade.ProfitPercent** Profit or loss in percent
* **Trade.ProfitPercentWithCommission** Profit or loss in percent with commissions
* **Trade.ProfitPoints** Profit or loss in points/pips
* **Trade.Quantity** Quantity of stocks/contracts/ETFs/etc.
* **Trade.TimeFrame** Timeframe in which the trade was opened
* **Trade.Url** URL for the snapshot of the chart at the moment of creation

#### Example <a href="#example_26" id="example_26"></a>

```csharp
protected override void OnDispose()
{

  foreach (ITradeState tradeState in Performance.AllTrades)
  {
  ITrade trade = tradeState.Trade;
  if (trade == null) continue;

    Print("Trade #"+trade.Id);
    Print("--------------------------------------------");
    Print("Average price " + trade.AvgPrice);
    Print("Realized P/L " + trade.ClosedProfitLoss);
    Print("Commissions " + trade.Commission);
    Print("Time of entry " + trade.CreatedDateTime);
    Print("Entry reason " + trade.EntryReason);
    Print("Time of exit " + trade.ExitDateTime);
    Print("Exit price " + trade.ExitPrice);
    Print("Exit reason " + trade.ExitReason);
    Print("Instrument " + trade.Instrument);
    Print("Positioning " + trade.PositionType);
    Print("Unrealized P/L " + trade.OpenProfitLoss);
    Print("P/L (currency) " + trade.ProfitCurrency);
    Print("P/L " + trade.ProfitLoss);
    Print("P/L (in percent) " + trade.ProfitPercent);
    Print("P/L (% with commission)" + trade.ProfitPercentWithCommission);
    Print("PL (in points) " + trade.ProfitPoints);
    Print("Quantity " + trade.Quantity);
    Print("Timeframe " + trade.TimeFrame);
    Print("URL for the snapshot " + trade.Url);
  }
}
```

### Unmanaged <a href="#unmanaged" id="unmanaged"></a>

## Backtesting and Optimization <a href="#backtesting-and-optimization" id="backtesting-and-optimization"></a>

### Performance Characteristics <a href="#performance-characteristics" id="performance-characteristics"></a>

Performance characteristics are the various factors that can be calculated for a list of trades. The trades can be generated by a strategy in real-time or based on a backtest.

The following are available:

* all trades
* all long trades
* all short trades
* all winning trades
* all losing trades

See [*Performance*.](#performance)

The individual factors are:

**AvgEtd** The average drawdown at the end of a trade \<TradeCollection>.TradesPerformance.\<TradesPerformanceValues>.AvgEtd

```csharp
Print("Average ETD of all trades is: " + Performance.AllTrades.TradesPerformance.Currency.AvgEtd);
```

**AvgMae** Average maximum adverse excursion \<TradeCollection>.TradesPerformance.\<TradesPerformanceValues>.AvgMae

```csharp
Print("Average MAE of all trades is: " + Performance.AllTrades.TradesPerformance.Currency.AvgMae);
```

**AvgMfe** Average maximum favorable excursion \<TradeCollection>.TradesPerformance.\<TradesPerformanceValues>.AvgMfe

```csharp
Print("Average MFE of all trades is: " + Performance.AllTrades.TradesPerformance.Currency.AvgMfe);
```

**AvgProfit** Average profit for all trades \<TradeCollection>.TradesPerformance.\<TradesPerformanceValues>.AvgProfit

```csharp
Print("Average profit of all trades is: " + Performance.AllTrades.TradesPerformance.Currency.AvgProfit);
```

**CumProfit** The cumulative winnings over all trades \<TradeCollection>.TradesPerformance.\<TradesPerformanceValues>.CumProfit

```csharp
Print("Average cumulative profit of all trades is: " + Performance.AllTrades.TradesPerformance.Currency.CumProfit);
```

**DrawDown** The drawdown for all trades \<TradeCollection>.TradesPerformance.\<TradesPerformanceValues>.DrawDow

```csharp
Print("Drawdown of all trades is: " + Performance.AllTrades.TradesPerformance.Currency.DrawDown);
```

**LargestLoser** The largest losing trade \<TradeCollection>.TradesPerformance.\<TradesPerformanceValues>.LargestLoser

```csharp
Print("Largest loss of all trades is: " + Performance.AllTrades.TradesPerformance.Currency.LargestLoser);
```

**LargestWinner** The largest winning trade \<TradeCollection>.TradesPerformance.\<TradesPerformanceValues>.LargestWinner

```csharp
Print("Largest win of all trades is: " + Performance.AllTrades.TradesPerformance.Currency.LargestWinner);
```

**ProfitPerMonth** The total performance (wins/losses) for the month (also in percent) \<TradeCollection>.TradesPerformance.\<TradesPerformanceValues>.ProfitPerMonth

```csharp
Print("Profit per month of all trades is: " + Performance.AllTrades.TradesPerformance.Currency.ProfitPerMonth);
```

**StdDev** The standard deviation for the wins/losses. With this, you are able to identify outliers. The smaller the standard deviation, the higher the expectation of winnings.

**All factors are double values.**

![Performance Characteristics](https://agenatrader.github.io/AgenaScript-documentation/media/image10.png)

[Next ](https://agenatrader.github.io/AgenaScript-documentation/keywords/)[ Previous](https://agenatrader.github.io/AgenaScript-documentation/events/)

***

Built with [MkDocs](https://www.mkdocs.org/) using a [theme](https://github.com/snide/sphinx_rtd_theme) provided by [Read the Docs](https://readthedocs.org/).
