Martingale with Trailling Stop free

by willbs in category Trend at 13/01/2023
Description

It uses several parameters such as Initial Volume, Stop Loss, Take Profit, Trailing Stop Loss, and Spread Limit to execute trades on the market.

The robot has a random trade type generator that can either select a buy or sell trade. The robot also has a closed position event handler that will execute a new trade if the previous one closed with a profit, or will double the volume of the previous trade if the previous one closed with a loss. Additionally, it stops the bot if it runs out of money.

Tip: Run extended optimization to get better parameters, at least 2 years. The print values ​​are for only 1 week.

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
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots {
  // Attribute to set the robot's time zone and access rights
  [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
  public class SampleMartingaleRobot: Robot {
    // Parameters for initial volume, stop loss, take profit, trailing stop loss, and spread limit
    [Parameter("Initial Volume", DefaultValue = 1000, MinValue = 0)]
    public int InitialVolume {
      get;
      set;
    }

    [Parameter("Stop Loss", DefaultValue = 15)]
    public int StopLoss {
      get;
      set;
    }

    [Parameter("Take Profit", DefaultValue = 24)]
    public int TakeProfit {
      get;
      set;
    }

    [Parameter("Trailing Stop Loss", DefaultValue = 21)]
    public int TrailingStopLoss {
      get;
      set;
    }

    [Parameter("Spread Limit", DefaultValue = 0.5)]
    public double SpreadLimit {
      get;
      set;
    }

    // Random number generator
    private Random random = new Random();

    // Function that is called when the robot starts
    protected override void OnStart() {
      // Event handler for when positions are closed
      Positions.Closed += OnPositionsClosed;

      // Check if spread is within the limit and execute an initial order
      if (Symbol.Spread <= SpreadLimit) {
        ExecuteOrder(InitialVolume, GetRandomTradeType());
      }
    }

    // Function to execute an order
    private void ExecuteOrder(long volume, TradeType tradeType) {
      // Check if spread is within the limit
      if (Symbol.Spread <= SpreadLimit) {
        var result = ExecuteMarketOrder(tradeType, SymbolName, volume, "Martingale", StopLoss, TakeProfit);
        // Modify stop loss if order execution is successful
        if (result.IsSuccessful) {
          result.Position.ModifyStopLossPips(TrailingStopLoss);
        }

        // If error is not enough money, stop the robot
        if (result.Error == ErrorCode.NoMoney)
          Stop();
      }
    }

    // Event handler function for when positions are closed
    private void OnPositionsClosed(PositionClosedEventArgs args) {
      Print("Closed");
      var position = args.Position;

      // Check if the closed position has the correct label and symbol
      if (position.Label != "Martingale" || position.SymbolName != SymbolName)
        return;

      // If the position has a positive gross profit, execute another order with the initial volume
      if (position.GrossProfit > 0) {
        if (Symbol.Spread <= SpreadLimit) {
          ExecuteOrder(InitialVolume, GetRandomTradeType());
        }
      }
      // If the position has a negative gross profit, execute another order with double the previous volume
      else {
        if (Symbol.Spread <= SpreadLimit) {
          ExecuteOrder((int) position.VolumeInUnits * 2, position.TradeType);
        }
      }
    }

    // Function to generate a random trade type (buy or sell)
    private TradeType GetRandomTradeType() {
      return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
    }
  }
}
Comments

AmandaOralie812 - January 13, 2023 @ 09:03

If you're unsure of where to begin, I suggest looking surviv io  at some of the great oscillator cBot indications, such Awesome Oscillator cBot. This may provide you an advantage over other traders by re-entering the beat-down mode of your emotions.

willbs - January 15, 2023 @ 21:17

cdc1918, can you do something similar using ChatGPT?

howdydynamo - February 08, 2023 @ 12:14

The Code you share is capable for every CMS?

I love create on WP. You can check here and confirm me.

Check Now

0