簡単に学習できるPine言語で 戦略を書いていないなら...

作者: リン・ハーン小さな夢, 作成日:2022-06-01 17:37:55, 更新日:2023-09-18 20:19:45

img

簡単に学習できるPine言語で 戦略を書いていないなら...

TradingViewのオープンソース戦略は爆発的に多く,多くの優れた戦略,アイデア,指標が実現できないのは残念です. FMZは量化取引技術を多くのトレーダーに普及させることに尽力しているため,この問題を解決する衝動を抑えることはできません.

この需要は耐えられない!

そこで,プログラミング開発のコードの世界では,山を登り,千里山を走り,9*9=81の坑を乗り越え,無数の眠れない夜を過ごし,壁の角が小山のような赤牛空を積み上げた.ついにFMZはPine言語に対応し,様々なTradingViewのPineスクリプトが入手可能になった.

パイン言語については,私が最近まで独学したばかりです.しかし,実際は,量化取引として使用するパイン言語は,本当にシンプルで,学習が簡単です. 信じられない? 見ろ,私の洋が網の戦略を書いてくれたんだ

/*backtest
start: 2021-06-01 00:00:00
end: 2022-05-23 00:00:00
period: 1h
basePeriod: 1m
exchanges: [{"eid":"Bitfinex","currency":"BTC_USD"}]
args: [["v_input_float_1",500],["v_input_string_1",2],["v_input_float_2",0.01],["v_input_int_1",20],["v_input_int_2",500],["RunMode",1,358374],["MinStock",0.001,358374]]
*/

strategy(overlay=true)

varip beginPrice = 0
var spacing = input.float(-1, title="间距价格")
var dir = input.string("long", title="方向", options = ["long", "short", "both"])
var amount = input.float(-1, title="下单量")
var numbers = input.int(-1, title="网格数量")
var profit = input.int(-1, title="盈利价差") / syminfo.mintick

if spacing == -1 and amount == -1 and numbers == -1 and profit == -1
    runtime.error("参数错误")

if not barstate.ishistory and beginPrice == 0 
    beginPrice := close 

findTradeId(id) =>
    ret = "notFound"
    for i = 0 to strategy.opentrades - 1
        if strategy.opentrades.entry_id(i) == id 
            ret := strategy.opentrades.entry_id(i)
    ret 

// 实时K线阶段
if not barstate.ishistory
    // 检索网格
    for i = 1 to numbers
        // 做多
        direction = dir == "both" ? "long" : dir 
        plot(beginPrice-i*spacing, direction+str.tostring(i), color.green)
        if direction == "long" and beginPrice-i*spacing > 0 and beginPrice-i*spacing < close and findTradeId(direction+str.tostring(i)) == "notFound"
            strategy.order(direction+str.tostring(i), strategy.long,  qty=amount, limit=beginPrice-i*spacing)
            strategy.exit("exit-"+direction+str.tostring(i), direction+str.tostring(i), qty_percent=100, profit=profit)
        // 做空
        direction := dir == "both" ? "short" : dir 
        plot(beginPrice+i*spacing, direction+str.tostring(i), color.red)
        if direction == "short" and beginPrice+i*spacing > close and findTradeId(direction+str.tostring(i)) == "notFound"
            strategy.order(direction+str.tostring(i), strategy.short, qty=amount, limit=beginPrice+i*spacing)
            strategy.exit("exit-"+direction+str.tostring(i), direction+str.tostring(i), qty_percent=100, profit=profit)

FMZの実体盤,復習ツール,多くの機能とPine言語の使いやすさに加え,パラメータ設定,復習配置コードを算出すると,合計50行を超えないコードです. 入門の同学は,格子を書くためにもはや頭痛を感じる必要はありません.

もちろん,この戦略はネット戦略であり,ネット戦略には硬い傷もあります,または包囲された印刷機ではありません. 鍵は使用方法やパラメータです. これは概要しません.

コード解説

簡単なコードで説明します. 簡単な言語で説明します. 簡単な言語で説明します. 簡単な言語で説明します.

始まり/*backtestそして*/包装された内容はFMZの回測配置コードであり,これはFMZの機能であり,Pine言語の内容ではありません. もちろん,この部分を書き留めることもできます.回測時に,回測配置とパラメータを設定するためにパラメータコントロールを手動でクリックする必要があります.

/*backtest
start: 2021-06-01 00:00:00
end: 2022-05-23 00:00:00
period: 1h
basePeriod: 1m
exchanges: [{"eid":"Bitfinex","currency":"BTC_USD"}]
args: [["v_input_float_1",500],["v_input_string_1",2],["v_input_float_2",0.01],["v_input_int_1",20],["v_input_int_2",500],["RunMode",1,358374],["MinStock",0.001,358374]]
*/

このコードは:

strategy(overlay=true)

