Price Level - CloseAllPositions - Logic

Created at 26 Jun 2019, 09:59
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!
Symposium's avatar

Symposium

Joined 16.07.2018

Price Level - CloseAllPositions - Logic
26 Jun 2019, 09:59


using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
 
namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class profit : Robot
    {
        [Parameter("Max Price Level", DefaultValue = 1.1900)]
        public int Max { get; set; }

        
        
protected override void OnTick()
        {
                       
            foreach (var position in Positions)
                if (Price (1.1900) > Max)                   
                     
                    ClosePosition(position);
                    Stop();
                }

@Symposium
Replies

Symposium
26 Jun 2019, 10:09

(REF: Code in Post Above )  Hi Panagiotis, I'm requiring a code snipet that will allow a Parameter set Price Level to Close All Positions and Stop the cBot.

ie: If price moves to (any pair) eg: 1.9000 = "Max" Parameter. The code will close all Positions and Stop the Bot.

It's for risk managment of a strategy using a range based system to protect against Trending price action.

Obviously there will be a Max and Min Parameter. If you can repair the code for the Max, I can replicate the Min.

Open to anyone that can help.... Thanks in Advance.... Cheers.


@Symposium

PanagiotisCharalampous
26 Jun 2019, 10:20

Hi Symposium,

This should work

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class profit : Robot
    {
        [Parameter("Max Price Level", DefaultValue = 1.19)]
        public int Max { get; set; }



        protected override void OnTick()
        {
            if (Symbol.Bid > Max)
            {
                foreach (var position in Positions)
                    ClosePosition(position);
                Stop();
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous

Symposium
26 Jun 2019, 12:49

Thank you Panagiotis.... as always with your code.... works perfectly..... Maybe a great addition to other buddying programers Code Snipet Libraries... 

 

 


@Symposium

Symposium
27 Jun 2019, 01:28

Sorry Panagiotis...

I can only input whole numbers ie: XAUUSD 1410, If I input 1410.60 the Parameter changes to Red and doesn't accept the Value? 

I have included a minimum value in the Parameter.... which hasn't fixed the issue...... Can this be fixed....?

 

[Parameter("Max Price Level", DefaultValue = 0, MinValue = 0)]
        public int Max { get; set; }

        [Parameter("Min Price Level", DefaultValue = 0, MinValue = 0)]
        public int Min { get; set; }

 


@Symposium

PanagiotisCharalampous
27 Jun 2019, 09:54

Hi Symposium,

You should make the parameter a double.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Symposium
27 Jun 2019, 10:56

Big, Big Thanks... Panagiotis.... problem solved... :-)


@Symposium