Open Forex4you Account

Server รัน EA 1000 / ปี

สั่งชื้อคู่มือการโปรแรกม MQL4

Author Topic: การเก็บข้อมูล หรือ array  (Read 10139 times)

gunlanutt

  • Newbie
  • *
  • Posts: 46
การเก็บข้อมูล หรือ array
« on: ตุลาคม 22, 2015, 10:57:45 am »
เรียนท่าน Admin หรือท่านผู้รู้  ครับ

คือผมได้เขียนอินดิเคเตอร์ง่าย ๆ ตัวหนึ่งเพื่อคำนวณหาค่า Risk/Reward แต่ว่ามันคำนวณให้เพียงออร์เดอร์เดียว
หากผมต้องการเก็บข้อมูล แต่ละออร์เดอร์ ซึ่งในหนึ่งออร์เดอร์จะมี ราคาเปิด lot, sl, tp
แล้วนำมาคำนวณเพื่อหาความเสี่ยง/กำไร ต่อจำนวนทุนของเรา คำนวณออกมาเป็นเปอร์เซ็นต์ ผมต้องการนำทุกออร์เดอร์มาคำนวณด้วยหมดเลย
ผมต้องเขียนโค๊ดอย่างไรครับถึงจะให้เก็บข้อมูลแล้วคำนวณให้ คือผมไม่มีความรู้เรื่อง array หรือการเก็บข้อมูลเลยครับ
ท่าน admin หรือ ท่านผู้รู้ช่วยชี้แนะ เขียนโค๊ดต่อให้ผมด้วยครับ

//+------------------------------------------------------------------+
//|                                                  Risk / Reward Ratio.mq4 |
//|                                                                 Bruno Gaiteiro |
//|                                                        bgaiteiro@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright ฉ 2009, bgaiteiro"
#property link      "bgaiteiro@gmail.com"

#property indicator_chart_window
//---- input parameters

extern   int SL_position_size = 20;
int      OpenOrders=0, cnt=0;
int      Order_Type=0;
int      i=0;
double   RiskReward_ratio=0;
double   SL_price=0, TP_price=0, Open_Price=0;
double   Risk=0;
double   Reward=0;
string   Text="";
double   position_size=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//----

//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   //----

   OpenOrders=0;
   for(cnt=0;cnt<OrdersTotal();cnt++)  
   {
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
     if (OrderSymbol()==Symbol())
     {            
          OpenOrders++;
          Open_Price   =  OrderOpenPrice();
          TP_price     =  OrderTakeProfit();
          SL_price     =  OrderStopLoss();
          Order_Type   =  OrderType();
     }
   }    
  
     for(cnt=OrdersTotal();cnt>=0;cnt--)
     {
        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
          Order_Type=OrderType();
        if (OrderSymbol()==Symbol())
        {
         if (Order_Type == OP_SELL) { Risk   = ((((SL_price - Open_Price)/Point)*OrderLots()*1000))/AccountBalance();
                                      Reward = ((((Open_Price - TP_price)/Point)*OrderLots()*1000))/AccountBalance();}
         if (Order_Type == OP_BUY)  { Risk   = ((((Open_Price - SL_price)/Point)*OrderLots()*1000))/AccountBalance();
                                      Reward = ((((TP_price - Open_Price)/Point)*OrderLots()*1000))/AccountBalance();}
        }
     }
   position_size = ((((SL_position_size/Point)/1000)/(AccountBalance() * 10)/100));
   Text =   " Risk/Reward Ratio     1 : " + DoubleToStr(Reward/Risk,2) ;
   if(OpenOrders=0){Text=" ";}
   Comment(Text,
           "\n Position_LotSize = ",position_size,
           "\n TakeProfkt = ",Reward," % ",
           "\n StopLoss = ",Risk," % ");
 
   return(0);
  }
//+------------------------------------------------------------------+//+------------------------------------------------------------------+
« Last Edit: ตุลาคม 22, 2015, 11:17:00 am by gunlanutt »

admin

  • Administrator
  • Hero Member
  • *****
  • Posts: 2386
Re: การเก็บข้อมูล หรือ array
« Reply #1 on: ตุลาคม 22, 2015, 11:49:22 am »
มีคำถาม
1.ตัวนี้เป็น Indy หรือ EA
2.ค่าที่บอกว่าออกมาแค่ค่าเดียวนี้คือ ค่าสุดท้ายหรือค่าอะไรครับ
3.ที่ต้องการให้เก็บค่าใน Array เป็นค่าอะไรครับ