varip beginPrice = 0
var spacing = input.float(-1, title="间距价格")
var dir = input.string("long", title="方向", options = ["long", "short", "both"])
var amount = input.float(-1, title="下单量")
var numbers = input.int(-1, title="网格数量")
var profit = input.int(-1, title="盈利点数") / syminfo.mintick
  • strategy(overlay=true): 設定するいくつかのオプション,overlay=true は,参数を与えることです.overlaytrue を指定して,図を描くとき,図の主図に描く (K線図は主図であり,理解が簡単です).
  • varip beginPrice = 0:キーワードvaripで変数を宣言します.beginPriceの初期値が0で,この値はグリッドとして使用される初期値です.
  • var spacing = input.float(-1, title="间距价格"): 設定する戦略パラメータは,パラメータ名は間隔価格,すなわち各グリッドポイントの間隔,設定100は価格100回,取引1回.
  • var dir = input.string("long", title="方向", options = ["long", "short", "both"]): 設定するポリシーパラメータは,方向と呼ばれる. このパラメータは,ドラッグボックス付きのオプションで,long,short,both を選択できます.
  • var amount = input.float(-1, title="下单量"): 格子点取引ごとに取引量を制御するパラメータを設定します.
  • var numbers = input.int(-1, title="网格数量"): 格子点数, 20 を設定すると,一方向に 20 つの格子点である.
  • var profit = input.int(-1, title="盈利价差") / syminfo.mintick: 格子点ごとに保有する株価の利潤を制御するパラメータを設定し,価格差を平衡します.

この記事へのトラックバック一覧です.

if spacing == -1 and amount == -1 and numbers == -1 and profit == -1
    runtime.error("参数错误")

設定されていない場合は,この手順を停止します (設定されていないパラメータは,盲目に行動できません - ハッハ!)

行け! 行け!

if not barstate.ishistory and beginPrice == 0 
    beginPrice := close 

これは,戦略がリアルタイムK線段階にあり,beginPrice == 0であるとき,beginPriceに変更値を与え,現在の最新価格に変更するという意味です. 戦略が正式に実行されているとき,最初の現在の価格がグリッドの初期価格であると理解できます. スクリプトは歴史的なK線BAR段階のものであるため,戦略は歴史的なBAR段階で論理を繰り返すので,歴史BARにグリッドを配置することは確かに意味がありません.

歴史のBAR段階とは何か?

簡単な例として,現在の瞬間Aでは,戦略が実行を開始し,戦略は100のK線BARのデータを入手し,時間の経過とともに100のBARが101になり,102...Nになります. Aから実行開始するときに101番目のBARはリアルタイムK線段階であり,この時点で最新のリアルタイムデータです.

barstate.ishistoryこれはPine言語の内置変数で,現在のBARが歴史段階のBARである場合,barstate.ishistorytrue は true で,history の段階でない BAR は false です.したがって not barstate.ishistory は true で,リアルタイムで K ラインの段階にある.

この関数は,この関数と,この関数の両方を

findTradeId(id) =>
    ret = "notFound"
    for i = 0 to strategy.opentrades - 1
        if strategy.opentrades.entry_id(i) == id 
            ret := strategy.opentrades.entry_id(i)
    ret 

この関数の役割は,現在開かれているすべてのオーダーの中で,あるIDが存在するかどうかを調べ,findTradeId関数の呼び出しで存在する場合,存在する单元のIDを返します (注意,このIDは取引所のオーダーIDではなく,ポリシーがオーダーに与える名前またはラベルとして理解されます).存在しない場合は,文字列"notFound"を返します.

ネットワークの設定は,次のようになります:

// 实时K线阶段
if not barstate.ishistory
    // 检索网格
    for i = 1 to numbers
        // 做多
        direction = dir == "both" ? "long" : dir 
        plot(beginPrice-i*spacing, direction+str.tostring(i), color.green)
        if direction == "long" and beginPrice-i*spacing > 0 and beginPrice-i*spacing < close and findTradeId(direction+str.tostring(i)) == "notFound"
            strategy.order(direction+str.tostring(i), strategy.long,  qty=amount, limit=beginPrice-i*spacing)
            strategy.exit("exit-"+direction+str.tostring(i), direction+str.tostring(i), qty_percent=100, profit=profit)
        // 做空
        direction := dir == "both" ? "short" : dir 
        plot(beginPrice+i*spacing, direction+str.tostring(i), color.red)
        if direction == "short" and beginPrice+i*spacing > close and findTradeId(direction+str.tostring(i)) == "notFound"
            strategy.order(direction+str.tostring(i), strategy.short, qty=amount, limit=beginPrice+i*spacing)
            strategy.exit("exit-"+direction+str.tostring(i), direction+str.tostring(i), qty_percent=100, profit=profit)

forループを使用し,numbersパラメータの数値に基づいて回転回数を決定する.すなわち,対応する個数のオーダーを配置する. ■dirパラメータに基づいて,directionを設定する. ■findTradeId関数を使用して,現在の格子位置のラベルのオーダーが既に開かれているかどうかを調べ,開いていない場合にのみリストを下げる. (開いた場合,確実に重複できない). ■下記のリストはstrategy.order関数を使用してlimitパラメータをリストとして設定する. ■下記のリストの同時下記の対応平行リストは平行リストを使用する. ■平行リストはstrategy.exit関数を使用して,profitパラメータを指定し,profitポイントを指定する.

img

img

img

img

利回り曲線を見ると,格子にはリスクがあり,勝負ではないが,大きなスケールで格子を引き上げるリスクは少し小さい.

簡単に学習できるPine言語で 戦略を書いていないなら...


関連性

もっと

ストーカーこの胎児教育レベルのチュートリアルは,何冊か増やせるか,特にpineのチュートリアルは ((知識料) hhh 。夢総数v587

アルトロンありがとうございました

2009年に結婚した夢の総額V5

小さな夢バイビリ・ビリ・ビリ・ビリ・ビリ・ビリ・ビリ・ビリ・ビリ・ビリ・ビリ

小さな夢ありがとう,FMZを応援してくれて.

小さな夢B ステーションにはPINEの言語紹介ビデオもあります.