MC-设置 止盈

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;
 
namespace PowerLanguage.Strategy
{
    [IOGMode(IOGMode.Enabled)]
    public class ExampleTakeProfit : SignalObject
    {
        private IOrderMarket buyOrder, sellTimeStopOrder;
        private IOrderPriced sellTPorder;
        private double takeProfitPrice;
 
        [Input]
        public int TakeProfitTicks { get; set; }
 
        public ExampleTakeProfit(object _ctx) : base(_ctx) { }
 
        protected override void Create()
        {
            buyOrder = OrderCreator.MarketNextBar(new SOrderParameters(
                Contracts.Default, EOrderAction.Buy));
 
            sellTPorder = OrderCreator.Limit(new SOrderParameters(
                Contracts.Default, "TakeProfit", EOrderAction.Sell));
 
            sellTimeStopOrder = OrderCreator.MarketNextBar(new SOrderParameters(
                Contracts.Default, "TimeStop", EOrderAction.Sell));
 
            TakeProfitTicks = 200; // Default value
        }
 
        protected override void StartCalc()
        {
            Output.Clear();     // Clear Editor Output tab
        }
 
        protected override void CalcBar()
        {
            // When flat, enter a long position every fifth bar
            if ((StrategyInfo.MarketPosition == 0) && (Bars.CurrentBar % 5 == 0))
            {
                // Submit the order at the open of the bar
                if (Bars.Status == EBarState.Open)
                {
                    buyOrder.Send();
 
                    Output.WriteLine("{0} - Submitted buy order",
                        Bars.Time[0].ToString("d-M HH:mm"));
                }
            }
 
            // Management of open long position
            if (StrategyInfo.MarketPosition > 0)
            {
                // First, when the position is just opened,
                // we need to calculate the take profit price
                int barsInPosition = Bars.CurrentBar -
                    Positions[0].OpenTrades[0].EntryOrder.BarNumber;
 
                // When the position is just opened, calculate the take profit price
                if (barsInPosition == 0)
                {
                    takeProfitPrice = Positions[0].OpenTrades[0].EntryOrder.Price +
                        (TakeProfitTicks * (Bars.Info.MinMove / Bars.Info.PriceScale));
 
                    // Only output info to the Output log at the open
                    // of the bar (prevents cluttering the log)
                    if (Bars.Status == EBarState.Open)
                    {
                        Output.WriteLine("{0} - Take profit price: {1}",
                            Bars.Time[0].ToString("d-M HH:mm"),
                            takeProfitPrice);
                    }
                }
 
                // Submit take profit order as long as there is an open long position
                sellTPorder.Send(takeProfitPrice);
 
                if (Bars.Status == EBarState.Close)     // Prevents cluttering the output
                {
                    Output.WriteLine("{0} - Sending limit order @ {1}",
                        Bars.Time[0].ToString("d-M HH:mm"),
                        takeProfitPrice);
                }
 
                // To prevent positions that are never closed, exit after more than 5 bars
                if (barsInPosition > 5)
                {
                    sellTimeStopOrder.Send();
 
                    if (Bars.Status == EBarState.Open)  // Prevents cluttering the output
                    {
                        Output.WriteLine("{0} - Sending time stop order",
                            Bars.Time[0].ToString("d-M HH:mm"));
                    }
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/aliblogs/p/5493818.html