gunlanutt

  • Newbie
  • *
  • Posts: 46
Re: การเก็บข้อมูล หรือ array
« Reply #2 on: ตุลาคม 22, 2015, 01:06:43 pm »
1. ตัวนี้เป็น indicator ครับ
2. ตัวนี้ค่าที่ออกมาเป็นค่าสุดท้ายครับ แต่ที่ต้องการคือให้เก็บทุกค่าแล้วเอามาคำนวณเป็นเปอร์เซ็นต์ต่อ Account Balance() ครับ
3. ที่ต้องการให้เก็บใน Array คือค่า SL_price, TP_Price, Open_Price และ OrderType() ครับ

ขอบคุณครับ

gunlanutt

  • Newbie
  • *
  • Posts: 46
Re: การเก็บข้อมูล หรือ array
« Reply #3 on: ตุลาคม 22, 2015, 04:04:18 pm »
ไม่รู้เขียนผิดตรงไหนครับ คืออยากให้มันเก็บข้อมูล


//+------------------------------------------------------------------+
//|                                          Risk / Reward Ratio.mq4 |
//|                                                   Bruno Gaiteiro |
//|                                              gunlanutt@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright ฉ 2015  Nutt Trader"
#property link      "gunlanutt@gmail.com"

#property indicator_chart_window
//---- input parameters

extern   int SL_position_size = 20;
int      OpenOrders=0, cnt=0;
int      Order_Type1=0;
int      i=0;
double   RiskReward_ratio=0;
double   SL_price1=0, TP_price1=0, Open_Price1=0;
double   Risk=0;
double   Reward=0;
string   Text="";
double   position_size=0;
//*
double   Open_Price[10];
double   TP_price[10];
double   SL_price[10];
double   Order_Type[10];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//----

//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   //----

   OpenOrders=0;
   for(cnt=0;cnt<OrdersTotal();cnt++)   
   {
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
     if (OrderSymbol()==Symbol())
     {            
          OpenOrders++;
     }
   }     
/*
   Comment(Text,
           "\n Position_LotSize = ",position_size,
           "\n TakeProfkt = ",Reward," % ",
           "\n StopLoss = ",Risk," % ");
*/
//+------------------------------------------------------------------+//+------------------------------------------------------------------+/*
int time=0;
   for(cnt=0;cnt<OrdersTotal();cnt++)   
   {
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
     if (OrderSymbol()==Symbol())
        {
                  if (time==10)
                     { 
                        Open_Price[10]   =  OrderOpenPrice();
                        TP_price[10]     =  OrderTakeProfit();
                        SL_price[10]     =  OrderStopLoss();
                        Order_Type[10]   =  OrderType();
                        time++;
                        cnt=OrdersTotal();   
                     }                             

                  if (time==9)
                     { 
                        Open_Price[9]   =  OrderOpenPrice();
                        TP_price[9]     =  OrderTakeProfit();
                        SL_price[9]     =  OrderStopLoss();
                        Order_Type[9]   =  OrderType();
                        time++;
                     }                             
                                     
                  if (time==8)
                     { 
                        Open_Price[8]   =  OrderOpenPrice();
                        TP_price[8]     =  OrderTakeProfit();
                        SL_price[8]     =  OrderStopLoss();
                        Order_Type[8]   =  OrderType();
                        time++;
                     }                             
                  if (time==7)
                     { 
                        Open_Price[7]   =  OrderOpenPrice();
                        TP_price[7]     =  OrderTakeProfit();
                        SL_price[7]     =  OrderStopLoss();
                        Order_Type[7]   =  OrderType();
                        time++;
                     }                             
                  if (time==6)
                     { 
                        Open_Price[6]   =  OrderOpenPrice();
                        TP_price[6]     =  OrderTakeProfit();
                        SL_price[6]     =  OrderStopLoss();
                        Order_Type[6]   =  OrderType();
                        time++;
                     }                             
                  if (time==5)
                     { 
                        Open_Price[5]   =  OrderOpenPrice();
                        TP_price[5]     =  OrderTakeProfit();
                        SL_price[5]     =  OrderStopLoss();
                        Order_Type[5]   =  OrderType();
                        time++;
                     }                             
                  if (time==4)
                     { 
                        Open_Price[4]   =  OrderOpenPrice();
                        TP_price[4]     =  OrderTakeProfit();
                        SL_price[4]     =  OrderStopLoss();
                        Order_Type[4]   =  OrderType();
                        time++;
                     }                             
                  if (time==3)
                     { 
                        Open_Price[3]   =  OrderOpenPrice();
                        TP_price[3]     =  OrderTakeProfit();
                        SL_price[3]     =  OrderStopLoss();
                        Order_Type[3]   =  OrderType();
                        time++;
                     }                             
                  if (time==2)
                     { 
                        Open_Price[2]   =  OrderOpenPrice();
                        TP_price[2]     =  OrderTakeProfit();
                        SL_price[2]     =  OrderStopLoss();
                        Order_Type[2]   =  OrderType();
                        time++;
                     }                             
                  if (time==1)
                     { 
                        Open_Price[1]   =  OrderOpenPrice();
                        TP_price[1]     =  OrderTakeProfit();
                        SL_price[1]     =  OrderStopLoss();
                        Order_Type[1]   =  OrderType();
                        time++;
                     }                             
                  if (time==0)
                     { 
                        Open_Price[0]   =  OrderOpenPrice();
                        TP_price[0]     =  OrderTakeProfit();
                        SL_price[0]     =  OrderStopLoss();
                        Order_Type[0]   =  OrderType();
                        time++;
                     }                             
         }
   }
   return(0);
}

