Category Trend  Published on 03/11/2023

wws33

Description

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
   [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
   public class CustomBot : Robot
   {
       [Parameter("Initial Volume (Lots)", DefaultValue = 0.01, MinValue = 0.01)]
       public double InitialVolume { get; set; }

       [Parameter("Enable Pyramid", DefaultValue = false)]
       public bool EnablePyramid { get; set; }

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

       [Parameter("Step Multiplier", DefaultValue = 1.0, MinValue = 1.0)]
       public double StepMultiplier { get; set; }

       [Parameter("Max Positions", DefaultValue = 10, MinValue = 1)]
       public int MaxPositions { get; set; }

       [Parameter("Min Pips for Next Step", DefaultValue = 10, MinValue = 1)]
       public int MinPipsForNextStep { get; set; }

       private WellesWilderSmoothing highWWS33;
       private WellesWilderSmoothing lowWWS33;
       private WellesWilderSmoothing highWWS144;
       private WellesWilderSmoothing lowWWS144;
       private bool pyramidTriggered = false;
       private double currentVolume;
       private double lastLongPositionPrice = 0;
       private double lastShortPositionPrice = 0;

       protected override void OnStart()
       {
           highWWS33 = Indicators.WellesWilderSmoothing(MarketSeries.High, 33);
           lowWWS33 = Indicators.WellesWilderSmoothing(MarketSeries.Low, 33);
           highWWS144 = Indicators.WellesWilderSmoothing(MarketSeries.High, 144);
           lowWWS144 = Indicators.WellesWilderSmoothing(MarketSeries.Low, 144);
           currentVolume = Symbol.QuantityToVolumeInUnits(InitialVolume);
       }

       protected override void OnBar()
       {
           double currentHighWWS33 = highWWS33.Result.LastValue;
           double currentLowWWS33 = lowWWS33.Result.LastValue;
           double currentHighWWS144 = highWWS144.Result.LastValue;
           double currentLowWWS144 = lowWWS144.Result.LastValue;

           // Check for pyramid triggering condition
           if (!pyramidTriggered && currentHighWWS33 < currentLowWWS144 && currentLowWWS33 > currentHighWWS144)
           {
               pyramidTriggered = true;
           }
           else if (pyramidTriggered && (currentHighWWS33 > currentLowWWS144 || currentLowWWS33 < currentHighWWS144))
           {
               pyramidTriggered = false;
               currentVolume = Symbol.QuantityToVolumeInUnits(InitialVolume);
           }

           // Execute long trade if conditions are met
           if (Symbol.Ask > currentHighWWS33)
           {
               ExecuteLongTrade();
           }

           // Execute short trade if conditions are met
           if (Symbol.Bid < currentLowWWS33)
           {
               ExecuteShortTrade();
           }

           // Manage trailing stops
           ManageTrailingStops();
       }

       private void ExecuteLongTrade()
       {
           var longPositions = Positions.FindAll("MyBotLong", SymbolName);

           if (longPositions.Length == 0)
           {
               ExecuteMarketOrder(TradeType.Buy, SymbolName, currentVolume, "MyBotLong");
               lastLongPositionPrice = Symbol.Ask;
           }
           else if (EnablePyramid && pyramidTriggered && longPositions.Length < MaxPositions)
           {
               double pipsSinceLastLong = (Symbol.Ask - lastLongPositionPrice) / Symbol.PipSize;
               if (pipsSinceLastLong >= MinPipsForNextStep)
               {
                   currentVolume += Symbol.QuantityToVolumeInUnits(Step) * StepMultiplier;
                   ExecuteMarketOrder(TradeType.Buy, SymbolName, currentVolume, "MyBotLong");
                   lastLongPositionPrice = Symbol.Ask;
               }
           }
       }

       private void ExecuteShortTrade()
       {
           var shortPositions = Positions.FindAll("MyBotShort", SymbolName);

           if (shortPositions.Length == 0)
           {
               ExecuteMarketOrder(TradeType.Sell, SymbolName, currentVolume, "MyBotShort");
               lastShortPositionPrice = Symbol.Bid;
           }
           else if (EnablePyramid && pyramidTriggered && shortPositions.Length < MaxPositions)
           {
               double pipsSinceLastShort = (lastShortPositionPrice - Symbol.Bid) / Symbol.PipSize;
               if (pipsSinceLastShort >= MinPipsForNextStep)
               {
                   currentVolume += Symbol.QuantityToVolumeInUnits(Step) * StepMultiplier;
                   ExecuteMarketOrder(TradeType.Sell, SymbolName, currentVolume, "MyBotShort");
                   lastShortPositionPrice = Symbol.Bid;
               }
           }
       }

       private void ManageTrailingStops()
       {
           // The trailing stop logic remains unchanged
           foreach (var position in Positions)
           {
               double tslPrice;
               if (position.Label == "MyBotLong" && position.EntryPrice > 0)
               {
                   tslPrice = lowWWS33.Result.LastValue;
                   if (position.TradeType == TradeType.Buy && tslPrice > position.EntryPrice * (1 - 0.002))
                   {
                       ModifyPosition(position, tslPrice, position.TakeProfit);
                   }
               }
               else if (position.Label == "MyBotShort" && position.EntryPrice > 0)
               {
                   tslPrice = highWWS33.Result.LastValue;
                   if (position.TradeType == TradeType.Sell && tslPrice < position.EntryPrice * (1 + 0.002))
                   {
                       ModifyPosition(position, tslPrice, position.TakeProfit);
                   }
               }
           }
       }
   }
}
 


The author decided to hide the source code.
CR
creatorn906

Joined on 26.10.2023

  • Distribution: Paid
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: HTS Channels.algo
  • Rating: 0
  • Installs: 0
Comments
Log in to add a comment.
No comments found.