zoukankan      html  css  js  c++  java
  • MC- 交易并设置止损

    using System;
    using System.Drawing;
    using System.Linq;
    using PowerLanguage.Function;
    using ATCenterProxy.interop;
     
    namespace PowerLanguage.Strategy
    {
        public class Example_StopLimit : SignalObject
        {
            private IOrderMarket buyMarketOrder, sellMarketOrder;
            private IOrderStopLimit sellStopLimitOrder;
            private double sellStopPrice, sellLimitPrice;
     
            public Example_StopLimit(object _ctx) : base(_ctx) { }
     
            protected override void Create()
            {
                buyMarketOrder = OrderCreator.MarketNextBar(new SOrderParameters(
                    Contracts.Default, "EnterLong", EOrderAction.Buy));
     
                sellMarketOrder = OrderCreator.MarketNextBar(new SOrderParameters(
                    Contracts.Default, "ExitLong", EOrderAction.Sell));
     
                sellStopLimitOrder = OrderCreator.StopLimit(new SOrderParameters(
                    Contracts.Default, "StopLMT", EOrderAction.Sell));
            }
     
            protected override void StartCalc()
            {
                Output.Clear(); // Clear PowerLanguage Editor output tab
            }
     
            protected override void CalcBar()
            {
                // When flat, enter long on first bar of day
                if ((StrategyInfo.MarketPosition == 0) && (Bars.Time[0].Date != Bars.Time[1].Date))
                {
                    buyMarketOrder.Send();
     
                    sellStopPrice  = Bars.Low[0] - Bars.Range();
                    sellLimitPrice = Bars.Low[0] - (Bars.Range() * 1.5);
     
                    Output.WriteLine("{0} - Buy order submitted. Sell stop calculated @ {1} with limit {2}",
                        Bars.Time[0].ToString("d-M HH:mm:ss"),
                        sellStopPrice,
                        sellLimitPrice);
                }
     
                // Long order management
                if (StrategyInfo.MarketPosition > 0)
                {
                    // Submit the stop-limit order as long as there is an open position
                    sellStopLimitOrder.Send(sellStopPrice, sellLimitPrice);
     
                    Output.WriteLine("{0} - Submitting sell stop @ {1} with limit {2}",
                        Bars.Time[0].ToString("d-M HH:mm:ss"),
                        sellStopPrice,
                        sellLimitPrice);
     
                    // Time stop; exit the position after 15 bars
                    double barsInPosition = Bars.CurrentBar - CurrentPosition.OpenTrades[0].EntryOrder.BarNumber;
                    if (barsInPosition >= 15)
                    {
                        sellMarketOrder.Send();
     
                        Output.WriteLine("{0} - Position open for {1} bars, submitting exit long market order",
                            Bars.Time[0].ToString("d-M HH:mm:ss"),
                            barsInPosition);
                    }
                }
            }
        }
    }

     

  • 相关阅读:
    (转载) MTK芯片不开机必杀全攻略
    <19> MTK10A 修改模拟时钟表盘、表针的显示模式
    (转载) MTK flash
    (转载) MTK申请内存
    (转载) 标准C中的字符串操作函数
    pcb布线时线宽与耐流的关系
    (转载) vb6的数据类型
    (转载) MTK驱动开放基础知识
    (转载) MTK常用函数及宏定义
    xx了Windows正版验证
  • 原文地址:https://www.cnblogs.com/aliblogs/p/5493820.html
Copyright © 2011-2022 走看看