Help needed with take profit robot

Created at 30 Jan 2013, 14:32
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
JO

johndoe

Joined 30.01.2013

Help needed with take profit robot
30 Jan 2013, 14:32


Hi,

My goal is quite simple.  I want to code a robot that will close out (and hence take profit) from profitable positions.  Then automatically re-open the same position afterwards.

The problem I am having is that the API seems to expect the use of a Symbol object.  But the Symbol object is read-only.

This is what I've got so far.... (based on an API sample)

 

// -------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    This robot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk.
//
//    All changes to this file will be lost on next application start.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
//    This robot closes all profitable positions of the current account.
//
// -------------------------------------------------------------------------------

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot]
    public class SampleTakeNetProfits : Robot
    {
        protected override void OnStart()
        {
            foreach (var position in Account.Positions)
            {
                if (position.NetProfit > 0 )
                {
                    string posSymbol = position.SymbolCode;
                    int posVolume = position.Volume;
                    Trade.Close(position);
 // It does not like the next line... it expects a Symbol object.
Trade.CreateBuyMarketOrder(posSymbol,posVolume); } } } } }




@johndoe
Replies

admin
30 Jan 2013, 16:42

Hello,

The robot will use the symbol that it is attached to, for creating new orders. Therefore, you cannot use any symbol. 
The syntax should be:

Trade.CreateBuyMarketOrder(Symbol, posVolume); 


View the API Reference for the syntax: /api/internals/itrade

The ability to use other symbols is in the list for future implementation. 


@admin

johndoe
30 Jan 2013, 17:46

RE:

Thank you !  That explains it.


@johndoe