Help me fix this code

Created at 11 Oct 2023, 05:25
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!
MA

maxxx2532

Joined 11.10.2023

Help me fix this code
11 Oct 2023, 05:25


using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
using cAlgo.API.Responses;
using System;

[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class MartingaleRandomTrader : Robot
{
   private double initialLotSize = 0.02;
   private double lotSize = 0.02;
   private double maxLotSize = 0.08;
   private double stopLossPips = 50;
   private double takeProfitPips = 100;
   private Random random = new Random();

   protected override void OnStart()
   {
       Positions.Opened += OnPositionOpened;
       ExecuteRandomTrade();
   }

   private void OnPositionOpened(PositionOpenedEventArgs args)
   {
       // Check if the position was opened by the robot
       if (args.Position.Label.Contains("MartingaleRandomTrader"))
       {
           if (args.Position.Pips < 0)
           {
               // The position is in a loss
               if (lotSize < maxLotSize)
               {
                   // Double the lot size
                   lotSize *= 2;
               }
               else
               {
                   // Maximum lot size reached, revert to the initial lot size
                   lotSize = initialLotSize;
               }

               ExecuteRandomTrade();
           }
           else
           {
               // The position hit the take profit, so we can open a new random trade
               ExecuteRandomTrade();
           }
       }
   }

   private void ExecuteRandomTrade()
   {
       var randomDirection = random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
       var randomEntryPrice = Symbol.Ask + Symbol.PipSize * random.Next(1, 20) * (randomDirection == TradeType.Buy ? 1 : -1);

       var request = new MarketOrderRequest
       {
           Symbol = Symbol,
           TradeType = randomDirection,
           Volume = lotSize,
           Label = "MartingaleRandomTrader",
           StopLossPips = stopLossPips,
           TakeProfitPips = takeProfitPips,
           PositionType = PositionType.Intraday,
           EntryPrice = randomEntryPrice
       };

       ExecuteMarketOrder(request);
   }
}
 


@maxxx2532
Replies

PanagiotisChar
11 Oct 2023, 06:56 ( Updated at: 11 Oct 2023, 07:00 )

What exactly do you need to fix?


@PanagiotisChar

maxxx2532
11 Oct 2023, 07:52 ( Updated at: 11 Oct 2023, 12:16 )

RE: Help me fix this code

PanagiotisChar said: 

What exactly do you need to fix it

It error cannot run on ctrader but I don’t know how to fix it


@maxxx2532

PanagiotisChar
11 Oct 2023, 12:25

The cBot is referencing an unknown library called using cAlgo.API.Responses;

If you did not develop this, you should talk to the developer


@PanagiotisChar