金字塔决策交易系统

 找回密码
 

微信登录

微信扫一扫,快速登录

搜索
查看: 413|回复: 1

由于技术有限求大神帮我修改成可以应用到金字塔交易的代码,谢谢

[复制链接]

19

主题

52

帖子

52

积分

等级: 免费版

注册:
2024-6-4
曾用名:
发表于 2024-12-25 21:27 | 显示全部楼层 |阅读模式
//@version=5
indicator("My Script", overlay=true)

// 全局变量声明
n = input(10)  // MA均线周期设为10日
stopProfit = input(6)  // 止盈设为6跳
stopLoss = input(6)  // 止损设为6跳

// 计算10日均线
ma10Value = ta.sma(close, n)

// 获取当前持仓
// 在Pine Script中没有直接获取持仓的函数,因此这部分需要您根据实际情况进行调整
// 以下代码是假设您可以获取持仓信息的示例
currentPosition = securitypositionbyname("")

// 开仓逻辑
if currentPosition == 0
    // 多头开仓
    if ma10Value < close
        if firstTradeAfterLoss
            strategy.entry("Buy", strategy.long, 2)
        else
            strategy.entry("Buy", strategy.long, 1)
    // 空头开仓
    else if ma10Value > close
        if firstTradeAfterLoss
            strategy.entry("Short", strategy.short, 2)
        else
            strategy.entry("Short", strategy.short, 1)

// 平仓及后续处理逻辑
if currentPosition != 0
    // 多头平仓
    if currentPosition > 0
        if close - strategy.position_avg_price > stopProfit
            strategy.close("Buy")
            if firstTradeAfterLoss
                profitCountSinceLoss := profitCountSinceLoss + 1
            firstTradeAfterLoss := false
        else if strategy.position_avg_price - close > stopLoss
            strategy.close("Buy")
            firstTradeAfterLoss := true
            lossCountSinceLoss := lossCountSinceLoss + 1
    // 空头平仓
    else if currentPosition < 0
        if strategy.position_avg_price - close > stopProfit
            strategy.close("Short")
            if firstTradeAfterLoss
                profitCountSinceLoss := profitCountSinceLoss + 1
            firstTradeAfterLoss := false
        else if close - strategy.position_avg_price > stopLoss
            strategy.close("Short")
            firstTradeAfterLoss := true
            lossCountSinceLoss := lossCountSinceLoss + 1

    // 重置计数
    if firstTradeAfterLoss and (profitCountSinceLoss + lossCountSinceLoss >= 2)
        profitCountSinceLoss := 0
        lossCountSinceLoss := 0
        firstTradeAfterLoss := false

回复

使用道具 举报

38

主题

9989

帖子

9999

积分

Rank: 8Rank: 8

等级: 超级版主

注册:
2021-5-18
曾用名:
发表于 2024-12-26 09:21 | 显示全部楼层
这个需要用户把逻辑用文字描述出来,工作人员直接看代码是非常不好把控所有细节的
金字塔提供一对一VIP专业技术指导服务,技术团队实时响应您的日常使用问题与策略编写。联系电话:021-20339086
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 微信登录

本版积分规则

手机版|小黑屋|上海金之塔信息技术有限公司 ( 沪ICP备13035422号 )

GMT+8, 2025-4-21 08:20 , Processed in 0.125101 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表