All script positions are accessed through the ISecurity.Positions property.
For each position, a signal name is assigned. Only one line item with the same name can be active at a time.
For example, if you created a position "LE" and did not close it, then you cannot create a second position with the same name.
Position opening
To open positions, use the methods sec.Positions.BuyXXX (...) for buying and sec.Positions.SellXXX (...) for selling.
Buy:
sec.Positions.BuyAtMarket(i +1,2,"LE"); // Buy two market contracts at the next bar.sec.Positions.BuyAtPrice(i +1,2,65000,"LE"); // Set a limit order for the purchase of two contracts on the next bar at a price of 65,000.sec.Positions.BuyIfGreater(i +1,2,65000,"LE"); // Place a conditional order to buy two contracts on the next bar if the price is above 65,000.sec.Positions.BuyIfLess(i +1,2,65000,"LE"); // Place a conditional order to buy two contracts on the next bar if the price is below 65,000.
Sell:
sec.Positions.SellAtMarket(i +1,2,"LE"); // Sell two market contracts at the next bar.sec.Positions.SellAtPrice(i +1,2,65000,"LE"); // Set a limit order for the sale of two contracts at the next bar at a price of 65,000.sec.Positions.SellIfGreater(i +1,2,65000,"LE"); // Place a conditional order to sell two contracts at the next bar if the price is 65,000.sec.Positions.SellIfLess(i +1,2,65000,"LE"); // Place a conditional order to sell two contracts on the next bar if the price is below 65,000
Getting position
A commonly used method to get the position of GetLastActiveForSignal (...), example:
var pos =sec.Positions.GetLastActiveForSignal("LE", i); // Get the last active position with the name "LE" for bar number i.
The method searches for an active position with the name "LE" and returns it; if there are no positions, the method will return null.
There are also other methods for getting positions, they are all written like this: sec.Positions.GetXXX (...).
Position change
To change the position size, use the pos.ChangeAtXXX (...) methods, and the size of the new position is transferred.
For example, to increase a long position by 2 market contracts, you need to write pos.ChangeAtMarket (i + 1, pos.Shares + 2, "LX"); Here pos.Shares shows the current number of position contracts, and when we pass pos.Shares + 2, this means that we need to increase the position by 2 contracts.
If you pass a negative value, for example -3, then the position will turn into a short one with three contracts.
Examples:
pos.ChangeAtMarket(i +1,pos.Shares+1,"LX"); // Increase the position size by 1 lot on the next bar.pos.ChangeAtPrice(i +1,pos.AverageEntryPrice-100,pos.Shares+1,"LX"); // Increase the position size by 1 lot with a limit order at the price (average position price - 100).pos.ChangeAtProfit(i +1,pos.AverageEntryPrice+100,pos.Shares+1,"LX"); // Increase the position size by 1 lot at take profit at the price (average position price + 100).pos.ChangeAtStop(i +1,pos.AverageEntryPrice-100,pos.Shares+1,"LX"); // Increase the position size by 1 lot at stop loss, at a price (average position price - 100).
Closing position
To close a position, use the pos.CloseAtXXX (...) methods.
Examples:
pos.CloseAtMarket(i +1,"LX"); // Close the market position on the next bar.pos.CloseAtPrice(i +1,longPos.EntryPrice+100,"LX"); // Set a limit order to close a position at a price (entry price + 100).pos.CloseAtProfit(i +1,longPos.EntryPrice+100,"LX"); // Set take profit to close a position at a price (entry price + 100).pos.CloseAtStop(i +1,longPos.EntryPrice-100,"LX"); // Set a stop loss to close the position at the price (entry price - 100).
It is important to note that methods that close a position by condition (CloseAtPrice, CloseAtProfit, CloseAtStop) must be set on each bar until the position closes, otherwise TSLab will remove the closing order on the exchange.
For example, we’ll write a script that will buy after a growing candle in the market and set a stop of 200 points and a take of 400 points:
using TSLab.Script;
using TSLab.Script.Handlers;
namespace MyLib
{
public class ExampleBuy : IExternalScript
{
public void Execute(IContext ctx, ISecurity sec)
{
for (int i = 0; i < ctx.BarsCount; i++)
{
var signalBuy = sec.Bars[i].Close > sec.Bars[i].Open; // define a growing candle
var longPos = sec.Positions.GetLastActiveForSignal("LE", i); // get active position
if (longPos == null)
{
// if there is no position, then check the signal
if (signalBuy)
{
// if there is a buy signal, then buy by market
sec.Positions.BuyAtMarket(i + 1, 1, "LE");
}
}
else
{
// if there is a position, then we put a stop loss and take profit
longPos.CloseAtStop(i + 1, longPos.EntryPrice - 200, "LXS");
longPos.CloseAtProfit(i + 1, longPos.EntryPrice + 400, "LXP");
}
}
}
}
}