admin

  • Administrator
  • Hero Member
  • *****
  • Posts: 2386
Re: การเก็บข้อมูล หรือ array
« Reply #4 on: ตุลาคม 22, 2015, 11:59:49 pm »
ตรง time== 9 ,time==8 

พวกนี้คืออะไรครับ

admin

  • Administrator
  • Hero Member
  • *****
  • Posts: 2386
Re: การเก็บข้อมูล หรือ array
« Reply #5 on: ตุลาคม 23, 2015, 12:02:26 am »
1. ตัวนี้เป็น indicator ครับ
2. ตัวนี้ค่าที่ออกมาเป็นค่าสุดท้ายครับ แต่ที่ต้องการคือให้เก็บทุกค่าแล้วเอามาคำนวณเป็นเปอร์เซ็นต์ต่อ Account Balance() ครับ
3. ที่ต้องการให้เก็บใน Array คือค่า SL_price, TP_Price, Open_Price และ OrderType() ครับ

ขอบคุณครับ

ถามที่ผมเข้าใจ คือ คุณต้องการ เอาค่า SL_price, TP_Price, Open_Price และ OrderType()  ของ Order ที่กำลังเปิดอยู่มาเก็บใน array ใช้หรือเปล่าครับ

gunlanutt

  • Newbie
  • *
  • Posts: 46
Re: การเก็บข้อมูล หรือ array
« Reply #6 on: ตุลาคม 23, 2015, 11:52:09 am »
ขอบคุณ admin มาก มากเลยครับ แต่ตอนนี้ผมคิดว่าผมได้แล้วครับ
ผมลองเปิดโพสต์เก่า ๆ เรื่องการเก็บค่าของ zigzag จากลิงค์นี้

http://www.thaiforexea.com/index.php/topic,20.msg110.html#msg110

แล้วลองแกะโค๊ดดูครับ เพราะเห็นว่ามีการเก็บค่าเหมือนกัน อ่านซ้ำไปซ้ำมาหลายรอบ และนั่งลองผิดลองถูก ตอนนี้คิดว่าพอใจกับผลลัพธ์แล้ว

หลักการอินดิเคเตอร์ตัวนี้ก็คือ เมื่อเราเปิดออร์เดอร์ ไม่ว่าจะกี่ออร์เดอร์ก็ตาม (ในที่นี้ผมเขียนสูงสุดเพียง 10 ออร์เดอร์)
มันก็จะทำการ
     - คำนวณจุดเปิดกับ tp ว่าจะได้กำไรเท่ากับกี่เปอร์เซ็นต์ของทุน (Reward)
     - คำนวณจุดเปิดกับ sl ว่ามีความเสี่ยงเท่าไหร่เมื่อเทียบกับทุน (Risk)
     - นำผลลัพธ์ของ risk/reward ว่ามีอัตราส่วนเท่าไหร่

อาจจะเขียนโค๊ดไม่ดีพอนะครับ เพราะไม่เข้าใจหลักการอย่างจริงๆ
ที่ลองนำมาเขียนเพราะไปอ่านเรื่องการจัดการเงินทุน เรื่องการโยนลูกเต๋า เรื่องการจัดการความเสี่ยง การล้างพอร์ต แล้วเห็นว่ามีประโยชน์น่าสนใจครับ

ขอบคุณครับผม