Quick Answer
CS0200: Property or indexer 'Position.StopLoss' cannot be assigned to -- it is read only means that you wrote a value into a property that only allows reading. In a cBot this is almost always one of the platform objects: Account, Position, Symbol, Bars, Positions. They show you the state of the account and the market, and you change that state by calling a method (for example a modify or close method of the Robot class), not by assigning to the property.
The fix has two parts. First, find out whether the value is something you may change at all (a stop loss: yes, through a method; the account balance: no). Second, if you only need a number to calculate with, copy it into your own variable and change the copy.
From the author: I build cBots in cTrader Automate and convert strategies from Pine Script and MQL5 to C#. CS0200 is common in converted code. In Pine Script a variable such as
strategy.position_sizeis read only too, but MQL5 code often writes into plain variables and structures (request.sl = value), and that habit moves to C# asposition.StopLoss = value. The compiler stops it.
Key Facts
| What you see | Usual cause | Fix |
|---|---|---|
| Position.StopLoss or TakeProfit cannot be assigned | A stop is changed with = | Call the modify method of the Robot class |
| Account.Balance cannot be assigned | Trying to set a test balance | Copy it into your own variable |
| Positions.Count cannot be assigned | Trying to reset a counter | Keep your own counter |
| Symbol.Bid or Ask cannot be assigned | Overriding a price for a test | Use your own variable for the test price |
| A property of your own class | The property has only get | Add set, or set it in the constructor |
| 'x' cannot be assigned to on a method result | Assigning to Bars.ClosePrices.Last(1) | Assign to a variable (this is a related error, see below) |
What the Error Means
A C# property can have a get part (read), a set part (write), or both. The cTrader API gives you the objects of the platform with get only, so that your code cannot change the account or the market by accident. When you write position.StopLoss = 1.1000;, the compiler sees an assignment to a property that has no set part, and it reports CS0200.
The cTrader reference lists the members of Position with their types. For example StopLoss and TakeProfit are double? (they can be empty) and are read only, and Pips is a double that is read only too. Open the reference for the member that the message names, and check whether it is marked as read only.
The Broken cBot
This cBot only prints. It has no order calls. Each marked line has a CS0200 error:
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class BrokenBot : Robot
{
[Parameter("Stop (pips)", DefaultValue = 20, MinValue = 1)]
public int StopPips { get; set; }
protected override void OnBar()
{
Account.Balance = 10000; // CS0200: Account.Balance is read only
foreach (var position in Positions)
{
double newStop = position.EntryPrice - StopPips * Symbol.PipSize;
position.StopLoss = newStop; // CS0200: Position.StopLoss is read only
}
Print("Open positions: {0}", Positions.Count);
}
}
}
The compiler reports the message with the name of the object and the property, for example Property or indexer 'Account.Balance' cannot be assigned to -- it is read only. Check the exact text in your build.
Cause 1: Changing a Stop With =
This is the most common case. A stop loss belongs to the position on the server, so the change has to go through a request, and the request can fail or be rejected. That is why it is a method that returns a result and not a property:
// Pattern only: choose the overload that fits your values (see below)
TradeResult result = ModifyPosition(position, newStop, position.TakeProfit, protectionType);
if (!result.IsSuccessful)
Print("Modify failed: {0}", result.Error);
The Robot class has many overloads of ModifyPosition. According to the cTrader reference, one of them takes the position, a stop loss, a take profit and a protection type, and others take protection objects for absolute or relative levels. The list differs between versions of the API, so open the reference or the code completion of your cTrader version, pick the overload that matches your values (absolute prices or relative pips) and check how the protection type is meant to be used. The snippet above is a pattern, not a finished line: protectionType stands for the value you choose. The same idea applies to closing a position (a method), to cancelling a pending order (a method) and to changing its price (a method). Do not assign; call.
Also read the result. A modify call can fail because the new stop is too close to the price or on the wrong side, as in the MQL5 invalid stops guide for the other platform. A bot that ignores the result looks as if it worked, and the stop stays where it was.
Cause 2: Setting a Test Value in a Platform Object
When you convert a backtest idea, you sometimes want to "pretend the balance is 10,000". The platform objects do not allow it, and they should not: the balance belongs to the account. Use your own variable and use it everywhere in your calculation:
double balanceForSizing = 10000; // your own number, for a test
double riskMoney = balanceForSizing * RiskPercent / 100.0;
For a final bot, read Account.Balance or Account.Equity directly. The same applies to prices: a test price goes in a variable such as double testPrice, not in Symbol.Bid.
Cause 3: Counters and Collections
Positions.Count is the number of open positions. It changes when positions open or close, and you cannot write to it. If you need a counter of your own (for example "trades taken today"), keep it in a field and change it in your code:
private int _tradesToday;
protected override void OnBar()
{
if (Server.Time.Date != _lastDay)
{
_lastDay = Server.Time.Date;
_tradesToday = 0;
}
}
(This needs using System; for DateTime, and a field private DateTime _lastDay;.)
Cause 4: Your Own Class With a Read-Only Property
If the property belongs to your own helper class, CS0200 means that it was declared without a set:
public class RiskSettings
{
public double RiskPercent { get; } // read only: set in the constructor only
public RiskSettings(double riskPercent)
{
RiskPercent = riskPercent; // allowed here
}
}
Assigning settings.RiskPercent = 2.0; somewhere else gives CS0200. Add set if the value must change, or create a new object with the new value. If the property has { get; private set; }, an assignment from outside the class gives a different error about accessibility (CS0272).
A Related Error: Assigning to a Method Result
Bars.ClosePrices.Last(1) = 1.1; is a different mistake. Last(1) returns a value, not a place that can store one. The compiler reports that the left side of an assignment must be a variable, property or indexer (error CS0131). The fix is the same idea: price series are data from the market, and you keep your own calculated numbers in your own variables or in a double[]. Check the exact text in your build.
The Fixed cBot
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class FixedBot : Robot
{
[Parameter("Stop (pips)", DefaultValue = 20, MinValue = 1)]
public int StopPips { get; set; }
protected override void OnBar()
{
double balanceForSizing = Account.Balance; // a copy, not an assignment
foreach (var position in Positions)
{
double newStop = position.EntryPrice - StopPips * Symbol.PipSize;
if (position.TradeType == TradeType.Sell)
newStop = position.EntryPrice + StopPips * Symbol.PipSize;
// The real change would be a call to ModifyPosition (check the overloads
// of your cTrader version). Here the bot only prints what it would do.
Print("Position {0}: stop now {1}, would be {2}", position.Id, position.StopLoss, newStop);
}
Print("Balance {0}, open positions {1}", balanceForSizing, Positions.Count);
}
}
}
The bot only prints, so you can compare the values with the chart. Add the modify call after you know which overload your version has, and test it on a demo account.
Similar Errors
| Error | Message | What it means |
|---|---|---|
| CS0200 | Property or indexer cannot be assigned to, it is read only | The property has no set |
| CS0131 | The left-hand side of an assignment must be a variable, property or indexer | You assign to a method result |
| CS0272 | The property or indexer cannot be used, the set accessor is inaccessible | private set used from outside |
| CS0120 | An object reference is required for the non-static member | An instance member used without an object |
| CS1061 | 'X' does not contain a definition for 'Y' | The member after the dot does not exist |
Checklist: What to Check When You See CS0200
- Read the name of the property in the message. Is it a platform object (
Account,Position,Symbol) or your own class? - Open the reference for that member and check that it is read only.
- Do you want to change the real state? Use the method of the
Robotclass (modify, close, cancel), and read its result. - Do you only need a number to calculate with? Copy it into your own variable.
- Is it your own class? Add
set, or set the value in the constructor. - Look for assignments to method results (
...Last(1) = x). - Build again after each change, and print the values.
- Test on a demo account before the cBot trades with real money.
Common Mistakes
- Writing
position.StopLoss = valueas in an MQL5 request structure. - Trying to set the balance to run a "what if" test.
- Resetting
Positions.Countinstead of keeping a counter. - Ignoring the result of a modify call.
- Declaring a property without
setand then assigning it. - Mixing platform data and your own calculations in the same variable.
FAQ
Related Guides
- cTrader C# error CS0103: the name does not exist in the current context.
- cTrader C# error CS1061: the object is found, the member is not.
- cTrader C# error CS0120: a member that needs a cBot object is used without one.
- cTrader C# error CS0165: a variable is read before it has a value.
- cTrader C# error CS0029: a value of the wrong type.
- cTrader C# error CS0266: a
doublestored in anint. - Pine Script strategy.exit to cTrader C#: stop loss and take profit in pips.
- Pine Script to cTrader C# cBot: the full conversion process.
- What to check in a trading bot before going live: a pre-live checklist for stops, lot size, spread and trade results.
- cTrader C# error CS0182: an attribute argument such as
DefaultValuethat is not a constant.
Fix Compile Errors Without Guessing
If a cBot or an Expert Advisor does not build, paste the code and the error message into the CodeFlowOS Error Fixer. It suggests a minimal fix, explains each error and shows what changed. A fix uses 1 credit, with up to 3 attempts included, and the compiler check is optional. Outputs are not guaranteed, and code that compiles can still have wrong trading logic, so always review the result. Before a bot goes live, the Code Review is free during the early-bird period. CodeFlowOS also converts strategies between Pine Script, MQL5 and cTrader C#: try the converter page.
This article is educational. It does not describe trading performance and is not financial advice. Always build the code in cTrader Automate and backtest it before using it on a live account.