Simple Key Entry free

by k0naka1jp in category Other at 26/05/2020
Description

This bot was created for the purpose of confirming manual trading with past data by BackTest in visual mode, but it can also be used for real trading.

Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
//This bot was created for the purpose of confirming manual trading with past data by BackTest, but it can also be used for real trading.
//
//<<<<< Entry >>>>>
// Buy : Shift + Left Mouse
// Sell: Ctrl + Left Mouse
//
//<<<<< Close >>>>>
// We will close Profit / Loss as you set or at any time.
//
// Only profit positions : Alt + Shift + Left Mouse
// Only Loss positions   : Alt + Ctrl  + Left Mouse
// All positions         : Alt + Shift + Ctrl + Left Mouse
//
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SimpleKeyEntry : Robot
    {
        private const string HowToUseText = "<<<<< Entry >>>>>\n Buy: Shift + Left Mouse\n Sell:Ctrl + Left Mouse\n\n <<<<< Close >>>>>\n Only profit positions : Alt + Shift + Left Mouse \n Only Loss positions : Alt + Ctrl  + Left Mouse \n All positions: Alt + Shift + Ctrl + Left Mouse\n";

        [Parameter("Stop Loss (pips)", Group = "Protection", DefaultValue = 30, MinValue = 1)]
        public int StopLossPips { get; set; }

        [Parameter("Take Profit (pips)", Group = "Protection", DefaultValue = 30, MinValue = 1)]
        public int TakeProfitPips { get; set; }

        [Parameter("Volume", Group = "Volume", DefaultValue = 10000, MinValue = 0, Step = 1)]
        public double Volume { get; set; }

        private string label = "SimpleKeyEntry";

        protected override void OnStart()
        {
            Chart.MouseDown += onChartMouseDown;
            howToUseText();
        }
        private void howToUseText()
        {
            Chart.DrawStaticText(label, HowToUseText, VerticalAlignment.Bottom, HorizontalAlignment.Left, Chart.ColorSettings.ForegroundColor);
        }
        private void onChartMouseDown(ChartMouseEventArgs args)
        {
            if (args.AltKey)
            {
                if (args.ShiftKey && args.CtrlKey)
                {
                    allClose();
                }
                else
                {
                    if (args.ShiftKey)
                    {
                        netProfitOnlyClose();
                    }
                    else if (args.CtrlKey)
                    {
                        netLossOnlyClose();
                    }
                }
            }
            else
            {
                if (args.ShiftKey)
                {
                    entry(TradeType.Buy);
                }
                else if (args.CtrlKey)
                {
                    entry(TradeType.Sell);
                }
            }
        }
        private void entry(TradeType type)
        {
            var order = ExecuteMarketOrder(type, SymbolName, Volume, label, StopLossPips, TakeProfitPips);
            if (!order.IsSuccessful)
            {
                Print("Error:{0}", order.Error.ToString());
                //throw new Exception(order.Error.ToString());
            }
        }
        private void allClose()
        {
            var positions = Positions.FindAll(label);
            foreach (Position p in positions)
            {
                ClosePosition(p);
            }
        }
        private void netProfitOnlyClose()
        {
            var positions = Positions.FindAll(label);
            foreach (Position p in positions)
            {
                if (p.NetProfit > 0)
                {
                    ClosePosition(p);
                }
            }
        }
        private void netLossOnlyClose()
        {
            var positions = Positions.FindAll(label);
            foreach (Position p in positions)
            {
                if (p.NetProfit < 0)
                {
                    ClosePosition(p);
                }
            }
        }
    }
}
Comments
0