Calculate return and place order

Created at 15 Mar 2016, 12: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!
BA

badger.cook

Joined 25.02.2016

Calculate return and place order
15 Mar 2016, 12:32


Goodmorning all, 

Here is a "simple" problem I visibly can't solve by myself :

I would like to calcule the return of two different currencies (for instance : EURUSD and CHFUSD) between two bars. After that, I would like to use the computed return and multiply it by a "base" (i.e. 100) set at a specific date (9:00 GMT+1) each day, and use this result to place orders along the day. 

It looks like this :

  • 15/03/2016 9:00 : EUR/USD = 100 and USD/CHF = 100

Let's say EUR/USD has returned 0.5% between 9:00 and 9:01 and USD/CHF -0.5%, I'd like to have something like that.

  • 15/03/2016 9:01 : EUR/USD = 100 * (1+0.005) and USD/CHF = 100 * (1-0.005)

=> if EUR/USD index value > USD/CHF index value : SELL order (that's a stupid example btw)

Can somebody help with that ?

Thanks a lot for your help,

B.


@badger.cook
Replies

badger.cook
15 Mar 2016, 13:35

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class MacdBot : Robot
    {

	[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, ScalePrecision = 5)]

        [Parameter("Source EURUSD")]
        public DataSeries Source1 { get; set; }

        [Parameter("Source USDCHF")]
        public DataSeries Source2 { get; set; }

	[Parameter(DefaultValue = 1)]
	public int Period { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 5)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 5)]
        public int StopLoss { get; set; }


	private PriceROC priceROCEURUSD;
	private PriceROC priceROCUSDCHF;


        private const string label = "BOT";


        protected override void OnStart()
        {

	m1EURUSD = MarketData.GetSeries(Source1, TimeFrame.Minute1);
	m1USDCHF = MarketData.GetSeries(Source2, TimeFrame.Minute1);

    	priceROCEURUSD = Indicators.PriceROC(Source1, Period);
    	priceROCUSDCHF = Indicators.PriceROC(Source2, Period);

        }


	public override void Calculate(int index)
        {

	ResultEURUSD[index] = (1 + priceROCEURUSD.Result.Last(0)) * 100
	ResultUSDCHF[index] = (1 + priceROCUSDCHF.Result.Last(0)) * 100

        }


        protected override void OnBar()
        {

            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);


            if (ResultEURUSD[index].Result(0) > ResultUSDCHF[index].Result(0) && ResultEURUSD[index].Result(1) < ResultUSDCHF[index].Result(1))
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol1);
                ExecuteMarketOrder(TradeType.Sell, Symbol2);
            }

            if (ResultEURUSD[index].Result(0) < ResultUSDCHF[index].Result(0) && ResultEURUSD[index].Result(1) > ResultUSDCHF[index].Result(1))
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol1);
                ExecuteMarketOrder(TradeType.Buy, Symbol2);
            }


        }

    }
}

This is what I currently have but I'm sure I'm missing something. Thanks for your help.


@badger.cook

croucrou
15 Mar 2016, 14:59

"Last" refers to price on close of the bar, so the first available value is on the last bar, which is "1".

To access price value on current bar, you could use "lastvalue", which you rather would not need.


@croucrou

croucrou
17 Mar 2016, 13:37

Correct me, if I am not right.


@croucrou

badger.cook
17 Mar 2016, 13:50

Sorry for the delay. I used (0) and (1) because I saw it on another robot based on MA and EMA, and which was working.

What I want to do here is to create a kind of "index" that moves with the bar return of the currency. Based on the value of the two indices calculated by the robot (one for EURUSD and one for USDCHF), I want to make buy and sell orders for these two currency pairs.

I first have no idea how to create the "indices" and second have no idea how I could specify I want the last value of the index or the value 1 or 2 bars ago. 

Could somebody help with that ? 

Thanks a lot,

B.


@badger.cook

Spotware
24 Mar 2016, 13:58

Dear Trader,

Please have a look at the API Guides and Last() method in our API Reference.


@Spotware