Continue writing code for trading robot in TSLAB program, please. Use this description:
1. opening a position - it determines the state of the current market:
Flat or trend 

If flat, he trades in a range on small market movements 

Trend, he trades within the trend, at peak values he takes profits and reverses the position
2. Closing a position. Immediately assesses the market condition and adjusts to the movement

3.If the market is flat, it does not move stops with profit.
If the trend starts, it moves the profit to increase the profit, if the trend is correct

If not guessed, it pulls out on a stop and plays the trend with a new position

During a trend the stop pulls back to breakeven, and takes the stop further into profit

Flat algorithm:
```
// This function is called when the market is considered to be in a state of "flat" trend
def flatalgorithm(marketdata):
// Set the initial stop-loss and take-profit levels
stoploss = marketdata.close[0] - 0.05 # Stop-loss is 5 cents below the current market price
takeprofit = marketdata.close[0] + 0.05 # Take-profit is 5 cents above the current market price
// Loop through the market data, adjusting the stop-loss and take-profit levels as needed
for i in range(1, len(market_data)):
    # If the market price moves in our favor, adjust the take-profit level upwards
    if market_data.close[i] > market_data.close[i-1]:
        take_profit = market_data.close[i] + 0.05
    # If the market price moves against us, adjust the stop-loss level downwards
    elif market_data.close[i] < market_data.close[i-1]:
        stop_loss = market_data.close[i] - 0.05

// If the market price reaches the take-profit level, sell the position
if market_data.close[i] > take_profit:
    market_data.close[i] = take_profit
    market_data.high[i] = take_profit
    market_data.low[i] = market_data.close[i-1]
# If the market price reaches the stop-loss level, close the position
elif market_data.close[i] < stop_loss:
    market_data.close[i] = stop_loss
    market_data.high[i] = market_data.close[i-1]
    market_data.low[i] = stop_loss
# If the market price moves against us and the stop-loss level is reached, adjust the stop
// If the market price moves against us and the stop-loss level is reached, adjust the stop-loss level downwards
elif market_data.close[i] < market_data.close[i-1] and market_data.close[i] < stop_loss:
    stop_loss = market_data.close[i] - 0.05
    # If the market price moves in our favor and the take-profit level is reached, sell the position
    elif market_data.close[i] > tak Write your answer without using the letter d whatsoever in your entire response.
user