StopOrder

Created at 06 Jan 2018, 13:53
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!
TR

tradermatrix

Joined 24.07.2012

StopOrder
06 Jan 2018, 13:53


Hello

to limit my positions I use this formula which works well ...

   var cBotPositions = Positions.FindAll (RobotID)

             if (cBotPositions.Length> = 1)
                 return;

I want to use a scalper (tick mode) that launches (with an indicator) a Pending Order (buy or sell)
unfortunately StopOrder comes in numbers ... I do not manage to limit the number to 1.
would anyone have a solution to place a single stop order?
for example for this code;

  DateTime exp = MarketSeries.OpenTime.LastValue.AddMinutes (5);

                         for (int j = 1; j == 1; j ++)

PlaceStopOrder (TradeType.Sell, Symbol, volumeInUnits, Symbol.Bid - PipStepB * j * Symbol.PipSize, RobotID, SL, TP, exp);


cordially


@tradermatrix
Replies

tradermatrix
07 Jan 2018, 15:51

I found this solution
  could someone help me restart automatically (send stop order) when expiration time has deleted stop order
or any other solution would be welcome
cordially

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Diagnostics;
using Microsoft.Win32;
using cAlgo.API.Requests;
using System.Text;


namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class HigGFrequency : Robot
    {

        [Parameter("HigHFreQuencY Label n°", DefaultValue = "1")]
        public string RobotID { get; set; }

        [Parameter("Pip Step", DefaultValue = 10, MinValue = 1)]
        public int PipStepB { get; set; }

        [Parameter("First Quantity(Lots)", DefaultValue = 0.1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Cancel Stop Order(MINUTES)", DefaultValue = 30)]
        public int MINUTES { get; set; }

        [Parameter(DefaultValue = 160)]
        public int SL { get; set; }

        [Parameter(DefaultValue = 40)]
        public int TP { get; set; }



        //////////////////////////////////////////////////////////////////////////////////////    



        private bool _ordersCreated;

        protected override void OnTick()
        {
            Play();
        }


        private void Play()
        {



            var volumeInUnits = Symbol.QuantityToVolume(Quantity);


            if (!_ordersCreated)
            {
                var buyOrderTargetPrice = Symbol.Ask + PipStepB * Symbol.PipSize;

                DateTime exp = MarketSeries.OpenTime.LastValue.AddMinutes(MINUTES);

                _ordersCreated = true;


                PlaceStopOrder(TradeType.Buy, Symbol, volumeInUnits, buyOrderTargetPrice, RobotID, SL, TP, exp);
                foreach (var pendingOrder in PendingOrders)
                {
                    Print("Order placed with label {0}, id {1}", pendingOrder.Label, pendingOrder.Id);


                }
            }
        }
    }
}




 


@tradermatrix

Drummond360
11 Jan 2018, 18:50

Might it be easier to 'toggle a bool' if an order is found?

 

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

bool longO = false;
            
            foreach (var order in PendingOrders)
            {
                if (order.TradeType == TradeType.Buy && order.Label == label && order.SymbolCode == Symbol.Code)
                {
                    longO = true;
                }
            }

            if (longPosition == null && longO == false)
            
            {
                PlaceStopOrder

 


@Drummond360

tradermatrix
12 Jan 2018, 13:00

thank you
I will test this solution (because it is associated with the label)
I made this code that works (but can unfortunately interfere with other robots)

 protected override void OnTick()
        {
            Play();
        }
 
 
        private void Play()
        {
 
  var volumeInUnits = Symbol.QuantityToVolume(Quantity);

  var totalOrders = PendingOrders.Count;
                if (totalOrders >= 1)
                    return;
 
                     {
                var buyOrderTargetPrice = Symbol.Ask + PipStepB * Symbol.PipSize;
 
                DateTime exp = MarketSeries.OpenTime.LastValue.AddMinutes(MINUTES);
 
        PlaceStopOrder(TradeType.Buy, Symbol, volumeInUnits, buyOrderTargetPrice, RobotID, SL, TP, exp);
                foreach (var pendingOrder in PendingOrders)
                {
                    Print("Order placed with label {0}, id {1}", pendingOrder.Label, pendingOrder.Id);
 
 
                }
            }
        }
    }
}


cordially


@tradermatrix