You need to enable JavaScript in order to visit our site!
or via PTMC account
Restore password
or via PTMC account
or via PTMC account
Restore password
or via PTMC account
or via PTMC account
Restore password
or via PTMC account
PTMC team 28.01 2014

Automated Trading Operations in Protrader (C#)


Trading operations in Protrader can be separated into the following three operations:

1. Formation of a request

2. Sending of a request

3. Receiving information about a result: opening of a position or an error message

Before performing any of these operations on an account you should check to make sure that the platform is connected to the server and receiving actual market information.

You can check the status of your connection to the broker’s server with the following piece of code:

//This checks whether the platform is connected to the broker server
//if there is no connection a message is logged
Print("Check Connection!");

You can check the presence of quotes for a given instrument with the following piece of code:

//here we make the object lastQuote
Quote lastQuote = Instruments.Current.LastQuote;
//here we check whether there are quotes coming in for this instrument
//if there are no quotes a message is logged
if (lastQuote==null)
Print("No price available!");

When you form a request you need to specify your operation parameters. For example, if you want to buy one lot of the current instrument Forming a requerst:

request = new NewOrderRequest();
request.Instrument = Instruments.Current;
request.Account = Accounts.Current;
request.Amount = 1;
request.Side = Operation.Buy;
//stop loss in ticks or pips relative to opening order price
request.StopLossOffset = 400.0;
//take profit in ticks or pips relative to opening order price
request.TakeProfitOffset = 1000.0;
//maximum slippage
request.MarketRange = 10;
//order type
request.Type = OrdersType.Market;

This is a completed request and now you can send it to a server with the method Orders.Send(...):

Orders.Send(NewOrderRequest request);

Here you can use the prepared request as a method parameter:

//send the request and save the order identifier
//in the orderId field for future use
string orderId = Orders.Send(request);
//the identifier is entered into the log;
//if there is an error in the trading operation the orderId will be -1;

You can get the full code here: SimpleBuy.cs. After starting the trading system SimpleBuy a long position will be opened for the current instrument. Systems SimpleSell.cs , SimpleBuyStop.cs , SimpleBuyLimit.cs work similarly. The code in these files are provided with detailed commentary. Download and try this and if there are any questions ask them below or on the forum.

The system SimpleCloseAll.cs closes all open positions for the current account:

//this closes all opened positions for the current account
allPosClosed = Positions.CloseAllByAccount( Accounts.Current );
if(allPosClosed == true)
Print("All Position Closed");

Upon successful completion the type bool allPosClosed is assigned the value true. In the examples there we showed the functions OnQuote() и Complete(). The function OnQuote() launches each time there is a new quote. If you system doesn’t require analysis of Level2 or trades, you can rely entirely on this function. For example I built the Regression Trader system in this way. The function Complete() is called before at the end-point of the system.


Of course, in real life a system that simply buys and sells orders after we press the button is not very useful to us. Algorithmic systems should ideally receive and analyze market information, open and close positions based on this information, and then modify orders based on conditions or react accordingly based on the market situation.

The methods for receiving information in Protrader (real time quotes and historical data) will be our next topic.

SimpleBuy.cs , SimpleSell.cs , SimpleBuyStop.cs , SimpleBuyLimit.cs , SimpleCloseAll.cs , Regression Trader

Have not tried PTMC yet? There is no better way to boost knowledge than to use it! Start trading with PTMC now!

Join PTMC community to post your comments
No comments yet. Be the first.
PTMC is a professional trading platform that combines all the best charting and analytical tools to trade on different financial markets. It is developed by PFSOFT UK LTD, a global provider of trading technology for banks & brokers
© 2024. PTMC is based on Protrader technology
71-75, Shelton Street,
Covent Garden,
London, WC2H9SQ,
United Kingdom
Support & Development
PFSOFT Ukraine
Office #701
Mechnikova Street, 10B
49000, Dnipro, Ukraine
Contact us
Phone: +380 56 740 30 01
Skype: PTMC_support
Find more ways to contact
Join us in social networks