zoukankan      html  css  js  c++  java
  • 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"));
                        }
                    }
                }
            }
        }
    }
  • 相关阅读:
    .NET Core 之 MSBuild 介绍
    棒!使用.NET Core构建3D游戏引擎
    在ASP.NET Core MVC中构建简单 Web Api
    TypeScript 中的 SOLID 原则
    《C# 并发编程 · 经典实例》读书笔记
    ASP.NET Core 中的那些认证中间件及一些重要知识点
    消息队列 Kafka 的基本知识及 .NET Core 客户端
    【数列区间询问中的分块思想】
    2013 Multi-University Training Contest 8
    2013 Multi-University Training Contest 7
  • 原文地址:https://www.cnblogs.com/aliblogs/p/5493818.html
Copyright © 2011-2022 走看看