Индикатор не строится на основе данных разных валют

Образцы, куски исходников, вопросы. Обсуждаем программирование.
Ответить
Сообщение
Автор
McMoney
Сообщения: 1
Зарегистрирован: Вт дек 26, 2006 2:51 pm

Индикатор не строится на основе данных разных валют

#1 Сообщение McMoney » Вт дек 26, 2006 3:02 pm

Добрый день.
Пытаюсь решить не совсем обычную задачу. Необходимо построить SMМА разных пар в одном окне.
Задача сводится к следующему:
в отдельном окне на графике GBPUSD построить SMMA GBPUSD и USDCHF. В одиночку получается (т.е. я могу нарисовать SMMA GBPUSD где угодно, хоть на каде), когда же две скользящие оказываются на одном графике - рисуется прямая.
ЗЫ Прошу простить если вопрос чересчур дилетантский как для программиста. по комментам видно, сколько я перебрал вариантов :((

Код: Выделить всё

library MAMY;

uses
  graphics,
  IndicatorInterfaceUnit,
  TechnicalFunctions in 'TechnicalFunctions.pas' ;


var
  // External variables
  period: integer;
  Shift: integer;
  MAtype: integer;
  ApplyToPrice: integer;


  // Buffers
  SMA_GBPUSD: TIndexBuffer;
  SMA_USDCHF: TIndexBuffer;
//  SMA_ABS: TIndexBuffer;


//---------------------------------------------------------------------------
// Initialize indicator
//---------------------------------------------------------------------------
procedure Init; stdcall;
begin
  // define properties
  IndicatorShortName('MAMY');
  SetOutputWindow(ow_SeparateWindow);

  // register options
  AddSeparator('Common');

  RegOption('Period', ot_Integer, period);
  SetOptionRange('Period', 1, MaxInt);
  period := 8;

  RegOption('Shift', ot_Integer, Shift);
  Shift := 0;

  RegMATypeOption(MAtype);

  RegApplyToPriceOption(ApplyToPrice);

  // create buffers
  IndicatorBuffers(2);
  SMA_GBPUSD := CreateIndexBuffer;
  SMA_USDCHF := CreateIndexBuffer;
//  SMA_ABS := CreateIndexBuffer;

  SetIndexBuffer(0, SMA_GBPUSD);
  SetIndexStyle(0, ds_Line, psSolid, 1, clYellow);
  SetIndexLabel(0, 'MA1');
  SetIndexBuffer(1, SMA_USDCHF);
  SetIndexStyle(1, ds_Line, psSolid, 1, clRed);
  SetIndexLabel(1, 'MA2');
//  SetIndexBuffer(2, SMA_ABS);
//  SetIndexStyle(2, ds_Line, psSolid, 1, clYellow);
//  SetIndexLabel(2, 'MA3');

//SetFixedMinMaxValues(0, 5);          // масштабировать между 0 и 100
//  AddLevel(1, psDot, 1, clGray);        // задать уровень 20
//  AddLevel(80, psDot, 1, clGray);        // задать уровень 80

end;

//---------------------------------------------------------------------------
// Deinitialize indicator
//---------------------------------------------------------------------------
procedure Done; stdcall;
begin

end;

//---------------------------------------------------------------------------
// Calculate requested bar
//---------------------------------------------------------------------------
procedure Calculate(index: integer); stdcall;

{-----Get price--------------------------------------------------------------}
function myGetPrice(index: integer; PriceType: TPriceType; mySymbol:string): double;
begin
  case PriceType of
    pt_Close: result := iClose(mySymbol, PERIOD_M1, index);
    pt_Open:  result := iOpen(mySymbol, PERIOD_M1, index);
    pt_High:  result := iHigh(mySymbol, PERIOD_M1, index);
    pt_Low:   result := iLow(mySymbol, PERIOD_M1, index);
    pt_HL2:   result := (iHigh(mySymbol, PERIOD_M1, index) + iLow(mySymbol, PERIOD_M1, index))/2;
    pt_HLC3:  result := (iHigh(mySymbol, PERIOD_M1, index) + iLow(mySymbol, PERIOD_M1, index) + iClose(mySymbol, PERIOD_M1, index))/3;
    pt_HLCC4: result := (iHigh(mySymbol, PERIOD_M1, index) + iLow(mySymbol, PERIOD_M1, index) + iClose(mySymbol, PERIOD_M1, index)*2)/4;
    else      result := 0;
  end;
end;

function  myGetMA(index, shift, period: integer; maType: TMAType;
  ApplyTo: TPriceType; prev: double = 0; mySymbol: string=''): double;
var
  i: integer;
  k, sum, weight: double;
begin
  case MAtype of
    ma_SMA:
       begin
         // SMA
         if (index + shift + period >= Bars) or (index + shift < 0) then
           result := 0
         else
           begin
             sum := 0;
             for i:=index to index + period - 1 do
               sum := sum + myGetPrice(i + Shift, ApplyTo, mySymbol);
             result := sum/period;
           end;
       end;

    ma_EMA:
       begin
         // EMA
         k := 2/(period + 1);
         i := index + shift;
         if (i > Bars - 1) or (i < 0) then
           result := 0
         else
           begin
             if prev = 0 then
               result := myGetPrice(i, ApplyTo, mySymbol)
             else
               result := prev + k*(myGetPrice(i, ApplyTo, mySymbol) - prev);
           end;
       end;

    ma_WMA:
       begin
         // WMA
         if (index + shift + period >= Bars) or (index + shift < 0) then
           result := 0
         else
           begin
             sum := 0;
             weight := 0;
             for i:=0 to period - 1 do
               begin
                 sum := sum + myGetPrice(index + i + shift, ApplyTo, mySymbol)*(period - i);
                 weight := weight + (period - i);
               end;
             result := sum/weight;
           end;
       end;

    else
      begin
        // SSMA
        if (index + shift + period >= Bars) or (index + shift < 0) then
           result := 0
         else
           begin
             if prev = 0 then
               result := myGetMA(index, shift, period, ma_SMA, ApplyTo, prev, mySymbol)
             else
               result := (prev*period - prev + myGetPrice(index + shift, ApplyTo, mySymbol))/period;
           end;
      end;
  end; {case}
end;

begin
//SMA_GBPUSD[index] := myGetMA(index, shift, period, TMAType(MAtype), TPriceType(ApplyToPrice),
//   SMA_GBPUSD[index + 1], 'GBPUSD');
SMA_GBPUSD[index] := iClose('GBPUSD', TimeFrame, index+1) ;
SMA_USDCHF[index] := iOpen('USDCHF', TimeFrame, index+1) ;

 // SMA_GBPUSD[index] := iClose('GBPUSD', PERIOD_M1, index) ;
//SMA_USDCHF[index] := index;

//SMA_USDCHF[index] := myGetPrice(index, TPriceType(ApplyToPrice), 'USDCHF') ;

//  SMA_USDCHF[index] := myGetMA(index, shift, period, TMAType(MAtype), TPriceType(ApplyToPrice),
//    SMA_USDCHF[index + 1], 'USDCHF');
  //SMA_ABS[index]:= SMA_GBPUSD[index]-SMA_USDCHF[index];

  // recalculate last shifted value if shift < 0
  if (shift < 0) and (index = 0) and (Bars > abs(shift)) and (Bars > period) then
    Calculate(abs(shift));

end;


exports

Init, Done, Calculate;

end.
Спасибо за внимание к моей проблеме

Ответить