Opposite Trade Robot

progy85 since: 07 Jan 2021;

  03 May 2021, 20:34
Opposite Trade Robot

Hi,

I want to make bot for opposite trade.

I have GBPUSD:
1. Buy 1 lot
2. Sell Stop 1.4 lot (if reach -10 pips from my first buy entry price)
3. If reach again to the first buy trade I need again to buy 1 lot but only if sell stop (second trade is active)
This I will use over and over when reach 10 pips for buy or sell.
4. After reach first 10 pips (sell or buy)  I need to closed all my opened trades .



Explanation about my code:
First I use Start for first Buy trade and in the same time use PlaceStopOrder for  sell (entry price is ask price - 10 pips).
With text label I recognized what trade is currently open, but now how can I open other trades.

1. - Buy (entry price: 1.3900)
2. - Sell (entry price: 1.3900-10 pips)
3. - Buy (entry price: 1.3900), but only is 2 trade is open.
4. - Sell (1.3900-10 pips), but only is but only 3 is open.
5. - Buy (entry price: 1.3900) ut only is but only 4 is open.

and so on...

Is there any guy can help me with my code? Thanks in advance.


Below is my code:


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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class OppositeTradingBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        private void ExecuteOrder(double quantity, TradeType tradeType, string label, double stopLoss, double takeProfit)
        {
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(quantity);
            var result = ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, label, stopLoss, takeProfit);
            if (result.Error == ErrorCode.NoMoney)
                Stop();
        }

        double firstTradePrice = 0;
        protected override void OnStart()
        {
            Positions.Opened += OnPositionsOpened;
            ExecuteOrder(1, TradeType.Buy, "1", 0, 10);
            PlaceStopOrder(TradeType.Sell, SymbolName, Symbol.QuantityToVolumeInUnits(1.4), Symbol.Ask - 10 * Symbol.PipSize, "2", 0, 10);
            firstTradePrice = Symbol.Ask;
        }

        private void OnPositionsOpened(PositionOpenedEventArgs args)
        {
            var secondTrade = Positions.Where(x => x.Label == "2").OrderByDescending(x => x.EntryTime).Count();
            if (secondTrade == 1)
            {
                foreach (var trade in Positions)
                {
                    if (!trade.Label.StartsWith("3"))
                    {
                        PlaceStopOrder(TradeType.Buy, SymbolName, Symbol.QuantityToVolumeInUnits(1.4), firstTradePrice, "3", 0, 10);
                    }
                }
            }
        }

        protected override void OnTick()
        {

        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}


 

amusleh's avatar

amusleh since: 01 Mar 2021;

  04 May 2021, 11:09

Hi,

You should use the PendingOrders.Filled event, when your sell order is filled the event will be raised and you can do anything you want to after that.

Please check the API references for code examples (Example 5).

Community Developer | Spotware