PLUGIN THAT OPENS MULTI-CURRENCY PAIR AT ONCE

Created at 23 Sep 2024, 22:46
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!
OZ

ozonwafor

Joined 23.09.2024

PLUGIN THAT OPENS MULTI-CURRENCY PAIR AT ONCE
23 Sep 2024, 22:46


I am trying to create a plugin or cBot that has two button icons named A having green colour and the other name B having red colour. When I click the A button, it should place buy order for GBPUSD, EURUSD, XAUUSD, AUDUSD and place sell order for USDJPY, USDCHF. When I click the B button, it should place sell order for GBPUSD, EURUSD, XAUUSD, AUDUSD and buy order for USDJPY, USDCHF. use 2.5% of the equity for placing order for each pair.

 

I have tried and at this this point it keeps giving error about definition for the key not existing.

using cAlgo.API;
using cAlgo.API.Internals;
using System;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class GoodBadBot : Robot
    {
        [Parameter("Risk Percentage Per Pair", DefaultValue = 2.5)]
        public double RiskPercentage { get; set; }

        protected override void OnStart()
        {
            // Inform the user how to control the bot
            Print("Press 'G' for GOOD trades (Buy GBPUSD, EURUSD, XAUUSD, AUDUSD; Sell USDJPY, USDCHF)");
            Print("Press 'B' for BAD trades (Sell GBPUSD, EURUSD, XAUUSD, AUDUSD; Buy USDJPY, USDCHF)");

            // Register the keyboard event
            Chart.KeyDown += OnKeyDown;
        }

        private void OnKeyDown(ChartKeyboardEventArgs obj)
        {
            // Detect if G or B is pressed for Good/Bad trades
            if (obj.Key == Keys.G)
            {
                ExecuteGoodTrades();
            }
            else if (obj.Key == Keys.B)
            {
                ExecuteBadTrades();
            }
        }

        // Handler for GOOD trades: Buy GBPUSD, EURUSD, XAUUSD, AUDUSD; Sell USDJPY, USDCHF
        private void ExecuteGoodTrades()
        {
            double equity = Account.Equity;

            PlaceTrade(TradeType.Buy, "GBPUSD", equity);
            PlaceTrade(TradeType.Buy, "EURUSD", equity);
            PlaceTrade(TradeType.Buy, "XAUUSD", equity);
            PlaceTrade(TradeType.Buy, "AUDUSD", equity);

            PlaceTrade(TradeType.Sell, "USDJPY", equity);
            PlaceTrade(TradeType.Sell, "USDCHF", equity);

            Print("Executed GOOD trades");
        }

        // Handler for BAD trades: Sell GBPUSD, EURUSD, XAUUSD, AUDUSD; Buy USDJPY, USDCHF
        private void ExecuteBadTrades()
        {
            double equity = Account.Equity;

            PlaceTrade(TradeType.Sell, "GBPUSD", equity);
            PlaceTrade(TradeType.Sell, "EURUSD", equity);
            PlaceTrade(TradeType.Sell, "XAUUSD", equity);
            PlaceTrade(TradeType.Sell, "AUDUSD", equity);

            PlaceTrade(TradeType.Buy, "USDJPY", equity);
            PlaceTrade(TradeType.Buy, "USDCHF", equity);

            Print("Executed BAD trades");
        }

        // Method to place trade based on equity and risk percentage
        private void PlaceTrade(TradeType tradeType, string symbolName, double equity)
        {
            var symbol = Symbols.GetSymbol(symbolName);
            double riskAmount = equity * RiskPercentage / 100.0;
            double volumeInLots = symbol.QuantityToVolumeInUnits(riskAmount / symbol.PipValue);

            // Ensure that the volume is within the allowed range
            if (volumeInLots > 0)
            {
                ExecuteMarketOrder(tradeType, symbolName, volumeInLots);
                Print("Placed {0} order on {1} for {2} lots", tradeType, symbolName, volumeInLots);
            }
            else
            {
                Print("Volume too small for symbol {0}", symbolName);
            }
        }

        protected override void OnStop()
        {
            // Cleanup code if necessary
        }
    }
}


@ozonwafor
Replies

PanagiotisCharalampous
24 Sep 2024, 05:44

Hi there,

You have are referring to a plug in but you have posted the code for a cBot. You cannot run a cBot as a plugin. Where did you find this code?

Best regards,

Panagiotis

 


@PanagiotisCharalampous