Category Other  Published on 30/04/2024

Quick_Entry_Al

Description

Quick Entry using Hot Keys 

automatic lot size, fixed sl and tp

and break even function

 

 

 

 

 

 


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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MyRobot : Robot
    {
        [Parameter("RiskAmount", DefaultValue = 20)]
        public double RA { get; set; }
        [Parameter("SL", DefaultValue = 100)]
        public double Sl { get; set; }
        [Parameter("TP", DefaultValue = 400)]
        public double Tp { get; set; }
       
         
        

        protected override void OnStart()
        {
            
            Chart.MouseDown += OnChartMouseDown;
           
        }

        

        void OnChartMouseDown(ChartMouseEventArgs obj)
        {
            var Volu = RA/Sl/Symbol.PipValue;
            Volu = Symbol.NormalizeVolumeInUnits(Volu);
            if (obj.AltKey && !obj.CtrlKey && !obj.ShiftKey)    
            {
                if (obj.YValue > Symbol.Bid)
                    PlaceStopOrder(TradeType.Buy, Symbol, Volu, obj.YValue, "", Sl, Tp);
                else
                    PlaceLimitOrder(TradeType.Buy, Symbol, Volu, obj.YValue, "", Sl, Tp);
            }
               
            if (obj.CtrlKey && !obj.AltKey && !obj.ShiftKey)
            {
                if (obj.YValue > Symbol.Bid)
                    PlaceLimitOrder(TradeType.Sell, Symbol, Volu, obj.YValue, "", Sl, Tp);
                else
                    PlaceStopOrder(TradeType.Sell, Symbol, Volu, obj.YValue, "", Sl, Tp);
            }
              
            if (obj.AltKey && obj.CtrlKey && !obj.ShiftKey)
                CloseClosestPosition(obj.YValue);

            if (obj.ShiftKey && !obj.CtrlKey && obj.AltKey)
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volu, "", Sl, Tp);
            if (obj.ShiftKey && obj.CtrlKey && !obj.AltKey)
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volu, "", Sl, Tp);
            if (!obj.AltKey && !obj.CtrlKey && obj.ShiftKey)
                CancelClosestOrder(obj.YValue);
            if (obj.AltKey && obj.CtrlKey && obj.ShiftKey)
                BreakEvenPosition(obj.YValue);
        }

        protected override void OnTick()
        {
            Chart.DrawStaticText("Dashboard","$"+RA, VerticalAlignment.Top, HorizontalAlignment.Left, Color.Black);
            
           
        }

        protected override void OnStop()
        {

        }

        private void CloseClosestPosition(double price)
        {
            if (Positions.Count == 0)
                return;
            Position pos = Positions[0];
            foreach (var _pos in Positions)
            {
                if (Math.Abs(price - _pos.EntryPrice) < Math.Abs(price - pos.EntryPrice))
                    pos = _pos;
            }
            ClosePosition(pos);
        }
       
          private void BreakEvenPosition(double price)
        {
            if (Positions.Count == 0)
                return;         
            Position pos = Positions[0];
            if (pos.EntryPrice == pos.StopLoss)
                return;
            ModifyPosition(pos, pos.EntryPrice, pos.TakeProfit);
        }
        
       
        private void CancelClosestOrder(double price)
        {
            if (PendingOrders.Count == 0)
                return;
            PendingOrder ord = PendingOrders[0];
            foreach (var _ord in PendingOrders)
            {
                if (Math.Abs(price - _ord.TargetPrice) < Math.Abs(price - ord.TargetPrice))
                    ord = _ord;
            }
            CancelPendingOrder(ord);
        }

       
    }
}


IR
Irumbukkai

Joined on 02.02.2024 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Quick_Entry.algo
  • Rating: 0
  • Installs: 158
  • Modified: 30/04/2024 16:23
Comments
Log in to add a comment.
No comments found.